github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/actor/v7action/stack_test.go (about) 1 package v7action_test 2 3 import ( 4 "code.cloudfoundry.org/cli/actor/actionerror" 5 . "code.cloudfoundry.org/cli/actor/v7action" 6 "code.cloudfoundry.org/cli/actor/v7action/v7actionfakes" 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 8 "code.cloudfoundry.org/cli/resources" 9 "errors" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 ) 13 14 var _ = Describe("Stack", func() { 15 var ( 16 actor *Actor 17 fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient 18 ) 19 20 BeforeEach(func() { 21 fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient) 22 fakeConfig := new(v7actionfakes.FakeConfig) 23 actor = NewActor(fakeCloudControllerClient, fakeConfig, nil, nil) 24 }) 25 26 Describe("Get stack by name", func() { 27 28 var expectedErr error 29 var err error 30 var warnings Warnings 31 var stack Stack 32 33 JustBeforeEach(func() { 34 stack, warnings, err = actor.GetStackByName("some-stack-name") 35 }) 36 37 Describe("When there are errors", func() { 38 When("The client errors", func() { 39 BeforeEach(func() { 40 expectedErr = errors.New("CC Error") 41 fakeCloudControllerClient.GetStacksReturns( 42 []resources.Stack{}, 43 ccv3.Warnings{"warning-1", "warning-2"}, 44 expectedErr, 45 ) 46 }) 47 48 It("Returns the same error", func() { 49 Expect(err).To(MatchError(expectedErr)) 50 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 51 }) 52 }) 53 54 When("The stack does not exist", func() { 55 BeforeEach(func() { 56 fakeCloudControllerClient.GetStacksReturns( 57 []resources.Stack{}, 58 ccv3.Warnings{"warning-1", "warning-2"}, 59 actionerror.StackNotFoundError{Name: "some-stack-name"}, 60 ) 61 }) 62 63 It("Returns a StackNotFound error", func() { 64 Expect(err).To(MatchError(actionerror.StackNotFoundError{Name: "some-stack-name"})) 65 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 66 }) 67 }) 68 }) 69 70 Context("When there are no errors", func() { 71 72 When("The stack exists", func() { 73 expectedStack := resources.Stack{ 74 GUID: "some-stack-guid", 75 Name: "some-stack-name", 76 Description: "Some stack desc", 77 } 78 79 expectedParams := []ccv3.Query{{Key: ccv3.NameFilter, Values: []string{"some-stack-name"}}} 80 81 BeforeEach(func() { 82 fakeCloudControllerClient.GetStacksReturns( 83 []resources.Stack{expectedStack}, 84 ccv3.Warnings{"warning-1", "warning-2"}, 85 nil, 86 ) 87 }) 88 89 It("Returns the desired stack", func() { 90 91 actualParams := fakeCloudControllerClient.GetStacksArgsForCall(0) 92 Expect(actualParams).To(Equal(expectedParams)) 93 Expect(fakeCloudControllerClient.GetStacksCallCount()).To(Equal(1)) 94 Expect(stack.GUID).To(Equal(expectedStack.GUID)) 95 Expect(stack.Name).To(Equal(expectedStack.Name)) 96 Expect(stack.Description).To(Equal(expectedStack.Description)) 97 Expect(err).To(BeNil()) 98 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 99 }) 100 }) 101 }) 102 }) 103 104 Describe("GetStacks", func() { 105 var ( 106 ccv3Stacks []resources.Stack 107 stacks []Stack 108 109 stack1Name string 110 stack1Description string 111 stack2Name string 112 stack2Description string 113 114 warnings Warnings 115 executeErr error 116 ) 117 118 BeforeEach(func() { 119 ccv3Stacks = []resources.Stack{ 120 {Name: stack1Name, Description: stack1Description}, 121 {Name: stack2Name, Description: stack2Description}, 122 } 123 }) 124 125 JustBeforeEach(func() { 126 stacks, warnings, executeErr = actor.GetStacks() 127 }) 128 129 When("getting stacks returns an error", func() { 130 var expectedErr error 131 132 BeforeEach(func() { 133 expectedErr = errors.New("some error") 134 fakeCloudControllerClient.GetStacksReturns( 135 []resources.Stack{}, 136 ccv3.Warnings{"warning-1", "warning-2"}, expectedErr) 137 }) 138 139 It("returns warnings and the error", func() { 140 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 141 Expect(executeErr).To(MatchError(expectedErr)) 142 }) 143 }) 144 145 When("the GetStacks call is successful", func() { 146 When("the cloud controller returns back stacks", func() { 147 BeforeEach(func() { 148 fakeCloudControllerClient.GetStacksReturns( 149 ccv3Stacks, 150 ccv3.Warnings{"some-stack-warning"}, nil) 151 }) 152 153 It("returns back the stacks and warnings", func() { 154 Expect(executeErr).ToNot(HaveOccurred()) 155 Expect(stacks).To(ConsistOf(Stack{Name: stack1Name, Description: stack1Description}, Stack{Name: stack2Name, Description: stack2Description})) 156 Expect(warnings).To(ConsistOf("some-stack-warning")) 157 Expect(fakeCloudControllerClient.GetStacksCallCount()).To(Equal(1)) 158 }) 159 }) 160 161 When("the GetStacks call is unsuccessful", func() { 162 BeforeEach(func() { 163 fakeCloudControllerClient.GetStacksReturns( 164 nil, 165 ccv3.Warnings{"some-stack-warning"}, 166 errors.New("some-error")) 167 }) 168 169 It("returns an error and warnings", func() { 170 Expect(executeErr).To(MatchError("some-error")) 171 Expect(warnings).To(ConsistOf("some-stack-warning")) 172 }) 173 }) 174 }) 175 }) 176 })