github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/actor/v7action/space_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  
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("Space Manifest Actions", func() {
    18  	var (
    19  		actor                     *Actor
    20  		fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
    25  		actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil)
    26  	})
    27  
    28  	Describe("DiffSpaceManifest", func() {
    29  		var (
    30  			spaceGUID   string
    31  			rawManifest []byte
    32  
    33  			diff       resources.ManifestDiff
    34  			warnings   Warnings
    35  			executeErr error
    36  		)
    37  
    38  		BeforeEach(func() {
    39  			spaceGUID = "some-space-guid"
    40  			rawManifest = []byte("---\n- applications:\n name: my-app")
    41  		})
    42  
    43  		JustBeforeEach(func() {
    44  			diff, warnings, executeErr = actor.DiffSpaceManifest(spaceGUID, rawManifest)
    45  		})
    46  
    47  		When("getting the diff succeeds", func() {
    48  			BeforeEach(func() {
    49  				returnedDiff := resources.ManifestDiff{
    50  					Diffs: []resources.Diff{
    51  						{Op: resources.AddOperation, Path: "/some/path", Value: "wow"},
    52  					},
    53  				}
    54  
    55  				fakeCloudControllerClient.GetSpaceManifestDiffReturns(
    56  					returnedDiff,
    57  					ccv3.Warnings{"diff-manifest-warning"},
    58  					nil,
    59  				)
    60  			})
    61  
    62  			It("returns the diff and warnings", func() {
    63  				Expect(executeErr).NotTo(HaveOccurred())
    64  				Expect(diff).To(Equal(resources.ManifestDiff{
    65  					Diffs: []resources.Diff{
    66  						{Op: resources.AddOperation, Path: "/some/path", Value: "wow"},
    67  					},
    68  				}))
    69  				Expect(warnings).To(ConsistOf("diff-manifest-warning"))
    70  			})
    71  		})
    72  
    73  		When("getting the diff errors", func() {
    74  			BeforeEach(func() {
    75  				fakeCloudControllerClient.GetSpaceManifestDiffReturns(
    76  					resources.ManifestDiff{},
    77  					ccv3.Warnings{"diff-manifest-warning"},
    78  					errors.New("diff-manifest-error"),
    79  				)
    80  			})
    81  
    82  			It("returns the error and warnings", func() {
    83  				Expect(executeErr).To(MatchError("diff-manifest-error"))
    84  				Expect(warnings).To(ConsistOf("diff-manifest-warning"))
    85  			})
    86  		})
    87  	})
    88  
    89  	Describe("SetSpaceManifest", func() {
    90  		var (
    91  			spaceGUID   string
    92  			rawManifest []byte
    93  
    94  			warnings   Warnings
    95  			executeErr error
    96  		)
    97  
    98  		BeforeEach(func() {
    99  			spaceGUID = "some-space-guid"
   100  			rawManifest = []byte("---\n- applications:\n name: my-app")
   101  		})
   102  
   103  		JustBeforeEach(func() {
   104  			warnings, executeErr = actor.SetSpaceManifest(spaceGUID, rawManifest)
   105  		})
   106  
   107  		When("applying the manifest succeeds", func() {
   108  			BeforeEach(func() {
   109  				fakeCloudControllerClient.UpdateSpaceApplyManifestReturns(
   110  					"some-job-url",
   111  					ccv3.Warnings{"apply-manifest-1-warning"},
   112  					nil,
   113  				)
   114  			})
   115  
   116  			When("polling finishes successfully", func() {
   117  				BeforeEach(func() {
   118  					fakeCloudControllerClient.PollJobReturns(
   119  						ccv3.Warnings{"poll-1-warning"},
   120  						nil,
   121  					)
   122  				})
   123  
   124  				It("uploads the app manifest", func() {
   125  					Expect(executeErr).ToNot(HaveOccurred())
   126  					Expect(warnings).To(ConsistOf("apply-manifest-1-warning", "poll-1-warning"))
   127  
   128  					Expect(fakeCloudControllerClient.UpdateSpaceApplyManifestCallCount()).To(Equal(1))
   129  					guidInCall, appManifest := fakeCloudControllerClient.UpdateSpaceApplyManifestArgsForCall(0)
   130  					Expect(guidInCall).To(Equal("some-space-guid"))
   131  					Expect(appManifest).To(Equal(rawManifest))
   132  
   133  					Expect(fakeCloudControllerClient.PollJobCallCount()).To(Equal(1))
   134  					jobURL := fakeCloudControllerClient.PollJobArgsForCall(0)
   135  					Expect(jobURL).To(Equal(ccv3.JobURL("some-job-url")))
   136  				})
   137  			})
   138  
   139  			When("polling returns a generic error", func() {
   140  				var expectedErr error
   141  
   142  				BeforeEach(func() {
   143  					expectedErr = errors.New("poll-1-error")
   144  					fakeCloudControllerClient.PollJobReturns(
   145  						ccv3.Warnings{"poll-1-warning"},
   146  						expectedErr,
   147  					)
   148  				})
   149  
   150  				It("reports a polling error", func() {
   151  					Expect(executeErr).To(Equal(expectedErr))
   152  					Expect(warnings).To(ConsistOf("apply-manifest-1-warning", "poll-1-warning"))
   153  				})
   154  			})
   155  
   156  			When("polling returns an job failed error", func() {
   157  				var expectedErr error
   158  
   159  				BeforeEach(func() {
   160  					expectedErr = ccerror.V3JobFailedError{Detail: "some-job-failed"}
   161  					fakeCloudControllerClient.PollJobReturns(
   162  						ccv3.Warnings{"poll-1-warning"},
   163  						expectedErr,
   164  					)
   165  				})
   166  
   167  				It("reports a polling error", func() {
   168  					Expect(executeErr).To(Equal(actionerror.ApplicationManifestError{Message: "some-job-failed"}))
   169  					Expect(warnings).To(ConsistOf("apply-manifest-1-warning", "poll-1-warning"))
   170  				})
   171  			})
   172  		})
   173  
   174  		When("applying the manifest errors", func() {
   175  			var applyErr error
   176  
   177  			BeforeEach(func() {
   178  				applyErr = errors.New("some-apply-manifest-error")
   179  				fakeCloudControllerClient.UpdateSpaceApplyManifestReturns(
   180  					"",
   181  					ccv3.Warnings{"apply-manifest-1-warning"},
   182  					applyErr,
   183  				)
   184  			})
   185  
   186  			It("reports a error trying to apply the manifest", func() {
   187  				Expect(executeErr).To(Equal(applyErr))
   188  				Expect(warnings).To(ConsistOf("apply-manifest-1-warning"))
   189  			})
   190  		})
   191  	})
   192  })