github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/command/v6/delete_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/api/cloudcontroller/ccversion" 9 "code.cloudfoundry.org/cli/command/commandfakes" 10 "code.cloudfoundry.org/cli/command/translatableerror" 11 . "code.cloudfoundry.org/cli/command/v6" 12 "code.cloudfoundry.org/cli/command/v6/v6fakes" 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("delete-isolation-segment Command", func() { 21 var ( 22 cmd DeleteIsolationSegmentCommand 23 input *Buffer 24 testUI *ui.UI 25 fakeConfig *commandfakes.FakeConfig 26 fakeSharedActor *commandfakes.FakeSharedActor 27 fakeActor *v6fakes.FakeDeleteIsolationSegmentActor 28 binaryName string 29 executeErr error 30 isolationSegment string 31 ) 32 33 BeforeEach(func() { 34 input = NewBuffer() 35 testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer()) 36 fakeConfig = new(commandfakes.FakeConfig) 37 fakeSharedActor = new(commandfakes.FakeSharedActor) 38 fakeActor = new(v6fakes.FakeDeleteIsolationSegmentActor) 39 40 cmd = DeleteIsolationSegmentCommand{ 41 UI: testUI, 42 Config: fakeConfig, 43 SharedActor: fakeSharedActor, 44 Actor: fakeActor, 45 } 46 47 binaryName = "faceman" 48 fakeConfig.BinaryNameReturns(binaryName) 49 isolationSegment = "segment1" 50 }) 51 52 JustBeforeEach(func() { 53 executeErr = cmd.Execute(nil) 54 }) 55 56 When("the API version is below the minimum", func() { 57 BeforeEach(func() { 58 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinV3ClientVersion) 59 }) 60 61 It("returns a MinimumAPIVersionNotMetError", func() { 62 Expect(executeErr).To(MatchError(translatableerror.MinimumCFAPIVersionNotMetError{ 63 CurrentVersion: ccversion.MinV3ClientVersion, 64 MinimumVersion: ccversion.MinVersionIsolationSegmentV3, 65 })) 66 }) 67 }) 68 69 When("checking target fails", func() { 70 BeforeEach(func() { 71 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3) 72 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 73 }) 74 75 It("returns an error", func() { 76 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 77 78 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 79 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 80 Expect(checkTargetedOrg).To(BeFalse()) 81 Expect(checkTargetedSpace).To(BeFalse()) 82 }) 83 }) 84 85 When("the user is logged in", func() { 86 BeforeEach(func() { 87 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3) 88 fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil) 89 cmd.RequiredArgs.IsolationSegmentName = isolationSegment 90 }) 91 92 When("the -f flag is provided", func() { 93 BeforeEach(func() { 94 cmd.Force = true 95 }) 96 97 When("the iso segment exists", func() { 98 When("the delete is successful", func() { 99 BeforeEach(func() { 100 fakeActor.DeleteIsolationSegmentByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, nil) 101 }) 102 103 It("displays the header and ok", func() { 104 Expect(executeErr).ToNot(HaveOccurred()) 105 106 Expect(testUI.Out).To(Say("Deleting isolation segment segment1 as banana...")) 107 Expect(testUI.Out).To(Say("OK")) 108 109 Expect(testUI.Err).To(Say("I am a warning")) 110 Expect(testUI.Err).To(Say("I am also a warning")) 111 112 Expect(fakeActor.DeleteIsolationSegmentByNameCallCount()).To(Equal(1)) 113 Expect(fakeActor.DeleteIsolationSegmentByNameArgsForCall(0)).To(Equal("segment1")) 114 }) 115 }) 116 117 When("the delete is unsuccessful", func() { 118 var expectedErr error 119 120 BeforeEach(func() { 121 expectedErr = errors.New("I am an error") 122 fakeActor.DeleteIsolationSegmentByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, expectedErr) 123 }) 124 125 It("displays the header and error", func() { 126 Expect(executeErr).To(MatchError(expectedErr)) 127 128 Expect(testUI.Out).To(Say("Deleting isolation segment segment1 as banana...")) 129 130 Expect(testUI.Err).To(Say("I am a warning")) 131 Expect(testUI.Err).To(Say("I am also a warning")) 132 }) 133 }) 134 }) 135 136 When("the iso segment does not exist", func() { 137 BeforeEach(func() { 138 fakeActor.DeleteIsolationSegmentByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, actionerror.IsolationSegmentNotFoundError{}) 139 }) 140 141 It("displays does not exist warning", func() { 142 Expect(testUI.Out).To(Say("OK")) 143 Expect(executeErr).NotTo(HaveOccurred()) 144 Expect(testUI.Err).To(Say("Isolation segment %s does not exist.", isolationSegment)) 145 }) 146 }) 147 }) 148 149 When("the -f flag is not provided", func() { 150 When("the user chooses the default", func() { 151 BeforeEach(func() { 152 input.Write([]byte("\n")) 153 }) 154 155 It("cancels the deletion", func() { 156 Expect(testUI.Out).To(Say("Really delete the isolation segment %s?", isolationSegment)) 157 Expect(testUI.Out).To(Say("Delete cancelled")) 158 Expect(fakeActor.DeleteIsolationSegmentByNameCallCount()).To(Equal(0)) 159 }) 160 }) 161 162 When("the user inputs yes", func() { 163 BeforeEach(func() { 164 input.Write([]byte("yes\n")) 165 }) 166 167 It("deletes the isolation segment", func() { 168 Expect(testUI.Out).To(Say("Really delete the isolation segment %s?", isolationSegment)) 169 Expect(testUI.Out).To(Say("OK")) 170 Expect(fakeActor.DeleteIsolationSegmentByNameCallCount()).To(Equal(1)) 171 }) 172 }) 173 174 When("the user inputs no", func() { 175 BeforeEach(func() { 176 input.Write([]byte("no\n")) 177 }) 178 179 It("cancels the deletion", func() { 180 Expect(testUI.Out).To(Say("Really delete the isolation segment %s?", isolationSegment)) 181 Expect(testUI.Out).To(Say("Delete cancelled")) 182 Expect(fakeActor.DeleteIsolationSegmentByNameCallCount()).To(Equal(0)) 183 }) 184 }) 185 }) 186 }) 187 })