github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v7/stack_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/v7action" 7 8 "code.cloudfoundry.org/cli/actor/actionerror" 9 "code.cloudfoundry.org/cli/command/commandfakes" 10 . "code.cloudfoundry.org/cli/command/v7" 11 "code.cloudfoundry.org/cli/command/v7/v7fakes" 12 "code.cloudfoundry.org/cli/util/configv3" 13 "code.cloudfoundry.org/cli/util/ui" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 . "github.com/onsi/gomega/gbytes" 17 ) 18 19 var _ = Describe("Stack Command", func() { 20 var ( 21 cmd StackCommand 22 testUI *ui.UI 23 fakeConfig *commandfakes.FakeConfig 24 fakeSharedActor *commandfakes.FakeSharedActor 25 fakeActor *v7fakes.FakeStackActor 26 binaryName string 27 executeErr error 28 stackName string 29 ) 30 31 BeforeEach(func() { 32 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 33 fakeConfig = new(commandfakes.FakeConfig) 34 fakeSharedActor = new(commandfakes.FakeSharedActor) 35 fakeActor = new(v7fakes.FakeStackActor) 36 37 cmd = StackCommand{ 38 UI: testUI, 39 Config: fakeConfig, 40 SharedActor: fakeSharedActor, 41 Actor: fakeActor, 42 } 43 44 binaryName = "faceman" 45 fakeConfig.BinaryNameReturns(binaryName) 46 stackName = "some-stack-name" 47 48 cmd.RequiredArgs.StackName = stackName 49 }) 50 51 JustBeforeEach(func() { 52 executeErr = cmd.Execute(nil) 53 }) 54 55 Context("When the environment is not setup correctly", func() { 56 When("checking target fails", func() { 57 BeforeEach(func() { 58 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 59 }) 60 61 It("returns an error", func() { 62 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 63 64 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 65 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 66 Expect(fakeActor.GetStackByNameCallCount()).To(Equal(0)) 67 Expect(checkTargetedOrg).To(BeFalse()) 68 Expect(checkTargetedSpace).To(BeFalse()) 69 }) 70 }) 71 72 When("retrieving user information errors", func() { 73 var expectedErr error 74 75 BeforeEach(func() { 76 expectedErr = errors.New("some current user error") 77 fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr) 78 }) 79 80 It("return an error", func() { 81 Expect(executeErr).To(Equal(expectedErr)) 82 }) 83 }) 84 }) 85 86 Context("When the environment is setup correctly", func() { 87 BeforeEach(func() { 88 fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"}) 89 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"}) 90 fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil) 91 }) 92 93 Context("When the stack exists", func() { 94 BeforeEach(func() { 95 stack := v7action.Stack{ 96 Name: "some-stack-name", 97 GUID: "some-stack-guid", 98 Description: "some-stack-desc", 99 } 100 fakeActor.GetStackByNameReturns(stack, v7action.Warnings{"some-warning-1"}, nil) 101 }) 102 103 When("The --guid flag is not provided", func() { 104 It("Displays the stack information", func() { 105 Expect(executeErr).ToNot(HaveOccurred()) 106 Expect(fakeActor.GetStackByNameArgsForCall(0)).To(Equal("some-stack-name")) 107 Expect(fakeActor.GetStackByNameCallCount()).To(Equal(1)) 108 // NOTE: DISPLAY EXPECTS 109 Expect(testUI.Err).To(Say("some-warning-1")) 110 }) 111 }) 112 113 When("The --guid flag is provided", func() { 114 BeforeEach(func() { 115 cmd.GUID = true 116 }) 117 118 It("displays just the guid", func() { 119 Expect(executeErr).ToNot(HaveOccurred()) 120 Expect(fakeActor.GetStackByNameArgsForCall(0)).To(Equal("some-stack-name")) 121 Expect(fakeActor.GetStackByNameCallCount()).To(Equal(1)) 122 Expect(testUI.Err).To(Say("some-warning-1")) 123 }) 124 }) 125 }) 126 127 When("The Stack does not Exist", func() { 128 expectedError := actionerror.StackNotFoundError{Name: "some-stack-name"} 129 BeforeEach(func() { 130 fakeActor.GetStackByNameReturns( 131 v7action.Stack{}, 132 v7action.Warnings{"some-warning-1"}, 133 expectedError, 134 ) 135 }) 136 137 It("Fails and returns a StackNotFoundError", func() { 138 Expect(fakeActor.GetStackByNameArgsForCall(0)).To(Equal("some-stack-name")) 139 Expect(fakeActor.GetStackByNameCallCount()).To(Equal(1)) 140 Expect(executeErr).To(Equal(expectedError)) 141 Expect(testUI.Err).To(Say("some-warning-1")) 142 }) 143 }) 144 145 When("There was an error in the actor", func() { 146 BeforeEach(func() { 147 fakeActor.GetStackByNameReturns( 148 v7action.Stack{}, 149 v7action.Warnings{"some-warning-1"}, 150 errors.New("some-random-error"), 151 ) 152 }) 153 154 It("Fails and returns a StackNotFoundError", func() { 155 Expect(fakeActor.GetStackByNameArgsForCall(0)).To(Equal("some-stack-name")) 156 Expect(fakeActor.GetStackByNameCallCount()).To(Equal(1)) 157 Expect(executeErr).To(MatchError(errors.New("some-random-error"))) 158 Expect(testUI.Err).To(Say("some-warning-1")) 159 }) 160 }) 161 162 }) 163 164 })