github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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  		Context("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  		Context("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.V3UnexpectedResponseError{
    86  					ResponseCode: http.StatusTeapot,
    87  					V3ErrorResponse: ccerror.V3ErrorResponse{
    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  				}))
   102  				Expect(warnings).To(ConsistOf("this is a warning"))
   103  			})
   104  		})
   105  	})
   106  
   107  	Describe("GetBuild", func() {
   108  		Context("when the build exist", func() {
   109  			BeforeEach(func() {
   110  				response := `{
   111  					"created_at": "some-time",
   112  					"guid": "some-build-guid",
   113  					"state": "FAILED",
   114  					"error": "some error",
   115  					"droplet": {
   116  						"guid": "some-droplet-guid"
   117  					}
   118  				}`
   119  				server.AppendHandlers(
   120  					CombineHandlers(
   121  						VerifyRequest(http.MethodGet, "/v3/builds/some-build-guid"),
   122  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   123  					),
   124  				)
   125  			})
   126  
   127  			It("returns the queried build and all warnings", func() {
   128  				build, warnings, err := client.GetBuild("some-build-guid")
   129  				Expect(err).NotTo(HaveOccurred())
   130  
   131  				expectedBuild := Build{
   132  					CreatedAt:   "some-time",
   133  					GUID:        "some-build-guid",
   134  					State:       constant.BuildFailed,
   135  					Error:       "some error",
   136  					DropletGUID: "some-droplet-guid",
   137  				}
   138  				Expect(build).To(Equal(expectedBuild))
   139  				Expect(warnings).To(ConsistOf("this is a warning"))
   140  			})
   141  		})
   142  
   143  		Context("when the cloud controller returns errors and warnings", func() {
   144  			BeforeEach(func() {
   145  				response := ` {
   146  					"errors": [
   147  						{
   148  							"code": 10008,
   149  							"detail": "I can't even",
   150  							"title": "CF-UnprocessableEntity"
   151  						},
   152  						{
   153  							"code": 10010,
   154  							"detail": "Build not found",
   155  							"title": "CF-ResourceNotFound"
   156  						}
   157  					]
   158  				}`
   159  
   160  				server.AppendHandlers(
   161  					CombineHandlers(
   162  						VerifyRequest(http.MethodGet, "/v3/builds/some-build-guid"),
   163  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   164  					),
   165  				)
   166  			})
   167  
   168  			It("returns the error and all warnings", func() {
   169  				_, warnings, err := client.GetBuild("some-build-guid")
   170  				Expect(err).To(MatchError(ccerror.V3UnexpectedResponseError{
   171  					ResponseCode: http.StatusTeapot,
   172  					V3ErrorResponse: ccerror.V3ErrorResponse{
   173  						Errors: []ccerror.V3Error{
   174  							{
   175  								Code:   10008,
   176  								Detail: "I can't even",
   177  								Title:  "CF-UnprocessableEntity",
   178  							},
   179  							{
   180  								Code:   10010,
   181  								Detail: "Build not found",
   182  								Title:  "CF-ResourceNotFound",
   183  							},
   184  						},
   185  					},
   186  				}))
   187  				Expect(warnings).To(ConsistOf("this is a warning"))
   188  			})
   189  		})
   190  	})
   191  })