github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/actor/v2action/stack_test.go (about)

     1  package v2action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	. "code.cloudfoundry.org/cli/actor/v2action"
     7  	"code.cloudfoundry.org/cli/actor/v2action/v2actionfakes"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("Stack Actions", func() {
    15  	var (
    16  		actor                     *Actor
    17  		fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    18  	)
    19  
    20  	BeforeEach(func() {
    21  		fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    22  		actor = NewActor(fakeCloudControllerClient, nil, nil)
    23  	})
    24  
    25  	Describe("GetStack", func() {
    26  		Context("when the CC API client does not return any errors", func() {
    27  			BeforeEach(func() {
    28  				fakeCloudControllerClient.GetStackReturns(
    29  					ccv2.Stack{
    30  						Name:        "some-stack",
    31  						Description: "some stack description",
    32  					},
    33  					ccv2.Warnings{"get-stack-warning"},
    34  					nil,
    35  				)
    36  			})
    37  
    38  			It("returns the stack and all warnings", func() {
    39  				stack, warnings, err := actor.GetStack("stack-guid")
    40  				Expect(err).NotTo(HaveOccurred())
    41  				Expect(warnings).To(ConsistOf("get-stack-warning"))
    42  				Expect(stack).To(Equal(Stack{
    43  					Name:        "some-stack",
    44  					Description: "some stack description",
    45  				}))
    46  			})
    47  		})
    48  
    49  		Context("when the stack does not exist", func() {
    50  			BeforeEach(func() {
    51  				fakeCloudControllerClient.GetStackReturns(
    52  					ccv2.Stack{},
    53  					nil,
    54  					ccerror.ResourceNotFoundError{},
    55  				)
    56  			})
    57  
    58  			It("returns a StackNotFoundError", func() {
    59  				_, _, err := actor.GetStack("stack-guid")
    60  				Expect(err).To(MatchError(StackNotFoundError{GUID: "stack-guid"}))
    61  			})
    62  		})
    63  
    64  		Context("when the CC API client returns an error", func() {
    65  			var expectedErr error
    66  
    67  			BeforeEach(func() {
    68  				expectedErr = errors.New("get-stack-error")
    69  				fakeCloudControllerClient.GetStackReturns(
    70  					ccv2.Stack{},
    71  					ccv2.Warnings{"stack-warning"},
    72  					expectedErr,
    73  				)
    74  			})
    75  
    76  			It("returns the error and warnings", func() {
    77  				_, warnings, err := actor.GetStack("stack-guid")
    78  				Expect(err).To(MatchError("get-stack-error"))
    79  				Expect(warnings).To(ConsistOf("stack-warning"))
    80  			})
    81  		})
    82  	})
    83  
    84  	Describe("GetStackByName", func() {
    85  		Context("when the CC API client does not return any errors", func() {
    86  			Context("when it returns one stack", func() {
    87  				BeforeEach(func() {
    88  					fakeCloudControllerClient.GetStacksReturns(
    89  						[]ccv2.Stack{{
    90  							Name:        "some-stack",
    91  							Description: "some stack description",
    92  						}},
    93  						ccv2.Warnings{"get-stacks-warning"},
    94  						nil,
    95  					)
    96  				})
    97  
    98  				It("returns the stack and all warnings", func() {
    99  					stack, warnings, err := actor.GetStackByName("some-stack")
   100  					Expect(err).NotTo(HaveOccurred())
   101  					Expect(warnings).To(ConsistOf("get-stacks-warning"))
   102  					Expect(stack).To(Equal(Stack{
   103  						Name:        "some-stack",
   104  						Description: "some stack description",
   105  					}))
   106  
   107  					Expect(fakeCloudControllerClient.GetStacksCallCount()).To(Equal(1))
   108  					Expect(fakeCloudControllerClient.GetStacksArgsForCall(0)).To(Equal([]ccv2.Query{
   109  						{
   110  							Filter:   ccv2.NameFilter,
   111  							Operator: ccv2.EqualOperator,
   112  							Values:   []string{"some-stack"},
   113  						},
   114  					}))
   115  				})
   116  			})
   117  
   118  			Context("when it returns no stacks", func() {
   119  				BeforeEach(func() {
   120  					fakeCloudControllerClient.GetStacksReturns(
   121  						[]ccv2.Stack{},
   122  						ccv2.Warnings{"get-stacks-warning"},
   123  						nil,
   124  					)
   125  				})
   126  
   127  				It("returns a StackNotFoundError", func() {
   128  					_, warnings, err := actor.GetStackByName("some-stack")
   129  					Expect(err).To(MatchError(StackNotFoundError{Name: "some-stack"}))
   130  					Expect(warnings).To(ConsistOf("get-stacks-warning"))
   131  				})
   132  			})
   133  		})
   134  
   135  		Context("when the stack does not exist", func() {
   136  			BeforeEach(func() {
   137  				fakeCloudControllerClient.GetStackReturns(
   138  					ccv2.Stack{},
   139  					nil,
   140  					ccerror.ResourceNotFoundError{},
   141  				)
   142  			})
   143  
   144  			It("returns a StackNotFoundError", func() {
   145  				_, _, err := actor.GetStack("stack-guid")
   146  				Expect(err).To(MatchError(StackNotFoundError{GUID: "stack-guid"}))
   147  			})
   148  		})
   149  
   150  		Context("when the CC API client returns an error", func() {
   151  			var expectedErr error
   152  
   153  			BeforeEach(func() {
   154  				expectedErr = errors.New("get-stack-error")
   155  				fakeCloudControllerClient.GetStackReturns(
   156  					ccv2.Stack{},
   157  					ccv2.Warnings{"stack-warning"},
   158  					expectedErr,
   159  				)
   160  			})
   161  
   162  			It("returns the error and warnings", func() {
   163  				_, warnings, err := actor.GetStack("stack-guid")
   164  				Expect(err).To(MatchError("get-stack-error"))
   165  				Expect(warnings).To(ConsistOf("stack-warning"))
   166  			})
   167  		})
   168  	})
   169  })