github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/create_isolation_segment_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/v3action" 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("create-isolation-segment Command", func() { 21 var ( 22 cmd v3.CreateIsolationSegmentCommand 23 testUI *ui.UI 24 fakeConfig *commandfakes.FakeConfig 25 fakeSharedActor *commandfakes.FakeSharedActor 26 fakeActor *v3fakes.FakeCreateIsolationSegmentActor 27 binaryName string 28 executeErr error 29 isolationSegment 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(v3fakes.FakeCreateIsolationSegmentActor) 37 38 cmd = v3.CreateIsolationSegmentCommand{ 39 UI: testUI, 40 Config: fakeConfig, 41 SharedActor: fakeSharedActor, 42 Actor: fakeActor, 43 } 44 45 binaryName = "faceman" 46 fakeConfig.BinaryNameReturns(binaryName) 47 isolationSegment = "segment1" 48 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3) 49 }) 50 51 JustBeforeEach(func() { 52 executeErr = cmd.Execute(nil) 53 }) 54 55 Context("when the API version is below the minimum", func() { 56 BeforeEach(func() { 57 fakeActor.CloudControllerAPIVersionReturns("0.0.0") 58 }) 59 60 It("returns a MinimumAPIVersionNotMetError", func() { 61 Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{ 62 CurrentVersion: "0.0.0", 63 MinimumVersion: ccversion.MinVersionIsolationSegmentV3, 64 })) 65 }) 66 }) 67 68 Context("when checking target fails", func() { 69 BeforeEach(func() { 70 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 71 }) 72 73 It("returns an error", func() { 74 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 75 76 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 77 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 78 Expect(checkTargetedOrg).To(BeFalse()) 79 Expect(checkTargetedSpace).To(BeFalse()) 80 }) 81 }) 82 83 Context("when the user is logged in", func() { 84 BeforeEach(func() { 85 fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil) 86 87 cmd.RequiredArgs.IsolationSegmentName = isolationSegment 88 }) 89 90 Context("when the create is successful", func() { 91 BeforeEach(func() { 92 fakeActor.CreateIsolationSegmentByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, nil) 93 }) 94 95 It("displays the header and ok", func() { 96 Expect(executeErr).ToNot(HaveOccurred()) 97 98 Expect(testUI.Out).To(Say("Creating isolation segment segment1 as banana...")) 99 Expect(testUI.Out).To(Say("OK")) 100 101 Expect(testUI.Err).To(Say("I am a warning")) 102 Expect(testUI.Err).To(Say("I am also a warning")) 103 104 Expect(fakeActor.CreateIsolationSegmentByNameCallCount()).To(Equal(1)) 105 Expect(fakeActor.CreateIsolationSegmentByNameArgsForCall(0)).To(Equal(v3action.IsolationSegment{Name: isolationSegment})) 106 }) 107 }) 108 109 Context("when the create is unsuccessful", func() { 110 Context("due to an unexpected error", func() { 111 var expectedErr error 112 113 BeforeEach(func() { 114 expectedErr = errors.New("I am an error") 115 fakeActor.CreateIsolationSegmentByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, expectedErr) 116 }) 117 118 It("displays the header and error", func() { 119 Expect(executeErr).To(MatchError(expectedErr)) 120 121 Expect(testUI.Out).To(Say("Creating isolation segment segment1 as banana...")) 122 123 Expect(testUI.Err).To(Say("I am a warning")) 124 Expect(testUI.Err).To(Say("I am also a warning")) 125 }) 126 }) 127 128 Context("due to an IsolationSegmentAlreadyExistsError", func() { 129 BeforeEach(func() { 130 fakeActor.CreateIsolationSegmentByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, actionerror.IsolationSegmentAlreadyExistsError{}) 131 }) 132 133 It("displays the header and ok", func() { 134 Expect(executeErr).ToNot(HaveOccurred()) 135 136 Expect(testUI.Out).To(Say("Creating isolation segment segment1 as banana...")) 137 Expect(testUI.Out).To(Say("OK")) 138 139 Expect(testUI.Err).To(Say("I am a warning")) 140 Expect(testUI.Err).To(Say("I am also a warning")) 141 Expect(testUI.Err).To(Say("Isolation segment %s already exists.", isolationSegment)) 142 }) 143 }) 144 }) 145 }) 146 })