github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/actor/v3action/application_manifest_test.go (about)

     1  package v3action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/v3action"
     8  	"code.cloudfoundry.org/cli/actor/v3action/v3actionfakes"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    11  	"code.cloudfoundry.org/cli/resources"
    12  
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("Application Manifest Actions", func() {
    18  	var (
    19  		actor                     *Actor
    20  		fakeCloudControllerClient *v3actionfakes.FakeCloudControllerClient
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		fakeCloudControllerClient = new(v3actionfakes.FakeCloudControllerClient)
    25  		actor = NewActor(fakeCloudControllerClient, nil, nil, nil)
    26  	})
    27  
    28  	Describe("ApplyApplicationManifest", func() {
    29  		var (
    30  			fakeParser *v3actionfakes.FakeManifestParser
    31  			spaceGUID  string
    32  
    33  			warnings   Warnings
    34  			executeErr error
    35  		)
    36  
    37  		BeforeEach(func() {
    38  			fakeParser = new(v3actionfakes.FakeManifestParser)
    39  			spaceGUID = "some-space-guid"
    40  		})
    41  
    42  		JustBeforeEach(func() {
    43  			warnings, executeErr = actor.ApplyApplicationManifest(fakeParser, spaceGUID)
    44  		})
    45  
    46  		When("given at least one application", func() {
    47  			BeforeEach(func() {
    48  				fakeParser.AppNamesReturns([]string{"app-1"})
    49  			})
    50  
    51  			When("getting the raw manifest bytes is successful", func() {
    52  				var manifestContent []byte
    53  
    54  				BeforeEach(func() {
    55  					manifestContent = []byte("some-manifest-contents")
    56  					fakeParser.RawAppManifestReturns(manifestContent, nil)
    57  				})
    58  
    59  				When("the app exists", func() {
    60  					BeforeEach(func() {
    61  						fakeCloudControllerClient.GetApplicationsReturns(
    62  							[]resources.Application{{GUID: "app-1-guid"}},
    63  							ccv3.Warnings{"app-1-warning"},
    64  							nil,
    65  						)
    66  					})
    67  
    68  					When("applying the manifest succeeds", func() {
    69  						BeforeEach(func() {
    70  							fakeCloudControllerClient.UpdateApplicationApplyManifestReturns(
    71  								"some-job-url",
    72  								ccv3.Warnings{"apply-manifest-1-warning"},
    73  								nil,
    74  							)
    75  						})
    76  
    77  						When("polling finishes successfully", func() {
    78  							BeforeEach(func() {
    79  								fakeCloudControllerClient.PollJobReturns(
    80  									ccv3.Warnings{"poll-1-warning"},
    81  									nil,
    82  								)
    83  							})
    84  
    85  							It("uploads the app manifest", func() {
    86  								Expect(executeErr).ToNot(HaveOccurred())
    87  								Expect(warnings).To(ConsistOf("app-1-warning", "apply-manifest-1-warning", "poll-1-warning"))
    88  
    89  								Expect(fakeParser.RawAppManifestCallCount()).To(Equal(1))
    90  								appName := fakeParser.RawAppManifestArgsForCall(0)
    91  								Expect(appName).To(Equal("app-1"))
    92  
    93  								Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(1))
    94  								queries := fakeCloudControllerClient.GetApplicationsArgsForCall(0)
    95  								Expect(queries).To(ConsistOf(
    96  									ccv3.Query{Key: ccv3.NameFilter, Values: []string{appName}},
    97  									ccv3.Query{Key: ccv3.SpaceGUIDFilter, Values: []string{spaceGUID}},
    98  								))
    99  
   100  								Expect(fakeCloudControllerClient.UpdateApplicationApplyManifestCallCount()).To(Equal(1))
   101  								guidInCall, appManifest := fakeCloudControllerClient.UpdateApplicationApplyManifestArgsForCall(0)
   102  								Expect(guidInCall).To(Equal("app-1-guid"))
   103  								Expect(appManifest).To(Equal(manifestContent))
   104  
   105  								Expect(fakeCloudControllerClient.PollJobCallCount()).To(Equal(1))
   106  								jobURL := fakeCloudControllerClient.PollJobArgsForCall(0)
   107  								Expect(jobURL).To(Equal(ccv3.JobURL("some-job-url")))
   108  							})
   109  						})
   110  
   111  						When("polling returns a generic error", func() {
   112  							var expectedErr error
   113  
   114  							BeforeEach(func() {
   115  								expectedErr = errors.New("poll-1-error")
   116  								fakeCloudControllerClient.PollJobReturns(
   117  									ccv3.Warnings{"poll-1-warning"},
   118  									expectedErr,
   119  								)
   120  							})
   121  
   122  							It("reports a polling error", func() {
   123  								Expect(executeErr).To(Equal(expectedErr))
   124  								Expect(warnings).To(ConsistOf("app-1-warning", "apply-manifest-1-warning", "poll-1-warning"))
   125  							})
   126  						})
   127  
   128  						When("polling returns an job failed error", func() {
   129  							var expectedErr error
   130  
   131  							BeforeEach(func() {
   132  								expectedErr = ccerror.V3JobFailedError{Detail: "some-job-failed"}
   133  								fakeCloudControllerClient.PollJobReturns(
   134  									ccv3.Warnings{"poll-1-warning"},
   135  									expectedErr,
   136  								)
   137  							})
   138  
   139  							It("reports a polling error", func() {
   140  								Expect(executeErr).To(Equal(actionerror.ApplicationManifestError{Message: "some-job-failed"}))
   141  								Expect(warnings).To(ConsistOf("app-1-warning", "apply-manifest-1-warning", "poll-1-warning"))
   142  							})
   143  						})
   144  					})
   145  
   146  					When("applying the manifest errors", func() {
   147  						var applyErr error
   148  
   149  						BeforeEach(func() {
   150  							applyErr = errors.New("some-apply-manifest-error")
   151  							fakeCloudControllerClient.UpdateApplicationApplyManifestReturns(
   152  								"",
   153  								ccv3.Warnings{"apply-manifest-1-warning"},
   154  								applyErr,
   155  							)
   156  						})
   157  
   158  						It("reports a error trying to apply the manifest", func() {
   159  							Expect(executeErr).To(Equal(applyErr))
   160  							Expect(warnings).To(ConsistOf("app-1-warning", "apply-manifest-1-warning"))
   161  						})
   162  					})
   163  				})
   164  
   165  				When("there's an error retrieving the application", func() {
   166  					var getAppErr error
   167  
   168  					BeforeEach(func() {
   169  						getAppErr = errors.New("get-application-error")
   170  
   171  						fakeCloudControllerClient.GetApplicationsReturns(
   172  							nil,
   173  							ccv3.Warnings{"app-1-warning"},
   174  							getAppErr,
   175  						)
   176  					})
   177  
   178  					It("returns error and warnings", func() {
   179  						Expect(executeErr).To(Equal(getAppErr))
   180  						Expect(warnings).To(ConsistOf("app-1-warning"))
   181  					})
   182  				})
   183  			})
   184  
   185  			When("generating the raw manifest errors", func() {
   186  				getManifestErr := errors.New("get-manifest-error")
   187  				BeforeEach(func() {
   188  					fakeParser.RawAppManifestReturns(nil, getManifestErr)
   189  				})
   190  
   191  				It("returns error", func() {
   192  					Expect(executeErr).To(Equal(getManifestErr))
   193  					Expect(warnings).To(BeEmpty())
   194  				})
   195  			})
   196  		})
   197  
   198  		Context("no applications", func() {
   199  			It("does nothing", func() {
   200  				Expect(executeErr).ToNot(HaveOccurred())
   201  				Expect(warnings).To(BeEmpty())
   202  			})
   203  		})
   204  	})
   205  })