github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/unshare_service_command_test.go (about) 1 package v3_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/actor/v2v3action" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccversion" 9 "code.cloudfoundry.org/cli/command/commandfakes" 10 "code.cloudfoundry.org/cli/command/translatableerror" 11 "code.cloudfoundry.org/cli/command/v3" 12 "code.cloudfoundry.org/cli/command/v3/v3fakes" 13 "code.cloudfoundry.org/cli/util/configv3" 14 "code.cloudfoundry.org/cli/util/ui" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 . "github.com/onsi/gomega/gbytes" 18 ) 19 20 var _ = Describe("unshare-service Command", func() { 21 var ( 22 cmd v3.UnshareServiceCommand 23 testUI *ui.UI 24 fakeConfig *commandfakes.FakeConfig 25 fakeSharedActor *commandfakes.FakeSharedActor 26 fakeActor *v3fakes.FakeUnshareServiceActor 27 input *Buffer 28 binaryName string 29 executeErr error 30 ) 31 32 BeforeEach(func() { 33 input = NewBuffer() 34 testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer()) 35 fakeConfig = new(commandfakes.FakeConfig) 36 fakeSharedActor = new(commandfakes.FakeSharedActor) 37 fakeActor = new(v3fakes.FakeUnshareServiceActor) 38 39 cmd = v3.UnshareServiceCommand{ 40 UI: testUI, 41 Config: fakeConfig, 42 SharedActor: fakeSharedActor, 43 Actor: fakeActor, 44 } 45 46 cmd.RequiredArgs.ServiceInstance = "some-service-instance" 47 48 binaryName = "faceman" 49 fakeConfig.BinaryNameReturns(binaryName) 50 fakeActor.CloudControllerV3APIVersionReturns(ccversion.MinVersionShareServiceV3) 51 }) 52 53 JustBeforeEach(func() { 54 executeErr = cmd.Execute(nil) 55 }) 56 57 Context("when the API version is below the minimum", func() { 58 BeforeEach(func() { 59 fakeActor.CloudControllerV3APIVersionReturns("0.0.0") 60 }) 61 62 It("returns a MinimumAPIVersionNotMetError", func() { 63 Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{ 64 CurrentVersion: "0.0.0", 65 MinimumVersion: ccversion.MinVersionShareServiceV3, 66 })) 67 }) 68 }) 69 70 Context("when checking target fails", func() { 71 BeforeEach(func() { 72 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 73 }) 74 75 It("returns an error", func() { 76 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 77 78 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 79 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 80 Expect(checkTargetedOrg).To(BeTrue()) 81 Expect(checkTargetedSpace).To(BeTrue()) 82 }) 83 }) 84 85 Context("when the user is logged in, and a space and org are targeted", func() { 86 BeforeEach(func() { 87 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 88 GUID: "some-org-guid", 89 Name: "some-org", 90 }) 91 fakeConfig.TargetedSpaceReturns(configv3.Space{ 92 GUID: "some-space-guid", 93 Name: "some-space", 94 }) 95 }) 96 97 Context("when an error occurs getting the current user", func() { 98 var expectedErr error 99 100 BeforeEach(func() { 101 expectedErr = errors.New("get current user error") 102 fakeConfig.CurrentUserReturns( 103 configv3.User{}, 104 expectedErr) 105 }) 106 107 It("returns the error", func() { 108 Expect(executeErr).To(MatchError(expectedErr)) 109 Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1)) 110 }) 111 }) 112 113 Context("when no errors occur getting the current user", func() { 114 BeforeEach(func() { 115 fakeConfig.CurrentUserReturns( 116 configv3.User{Name: "some-user"}, 117 nil) 118 }) 119 120 Context("when the '-f' flag is provided (to force non-prompting)", func() { 121 BeforeEach(func() { 122 cmd.Force = true 123 }) 124 125 Context("when the '-o' flag is NOT provided (when we want to unshare a space in the currently targeted org)", func() { 126 BeforeEach(func() { 127 cmd.SharedToSpaceName = "some-shared-to-space" 128 }) 129 130 Context("when no errors occur unsharing the service instance", func() { 131 BeforeEach(func() { 132 fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceReturns( 133 v2v3action.Warnings{"unshare-service-warning"}, 134 nil) 135 }) 136 137 It("unshares the service instance from the currently targetd org and provided space name, and displays all warnings", func() { 138 Expect(executeErr).ToNot(HaveOccurred()) 139 140 Expect(testUI.Out).To(Say("Unsharing service instance some-service-instance from org some-org / space some-shared-to-space as some-user\\.\\.\\.")) 141 Expect(testUI.Out).To(Say("OK")) 142 143 Expect(testUI.Err).ToNot(Say("WARNING: Unsharing this service instance will remove any service bindings that exist in any spaces that this instance is shared into. This could cause applications to stop working.")) 144 Expect(testUI.Err).To(Say("unshare-service-warning")) 145 146 Expect(fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceCallCount()).To(Equal(1)) 147 sharedToOrgNameArg, sharedToSpaceNameArg, serviceInstanceNameArg, currentlyTargetedSpaceGUIDArg := fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceArgsForCall(0) 148 Expect(sharedToOrgNameArg).To(Equal("some-org")) 149 Expect(sharedToSpaceNameArg).To(Equal("some-shared-to-space")) 150 Expect(serviceInstanceNameArg).To(Equal("some-service-instance")) 151 Expect(currentlyTargetedSpaceGUIDArg).To(Equal("some-space-guid")) 152 }) 153 }) 154 155 Context("when an error occurs unsharing the service instance", func() { 156 var expectedErr error 157 158 BeforeEach(func() { 159 expectedErr = errors.New("unshare error") 160 fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceReturns( 161 v2v3action.Warnings{"unshare-service-warning"}, 162 expectedErr) 163 }) 164 165 It("returns the error and displays all warnings", func() { 166 Expect(executeErr).To(MatchError(expectedErr)) 167 Expect(testUI.Err).To(Say("unshare-service-warning")) 168 }) 169 }) 170 171 Context("when the service instance is not shared to the space we want to unshare from", func() { 172 BeforeEach(func() { 173 fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceReturns( 174 v2v3action.Warnings{"unshare-service-warning"}, 175 actionerror.ServiceInstanceNotSharedToSpaceError{ServiceInstanceName: "some-service-instance"}) 176 }) 177 178 It("does not return an error, displays that the service instance is not shared and displays all warnings", func() { 179 Expect(executeErr).ToNot(HaveOccurred()) 180 Expect(testUI.Out).To(Say("Service instance some-service-instance is not shared with space some-shared-to-space in organization some-org\\.")) 181 Expect(testUI.Out).To(Say("OK")) 182 Expect(testUI.Err).To(Say("unshare-service-warning")) 183 }) 184 }) 185 }) 186 187 Context("when the '-o' flag is provided (when the space we want to unshare does not belong to the currently targeted org)", func() { 188 BeforeEach(func() { 189 cmd.SharedToSpaceName = "some-other-space" 190 cmd.SharedToOrgName = "some-other-org" 191 192 fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceReturns( 193 v2v3action.Warnings{"unshare-service-warning"}, 194 nil) 195 }) 196 197 It("performs the unshare with the provided org name", func() { 198 Expect(executeErr).ToNot(HaveOccurred()) 199 200 Expect(testUI.Out).To(Say("Unsharing service instance some-service-instance from org some-other-org / space some-other-space as some-user\\.\\.\\.")) 201 Expect(testUI.Out).To(Say("OK")) 202 203 Expect(testUI.Err).ToNot(Say("WARNING: Unsharing this service instance will remove any service bindings that exist in any spaces that this instance is shared into. This could cause applications to stop working.")) 204 Expect(testUI.Err).To(Say("unshare-service-warning")) 205 206 Expect(fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceCallCount()).To(Equal(1)) 207 sharedToOrgNameArg, sharedToSpaceNameArg, serviceInstanceNameArg, currentlyTargetedSpaceGUIDArg := fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceArgsForCall(0) 208 Expect(sharedToOrgNameArg).To(Equal("some-other-org")) 209 Expect(sharedToSpaceNameArg).To(Equal("some-other-space")) 210 Expect(serviceInstanceNameArg).To(Equal("some-service-instance")) 211 Expect(currentlyTargetedSpaceGUIDArg).To(Equal("some-space-guid")) 212 }) 213 }) 214 }) 215 216 Context("when the -f flag is NOT provided", func() { 217 BeforeEach(func() { 218 cmd.Force = false 219 cmd.SharedToSpaceName = "some-shared-to-space" 220 }) 221 222 Context("when the user inputs yes", func() { 223 BeforeEach(func() { 224 _, err := input.Write([]byte("y\n")) 225 Expect(err).ToNot(HaveOccurred()) 226 227 fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceReturns( 228 v2v3action.Warnings{"unshare-service-warning"}, 229 nil) 230 }) 231 232 It("unshares the service instance and displays all warnings", func() { 233 Expect(executeErr).ToNot(HaveOccurred()) 234 235 Expect(testUI.Out).To(Say("Really unshare the service instance\\? \\[yN\\]")) 236 Expect(testUI.Out).To(Say("Unsharing service instance some-service-instance from org some-org / space some-shared-to-space as some-user\\.\\.\\.")) 237 Expect(testUI.Out).To(Say("OK")) 238 239 Expect(testUI.Err).To(Say("WARNING: Unsharing this service instance will remove any service bindings that exist in any spaces that this instance is shared into. This could cause applications to stop working.")) 240 Expect(testUI.Err).To(Say("unshare-service-warning")) 241 242 Expect(fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceCallCount()).To(Equal(1)) 243 sharedToOrgNameArg, sharedToSpaceNameArg, serviceInstanceNameArg, currentlyTargetedSpaceGUIDArg := fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceArgsForCall(0) 244 Expect(sharedToOrgNameArg).To(Equal("some-org")) 245 Expect(sharedToSpaceNameArg).To(Equal("some-shared-to-space")) 246 Expect(serviceInstanceNameArg).To(Equal("some-service-instance")) 247 Expect(currentlyTargetedSpaceGUIDArg).To(Equal("some-space-guid")) 248 }) 249 }) 250 251 Context("when the user inputs no", func() { 252 BeforeEach(func() { 253 _, err := input.Write([]byte("n\n")) 254 Expect(err).ToNot(HaveOccurred()) 255 }) 256 257 It("cancels the unshared", func() { 258 Expect(executeErr).ToNot(HaveOccurred()) 259 260 Expect(testUI.Out).To(Say("Really unshare the service instance\\? \\[yN\\]")) 261 Expect(testUI.Out).To(Say("Unshare cancelled")) 262 263 Expect(testUI.Err).To(Say("WARNING: Unsharing this service instance will remove any service bindings that exist in any spaces that this instance is shared into. This could cause applications to stop working.")) 264 265 Expect(fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceCallCount()).To(Equal(0)) 266 }) 267 }) 268 269 Context("when the user chooses the default", func() { 270 BeforeEach(func() { 271 _, err := input.Write([]byte("\n")) 272 Expect(err).ToNot(HaveOccurred()) 273 }) 274 275 It("cancels the unshare", func() { 276 Expect(executeErr).ToNot(HaveOccurred()) 277 278 Expect(testUI.Out).To(Say("Really unshare the service instance\\? \\[yN\\]")) 279 Expect(testUI.Out).To(Say("Unshare cancelled")) 280 281 Expect(testUI.Err).To(Say("WARNING: Unsharing this service instance will remove any service bindings that exist in any spaces that this instance is shared into. This could cause applications to stop working.")) 282 283 Expect(fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceCallCount()).To(Equal(0)) 284 }) 285 }) 286 287 Context("when the user input is invalid", func() { 288 BeforeEach(func() { 289 _, err := input.Write([]byte("e\n\n")) 290 Expect(err).ToNot(HaveOccurred()) 291 }) 292 293 It("prompts for unshare confirmation again", func() { 294 Expect(executeErr).NotTo(HaveOccurred()) 295 296 Expect(testUI.Out).To(Say("Really unshare the service instance\\? \\[yN\\]")) 297 Expect(testUI.Out).To(Say("invalid input \\(not y, n, yes, or no\\)")) 298 Expect(testUI.Out).To(Say("Really unshare the service instance\\? \\[yN\\]")) 299 300 Expect(testUI.Err).To(Say("WARNING: Unsharing this service instance will remove any service bindings that exist in any spaces that this instance is shared into. This could cause applications to stop working.")) 301 302 Expect(fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceCallCount()).To(Equal(0)) 303 }) 304 }) 305 }) 306 }) 307 }) 308 })