github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v2/unbind_security_group_command_test.go (about) 1 package v2_test 2 3 import ( 4 "errors" 5 6 "github.com/liamawhite/cli-with-i18n/actor/sharedaction" 7 "github.com/liamawhite/cli-with-i18n/actor/v2action" 8 "github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccv2" 9 "github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccversion" 10 "github.com/liamawhite/cli-with-i18n/command/commandfakes" 11 "github.com/liamawhite/cli-with-i18n/command/flag" 12 "github.com/liamawhite/cli-with-i18n/command/translatableerror" 13 . "github.com/liamawhite/cli-with-i18n/command/v2" 14 "github.com/liamawhite/cli-with-i18n/command/v2/v2fakes" 15 "github.com/liamawhite/cli-with-i18n/util/configv3" 16 "github.com/liamawhite/cli-with-i18n/util/ui" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 . "github.com/onsi/gomega/gbytes" 20 ) 21 22 var _ = Describe("unbind-security-group Command", func() { 23 var ( 24 cmd UnbindSecurityGroupCommand 25 testUI *ui.UI 26 fakeConfig *commandfakes.FakeConfig 27 fakeSharedActor *commandfakes.FakeSharedActor 28 fakeActor *v2fakes.FakeUnbindSecurityGroupActor 29 binaryName string 30 executeErr error 31 ) 32 33 BeforeEach(func() { 34 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 35 fakeConfig = new(commandfakes.FakeConfig) 36 fakeSharedActor = new(commandfakes.FakeSharedActor) 37 fakeActor = new(v2fakes.FakeUnbindSecurityGroupActor) 38 39 cmd = UnbindSecurityGroupCommand{ 40 UI: testUI, 41 Config: fakeConfig, 42 SharedActor: fakeSharedActor, 43 Actor: fakeActor, 44 } 45 46 binaryName = "faceman" 47 fakeConfig.BinaryNameReturns(binaryName) 48 fakeConfig.ExperimentalReturns(true) 49 50 fakeConfig.CurrentUserReturns( 51 configv3.User{Name: "some-user"}, 52 nil) 53 }) 54 55 JustBeforeEach(func() { 56 executeErr = cmd.Execute(nil) 57 }) 58 59 Context("when getting the current user fails", func() { 60 var expectedErr error 61 62 BeforeEach(func() { 63 expectedErr = errors.New("getting user failed") 64 fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr) 65 }) 66 67 It("returns an error", func() { 68 Expect(executeErr).To(MatchError(expectedErr)) 69 }) 70 }) 71 72 Context("when checking target fails", func() { 73 BeforeEach(func() { 74 fakeSharedActor.CheckTargetReturns(sharedaction.NoOrganizationTargetedError{BinaryName: binaryName}) 75 }) 76 77 It("returns an error", func() { 78 Expect(executeErr).To(MatchError(translatableerror.NoOrganizationTargetedError{BinaryName: "faceman"})) 79 80 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 81 _, checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 82 Expect(checkTargetedOrg).To(BeTrue()) 83 Expect(checkTargetedSpace).To(BeTrue()) 84 }) 85 }) 86 87 Context("when lifecycle is 'some-lifecycle'", func() { 88 // By this point in execution, Goflags will have filtered any invalid 89 // lifecycle phase. We use 'some-lifecycle' to test that the command 90 // merely passes the value presented by Goflags. 91 BeforeEach(func() { 92 cmd.Lifecycle = flag.SecurityGroupLifecycle("some-lifecycle") 93 }) 94 95 Context("when only the security group is provided", func() { 96 BeforeEach(func() { 97 cmd.RequiredArgs.SecurityGroupName = "some-security-group" 98 }) 99 100 Context("when org and space are targeted", func() { 101 BeforeEach(func() { 102 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"}) 103 fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"}) 104 fakeActor.UnbindSecurityGroupByNameAndSpaceReturns( 105 v2action.Warnings{"unbind warning"}, 106 nil) 107 }) 108 109 It("unbinds the security group from the targeted space", func() { 110 Expect(executeErr).ToNot(HaveOccurred()) 111 112 Expect(testUI.Out).To(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", "some-security-group", "some-org", "some-space", "some-user")) 113 Expect(testUI.Out).To(Say("OK\n\n")) 114 Expect(testUI.Out).To(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 115 Expect(testUI.Err).To(Say("unbind warning")) 116 117 Expect(fakeConfig.TargetedOrganizationCallCount()).To(Equal(1)) 118 Expect(fakeConfig.TargetedSpaceCallCount()).To(Equal(1)) 119 Expect(fakeActor.UnbindSecurityGroupByNameAndSpaceCallCount()).To(Equal(1)) 120 securityGroupName, spaceGUID, lifecycle := fakeActor.UnbindSecurityGroupByNameAndSpaceArgsForCall(0) 121 Expect(securityGroupName).To(Equal("some-security-group")) 122 Expect(spaceGUID).To(Equal("some-space-guid")) 123 Expect(lifecycle).To(Equal(ccv2.SecurityGroupLifecycle("some-lifecycle"))) 124 }) 125 126 Context("when the actor returns a security group not found error", func() { 127 BeforeEach(func() { 128 fakeActor.UnbindSecurityGroupByNameAndSpaceReturns( 129 v2action.Warnings{"unbind warning"}, 130 v2action.SecurityGroupNotFoundError{Name: "some-security-group"}, 131 ) 132 }) 133 134 It("returns a translated security group not found error", func() { 135 Expect(testUI.Err).To(Say("unbind warning")) 136 137 Expect(executeErr).To(MatchError(translatableerror.SecurityGroupNotFoundError{Name: "some-security-group"})) 138 }) 139 }) 140 141 Context("when the actor returns a security group not bound error", func() { 142 BeforeEach(func() { 143 fakeActor.UnbindSecurityGroupByNameAndSpaceReturns( 144 v2action.Warnings{"unbind warning"}, 145 v2action.SecurityGroupNotBoundError{ 146 Name: "some-security-group", 147 Lifecycle: "some-lifecycle", 148 }) 149 }) 150 151 It("returns a translated security group not bound warning but has no error", func() { 152 Expect(testUI.Err).To(Say("unbind warning")) 153 Expect(testUI.Err).To(Say("Security group some-security-group not bound to this space for lifecycle phase 'some-lifecycle'.")) 154 155 Expect(testUI.Out).To(Say("OK")) 156 Expect(testUI.Out).NotTo(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 157 158 Expect(executeErr).NotTo(HaveOccurred()) 159 }) 160 }) 161 162 Context("when the actor returns an error", func() { 163 var expectedErr error 164 165 BeforeEach(func() { 166 expectedErr = errors.New("some unbind security group error") 167 fakeActor.UnbindSecurityGroupByNameAndSpaceReturns( 168 v2action.Warnings{"unbind warning"}, 169 expectedErr, 170 ) 171 }) 172 173 It("returns a translated security no found error", func() { 174 Expect(testUI.Err).To(Say("unbind warning")) 175 176 Expect(executeErr).To(MatchError(expectedErr)) 177 }) 178 }) 179 }) 180 }) 181 182 Context("when the security group, org, and space are provided", func() { 183 BeforeEach(func() { 184 cmd.RequiredArgs.SecurityGroupName = "some-security-group" 185 cmd.RequiredArgs.OrganizationName = "some-org" 186 cmd.RequiredArgs.SpaceName = "some-space" 187 }) 188 189 Context("when checking target fails", func() { 190 BeforeEach(func() { 191 fakeSharedActor.CheckTargetReturns(sharedaction.NoOrganizationTargetedError{BinaryName: binaryName}) 192 }) 193 194 It("returns an error", func() { 195 Expect(executeErr).To(MatchError(translatableerror.NoOrganizationTargetedError{BinaryName: "faceman"})) 196 197 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 198 _, checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 199 Expect(checkTargetedOrg).To(BeFalse()) 200 Expect(checkTargetedSpace).To(BeFalse()) 201 }) 202 }) 203 204 Context("when the user is logged in", func() { 205 BeforeEach(func() { 206 fakeActor.UnbindSecurityGroupByNameOrganizationNameAndSpaceNameReturns( 207 v2action.Warnings{"unbind warning"}, 208 nil) 209 }) 210 211 It("the security group is unbound from the targeted space", func() { 212 Expect(testUI.Out).To(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", "some-security-group", "some-org", "some-space", "some-user")) 213 Expect(testUI.Out).To(Say("OK\n\n")) 214 Expect(testUI.Out).To(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 215 Expect(testUI.Err).To(Say("unbind warning")) 216 217 Expect(fakeActor.UnbindSecurityGroupByNameOrganizationNameAndSpaceNameCallCount()).To(Equal(1)) 218 securityGroupName, orgName, spaceName, lifecycle := fakeActor.UnbindSecurityGroupByNameOrganizationNameAndSpaceNameArgsForCall(0) 219 Expect(securityGroupName).To(Equal("some-security-group")) 220 Expect(orgName).To(Equal("some-org")) 221 Expect(spaceName).To(Equal("some-space")) 222 Expect(lifecycle).To(Equal(ccv2.SecurityGroupLifecycle("some-lifecycle"))) 223 }) 224 }) 225 226 Context("when the actor returns a security group not found error", func() { 227 BeforeEach(func() { 228 fakeActor.UnbindSecurityGroupByNameOrganizationNameAndSpaceNameReturns( 229 v2action.Warnings{"unbind warning"}, 230 v2action.SecurityGroupNotFoundError{Name: "some-security-group"}, 231 ) 232 }) 233 234 It("returns a translated security group not found error", func() { 235 Expect(testUI.Err).To(Say("unbind warning")) 236 237 Expect(executeErr).To(MatchError(translatableerror.SecurityGroupNotFoundError{Name: "some-security-group"})) 238 }) 239 }) 240 241 Context("when the actor returns a security group not bound error", func() { 242 BeforeEach(func() { 243 fakeActor.UnbindSecurityGroupByNameOrganizationNameAndSpaceNameReturns( 244 v2action.Warnings{"unbind warning"}, 245 v2action.SecurityGroupNotBoundError{ 246 Name: "some-security-group", 247 Lifecycle: ccv2.SecurityGroupLifecycle("some-lifecycle"), 248 }) 249 }) 250 251 It("returns a translated security group not bound warning but has no error", func() { 252 Expect(testUI.Err).To(Say("unbind warning")) 253 Expect(testUI.Err).To(Say("Security group some-security-group not bound to this space for lifecycle phase 'some-lifecycle'.")) 254 255 Expect(testUI.Out).To(Say("OK")) 256 Expect(testUI.Out).NotTo(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 257 258 Expect(executeErr).NotTo(HaveOccurred()) 259 }) 260 }) 261 262 Context("when the actor returns an error", func() { 263 var expectedErr error 264 265 BeforeEach(func() { 266 expectedErr = errors.New("some unbind security group error") 267 fakeActor.UnbindSecurityGroupByNameOrganizationNameAndSpaceNameReturns( 268 v2action.Warnings{"unbind warning"}, 269 expectedErr, 270 ) 271 }) 272 273 It("returns a translated security no found error", func() { 274 Expect(testUI.Err).To(Say("unbind warning")) 275 276 Expect(executeErr).To(MatchError(expectedErr)) 277 }) 278 }) 279 }) 280 281 Context("when the security group and org are provided, but the space is not", func() { 282 BeforeEach(func() { 283 cmd.RequiredArgs.SecurityGroupName = "some-security-group" 284 cmd.RequiredArgs.OrganizationName = "some-org" 285 }) 286 287 It("an error is returned", func() { 288 Expect(executeErr).To(MatchError(translatableerror.ThreeRequiredArgumentsError{ 289 ArgumentName1: "SECURITY_GROUP", 290 ArgumentName2: "ORG", 291 ArgumentName3: "SPACE"})) 292 293 Expect(testUI.Out).NotTo(Say("Unbinding security group")) 294 295 Expect(fakeActor.UnbindSecurityGroupByNameOrganizationNameAndSpaceNameCallCount()).To(Equal(0)) 296 }) 297 }) 298 }) 299 300 Context("when lifecycle is 'running'", func() { 301 BeforeEach(func() { 302 cmd.Lifecycle = flag.SecurityGroupLifecycle(ccv2.SecurityGroupLifecycleRunning) 303 fakeActor.CloudControllerAPIVersionReturns("2.34.0") 304 }) 305 306 It("does no version check", func() { 307 Expect(executeErr).NotTo(HaveOccurred()) 308 }) 309 }) 310 311 Context("when lifecycle is 'staging'", func() { 312 BeforeEach(func() { 313 cmd.Lifecycle = flag.SecurityGroupLifecycle(ccv2.SecurityGroupLifecycleStaging) 314 }) 315 316 Context("when the version check fails", func() { 317 BeforeEach(func() { 318 fakeActor.CloudControllerAPIVersionReturns("2.34.0") 319 }) 320 321 It("returns a MinimumAPIVersionNotMetError", func() { 322 Expect(executeErr).To(MatchError(translatableerror.LifecycleMinimumAPIVersionNotMetError{ 323 CurrentVersion: "2.34.0", 324 MinimumVersion: ccversion.MinVersionLifecyleStagingV2, 325 })) 326 Expect(fakeActor.CloudControllerAPIVersionCallCount()).To(Equal(1)) 327 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(0)) 328 }) 329 }) 330 }) 331 })