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