github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/volume/create_test.go (about) 1 package volume 2 3 import ( 4 "io/ioutil" 5 "reflect" 6 "strings" 7 "testing" 8 9 "github.com/docker/cli/internal/test" 10 "github.com/docker/docker/api/types" 11 volumetypes "github.com/docker/docker/api/types/volume" 12 "github.com/pkg/errors" 13 "gotest.tools/v3/assert" 14 is "gotest.tools/v3/assert/cmp" 15 ) 16 17 func TestVolumeCreateErrors(t *testing.T) { 18 testCases := []struct { 19 args []string 20 flags map[string]string 21 volumeCreateFunc func(volumetypes.VolumeCreateBody) (types.Volume, error) 22 expectedError string 23 }{ 24 { 25 args: []string{"volumeName"}, 26 flags: map[string]string{ 27 "name": "volumeName", 28 }, 29 expectedError: "Conflicting options: either specify --name or provide positional arg, not both", 30 }, 31 { 32 args: []string{"too", "many"}, 33 expectedError: "requires at most 1 argument", 34 }, 35 { 36 volumeCreateFunc: func(createBody volumetypes.VolumeCreateBody) (types.Volume, error) { 37 return types.Volume{}, errors.Errorf("error creating volume") 38 }, 39 expectedError: "error creating volume", 40 }, 41 } 42 for _, tc := range testCases { 43 cmd := newCreateCommand( 44 test.NewFakeCli(&fakeClient{ 45 volumeCreateFunc: tc.volumeCreateFunc, 46 }), 47 ) 48 cmd.SetArgs(tc.args) 49 for key, value := range tc.flags { 50 cmd.Flags().Set(key, value) 51 } 52 cmd.SetOut(ioutil.Discard) 53 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 54 } 55 } 56 57 func TestVolumeCreateWithName(t *testing.T) { 58 name := "foo" 59 cli := test.NewFakeCli(&fakeClient{ 60 volumeCreateFunc: func(body volumetypes.VolumeCreateBody) (types.Volume, error) { 61 if body.Name != name { 62 return types.Volume{}, errors.Errorf("expected name %q, got %q", name, body.Name) 63 } 64 return types.Volume{ 65 Name: body.Name, 66 }, nil 67 }, 68 }) 69 70 buf := cli.OutBuffer() 71 72 // Test by flags 73 cmd := newCreateCommand(cli) 74 cmd.Flags().Set("name", name) 75 assert.NilError(t, cmd.Execute()) 76 assert.Check(t, is.Equal(name, strings.TrimSpace(buf.String()))) 77 78 // Then by args 79 buf.Reset() 80 cmd = newCreateCommand(cli) 81 cmd.SetArgs([]string{name}) 82 assert.NilError(t, cmd.Execute()) 83 assert.Check(t, is.Equal(name, strings.TrimSpace(buf.String()))) 84 } 85 86 func TestVolumeCreateWithFlags(t *testing.T) { 87 expectedDriver := "foo" 88 expectedOpts := map[string]string{ 89 "bar": "1", 90 "baz": "baz", 91 } 92 expectedLabels := map[string]string{ 93 "lbl1": "v1", 94 "lbl2": "v2", 95 } 96 name := "banana" 97 98 cli := test.NewFakeCli(&fakeClient{ 99 volumeCreateFunc: func(body volumetypes.VolumeCreateBody) (types.Volume, error) { 100 if body.Name != "" { 101 return types.Volume{}, errors.Errorf("expected empty name, got %q", body.Name) 102 } 103 if body.Driver != expectedDriver { 104 return types.Volume{}, errors.Errorf("expected driver %q, got %q", expectedDriver, body.Driver) 105 } 106 if !reflect.DeepEqual(body.DriverOpts, expectedOpts) { 107 return types.Volume{}, errors.Errorf("expected drivers opts %v, got %v", expectedOpts, body.DriverOpts) 108 } 109 if !reflect.DeepEqual(body.Labels, expectedLabels) { 110 return types.Volume{}, errors.Errorf("expected labels %v, got %v", expectedLabels, body.Labels) 111 } 112 return types.Volume{ 113 Name: name, 114 }, nil 115 }, 116 }) 117 118 cmd := newCreateCommand(cli) 119 cmd.Flags().Set("driver", "foo") 120 cmd.Flags().Set("opt", "bar=1") 121 cmd.Flags().Set("opt", "baz=baz") 122 cmd.Flags().Set("label", "lbl1=v1") 123 cmd.Flags().Set("label", "lbl2=v2") 124 assert.NilError(t, cmd.Execute()) 125 assert.Check(t, is.Equal(name, strings.TrimSpace(cli.OutBuffer().String()))) 126 }