github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/command/v3/reset_org_default_isolation_segment_command_test.go (about) 1 package v3_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/sharedaction" 7 "code.cloudfoundry.org/cli/actor/v2action" 8 "code.cloudfoundry.org/cli/actor/v3action" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccversion" 10 "code.cloudfoundry.org/cli/command/commandfakes" 11 "code.cloudfoundry.org/cli/command/translatableerror" 12 "code.cloudfoundry.org/cli/command/v3" 13 "code.cloudfoundry.org/cli/command/v3/v3fakes" 14 "code.cloudfoundry.org/cli/util/configv3" 15 "code.cloudfoundry.org/cli/util/ui" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 . "github.com/onsi/gomega/gbytes" 19 ) 20 21 var _ = Describe("reset-org-default-isolation-segment Command", func() { 22 var ( 23 cmd v3.ResetOrgDefaultIsolationSegmentCommand 24 testUI *ui.UI 25 fakeConfig *commandfakes.FakeConfig 26 fakeSharedActor *commandfakes.FakeSharedActor 27 fakeActor *v3fakes.FakeResetOrgDefaultIsolationSegmentActor 28 fakeActorV2 *v3fakes.FakeResetOrgDefaultIsolationSegmentActorV2 29 binaryName string 30 executeErr error 31 orgName string 32 ) 33 34 BeforeEach(func() { 35 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 36 fakeConfig = new(commandfakes.FakeConfig) 37 fakeSharedActor = new(commandfakes.FakeSharedActor) 38 fakeActor = new(v3fakes.FakeResetOrgDefaultIsolationSegmentActor) 39 fakeActorV2 = new(v3fakes.FakeResetOrgDefaultIsolationSegmentActorV2) 40 41 cmd = v3.ResetOrgDefaultIsolationSegmentCommand{ 42 UI: testUI, 43 Config: fakeConfig, 44 SharedActor: fakeSharedActor, 45 Actor: fakeActor, 46 ActorV2: fakeActorV2, 47 } 48 49 binaryName = "faceman" 50 fakeConfig.BinaryNameReturns(binaryName) 51 orgName = "some-org" 52 53 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3) 54 cmd.RequiredArgs.OrgName = orgName 55 }) 56 57 JustBeforeEach(func() { 58 executeErr = cmd.Execute(nil) 59 }) 60 61 Context("when the API version is below the minimum", func() { 62 BeforeEach(func() { 63 fakeActor.CloudControllerAPIVersionReturns("0.0.0") 64 }) 65 66 It("returns a MinimumAPIVersionNotMetError", func() { 67 Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{ 68 CurrentVersion: "0.0.0", 69 MinimumVersion: ccversion.MinVersionIsolationSegmentV3, 70 })) 71 }) 72 }) 73 74 Context("when checking target fails", func() { 75 BeforeEach(func() { 76 fakeSharedActor.CheckTargetReturns(sharedaction.NotLoggedInError{BinaryName: binaryName}) 77 }) 78 79 It("returns an error", func() { 80 Expect(executeErr).To(MatchError(translatableerror.NotLoggedInError{BinaryName: binaryName})) 81 82 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 83 _, checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 84 Expect(checkTargetedOrg).To(BeTrue()) 85 Expect(checkTargetedSpace).To(BeFalse()) 86 }) 87 }) 88 89 Context("when checking file succeeds", func() { 90 BeforeEach(func() { 91 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 92 Name: orgName, 93 GUID: "some-org-guid", 94 }) 95 }) 96 97 Context("when the user is not logged in", func() { 98 var expectedErr error 99 100 BeforeEach(func() { 101 expectedErr = errors.New("some current user error") 102 fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr) 103 }) 104 105 It("return an error", func() { 106 Expect(executeErr).To(Equal(expectedErr)) 107 108 Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1)) 109 }) 110 }) 111 112 Context("when the user is logged in", func() { 113 BeforeEach(func() { 114 fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil) 115 }) 116 117 Context("when the org lookup is unsuccessful", func() { 118 BeforeEach(func() { 119 fakeActorV2.GetOrganizationByNameReturns(v2action.Organization{}, v2action.Warnings{"warning-1", "warning-2"}, v2action.OrganizationNotFoundError{Name: orgName}) 120 }) 121 122 It("returns the warnings and error", func() { 123 Expect(executeErr).To(MatchError(translatableerror.OrganizationNotFoundError{Name: orgName})) 124 Expect(testUI.Err).To(Say("warning-1")) 125 Expect(testUI.Err).To(Say("warning-2")) 126 }) 127 }) 128 129 Context("when the org lookup is successful", func() { 130 BeforeEach(func() { 131 fakeActorV2.GetOrganizationByNameReturns(v2action.Organization{ 132 Name: orgName, 133 GUID: "some-org-guid", 134 }, v2action.Warnings{"warning-1", "warning-2"}, nil) 135 }) 136 137 Context("when the reset succeeds", func() { 138 BeforeEach(func() { 139 fakeActor.ResetOrganizationDefaultIsolationSegmentReturns(v3action.Warnings{"warning-3", "warning-4"}, nil) 140 }) 141 142 It("displays the header and okay", func() { 143 Expect(executeErr).ToNot(HaveOccurred()) 144 145 Expect(testUI.Out).To(Say("Resetting default isolation segment of org %s as banana...", orgName)) 146 147 Expect(testUI.Out).To(Say("OK\n\n")) 148 149 Expect(testUI.Err).To(Say("warning-1")) 150 Expect(testUI.Err).To(Say("warning-2")) 151 Expect(testUI.Err).To(Say("warning-3")) 152 Expect(testUI.Err).To(Say("warning-4")) 153 154 Expect(testUI.Out).To(Say("Applications in spaces of this org that have no isolation segment assigned will be placed in the platform default isolation segment.")) 155 Expect(testUI.Out).To(Say("Running applications need a restart to be moved there.")) 156 157 Expect(fakeActor.ResetOrganizationDefaultIsolationSegmentCallCount()).To(Equal(1)) 158 orgGUID := fakeActor.ResetOrganizationDefaultIsolationSegmentArgsForCall(0) 159 Expect(orgGUID).To(Equal("some-org-guid")) 160 }) 161 }) 162 163 Context("when the reset errors", func() { 164 var expectedErr error 165 BeforeEach(func() { 166 expectedErr = errors.New("some error") 167 fakeActor.ResetOrganizationDefaultIsolationSegmentReturns(v3action.Warnings{"warning-3", "warning-4"}, expectedErr) 168 }) 169 170 It("returns the warnings and error", func() { 171 Expect(executeErr).To(MatchError(expectedErr)) 172 173 Expect(testUI.Out).To(Say("Resetting default isolation segment of org %s as banana...", orgName)) 174 Expect(testUI.Err).To(Say("warning-1")) 175 Expect(testUI.Err).To(Say("warning-2")) 176 Expect(testUI.Err).To(Say("warning-3")) 177 Expect(testUI.Err).To(Say("warning-4")) 178 }) 179 }) 180 }) 181 }) 182 }) 183 })