github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/api/cloudcontroller/ccv2/application_instance_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("Application Instance", func() {
    14  	var client *Client
    15  
    16  	BeforeEach(func() {
    17  		client = NewTestClient()
    18  	})
    19  
    20  	Describe("GetApplicationInstancesByApplication", func() {
    21  		Context("when the app is found", func() {
    22  			BeforeEach(func() {
    23  
    24  				response := `{
    25  					"0": {
    26  						"state": "RUNNING",
    27  						"since": 1403140717.984577,
    28  						"details": "some detail"
    29  					},
    30  					"1": {
    31  						"state": "CRASHED",
    32  						"since": 2514251828.984577,
    33  						"details": "more details"
    34  					}
    35  				}`
    36  
    37  				server.AppendHandlers(
    38  					CombineHandlers(
    39  						VerifyRequest(http.MethodGet, "/v2/apps/some-app-guid/instances"),
    40  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    41  					),
    42  				)
    43  			})
    44  
    45  			It("returns the app instances and warnings", func() {
    46  				instances, warnings, err := client.GetApplicationInstancesByApplication("some-app-guid")
    47  				Expect(err).ToNot(HaveOccurred())
    48  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
    49  				Expect(instances).To(HaveLen(2))
    50  
    51  				Expect(instances[0]).To(Equal(ApplicationInstance{
    52  					ID:      0,
    53  					State:   ApplicationInstanceRunning,
    54  					Since:   1403140717.984577,
    55  					Details: "some detail",
    56  				},
    57  				))
    58  
    59  				Expect(instances[1]).To(Equal(ApplicationInstance{
    60  					ID:      1,
    61  					State:   ApplicationInstanceCrashed,
    62  					Since:   2514251828.984577,
    63  					Details: "more details",
    64  				},
    65  				))
    66  			})
    67  		})
    68  
    69  		Context("when the client returns an error", func() {
    70  			BeforeEach(func() {
    71  				response := `{
    72  					"code": 100004,
    73  					"description": "The app could not be found: some-app-guid",
    74  					"error_code": "CF-AppNotFound"
    75  				}`
    76  				server.AppendHandlers(
    77  					CombineHandlers(
    78  						VerifyRequest(http.MethodGet, "/v2/apps/some-app-guid/instances"),
    79  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    80  					),
    81  				)
    82  			})
    83  
    84  			It("returns the error and warnings", func() {
    85  				_, warnings, err := client.GetApplicationInstancesByApplication("some-app-guid")
    86  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{
    87  					Message: "The app could not be found: some-app-guid",
    88  				}))
    89  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
    90  			})
    91  		})
    92  	})
    93  })