github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+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{PackageGUID: "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  					DropletGUID: "some-droplet-guid",
    54  				}))
    55  			})
    56  		})
    57  
    58  		Context("when cc returns back an error or warnings", func() {
    59  			BeforeEach(func() {
    60  				response := ` {
    61    "errors": [
    62      {
    63        "code": 10008,
    64        "detail": "I can't even",
    65        "title": "CF-UnprocessableEntity"
    66      },
    67      {
    68        "code": 10010,
    69        "detail": "Package not found",
    70        "title": "CF-ResourceNotFound"
    71      }
    72    ]
    73  }`
    74  				server.AppendHandlers(
    75  					CombineHandlers(
    76  						VerifyRequest(http.MethodPost, "/v3/builds"),
    77  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    78  					),
    79  				)
    80  			})
    81  
    82  			It("returns the error and all warnings", func() {
    83  				_, warnings, err := client.CreateBuild(Build{PackageGUID: "some-package-guid"})
    84  				Expect(err).To(MatchError(ccerror.V3UnexpectedResponseError{
    85  					ResponseCode: http.StatusTeapot,
    86  					V3ErrorResponse: ccerror.V3ErrorResponse{
    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  				}))
   101  				Expect(warnings).To(ConsistOf("this is a warning"))
   102  			})
   103  		})
   104  	})
   105  
   106  	Describe("GetBuild", func() {
   107  		Context("when the build exist", 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 := Build{
   131  					CreatedAt:   "some-time",
   132  					GUID:        "some-build-guid",
   133  					State:       BuildStateFailed,
   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  		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  						Errors: []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  })