github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/api/cloudcontroller/ccv3/sidecar_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/types"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/ghttp"
    12  	. "github.com/onsi/gomega/gstruct"
    13  )
    14  
    15  var _ = Describe("Sidecar", func() {
    16  	var client *Client
    17  
    18  	BeforeEach(func() {
    19  		client, _ = NewTestClient()
    20  	})
    21  
    22  	Describe("GetProcessSidecars", func() {
    23  		var (
    24  			processSidecars []Sidecar
    25  			warnings        []string
    26  			err             error
    27  		)
    28  
    29  		JustBeforeEach(func() {
    30  			processSidecars, warnings, err = client.GetProcessSidecars("some-process-guid")
    31  		})
    32  
    33  		When("the process has sidecars", func() {
    34  			BeforeEach(func() {
    35  				response := `{
    36  "resources": [
    37      {
    38        "guid": "process-1-guid",
    39        "name": "auth-sidecar",
    40        "command": "bundle exec rackup",
    41        "process_types": ["web", "worker"],
    42        "memory_in_mb": 300,
    43        "relationships": {
    44          "app": {
    45            "data": {
    46              "guid": "process-1-guid"
    47            }
    48          }
    49        },
    50        "created_at": "2017-02-01T01:33:58Z",
    51        "updated_at": "2017-02-01T01:33:58Z"
    52      },
    53      {
    54        "guid": "process-2-guid",
    55        "name": "echo-sidecar",
    56        "command": "start-echo-server",
    57        "process_types": ["web"],
    58        "memory_in_mb": 300,
    59        "relationships": {
    60          "app": {
    61            "data": {
    62              "guid": "process-2-guid"
    63            }
    64          }
    65        },
    66        "created_at": "2017-02-01T01:33:59Z",
    67        "updated_at": "2017-02-01T01:33:59Z"
    68      }
    69    ]
    70  }
    71  }`
    72  				server.AppendHandlers(
    73  					CombineHandlers(
    74  						VerifyRequest(http.MethodGet, "/v3/processes/some-process-guid/sidecars"),
    75  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    76  					),
    77  				)
    78  			})
    79  
    80  			It("returns the sidecars and all warnings", func() {
    81  				Expect(err).NotTo(HaveOccurred())
    82  				Expect(warnings).To(ConsistOf("this is a warning"))
    83  				Expect(warnings).To(ConsistOf("this is a warning"))
    84  				Expect(len(processSidecars)).To(Equal(2))
    85  				Expect(processSidecars[0]).To(MatchAllFields(Fields{
    86  					"GUID":    Equal("process-1-guid"),
    87  					"Name":    Equal("auth-sidecar"),
    88  					"Command": Equal(types.FilteredString{IsSet: true, Value: "bundle exec rackup"}),
    89  				}))
    90  				Expect(processSidecars[1]).To(MatchAllFields(Fields{
    91  					"GUID":    Equal("process-2-guid"),
    92  					"Name":    Equal("echo-sidecar"),
    93  					"Command": Equal(types.FilteredString{IsSet: true, Value: "start-echo-server"}),
    94  				}))
    95  			})
    96  		})
    97  
    98  		When("the process has no sidecars", func() {
    99  			BeforeEach(func() {
   100  				response := `{
   101  					"resources": []
   102  				}`
   103  				server.AppendHandlers(
   104  					CombineHandlers(
   105  						VerifyRequest(http.MethodGet, "/v3/processes/some-process-guid/sidecars"),
   106  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   107  					),
   108  				)
   109  			})
   110  
   111  			It("does not error", func() {
   112  				Expect(err).NotTo(HaveOccurred())
   113  			})
   114  		})
   115  
   116  		When("the process does not exist", func() {
   117  			BeforeEach(func() {
   118  				response := `{
   119  					"errors": [
   120  						{
   121  							"detail": "Process not found",
   122  							"title": "CF-ResourceNotFound",
   123  							"code": 10010
   124  						}
   125  					]
   126  				}`
   127  
   128  				server.AppendHandlers(
   129  					CombineHandlers(
   130  						VerifyRequest(http.MethodGet, "/v3/processes/some-process-guid/sidecars"),
   131  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   132  					),
   133  				)
   134  
   135  			})
   136  
   137  			It("returns an error and warnings", func() {
   138  				Expect(err).To(MatchError(ccerror.ProcessNotFoundError{}))
   139  				Expect(warnings).To(ConsistOf("this is a warning"))
   140  			})
   141  		})
   142  
   143  		When("the cloud controller returns errors and warnings", func() {
   144  			BeforeEach(func() {
   145  				response := `{
   146  					"errors": [
   147  						{
   148  							"code": 10008,
   149  							"detail": "The request is semantically invalid: command presence",
   150  							"title": "CF-UnprocessableEntity"
   151  						},
   152  						{
   153  							"code": 10009,
   154  							"detail": "Some CC Error",
   155  							"title": "CF-SomeNewError"
   156  						}
   157  					]
   158  				}`
   159  				server.AppendHandlers(
   160  					CombineHandlers(
   161  						VerifyRequest(http.MethodGet, "/v3/processes/some-process-guid/sidecars"),
   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  				Expect(err).To(MatchError(ccerror.MultiError{
   169  					ResponseCode: http.StatusTeapot,
   170  					Errors: []ccerror.V3Error{
   171  						{
   172  							Code:   10008,
   173  							Detail: "The request is semantically invalid: command presence",
   174  							Title:  "CF-UnprocessableEntity",
   175  						},
   176  						{
   177  							Code:   10009,
   178  							Detail: "Some CC Error",
   179  							Title:  "CF-SomeNewError",
   180  						},
   181  					},
   182  				}))
   183  				Expect(warnings).To(ConsistOf("this is a warning"))
   184  			})
   185  		})
   186  	})
   187  })