github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/command/v3/set_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("set-org-default-isolation-segment Command", func() { 22 var ( 23 cmd v3.SetOrgDefaultIsolationSegmentCommand 24 testUI *ui.UI 25 fakeConfig *commandfakes.FakeConfig 26 fakeSharedActor *commandfakes.FakeSharedActor 27 fakeActor *v3fakes.FakeSetOrgDefaultIsolationSegmentActor 28 fakeActorV2 *v3fakes.FakeSetOrgDefaultIsolationSegmentActorV2 29 binaryName string 30 executeErr error 31 isolationSegment string 32 org string 33 ) 34 35 BeforeEach(func() { 36 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 37 fakeConfig = new(commandfakes.FakeConfig) 38 fakeSharedActor = new(commandfakes.FakeSharedActor) 39 fakeActor = new(v3fakes.FakeSetOrgDefaultIsolationSegmentActor) 40 fakeActorV2 = new(v3fakes.FakeSetOrgDefaultIsolationSegmentActorV2) 41 42 cmd = v3.SetOrgDefaultIsolationSegmentCommand{ 43 UI: testUI, 44 Config: fakeConfig, 45 SharedActor: fakeSharedActor, 46 Actor: fakeActor, 47 ActorV2: fakeActorV2, 48 } 49 50 binaryName = "faceman" 51 fakeConfig.BinaryNameReturns(binaryName) 52 org = "some-org" 53 isolationSegment = "segment1" 54 55 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3) 56 }) 57 58 JustBeforeEach(func() { 59 executeErr = cmd.Execute(nil) 60 }) 61 62 Context("when the API version is below the minimum", func() { 63 BeforeEach(func() { 64 fakeActor.CloudControllerAPIVersionReturns("0.0.0") 65 }) 66 67 It("returns a MinimumAPIVersionNotMetError", func() { 68 Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{ 69 CurrentVersion: "0.0.0", 70 MinimumVersion: ccversion.MinVersionIsolationSegmentV3, 71 })) 72 }) 73 }) 74 75 Context("when checking target fails", func() { 76 BeforeEach(func() { 77 fakeSharedActor.CheckTargetReturns(sharedaction.NotLoggedInError{BinaryName: binaryName}) 78 }) 79 80 It("returns an error", func() { 81 Expect(executeErr).To(MatchError(translatableerror.NotLoggedInError{BinaryName: binaryName})) 82 83 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 84 _, checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 85 Expect(checkTargetedOrg).To(BeFalse()) 86 Expect(checkTargetedSpace).To(BeFalse()) 87 }) 88 }) 89 90 Context("when fetching the user fails", func() { 91 BeforeEach(func() { 92 fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("some-error")) 93 }) 94 95 It("returns an error", func() { 96 Expect(executeErr).To(MatchError("some-error")) 97 }) 98 }) 99 100 Context("when the user is logged in", func() { 101 BeforeEach(func() { 102 fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil) 103 104 cmd.RequiredArgs.OrganizationName = org 105 cmd.RequiredArgs.IsolationSegmentName = isolationSegment 106 }) 107 108 Context("when the org lookup is unsuccessful", func() { 109 BeforeEach(func() { 110 fakeActorV2.GetOrganizationByNameReturns(v2action.Organization{}, v2action.Warnings{"I am a warning", "I am also a warning"}, v2action.OrganizationNotFoundError{Name: org}) 111 }) 112 113 It("returns the warnings and error", func() { 114 Expect(executeErr).To(MatchError(translatableerror.OrganizationNotFoundError{Name: org})) 115 Expect(testUI.Err).To(Say("I am a warning")) 116 Expect(testUI.Err).To(Say("I am also a warning")) 117 }) 118 }) 119 120 Context("when the org lookup is successful", func() { 121 BeforeEach(func() { 122 fakeActorV2.GetOrganizationByNameReturns(v2action.Organization{ 123 Name: org, 124 GUID: "some-org-guid", 125 }, v2action.Warnings{"org-warning-1", "org-warning-2"}, nil) 126 }) 127 128 Context("when the isolation segment lookup is unsuccessful", func() { 129 BeforeEach(func() { 130 fakeActor.GetIsolationSegmentByNameReturns(v3action.IsolationSegment{}, v3action.Warnings{"iso-seg-warning-1", "iso-seg-warning-2"}, v3action.IsolationSegmentNotFoundError{Name: isolationSegment}) 131 }) 132 133 It("returns the warnings and error", func() { 134 Expect(executeErr).To(MatchError(translatableerror.IsolationSegmentNotFoundError{Name: isolationSegment})) 135 Expect(testUI.Err).To(Say("org-warning-1")) 136 Expect(testUI.Err).To(Say("org-warning-2")) 137 Expect(testUI.Err).To(Say("iso-seg-warning-1")) 138 Expect(testUI.Err).To(Say("iso-seg-warning-2")) 139 }) 140 }) 141 142 Context("when the entitlement is successful", func() { 143 BeforeEach(func() { 144 fakeActor.GetIsolationSegmentByNameReturns(v3action.IsolationSegment{GUID: "some-iso-guid"}, v3action.Warnings{"iso-seg-warning-1", "iso-seg-warning-2"}, nil) 145 fakeActor.SetOrganizationDefaultIsolationSegmentReturns(v3action.Warnings{"entitlement-warning", "banana"}, nil) 146 }) 147 148 It("Displays the header and okay", func() { 149 Expect(executeErr).ToNot(HaveOccurred()) 150 151 Expect(testUI.Out).To(Say("Setting isolation segment %s to default on org %s as banana\\.\\.\\.", isolationSegment, org)) 152 Expect(testUI.Out).To(Say("OK")) 153 154 Expect(testUI.Err).To(Say("org-warning-1")) 155 Expect(testUI.Err).To(Say("org-warning-2")) 156 Expect(testUI.Err).To(Say("iso-seg-warning-1")) 157 Expect(testUI.Err).To(Say("iso-seg-warning-2")) 158 Expect(testUI.Err).To(Say("entitlement-warning")) 159 Expect(testUI.Err).To(Say("banana")) 160 161 Expect(testUI.Out).To(Say("In order to move running applications to this isolation segment, they must be restarted\\.")) 162 163 Expect(fakeActor.SetOrganizationDefaultIsolationSegmentCallCount()).To(Equal(1)) 164 orgGUID, isoSegGUID := fakeActor.SetOrganizationDefaultIsolationSegmentArgsForCall(0) 165 Expect(orgGUID).To(Equal("some-org-guid")) 166 Expect(isoSegGUID).To(Equal("some-iso-guid")) 167 }) 168 169 Context("when the entitlement errors", func() { 170 BeforeEach(func() { 171 fakeActor.SetOrganizationDefaultIsolationSegmentReturns(v3action.Warnings{"entitlement-warning", "banana"}, v3action.IsolationSegmentNotFoundError{Name: isolationSegment}) 172 }) 173 174 It("returns the warnings and error", func() { 175 Expect(testUI.Out).To(Say("Setting isolation segment %s to default on org %s as banana\\.\\.\\.", isolationSegment, org)) 176 Expect(testUI.Err).To(Say("org-warning-1")) 177 Expect(testUI.Err).To(Say("org-warning-2")) 178 Expect(testUI.Err).To(Say("iso-seg-warning-1")) 179 Expect(testUI.Err).To(Say("iso-seg-warning-2")) 180 Expect(testUI.Err).To(Say("entitlement-warning")) 181 Expect(testUI.Err).To(Say("banana")) 182 Expect(executeErr).To(MatchError(translatableerror.IsolationSegmentNotFoundError{Name: isolationSegment})) 183 }) 184 }) 185 }) 186 }) 187 }) 188 })