github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/delete_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("delete-isolation-segment Command", func() { 21 var ( 22 cmd v3.DeleteIsolationSegmentCommand 23 input *Buffer 24 testUI *ui.UI 25 fakeConfig *commandfakes.FakeConfig 26 fakeSharedActor *commandfakes.FakeSharedActor 27 fakeActor *v3fakes.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(v3fakes.FakeDeleteIsolationSegmentActor) 39 40 cmd = v3.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 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3) 51 }) 52 53 JustBeforeEach(func() { 54 executeErr = cmd.Execute(nil) 55 }) 56 57 Context("when the API version is below the minimum", func() { 58 BeforeEach(func() { 59 fakeActor.CloudControllerAPIVersionReturns("0.0.0") 60 }) 61 62 It("returns a MinimumAPIVersionNotMetError", func() { 63 Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{ 64 CurrentVersion: "0.0.0", 65 MinimumVersion: ccversion.MinVersionIsolationSegmentV3, 66 })) 67 }) 68 }) 69 70 Context("when checking target fails", func() { 71 BeforeEach(func() { 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 Context("when the user is logged in", func() { 86 BeforeEach(func() { 87 fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil) 88 cmd.RequiredArgs.IsolationSegmentName = isolationSegment 89 }) 90 91 Context("when the -f flag is provided", func() { 92 BeforeEach(func() { 93 cmd.Force = true 94 }) 95 96 Context("when the iso segment exists", func() { 97 Context("when the delete is successful", func() { 98 BeforeEach(func() { 99 fakeActor.DeleteIsolationSegmentByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, nil) 100 }) 101 102 It("displays the header and ok", func() { 103 Expect(executeErr).ToNot(HaveOccurred()) 104 105 Expect(testUI.Out).To(Say("Deleting isolation segment segment1 as banana...")) 106 Expect(testUI.Out).To(Say("OK")) 107 108 Expect(testUI.Err).To(Say("I am a warning")) 109 Expect(testUI.Err).To(Say("I am also a warning")) 110 111 Expect(fakeActor.DeleteIsolationSegmentByNameCallCount()).To(Equal(1)) 112 Expect(fakeActor.DeleteIsolationSegmentByNameArgsForCall(0)).To(Equal("segment1")) 113 }) 114 }) 115 116 Context("when the delete is unsuccessful", func() { 117 var expectedErr error 118 119 BeforeEach(func() { 120 expectedErr = errors.New("I am an error") 121 fakeActor.DeleteIsolationSegmentByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, expectedErr) 122 }) 123 124 It("displays the header and error", func() { 125 Expect(executeErr).To(MatchError(expectedErr)) 126 127 Expect(testUI.Out).To(Say("Deleting isolation segment segment1 as banana...")) 128 129 Expect(testUI.Err).To(Say("I am a warning")) 130 Expect(testUI.Err).To(Say("I am also a warning")) 131 }) 132 }) 133 }) 134 135 Context("when the iso segment does not exist", func() { 136 BeforeEach(func() { 137 fakeActor.DeleteIsolationSegmentByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, actionerror.IsolationSegmentNotFoundError{}) 138 }) 139 140 It("displays does not exist warning", func() { 141 Expect(testUI.Out).To(Say("OK")) 142 Expect(executeErr).NotTo(HaveOccurred()) 143 Expect(testUI.Err).To(Say("Isolation segment %s does not exist.", isolationSegment)) 144 }) 145 }) 146 }) 147 148 Context("when the -f flag is not provided", func() { 149 Context("when the user chooses the default", func() { 150 BeforeEach(func() { 151 input.Write([]byte("\n")) 152 }) 153 154 It("cancels the deletion", func() { 155 Expect(testUI.Out).To(Say("Really delete the isolation segment %s?", isolationSegment)) 156 Expect(testUI.Out).To(Say("Delete cancelled")) 157 Expect(fakeActor.DeleteIsolationSegmentByNameCallCount()).To(Equal(0)) 158 }) 159 }) 160 161 Context("when the user inputs yes", func() { 162 BeforeEach(func() { 163 input.Write([]byte("yes\n")) 164 }) 165 166 It("deletes the isolation segment", func() { 167 Expect(testUI.Out).To(Say("Really delete the isolation segment %s?", isolationSegment)) 168 Expect(testUI.Out).To(Say("OK")) 169 Expect(fakeActor.DeleteIsolationSegmentByNameCallCount()).To(Equal(1)) 170 }) 171 }) 172 173 Context("when the user inputs no", func() { 174 BeforeEach(func() { 175 input.Write([]byte("no\n")) 176 }) 177 178 It("cancels the deletion", func() { 179 Expect(testUI.Out).To(Say("Really delete the isolation segment %s?", isolationSegment)) 180 Expect(testUI.Out).To(Say("Delete cancelled")) 181 Expect(fakeActor.DeleteIsolationSegmentByNameCallCount()).To(Equal(0)) 182 }) 183 }) 184 }) 185 }) 186 })