github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/cloudcontroller/ccv2/resource_test.go (about)

     1  package ccv2_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     7  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/ghttp"
    11  )
    12  
    13  var _ = Describe("Resource", func() {
    14  	var client *Client
    15  
    16  	BeforeEach(func() {
    17  		client = NewTestClient()
    18  	})
    19  
    20  	Describe("UpdateResourceMatch", func() {
    21  		var (
    22  			resourcesToMatch []Resource
    23  
    24  			matchedResources []Resource
    25  			warnings         Warnings
    26  			executeErr       error
    27  		)
    28  
    29  		JustBeforeEach(func() {
    30  			matchedResources, warnings, executeErr = client.UpdateResourceMatch(resourcesToMatch)
    31  		})
    32  
    33  		Context("when resource matching is successful", func() {
    34  			BeforeEach(func() {
    35  				responseBody := `[
    36  						{
    37  							"fn":   "some-file-1",
    38  							"mode": "744",
    39  							"sha1": "some-sha-1",
    40  							"size": 1
    41  						},
    42  						{
    43  							"fn":   "some-file-3",
    44  							"mode": "744",
    45  							"sha1": "some-sha-3",
    46  							"size": 3
    47  						}
    48  					]`
    49  
    50  				// Note: ordering matters with VerifyBody
    51  				expectedRequestBody := []map[string]interface{}{
    52  					map[string]interface{}{
    53  						"fn":   "some-file-1",
    54  						"mode": "744",
    55  						"sha1": "some-sha-1",
    56  						"size": 1,
    57  					},
    58  					map[string]interface{}{
    59  						"fn":   "some-file-2",
    60  						"mode": "744",
    61  						"sha1": "some-sha-2",
    62  						"size": 2,
    63  					},
    64  					map[string]interface{}{
    65  						"fn":   "some-file-3",
    66  						"mode": "744",
    67  						"sha1": "some-sha-3",
    68  						"size": 3,
    69  					},
    70  				}
    71  
    72  				server.AppendHandlers(
    73  					CombineHandlers(
    74  						VerifyRequest(http.MethodPut, "/v2/resource_match"),
    75  						VerifyJSONRepresenting(expectedRequestBody),
    76  						RespondWith(http.StatusCreated, responseBody, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    77  					),
    78  				)
    79  
    80  				resourcesToMatch = []Resource{
    81  					{
    82  						Filename: "some-file-1",
    83  						Mode:     0744,
    84  						SHA1:     "some-sha-1",
    85  						Size:     1,
    86  					},
    87  					{
    88  						Filename: "some-file-2",
    89  						Mode:     0744,
    90  						SHA1:     "some-sha-2",
    91  						Size:     2,
    92  					},
    93  					{
    94  						Filename: "some-file-3",
    95  						Mode:     0744,
    96  						SHA1:     "some-sha-3",
    97  						Size:     3,
    98  					},
    99  				}
   100  			})
   101  
   102  			It("returns the resources and warnings", func() {
   103  				Expect(executeErr).ToNot(HaveOccurred())
   104  				Expect(warnings).To(ConsistOf("this is a warning"))
   105  
   106  				Expect(matchedResources).To(ConsistOf(
   107  					Resource{
   108  						Filename: "some-file-1",
   109  						Mode:     0744,
   110  						SHA1:     "some-sha-1",
   111  						Size:     1,
   112  					},
   113  					Resource{
   114  						Filename: "some-file-3",
   115  						Mode:     0744,
   116  						SHA1:     "some-sha-3",
   117  						Size:     3,
   118  					}))
   119  			})
   120  		})
   121  
   122  		Context("when the cc returns an error", func() {
   123  			BeforeEach(func() {
   124  				response := `{
   125  					"code": 10001,
   126  					"description": "Some Error",
   127  					"error_code": "CF-SomeError"
   128  				}`
   129  				server.AppendHandlers(
   130  					CombineHandlers(
   131  						VerifyRequest(http.MethodPut, "/v2/resource_match"),
   132  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   133  					),
   134  				)
   135  			})
   136  
   137  			It("returns an error", func() {
   138  				Expect(executeErr).To(MatchError(ccerror.V2UnexpectedResponseError{
   139  					ResponseCode: http.StatusTeapot,
   140  					V2ErrorResponse: ccerror.V2ErrorResponse{
   141  						Code:        10001,
   142  						Description: "Some Error",
   143  						ErrorCode:   "CF-SomeError",
   144  					},
   145  				}))
   146  				Expect(warnings).To(ConsistOf("this is a warning"))
   147  			})
   148  		})
   149  	})
   150  })