github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/manifest_test.go (about)

     1  package ccv3_test
     2  
     3  import (
     4  	"errors"
     5  	"net/http"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     9  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/ccv3fakes"
    11  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
    12  	"code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    13  	"code.cloudfoundry.org/cli/resources"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("Application Manifest", func() {
    19  	var (
    20  		client    *Client
    21  		requester *ccv3fakes.FakeRequester
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		requester = new(ccv3fakes.FakeRequester)
    26  		client, _ = NewFakeRequesterTestClient(requester)
    27  	})
    28  
    29  	Describe("GetApplicationManifest", func() {
    30  		var (
    31  			appGUID string
    32  
    33  			rawManifest []byte
    34  			warnings    Warnings
    35  			executeErr  error
    36  
    37  			expectedYAML []byte
    38  		)
    39  
    40  		BeforeEach(func() {
    41  			appGUID = "some-app-guid"
    42  		})
    43  
    44  		JustBeforeEach(func() {
    45  			rawManifest, warnings, executeErr = client.GetApplicationManifest(appGUID)
    46  		})
    47  
    48  		When("getting the manifest is successful", func() {
    49  			BeforeEach(func() {
    50  				expectedYAML = []byte("---\n- banana")
    51  				requester.MakeRequestReceiveRawReturns(expectedYAML, Warnings{"this is a warning"}, nil)
    52  			})
    53  
    54  			It("makes the correct request", func() {
    55  				Expect(requester.MakeRequestReceiveRawCallCount()).To(Equal(1))
    56  				requestName, uriParams, responseBody := requester.MakeRequestReceiveRawArgsForCall(0)
    57  				Expect(requestName).To(Equal(internal.GetApplicationManifestRequest))
    58  				Expect(uriParams).To(Equal(internal.Params{"app_guid": appGUID}))
    59  				Expect(responseBody).To(Equal("application/x-yaml"))
    60  			})
    61  
    62  			It("the manifest and warnings", func() {
    63  				Expect(executeErr).NotTo(HaveOccurred())
    64  				Expect(warnings).To(ConsistOf("this is a warning"))
    65  
    66  				Expect(rawManifest).To(Equal(expectedYAML))
    67  			})
    68  		})
    69  
    70  		When("the cloud controller returns errors and warnings", func() {
    71  			BeforeEach(func() {
    72  				errors := []ccerror.V3Error{
    73  					{
    74  						Code:   10008,
    75  						Detail: "The request is semantically invalid: command presence",
    76  						Title:  "CF-UnprocessableEntity",
    77  					},
    78  					{
    79  						Code:   10010,
    80  						Detail: "App not found",
    81  						Title:  "CF-AppNotFound",
    82  					},
    83  				}
    84  
    85  				requester.MakeRequestReceiveRawReturns(
    86  					nil,
    87  					Warnings{"this is a warning"},
    88  					ccerror.MultiError{ResponseCode: http.StatusTeapot, Errors: errors},
    89  				)
    90  
    91  			})
    92  
    93  			It("returns the error and all warnings", func() {
    94  				Expect(executeErr).To(MatchError(ccerror.MultiError{
    95  					ResponseCode: http.StatusTeapot,
    96  					Errors: []ccerror.V3Error{
    97  						{
    98  							Code:   10008,
    99  							Detail: "The request is semantically invalid: command presence",
   100  							Title:  "CF-UnprocessableEntity",
   101  						},
   102  						{
   103  							Code:   10010,
   104  							Detail: "App not found",
   105  							Title:  "CF-AppNotFound",
   106  						},
   107  					},
   108  				}))
   109  				Expect(warnings).To(ConsistOf("this is a warning"))
   110  			})
   111  		})
   112  	})
   113  
   114  	Describe("GetSpaceManifestDiff", func() {
   115  		var (
   116  			spaceGUID   string
   117  			rawManifest []byte
   118  
   119  			manifestDiff resources.ManifestDiff
   120  			warnings     Warnings
   121  			executeErr   error
   122  		)
   123  
   124  		BeforeEach(func() {
   125  			spaceGUID = "some-space-guid"
   126  			rawManifest = []byte("---\n- banana")
   127  		})
   128  
   129  		JustBeforeEach(func() {
   130  			manifestDiff, warnings, executeErr = client.GetSpaceManifestDiff(spaceGUID, rawManifest)
   131  		})
   132  
   133  		When("getting the space manifest is successful", func() {
   134  			BeforeEach(func() {
   135  				requester.MakeRequestSendRawReturns(
   136  					"",
   137  					ccv3.Warnings{"warning-1"},
   138  					nil,
   139  				)
   140  			})
   141  
   142  			It("makes the correct request", func() {
   143  				Expect(requester.MakeRequestSendRawCallCount()).To(Equal(1))
   144  			})
   145  
   146  			It("returns the diff and warnings", func() {
   147  				Expect(executeErr).NotTo(HaveOccurred())
   148  				Expect(warnings).To(ConsistOf("warning-1"))
   149  				Expect(manifestDiff).To(matchers.HaveTypeOf(resources.ManifestDiff{}))
   150  			})
   151  		})
   152  
   153  		When("the cloud controller returns errors and warnings", func() {
   154  			BeforeEach(func() {
   155  				requester.MakeRequestSendRawReturns(
   156  					"",
   157  					ccv3.Warnings{"warning-1"},
   158  					errors.New("request-error"),
   159  				)
   160  			})
   161  
   162  			It("returns the error and warnings", func() {
   163  				Expect(executeErr).To(MatchError("request-error"))
   164  				Expect(warnings).To(ConsistOf("warning-1"))
   165  			})
   166  		})
   167  	})
   168  })