github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+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  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/ghttp"
    11  )
    12  
    13  var _ = Describe("Build", func() {
    14  	var client *Client
    15  
    16  	BeforeEach(func() {
    17  		client = NewTestClient()
    18  	})
    19  
    20  	Describe("CreateBuild", func() {
    21  		Context("when the build successfully is created", func() {
    22  			BeforeEach(func() {
    23  				response := `{
    24  					"guid": "some-build-guid",
    25  					"state": "STAGING",
    26  					"droplet": {
    27  						"guid": "some-droplet-guid"
    28  					}
    29  				}`
    30  
    31  				expectedBody := map[string]interface{}{
    32  					"package": map[string]interface{}{
    33  						"guid": "some-package-guid",
    34  					},
    35  				}
    36  				server.AppendHandlers(
    37  					CombineHandlers(
    38  						VerifyRequest(http.MethodPost, "/v3/builds"),
    39  						VerifyJSONRepresenting(expectedBody),
    40  						RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    41  					),
    42  				)
    43  			})
    44  
    45  			It("returns the created build and warnings", func() {
    46  				build, warnings, err := client.CreateBuild(Build{Package: Package{GUID: "some-package-guid"}})
    47  
    48  				Expect(err).NotTo(HaveOccurred())
    49  				Expect(warnings).To(ConsistOf("this is a warning"))
    50  				Expect(build).To(Equal(Build{
    51  					GUID:  "some-build-guid",
    52  					State: BuildStateStaging,
    53  					Droplet: Droplet{
    54  						GUID: "some-droplet-guid",
    55  					},
    56  				}))
    57  			})
    58  		})
    59  
    60  		Context("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(Build{Package: Package{GUID: "some-package-guid"}})
    86  				Expect(err).To(MatchError(ccerror.V3UnexpectedResponseError{
    87  					ResponseCode: http.StatusTeapot,
    88  					V3ErrorResponse: ccerror.V3ErrorResponse{
    89  						[]ccerror.V3Error{
    90  							{
    91  								Code:   10008,
    92  								Detail: "I can't even",
    93  								Title:  "CF-UnprocessableEntity",
    94  							},
    95  							{
    96  								Code:   10010,
    97  								Detail: "Package not found",
    98  								Title:  "CF-ResourceNotFound",
    99  							},
   100  						},
   101  					},
   102  				}))
   103  				Expect(warnings).To(ConsistOf("this is a warning"))
   104  			})
   105  		})
   106  	})
   107  
   108  	Describe("GetBuild", func() {
   109  		Context("when the build exist", func() {
   110  			BeforeEach(func() {
   111  				response := `{
   112  					"guid": "some-build-guid",
   113  					"state": "STAGING",
   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 := Build{
   131  					GUID:  "some-build-guid",
   132  					State: BuildStateStaging,
   133  					Droplet: Droplet{
   134  						GUID: "some-droplet-guid",
   135  					},
   136  				}
   137  				Expect(build).To(Equal(expectedBuild))
   138  				Expect(warnings).To(ConsistOf("this is a warning"))
   139  			})
   140  		})
   141  
   142  		Context("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.V3UnexpectedResponseError{
   170  					ResponseCode: http.StatusTeapot,
   171  					V3ErrorResponse: ccerror.V3ErrorResponse{
   172  						[]ccerror.V3Error{
   173  							{
   174  								Code:   10008,
   175  								Detail: "I can't even",
   176  								Title:  "CF-UnprocessableEntity",
   177  							},
   178  							{
   179  								Code:   10010,
   180  								Detail: "Build not found",
   181  								Title:  "CF-ResourceNotFound",
   182  							},
   183  						},
   184  					},
   185  				}))
   186  				Expect(warnings).To(ConsistOf("this is a warning"))
   187  			})
   188  		})
   189  	})
   190  })