From 436d0b5cc176008bc3bfb3e2bbc4ff5487e80161 Mon Sep 17 00:00:00 2001 From: Gauri Prasad <51212198+gapra-msft@users.noreply.github.com> Date: Tue, 11 Jul 2023 12:05:48 -0700 Subject: [PATCH] Added tests for make (#2287) --- cmd/zt_make_test.go | 194 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 cmd/zt_make_test.go diff --git a/cmd/zt_make_test.go b/cmd/zt_make_test.go new file mode 100644 index 000000000..5707ae508 --- /dev/null +++ b/cmd/zt_make_test.go @@ -0,0 +1,194 @@ +// Copyright © 2017 Microsoft +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +package cmd + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func runMakeAndVerify(raw rawMakeCmdArgs, verifier func(err error)) { + // the simulated user input should parse properly + cooked, err := raw.cook() + if err != nil { + verifier(err) + return + } + + // the enumeration ends when process() returns + err = cooked.process() + + // the err is passed to verified, which knows whether it is expected or not + verifier(err) +} + +func TestMakeBlobContainer(t *testing.T) { + a := assert.New(t) + bsc := getBlobServiceClient() + cc, name := getContainerClient(a, bsc) + defer deleteContainer(a, cc) + + bscSAS := scenarioHelper{}.getBlobServiceClientWithSAS(a) + ccSAS := bscSAS.NewContainerClient(name) + + args := rawMakeCmdArgs{ + resourceToCreate: ccSAS.URL(), + } + + runMakeAndVerify(args, func(err error) { + a.Nil(err) + _, err = cc.GetProperties(ctx, nil) + a.Nil(err) + }) +} + +func TestMakeBlobContainerExists(t *testing.T) { + a := assert.New(t) + bsc := getBlobServiceClient() + cc, name := createNewContainer(a, bsc) + defer deleteContainer(a, cc) + + bscSAS := scenarioHelper{}.getBlobServiceClientWithSAS(a) + ccSAS := bscSAS.NewContainerClient(name) + + args := rawMakeCmdArgs{ + resourceToCreate: ccSAS.URL(), + } + + runMakeAndVerify(args, func(err error) { + a.NotNil(err) + a.Equal("the container already exists", err.Error()) + _, err = cc.GetProperties(ctx, nil) + a.Nil(err) + }) +} + +func TestMakeBlobFSFilesystem(t *testing.T) { + a := assert.New(t) + bsc := GetBFSSU() + fsc, name := getFilesystemURL(a, bsc) + defer deleteFilesystem(a, fsc) + + bscSAS := scenarioHelper{}.getRawAdlsServiceURLWithSAS(a) + ccSAS := bscSAS.NewFileSystemURL(name) + + args := rawMakeCmdArgs{ + resourceToCreate: ccSAS.String(), + } + + runMakeAndVerify(args, func(err error) { + a.Nil(err) + _, err = fsc.GetProperties(ctx) + a.Nil(err) + }) +} + +func TestMakeBlobFSFilesystemExists(t *testing.T) { + a := assert.New(t) + bsc := GetBFSSU() + fsc, name := getFilesystemURL(a, bsc) + _, err := fsc.Create(ctx) + a.Nil(err) + defer deleteFilesystem(a, fsc) + + bscSAS := scenarioHelper{}.getRawAdlsServiceURLWithSAS(a) + ccSAS := bscSAS.NewFileSystemURL(name) + + args := rawMakeCmdArgs{ + resourceToCreate: ccSAS.String(), + } + + runMakeAndVerify(args, func(err error) { + a.NotNil(err) + a.Equal("the file system already exists", err.Error()) + _, err = fsc.GetProperties(ctx) + a.Nil(err) + }) +} + +func TestMakeFileShare(t *testing.T) { + a := assert.New(t) + fsc := getFSU() + sc, name := getShareURL(a, fsc) + defer deleteShare(a, sc) + + fscSAS := scenarioHelper{}.getRawFileServiceURLWithSAS(a) + scSAS := fscSAS + scSAS.Path = name + + args := rawMakeCmdArgs{ + resourceToCreate: scSAS.String(), + } + + runMakeAndVerify(args, func(err error) { + a.Nil(err) + props, err := sc.GetProperties(ctx) + a.Nil(err) + a.EqualValues(5120, props.Quota()) + }) +} + +func TestMakeFileShareQuota(t *testing.T) { + a := assert.New(t) + fsc := getFSU() + sc, name := getShareURL(a, fsc) + defer deleteShare(a, sc) + + fscSAS := scenarioHelper{}.getRawFileServiceURLWithSAS(a) + scSAS := fscSAS + scSAS.Path = name + + args := rawMakeCmdArgs{ + resourceToCreate: scSAS.String(), + quota: 5, + } + + runMakeAndVerify(args, func(err error) { + a.Nil(err) + props, err := sc.GetProperties(ctx) + a.Nil(err) + a.EqualValues(args.quota, props.Quota()) + }) +} + +func TestMakeFileShareExists(t *testing.T) { + a := assert.New(t) + fsc := getFSU() + sc, name := getShareURL(a, fsc) + _, err := sc.Create(ctx, nil, 0) + a.Nil(err) + defer deleteShare(a, sc) + + fscSAS := scenarioHelper{}.getRawFileServiceURLWithSAS(a) + scSAS := fscSAS + scSAS.Path = name + + args := rawMakeCmdArgs{ + resourceToCreate: scSAS.String(), + } + + runMakeAndVerify(args, func(err error) { + a.NotNil(err) + a.Equal("the file share already exists", err.Error()) + _, err = sc.GetProperties(ctx) + a.Nil(err) + }) +} \ No newline at end of file