github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/stack/remove_test.go (about) 1 package stack 2 3 import ( 4 "errors" 5 "io/ioutil" 6 "strings" 7 "testing" 8 9 "github.com/docker/cli/internal/test" 10 "gotest.tools/v3/assert" 11 is "gotest.tools/v3/assert/cmp" 12 ) 13 14 func fakeClientForRemoveStackTest(version string) *fakeClient { 15 allServices := []string{ 16 objectName("foo", "service1"), 17 objectName("foo", "service2"), 18 objectName("bar", "service1"), 19 objectName("bar", "service2"), 20 } 21 allNetworks := []string{ 22 objectName("foo", "network1"), 23 objectName("bar", "network1"), 24 } 25 allSecrets := []string{ 26 objectName("foo", "secret1"), 27 objectName("foo", "secret2"), 28 objectName("bar", "secret1"), 29 } 30 allConfigs := []string{ 31 objectName("foo", "config1"), 32 objectName("foo", "config2"), 33 objectName("bar", "config1"), 34 } 35 return &fakeClient{ 36 version: version, 37 services: allServices, 38 networks: allNetworks, 39 secrets: allSecrets, 40 configs: allConfigs, 41 } 42 } 43 44 func TestRemoveWithEmptyName(t *testing.T) { 45 cmd := newRemoveCommand(test.NewFakeCli(&fakeClient{}), &orchestrator) 46 cmd.SetArgs([]string{"good", "' '", "alsogood"}) 47 cmd.SetOut(ioutil.Discard) 48 49 assert.ErrorContains(t, cmd.Execute(), `invalid stack name: "' '"`) 50 } 51 52 func TestRemoveStackVersion124DoesNotRemoveConfigsOrSecrets(t *testing.T) { 53 client := fakeClientForRemoveStackTest("1.24") 54 cmd := newRemoveCommand(test.NewFakeCli(client), &orchestrator) 55 cmd.SetArgs([]string{"foo", "bar"}) 56 57 assert.NilError(t, cmd.Execute()) 58 assert.Check(t, is.DeepEqual(buildObjectIDs(client.services), client.removedServices)) 59 assert.Check(t, is.DeepEqual(buildObjectIDs(client.networks), client.removedNetworks)) 60 assert.Check(t, is.Len(client.removedSecrets, 0)) 61 assert.Check(t, is.Len(client.removedConfigs, 0)) 62 } 63 64 func TestRemoveStackVersion125DoesNotRemoveConfigs(t *testing.T) { 65 client := fakeClientForRemoveStackTest("1.25") 66 cmd := newRemoveCommand(test.NewFakeCli(client), &orchestrator) 67 cmd.SetArgs([]string{"foo", "bar"}) 68 69 assert.NilError(t, cmd.Execute()) 70 assert.Check(t, is.DeepEqual(buildObjectIDs(client.services), client.removedServices)) 71 assert.Check(t, is.DeepEqual(buildObjectIDs(client.networks), client.removedNetworks)) 72 assert.Check(t, is.DeepEqual(buildObjectIDs(client.secrets), client.removedSecrets)) 73 assert.Check(t, is.Len(client.removedConfigs, 0)) 74 } 75 76 func TestRemoveStackVersion130RemovesEverything(t *testing.T) { 77 client := fakeClientForRemoveStackTest("1.30") 78 cmd := newRemoveCommand(test.NewFakeCli(client), &orchestrator) 79 cmd.SetArgs([]string{"foo", "bar"}) 80 81 assert.NilError(t, cmd.Execute()) 82 assert.Check(t, is.DeepEqual(buildObjectIDs(client.services), client.removedServices)) 83 assert.Check(t, is.DeepEqual(buildObjectIDs(client.networks), client.removedNetworks)) 84 assert.Check(t, is.DeepEqual(buildObjectIDs(client.secrets), client.removedSecrets)) 85 assert.Check(t, is.DeepEqual(buildObjectIDs(client.configs), client.removedConfigs)) 86 } 87 88 func TestRemoveStackSkipEmpty(t *testing.T) { 89 allServices := []string{objectName("bar", "service1"), objectName("bar", "service2")} 90 allServiceIDs := buildObjectIDs(allServices) 91 92 allNetworks := []string{objectName("bar", "network1")} 93 allNetworkIDs := buildObjectIDs(allNetworks) 94 95 allSecrets := []string{objectName("bar", "secret1")} 96 allSecretIDs := buildObjectIDs(allSecrets) 97 98 allConfigs := []string{objectName("bar", "config1")} 99 allConfigIDs := buildObjectIDs(allConfigs) 100 101 fakeClient := &fakeClient{ 102 version: "1.30", 103 services: allServices, 104 networks: allNetworks, 105 secrets: allSecrets, 106 configs: allConfigs, 107 } 108 fakeCli := test.NewFakeCli(fakeClient) 109 cmd := newRemoveCommand(fakeCli, &orchestrator) 110 cmd.SetArgs([]string{"foo", "bar"}) 111 112 assert.NilError(t, cmd.Execute()) 113 expectedList := []string{"Removing service bar_service1", 114 "Removing service bar_service2", 115 "Removing secret bar_secret1", 116 "Removing config bar_config1", 117 "Removing network bar_network1\n", 118 } 119 assert.Check(t, is.Equal(strings.Join(expectedList, "\n"), fakeCli.OutBuffer().String())) 120 assert.Check(t, is.Contains(fakeCli.ErrBuffer().String(), "Nothing found in stack: foo\n")) 121 assert.Check(t, is.DeepEqual(allServiceIDs, fakeClient.removedServices)) 122 assert.Check(t, is.DeepEqual(allNetworkIDs, fakeClient.removedNetworks)) 123 assert.Check(t, is.DeepEqual(allSecretIDs, fakeClient.removedSecrets)) 124 assert.Check(t, is.DeepEqual(allConfigIDs, fakeClient.removedConfigs)) 125 } 126 127 func TestRemoveContinueAfterError(t *testing.T) { 128 allServices := []string{objectName("foo", "service1"), objectName("bar", "service1")} 129 allServiceIDs := buildObjectIDs(allServices) 130 131 allNetworks := []string{objectName("foo", "network1"), objectName("bar", "network1")} 132 allNetworkIDs := buildObjectIDs(allNetworks) 133 134 allSecrets := []string{objectName("foo", "secret1"), objectName("bar", "secret1")} 135 allSecretIDs := buildObjectIDs(allSecrets) 136 137 allConfigs := []string{objectName("foo", "config1"), objectName("bar", "config1")} 138 allConfigIDs := buildObjectIDs(allConfigs) 139 140 removedServices := []string{} 141 cli := &fakeClient{ 142 version: "1.30", 143 services: allServices, 144 networks: allNetworks, 145 secrets: allSecrets, 146 configs: allConfigs, 147 148 serviceRemoveFunc: func(serviceID string) error { 149 removedServices = append(removedServices, serviceID) 150 151 if strings.Contains(serviceID, "foo") { 152 return errors.New("") 153 } 154 return nil 155 }, 156 } 157 cmd := newRemoveCommand(test.NewFakeCli(cli), &orchestrator) 158 cmd.SetOut(ioutil.Discard) 159 cmd.SetArgs([]string{"foo", "bar"}) 160 161 assert.Error(t, cmd.Execute(), "Failed to remove some resources from stack: foo") 162 assert.Check(t, is.DeepEqual(allServiceIDs, removedServices)) 163 assert.Check(t, is.DeepEqual(allNetworkIDs, cli.removedNetworks)) 164 assert.Check(t, is.DeepEqual(allSecretIDs, cli.removedSecrets)) 165 assert.Check(t, is.DeepEqual(allConfigIDs, cli.removedConfigs)) 166 }