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