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

     1  package ccv3_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     7  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     9  	"code.cloudfoundry.org/cli/resources"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/ghttp"
    13  )
    14  
    15  var _ = Describe("Build", func() {
    16  	var client *Client
    17  
    18  	BeforeEach(func() {
    19  		client, _ = NewTestClient()
    20  	})
    21  
    22  	Describe("CreateBuild", func() {
    23  		When("the build successfully is created", func() {
    24  			BeforeEach(func() {
    25  				response := `{
    26  					"guid": "some-build-guid",
    27  					"state": "STAGING",
    28  					"droplet": {
    29  						"guid": "some-droplet-guid"
    30  					}
    31  				}`
    32  
    33  				expectedBody := map[string]interface{}{
    34  					"package": map[string]interface{}{
    35  						"guid": "some-package-guid",
    36  					},
    37  				}
    38  				server.AppendHandlers(
    39  					CombineHandlers(
    40  						VerifyRequest(http.MethodPost, "/v3/builds"),
    41  						VerifyJSONRepresenting(expectedBody),
    42  						RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    43  					),
    44  				)
    45  			})
    46  
    47  			It("returns the created build and warnings", func() {
    48  				build, warnings, err := client.CreateBuild(resources.Build{PackageGUID: "some-package-guid"})
    49  
    50  				Expect(err).NotTo(HaveOccurred())
    51  				Expect(warnings).To(ConsistOf("this is a warning"))
    52  				Expect(build).To(Equal(resources.Build{
    53  					GUID:        "some-build-guid",
    54  					State:       constant.BuildStaging,
    55  					DropletGUID: "some-droplet-guid",
    56  				}))
    57  			})
    58  		})
    59  
    60  		When("cc returns back an error or warnings", func() {
    61  			BeforeEach(func() {
    62  				response := ` {
    63    "errors": [
    64      {
    65        "code": 10008,
    66        "detail": "I can't even",
    67        "title": "CF-UnprocessableEntity"
    68      },
    69      {
    70        "code": 10010,
    71        "detail": "Package not found",
    72        "title": "CF-ResourceNotFound"
    73      }
    74    ]
    75  }`
    76  				server.AppendHandlers(
    77  					CombineHandlers(
    78  						VerifyRequest(http.MethodPost, "/v3/builds"),
    79  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    80  					),
    81  				)
    82  			})
    83  
    84  			It("returns the error and all warnings", func() {
    85  				_, warnings, err := client.CreateBuild(resources.Build{PackageGUID: "some-package-guid"})
    86  				Expect(err).To(MatchError(ccerror.MultiError{
    87  					ResponseCode: http.StatusTeapot,
    88  					Errors: []ccerror.V3Error{
    89  						{
    90  							Code:   10008,
    91  							Detail: "I can't even",
    92  							Title:  "CF-UnprocessableEntity",
    93  						},
    94  						{
    95  							Code:   10010,
    96  							Detail: "Package not found",
    97  							Title:  "CF-ResourceNotFound",
    98  						},
    99  					},
   100  				}))
   101  				Expect(warnings).To(ConsistOf("this is a warning"))
   102  			})
   103  		})
   104  	})
   105  
   106  	Describe("GetBuild", func() {
   107  		When("the build exists", func() {
   108  			BeforeEach(func() {
   109  				response := `{
   110  					"created_at": "some-time",
   111  					"guid": "some-build-guid",
   112  					"state": "FAILED",
   113  					"error": "some error",
   114  					"droplet": {
   115  						"guid": "some-droplet-guid"
   116  					}
   117  				}`
   118  				server.AppendHandlers(
   119  					CombineHandlers(
   120  						VerifyRequest(http.MethodGet, "/v3/builds/some-build-guid"),
   121  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   122  					),
   123  				)
   124  			})
   125  
   126  			It("returns the queried build and all warnings", func() {
   127  				build, warnings, err := client.GetBuild("some-build-guid")
   128  				Expect(err).NotTo(HaveOccurred())
   129  
   130  				expectedBuild := resources.Build{
   131  					CreatedAt:   "some-time",
   132  					GUID:        "some-build-guid",
   133  					State:       constant.BuildFailed,
   134  					Error:       "some error",
   135  					DropletGUID: "some-droplet-guid",
   136  				}
   137  				Expect(build).To(Equal(expectedBuild))
   138  				Expect(warnings).To(ConsistOf("this is a warning"))
   139  			})
   140  		})
   141  
   142  		When("the cloud controller returns errors and warnings", func() {
   143  			BeforeEach(func() {
   144  				response := ` {
   145  					"errors": [
   146  						{
   147  							"code": 10008,
   148  							"detail": "I can't even",
   149  							"title": "CF-UnprocessableEntity"
   150  						},
   151  						{
   152  							"code": 10010,
   153  							"detail": "Build not found",
   154  							"title": "CF-ResourceNotFound"
   155  						}
   156  					]
   157  				}`
   158  
   159  				server.AppendHandlers(
   160  					CombineHandlers(
   161  						VerifyRequest(http.MethodGet, "/v3/builds/some-build-guid"),
   162  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   163  					),
   164  				)
   165  			})
   166  
   167  			It("returns the error and all warnings", func() {
   168  				_, warnings, err := client.GetBuild("some-build-guid")
   169  				Expect(err).To(MatchError(ccerror.MultiError{
   170  					ResponseCode: http.StatusTeapot,
   171  					Errors: []ccerror.V3Error{
   172  						{
   173  							Code:   10008,
   174  							Detail: "I can't even",
   175  							Title:  "CF-UnprocessableEntity",
   176  						},
   177  						{
   178  							Code:   10010,
   179  							Detail: "Build not found",
   180  							Title:  "CF-ResourceNotFound",
   181  						},
   182  					},
   183  				}))
   184  				Expect(warnings).To(ConsistOf("this is a warning"))
   185  			})
   186  		})
   187  	})
   188  })