github.com/cloudfoundry/cli@v7.1.0+incompatible/actor/v7action/stack_test.go (about)

     1  package v7action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    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, 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("there are errors", func() {
    38  			When("the client errors", func() {
    39  				BeforeEach(func() {
    40  					expectedErr = errors.New("CC Error")
    41  					fakeCloudControllerClient.GetStacksReturns(
    42  						[]ccv3.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  						[]ccv3.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("there are no errors", func() {
    71  
    72  			When("the stack exists", func() {
    73  				expectedStack := ccv3.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  						[]ccv3.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 []ccv3.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  			labelSelector string
   117  		)
   118  
   119  		BeforeEach(func() {
   120  			ccv3Stacks = []ccv3.Stack{
   121  				{Name: stack1Name, Description: stack1Description},
   122  				{Name: stack2Name, Description: stack2Description},
   123  			}
   124  		})
   125  
   126  		JustBeforeEach(func() {
   127  			stacks, warnings, executeErr = actor.GetStacks(labelSelector)
   128  		})
   129  
   130  		When("getting stacks returns an error", func() {
   131  			var expectedErr error
   132  
   133  			BeforeEach(func() {
   134  				expectedErr = errors.New("some error")
   135  				fakeCloudControllerClient.GetStacksReturns(
   136  					[]ccv3.Stack{},
   137  					ccv3.Warnings{"warning-1", "warning-2"}, expectedErr)
   138  			})
   139  
   140  			It("returns warnings and the error", func() {
   141  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   142  				Expect(executeErr).To(MatchError(expectedErr))
   143  			})
   144  		})
   145  
   146  		When("the GetStacks call is successful", func() {
   147  			When("the cloud controller returns back stacks", func() {
   148  				BeforeEach(func() {
   149  					fakeCloudControllerClient.GetStacksReturns(
   150  						ccv3Stacks,
   151  						ccv3.Warnings{"some-stack-warning"}, nil)
   152  				})
   153  
   154  				It("returns back the stacks and warnings", func() {
   155  					Expect(executeErr).ToNot(HaveOccurred())
   156  					Expect(stacks).To(ConsistOf(Stack{Name: stack1Name, Description: stack1Description}, Stack{Name: stack2Name, Description: stack2Description}))
   157  					Expect(warnings).To(ConsistOf("some-stack-warning"))
   158  					Expect(fakeCloudControllerClient.GetStacksCallCount()).To(Equal(1))
   159  				})
   160  				When("a label selctor is passed in", func() {
   161  					BeforeEach(func() {
   162  						labelSelector = "some-label-selector"
   163  					})
   164  
   165  					It("passes the selector to API", func() {
   166  						Expect(executeErr).ToNot(HaveOccurred())
   167  
   168  						Expect(fakeCloudControllerClient.GetStacksCallCount()).To(Equal(1))
   169  
   170  						expectedQuery := []ccv3.Query{
   171  							{Key: ccv3.LabelSelectorFilter, Values: []string{"some-label-selector"}},
   172  						}
   173  						actualQuery := fakeCloudControllerClient.GetStacksArgsForCall(0)
   174  						Expect(actualQuery).To(Equal(expectedQuery))
   175  
   176  					})
   177  				})
   178  			})
   179  
   180  			When("the GetStacks call is unsuccessful", func() {
   181  				BeforeEach(func() {
   182  					fakeCloudControllerClient.GetStacksReturns(
   183  						nil,
   184  						ccv3.Warnings{"some-stack-warning"},
   185  						errors.New("some-error"))
   186  				})
   187  
   188  				It("returns an error and warnings", func() {
   189  					Expect(executeErr).To(MatchError("some-error"))
   190  					Expect(warnings).To(ConsistOf("some-stack-warning"))
   191  				})
   192  			})
   193  		})
   194  	})
   195  })