github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/actor/v7action/application_manifest_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/ccerror"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    11  	"code.cloudfoundry.org/cli/resources"
    12  	"code.cloudfoundry.org/clock"
    13  
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("Application Manifest Actions", func() {
    19  	var (
    20  		actor                     *Actor
    21  		fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
    26  		actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, clock.NewClock())
    27  	})
    28  
    29  	Describe("ApplyApplicationManifest", func() {
    30  		var (
    31  			fakeParser *v7actionfakes.FakeManifestParser
    32  			spaceGUID  string
    33  
    34  			warnings   Warnings
    35  			executeErr error
    36  		)
    37  
    38  		BeforeEach(func() {
    39  			fakeParser = new(v7actionfakes.FakeManifestParser)
    40  			spaceGUID = "some-space-guid"
    41  		})
    42  
    43  		JustBeforeEach(func() {
    44  			warnings, executeErr = actor.ApplyApplicationManifest(fakeParser, spaceGUID)
    45  		})
    46  
    47  		When("given at least one application", func() {
    48  			BeforeEach(func() {
    49  				fakeParser.AppNamesReturns([]string{"app-1"})
    50  			})
    51  
    52  			When("getting the raw manifest bytes is successful", func() {
    53  				var manifestContent []byte
    54  
    55  				BeforeEach(func() {
    56  					manifestContent = []byte("some-manifest-contents")
    57  					fakeParser.RawAppManifestReturns(manifestContent, nil)
    58  				})
    59  
    60  				When("the app exists", func() {
    61  					BeforeEach(func() {
    62  						fakeCloudControllerClient.GetApplicationsReturns(
    63  							[]resources.Application{{GUID: "app-1-guid"}},
    64  							ccv3.Warnings{"app-1-warning"},
    65  							nil,
    66  						)
    67  					})
    68  
    69  					When("applying the manifest succeeds", func() {
    70  						BeforeEach(func() {
    71  							fakeCloudControllerClient.UpdateApplicationApplyManifestReturns(
    72  								"some-job-url",
    73  								ccv3.Warnings{"apply-manifest-1-warning"},
    74  								nil,
    75  							)
    76  						})
    77  
    78  						When("polling finishes successfully", func() {
    79  							BeforeEach(func() {
    80  								fakeCloudControllerClient.PollJobReturns(
    81  									ccv3.Warnings{"poll-1-warning"},
    82  									nil,
    83  								)
    84  							})
    85  
    86  							It("uploads the app manifest", func() {
    87  								Expect(executeErr).ToNot(HaveOccurred())
    88  								Expect(warnings).To(ConsistOf("app-1-warning", "apply-manifest-1-warning", "poll-1-warning"))
    89  
    90  								Expect(fakeParser.RawAppManifestCallCount()).To(Equal(1))
    91  								appName := fakeParser.RawAppManifestArgsForCall(0)
    92  								Expect(appName).To(Equal("app-1"))
    93  
    94  								Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(1))
    95  								queries := fakeCloudControllerClient.GetApplicationsArgsForCall(0)
    96  								Expect(queries).To(ConsistOf(
    97  									ccv3.Query{Key: ccv3.NameFilter, Values: []string{appName}},
    98  									ccv3.Query{Key: ccv3.SpaceGUIDFilter, Values: []string{spaceGUID}},
    99  								))
   100  
   101  								Expect(fakeCloudControllerClient.UpdateApplicationApplyManifestCallCount()).To(Equal(1))
   102  								guidInCall, appManifest := fakeCloudControllerClient.UpdateApplicationApplyManifestArgsForCall(0)
   103  								Expect(guidInCall).To(Equal("app-1-guid"))
   104  								Expect(appManifest).To(Equal(manifestContent))
   105  
   106  								Expect(fakeCloudControllerClient.PollJobCallCount()).To(Equal(1))
   107  								jobURL := fakeCloudControllerClient.PollJobArgsForCall(0)
   108  								Expect(jobURL).To(Equal(ccv3.JobURL("some-job-url")))
   109  							})
   110  						})
   111  
   112  						When("polling returns a generic error", func() {
   113  							var expectedErr error
   114  
   115  							BeforeEach(func() {
   116  								expectedErr = errors.New("poll-1-error")
   117  								fakeCloudControllerClient.PollJobReturns(
   118  									ccv3.Warnings{"poll-1-warning"},
   119  									expectedErr,
   120  								)
   121  							})
   122  
   123  							It("reports a polling error", func() {
   124  								Expect(executeErr).To(Equal(expectedErr))
   125  								Expect(warnings).To(ConsistOf("app-1-warning", "apply-manifest-1-warning", "poll-1-warning"))
   126  							})
   127  						})
   128  
   129  						When("polling returns an job failed error", func() {
   130  							var expectedErr error
   131  
   132  							BeforeEach(func() {
   133  								expectedErr = ccerror.V3JobFailedError{Detail: "some-job-failed"}
   134  								fakeCloudControllerClient.PollJobReturns(
   135  									ccv3.Warnings{"poll-1-warning"},
   136  									expectedErr,
   137  								)
   138  							})
   139  
   140  							It("reports a polling error", func() {
   141  								Expect(executeErr).To(Equal(actionerror.ApplicationManifestError{Message: "some-job-failed"}))
   142  								Expect(warnings).To(ConsistOf("app-1-warning", "apply-manifest-1-warning", "poll-1-warning"))
   143  							})
   144  						})
   145  					})
   146  
   147  					When("applying the manifest errors", func() {
   148  						var applyErr error
   149  
   150  						BeforeEach(func() {
   151  							applyErr = errors.New("some-apply-manifest-error")
   152  							fakeCloudControllerClient.UpdateApplicationApplyManifestReturns(
   153  								"",
   154  								ccv3.Warnings{"apply-manifest-1-warning"},
   155  								applyErr,
   156  							)
   157  						})
   158  
   159  						It("reports a error trying to apply the manifest", func() {
   160  							Expect(executeErr).To(Equal(applyErr))
   161  							Expect(warnings).To(ConsistOf("app-1-warning", "apply-manifest-1-warning"))
   162  						})
   163  					})
   164  				})
   165  
   166  				When("there's an error retrieving the application", func() {
   167  					var getAppErr error
   168  
   169  					BeforeEach(func() {
   170  						getAppErr = errors.New("get-application-error")
   171  
   172  						fakeCloudControllerClient.GetApplicationsReturns(
   173  							nil,
   174  							ccv3.Warnings{"app-1-warning"},
   175  							getAppErr,
   176  						)
   177  					})
   178  
   179  					It("returns error and warnings", func() {
   180  						Expect(executeErr).To(Equal(getAppErr))
   181  						Expect(warnings).To(ConsistOf("app-1-warning"))
   182  					})
   183  				})
   184  			})
   185  
   186  			When("generating the raw manifest errors", func() {
   187  				getManifestErr := errors.New("get-manifest-error")
   188  				BeforeEach(func() {
   189  					fakeParser.RawAppManifestReturns(nil, getManifestErr)
   190  				})
   191  
   192  				It("returns error", func() {
   193  					Expect(executeErr).To(Equal(getManifestErr))
   194  					Expect(warnings).To(BeEmpty())
   195  				})
   196  			})
   197  		})
   198  
   199  		Context("no applications", func() {
   200  			It("does nothing", func() {
   201  				Expect(executeErr).ToNot(HaveOccurred())
   202  				Expect(warnings).To(BeEmpty())
   203  			})
   204  		})
   205  	})
   206  
   207  	Describe("GetRawApplicationManifestByNameAndSpace", func() {
   208  		var (
   209  			appName   string
   210  			spaceGUID string
   211  
   212  			manifestBytes []byte
   213  			warnings      Warnings
   214  			executeErr    error
   215  		)
   216  
   217  		BeforeEach(func() {
   218  			appName = "some-app-name"
   219  			spaceGUID = "some-space-guid"
   220  		})
   221  
   222  		JustBeforeEach(func() {
   223  			manifestBytes, warnings, executeErr = actor.GetRawApplicationManifestByNameAndSpace(appName, spaceGUID)
   224  		})
   225  
   226  		When("getting the application is successful", func() {
   227  			BeforeEach(func() {
   228  				fakeCloudControllerClient.GetApplicationsReturns(
   229  					[]resources.Application{
   230  						{Name: appName, GUID: "some-app-guid"},
   231  					},
   232  					ccv3.Warnings{"get-application-warning"},
   233  					nil,
   234  				)
   235  			})
   236  
   237  			It("gets the application from the Cloud Controller", func() {
   238  				Expect(executeErr).ToNot(HaveOccurred())
   239  				Expect(warnings).To(ContainElement("get-application-warning"))
   240  
   241  				Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(1))
   242  				Expect(fakeCloudControllerClient.GetApplicationsArgsForCall(0)).To(ConsistOf(
   243  					ccv3.Query{Key: ccv3.NameFilter, Values: []string{appName}},
   244  					ccv3.Query{Key: ccv3.SpaceGUIDFilter, Values: []string{spaceGUID}},
   245  				))
   246  			})
   247  
   248  			When("getting the manifest is successful", func() {
   249  				var rawManifest []byte
   250  
   251  				BeforeEach(func() {
   252  					rawManifest = []byte("---\n- potato")
   253  					fakeCloudControllerClient.GetApplicationManifestReturns(
   254  						rawManifest,
   255  						ccv3.Warnings{"get-manifest-warnings"},
   256  						nil,
   257  					)
   258  				})
   259  
   260  				It("returns the raw manifest bytes and warnings", func() {
   261  					Expect(executeErr).ToNot(HaveOccurred())
   262  					Expect(warnings).To(ConsistOf("get-application-warning", "get-manifest-warnings"))
   263  					Expect(manifestBytes).To(Equal(rawManifest))
   264  				})
   265  
   266  				It("gets the manifest for the application", func() {
   267  					Expect(fakeCloudControllerClient.GetApplicationManifestCallCount()).To(Equal(1))
   268  					Expect(fakeCloudControllerClient.GetApplicationManifestArgsForCall(0)).To(Equal("some-app-guid"))
   269  				})
   270  			})
   271  
   272  			When("getting the manifest returns an error", func() {
   273  				var expectedErr error
   274  
   275  				BeforeEach(func() {
   276  					expectedErr = errors.New("some error")
   277  					fakeCloudControllerClient.GetApplicationManifestReturns(
   278  						nil,
   279  						ccv3.Warnings{"get-manifest-warnings"},
   280  						expectedErr,
   281  					)
   282  				})
   283  
   284  				It("returns the error and warnings", func() {
   285  					Expect(executeErr).To(Equal(expectedErr))
   286  					Expect(warnings).To(ConsistOf("get-application-warning", "get-manifest-warnings"))
   287  				})
   288  			})
   289  		})
   290  
   291  		When("getting the application returns an error", func() {
   292  			var expectedErr error
   293  
   294  			BeforeEach(func() {
   295  				expectedErr = errors.New("some error")
   296  				fakeCloudControllerClient.GetApplicationsReturns(
   297  					nil,
   298  					ccv3.Warnings{"get-application-warning"},
   299  					expectedErr,
   300  				)
   301  			})
   302  
   303  			It("returns the error and warnings", func() {
   304  				Expect(executeErr).To(Equal(expectedErr))
   305  				Expect(warnings).To(ConsistOf("get-application-warning"))
   306  				_ = manifestBytes //TODO DELETE ME
   307  			})
   308  		})
   309  	})
   310  })