github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/command/v6/reset_org_default_isolation_segment_command_test.go (about) 1 package v6_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 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/v6" 13 "code.cloudfoundry.org/cli/command/v6/v6fakes" 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 ResetOrgDefaultIsolationSegmentCommand 24 testUI *ui.UI 25 fakeConfig *commandfakes.FakeConfig 26 fakeSharedActor *commandfakes.FakeSharedActor 27 fakeActor *v6fakes.FakeResetOrgDefaultIsolationSegmentActor 28 fakeActorV2 *v6fakes.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(v6fakes.FakeResetOrgDefaultIsolationSegmentActor) 39 fakeActorV2 = new(v6fakes.FakeResetOrgDefaultIsolationSegmentActorV2) 40 41 cmd = 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 cmd.RequiredArgs.OrgName = orgName 54 }) 55 56 JustBeforeEach(func() { 57 executeErr = cmd.Execute(nil) 58 }) 59 60 When("the API version is below the minimum", func() { 61 BeforeEach(func() { 62 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinV3ClientVersion) 63 }) 64 65 It("returns a MinimumAPIVersionNotMetError", func() { 66 Expect(executeErr).To(MatchError(translatableerror.MinimumCFAPIVersionNotMetError{ 67 CurrentVersion: ccversion.MinV3ClientVersion, 68 MinimumVersion: ccversion.MinVersionIsolationSegmentV3, 69 })) 70 }) 71 }) 72 73 When("checking target fails", func() { 74 BeforeEach(func() { 75 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3) 76 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 77 }) 78 79 It("returns an error", func() { 80 Expect(executeErr).To(MatchError(actionerror.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 When("checking file succeeds", func() { 90 BeforeEach(func() { 91 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3) 92 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 93 Name: orgName, 94 GUID: "some-org-guid", 95 }) 96 }) 97 98 When("the user is not logged in", func() { 99 var expectedErr error 100 101 BeforeEach(func() { 102 expectedErr = errors.New("some current user error") 103 fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr) 104 }) 105 106 It("return an error", func() { 107 Expect(executeErr).To(Equal(expectedErr)) 108 109 Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1)) 110 }) 111 }) 112 113 When("the user is logged in", func() { 114 BeforeEach(func() { 115 fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil) 116 }) 117 118 When("the org lookup is unsuccessful", func() { 119 BeforeEach(func() { 120 fakeActorV2.GetOrganizationByNameReturns(v2action.Organization{}, v2action.Warnings{"warning-1", "warning-2"}, actionerror.OrganizationNotFoundError{Name: orgName}) 121 }) 122 123 It("returns the warnings and error", func() { 124 Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: orgName})) 125 Expect(testUI.Err).To(Say("warning-1")) 126 Expect(testUI.Err).To(Say("warning-2")) 127 }) 128 }) 129 130 When("the org lookup is successful", func() { 131 BeforeEach(func() { 132 fakeActorV2.GetOrganizationByNameReturns(v2action.Organization{ 133 Name: orgName, 134 GUID: "some-org-guid", 135 }, v2action.Warnings{"warning-1", "warning-2"}, nil) 136 }) 137 138 When("the reset succeeds", func() { 139 BeforeEach(func() { 140 fakeActor.ResetOrganizationDefaultIsolationSegmentReturns(v3action.Warnings{"warning-3", "warning-4"}, nil) 141 }) 142 143 It("displays the header and okay", func() { 144 Expect(executeErr).ToNot(HaveOccurred()) 145 146 Expect(testUI.Out).To(Say("Resetting default isolation segment of org %s as banana...", orgName)) 147 148 Expect(testUI.Out).To(Say("OK\n\n")) 149 150 Expect(testUI.Err).To(Say("warning-1")) 151 Expect(testUI.Err).To(Say("warning-2")) 152 Expect(testUI.Err).To(Say("warning-3")) 153 Expect(testUI.Err).To(Say("warning-4")) 154 155 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.")) 156 Expect(testUI.Out).To(Say("Running applications need a restart to be moved there.")) 157 158 Expect(fakeActor.ResetOrganizationDefaultIsolationSegmentCallCount()).To(Equal(1)) 159 orgGUID := fakeActor.ResetOrganizationDefaultIsolationSegmentArgsForCall(0) 160 Expect(orgGUID).To(Equal("some-org-guid")) 161 }) 162 }) 163 164 When("the reset errors", func() { 165 var expectedErr error 166 BeforeEach(func() { 167 expectedErr = errors.New("some error") 168 fakeActor.ResetOrganizationDefaultIsolationSegmentReturns(v3action.Warnings{"warning-3", "warning-4"}, expectedErr) 169 }) 170 171 It("returns the warnings and error", func() { 172 Expect(executeErr).To(MatchError(expectedErr)) 173 174 Expect(testUI.Out).To(Say("Resetting default isolation segment of org %s as banana...", orgName)) 175 Expect(testUI.Err).To(Say("warning-1")) 176 Expect(testUI.Err).To(Say("warning-2")) 177 Expect(testUI.Err).To(Say("warning-3")) 178 Expect(testUI.Err).To(Say("warning-4")) 179 }) 180 }) 181 }) 182 }) 183 }) 184 })