github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/command/v6/create_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/v3action" 8 "code.cloudfoundry.org/cli/command/commandfakes" 9 . "code.cloudfoundry.org/cli/command/v6" 10 "code.cloudfoundry.org/cli/command/v6/v6fakes" 11 "code.cloudfoundry.org/cli/util/configv3" 12 "code.cloudfoundry.org/cli/util/ui" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 . "github.com/onsi/gomega/gbytes" 16 ) 17 18 var _ = Describe("create-isolation-segment Command", func() { 19 var ( 20 cmd CreateIsolationSegmentCommand 21 testUI *ui.UI 22 fakeConfig *commandfakes.FakeConfig 23 fakeSharedActor *commandfakes.FakeSharedActor 24 fakeActor *v6fakes.FakeCreateIsolationSegmentActor 25 binaryName string 26 executeErr error 27 isolationSegment string 28 ) 29 30 BeforeEach(func() { 31 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 32 fakeConfig = new(commandfakes.FakeConfig) 33 fakeSharedActor = new(commandfakes.FakeSharedActor) 34 fakeActor = new(v6fakes.FakeCreateIsolationSegmentActor) 35 36 cmd = CreateIsolationSegmentCommand{ 37 UI: testUI, 38 Config: fakeConfig, 39 SharedActor: fakeSharedActor, 40 Actor: fakeActor, 41 } 42 43 binaryName = "faceman" 44 fakeConfig.BinaryNameReturns(binaryName) 45 isolationSegment = "segment1" 46 }) 47 48 JustBeforeEach(func() { 49 executeErr = cmd.Execute(nil) 50 }) 51 52 When("checking target fails", func() { 53 BeforeEach(func() { 54 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 55 }) 56 57 It("returns an error", func() { 58 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 59 60 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 61 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 62 Expect(checkTargetedOrg).To(BeFalse()) 63 Expect(checkTargetedSpace).To(BeFalse()) 64 }) 65 }) 66 67 When("the user is logged in", func() { 68 BeforeEach(func() { 69 fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil) 70 71 cmd.RequiredArgs.IsolationSegmentName = isolationSegment 72 }) 73 74 When("the create is successful", func() { 75 BeforeEach(func() { 76 fakeActor.CreateIsolationSegmentByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, nil) 77 }) 78 79 It("displays the header and ok", func() { 80 Expect(executeErr).ToNot(HaveOccurred()) 81 82 Expect(testUI.Out).To(Say("Creating isolation segment segment1 as banana...")) 83 Expect(testUI.Out).To(Say("OK")) 84 85 Expect(testUI.Err).To(Say("I am a warning")) 86 Expect(testUI.Err).To(Say("I am also a warning")) 87 88 Expect(fakeActor.CreateIsolationSegmentByNameCallCount()).To(Equal(1)) 89 Expect(fakeActor.CreateIsolationSegmentByNameArgsForCall(0)).To(Equal(v3action.IsolationSegment{Name: isolationSegment})) 90 }) 91 }) 92 93 When("the create is unsuccessful", func() { 94 Context("due to an unexpected error", func() { 95 var expectedErr error 96 97 BeforeEach(func() { 98 expectedErr = errors.New("I am an error") 99 fakeActor.CreateIsolationSegmentByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, expectedErr) 100 }) 101 102 It("displays the header and error", func() { 103 Expect(executeErr).To(MatchError(expectedErr)) 104 105 Expect(testUI.Out).To(Say("Creating isolation segment segment1 as banana...")) 106 107 Expect(testUI.Err).To(Say("I am a warning")) 108 Expect(testUI.Err).To(Say("I am also a warning")) 109 }) 110 }) 111 112 Context("due to an IsolationSegmentAlreadyExistsError", func() { 113 BeforeEach(func() { 114 fakeActor.CreateIsolationSegmentByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, actionerror.IsolationSegmentAlreadyExistsError{}) 115 }) 116 117 It("displays the header and ok", func() { 118 Expect(executeErr).ToNot(HaveOccurred()) 119 120 Expect(testUI.Out).To(Say("Creating isolation segment segment1 as banana...")) 121 Expect(testUI.Out).To(Say("OK")) 122 123 Expect(testUI.Err).To(Say("I am a warning")) 124 Expect(testUI.Err).To(Say("I am also a warning")) 125 Expect(testUI.Err).To(Say("Isolation segment %s already exists.", isolationSegment)) 126 }) 127 }) 128 }) 129 }) 130 })