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