github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/actor/v7action/deployment_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"
     9  	"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    11  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    12  	"code.cloudfoundry.org/cli/resources"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("Deployment Actions", func() {
    18  	var (
    19  		actor                     *Actor
    20  		executeErr                error
    21  		warnings                  v7action.Warnings
    22  		returnedDeploymentGUID    string
    23  		fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
    24  	)
    25  
    26  	BeforeEach(func() {
    27  		actor, fakeCloudControllerClient, _, _, _, _, _ = NewTestActor()
    28  		fakeCloudControllerClient.CreateApplicationDeploymentByRevisionReturns(
    29  			"some-deployment-guid",
    30  			ccv3.Warnings{"create-warning-1", "create-warning-2"},
    31  			errors.New("create-error"),
    32  		)
    33  	})
    34  
    35  	Describe("CreateDeploymentByApplicationAndRevision", func() {
    36  		JustBeforeEach(func() {
    37  			returnedDeploymentGUID, warnings, executeErr = actor.CreateDeploymentByApplicationAndRevision("some-app-guid", "some-revision-guid")
    38  		})
    39  
    40  		When("the client fails", func() {
    41  			BeforeEach(func() {
    42  				fakeCloudControllerClient.CreateApplicationDeploymentByRevisionReturns(
    43  					"some-deployment-guid",
    44  					ccv3.Warnings{"create-warning-1", "create-warning-2"},
    45  					errors.New("create-deployment-error"),
    46  				)
    47  			})
    48  
    49  			It("returns the warnings and error", func() {
    50  				Expect(executeErr).To(MatchError("create-deployment-error"))
    51  				Expect(warnings).To(ConsistOf("create-warning-1", "create-warning-2"))
    52  			})
    53  		})
    54  
    55  		It("delegates to the cloud controller client", func() {
    56  
    57  			Expect(fakeCloudControllerClient.CreateApplicationDeploymentByRevisionCallCount()).To(Equal(1), "CreateApplicationDeploymentByRevision call count")
    58  			givenAppGUID, givenRevisionGUID := fakeCloudControllerClient.CreateApplicationDeploymentByRevisionArgsForCall(0)
    59  
    60  			Expect(givenAppGUID).To(Equal("some-app-guid"))
    61  			Expect(givenRevisionGUID).To(Equal("some-revision-guid"))
    62  
    63  			Expect(returnedDeploymentGUID).To(Equal("some-deployment-guid"))
    64  			Expect(warnings).To(Equal(Warnings{"create-warning-1", "create-warning-2"}))
    65  		})
    66  	})
    67  
    68  	Describe("CreateDeploymentByApplicationAndDroplet", func() {
    69  		It("delegates to the cloud controller client", func() {
    70  			fakeCloudControllerClient.CreateApplicationDeploymentReturns("some-deployment-guid", ccv3.Warnings{"create-warning-1", "create-warning-2"}, errors.New("create-error"))
    71  
    72  			returnedDeploymentGUID, warnings, executeErr := actor.CreateDeploymentByApplicationAndDroplet("some-app-guid", "some-droplet-guid")
    73  
    74  			Expect(fakeCloudControllerClient.CreateApplicationDeploymentCallCount()).To(Equal(1))
    75  			givenAppGUID, givenDropletGUID := fakeCloudControllerClient.CreateApplicationDeploymentArgsForCall(0)
    76  
    77  			Expect(givenAppGUID).To(Equal("some-app-guid"))
    78  			Expect(givenDropletGUID).To(Equal("some-droplet-guid"))
    79  
    80  			Expect(returnedDeploymentGUID).To(Equal("some-deployment-guid"))
    81  			Expect(warnings).To(Equal(Warnings{"create-warning-1", "create-warning-2"}))
    82  			Expect(executeErr).To(MatchError("create-error"))
    83  		})
    84  	})
    85  
    86  	Describe("GetLatestActiveDeploymentForApp", func() {
    87  		var (
    88  			executeErr error
    89  			warnings   Warnings
    90  			deployment resources.Deployment
    91  
    92  			appGUID string
    93  		)
    94  
    95  		BeforeEach(func() {
    96  			appGUID = "some-app-guid"
    97  		})
    98  
    99  		JustBeforeEach(func() {
   100  			deployment, warnings, executeErr = actor.GetLatestActiveDeploymentForApp(appGUID)
   101  		})
   102  
   103  		It("delegates to the CC client", func() {
   104  			Expect(fakeCloudControllerClient.GetDeploymentsCallCount()).To(Equal(1))
   105  			Expect(fakeCloudControllerClient.GetDeploymentsArgsForCall(0)).To(Equal(
   106  				[]ccv3.Query{
   107  					{Key: ccv3.AppGUIDFilter, Values: []string{appGUID}},
   108  					{Key: ccv3.StatusValueFilter, Values: []string{string(constant.DeploymentStatusValueActive)}},
   109  					{Key: ccv3.OrderBy, Values: []string{ccv3.CreatedAtDescendingOrder}},
   110  					{Key: ccv3.PerPage, Values: []string{"1"}},
   111  				},
   112  			))
   113  		})
   114  
   115  		When("the cc client errors", func() {
   116  			BeforeEach(func() {
   117  				fakeCloudControllerClient.GetDeploymentsReturns(
   118  					[]resources.Deployment{},
   119  					ccv3.Warnings{"get-deployments-warning"},
   120  					errors.New("get-deployments-error"),
   121  				)
   122  			})
   123  
   124  			It("returns an error and warnings", func() {
   125  				Expect(executeErr).To(MatchError("get-deployments-error"))
   126  				Expect(warnings).To(ConsistOf("get-deployments-warning"))
   127  			})
   128  		})
   129  
   130  		When("there are no deployments returned", func() {
   131  			BeforeEach(func() {
   132  				fakeCloudControllerClient.GetDeploymentsReturns(
   133  					[]resources.Deployment{},
   134  					ccv3.Warnings{"get-deployments-warning"},
   135  					nil,
   136  				)
   137  			})
   138  
   139  			It("returns a deployment not found error and warnings", func() {
   140  				Expect(executeErr).To(Equal(actionerror.ActiveDeploymentNotFoundError{}))
   141  				Expect(warnings).To(ConsistOf("get-deployments-warning"))
   142  			})
   143  
   144  		})
   145  
   146  		When("everything succeeds", func() {
   147  			BeforeEach(func() {
   148  				fakeCloudControllerClient.GetDeploymentsReturns(
   149  					[]resources.Deployment{{GUID: "dep-guid"}},
   150  					ccv3.Warnings{"get-deployments-warning"},
   151  					nil,
   152  				)
   153  			})
   154  
   155  			It("returns a deployment not found error and warnings", func() {
   156  				Expect(executeErr).ToNot(HaveOccurred())
   157  				Expect(warnings).To(ConsistOf("get-deployments-warning"))
   158  				Expect(deployment).To(Equal(resources.Deployment{GUID: "dep-guid"}))
   159  			})
   160  
   161  		})
   162  	})
   163  
   164  	Describe("CancelDeployment", func() {
   165  		var (
   166  			deploymentGUID string
   167  
   168  			warnings   Warnings
   169  			executeErr error
   170  		)
   171  
   172  		BeforeEach(func() {
   173  			deploymentGUID = "dep-guid"
   174  		})
   175  
   176  		JustBeforeEach(func() {
   177  			warnings, executeErr = actor.CancelDeployment(deploymentGUID)
   178  		})
   179  
   180  		It("delegates to the cc client", func() {
   181  			Expect(fakeCloudControllerClient.CancelDeploymentCallCount()).To(Equal(1))
   182  			Expect(fakeCloudControllerClient.CancelDeploymentArgsForCall(0)).To(Equal(deploymentGUID))
   183  		})
   184  
   185  		When("the client fails", func() {
   186  			BeforeEach(func() {
   187  				fakeCloudControllerClient.CancelDeploymentReturns(ccv3.Warnings{"cancel-deployment-warnings"}, errors.New("cancel-deployment-error"))
   188  			})
   189  
   190  			It("returns the warnings and error", func() {
   191  				Expect(executeErr).To(MatchError("cancel-deployment-error"))
   192  				Expect(warnings).To(ConsistOf("cancel-deployment-warnings"))
   193  			})
   194  
   195  		})
   196  
   197  		When("the client succeeds", func() {
   198  			BeforeEach(func() {
   199  				fakeCloudControllerClient.CancelDeploymentReturns(ccv3.Warnings{"cancel-deployment-warnings"}, nil)
   200  			})
   201  
   202  			It("returns the warnings and error", func() {
   203  				Expect(executeErr).ToNot(HaveOccurred())
   204  				Expect(warnings).To(ConsistOf("cancel-deployment-warnings"))
   205  			})
   206  		})
   207  	})
   208  })