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