github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/ghttp"
    12  )
    13  
    14  var _ = Describe("Application Instance", func() {
    15  	var client *Client
    16  
    17  	BeforeEach(func() {
    18  		client = NewTestClient()
    19  	})
    20  
    21  	Describe("GetApplicationApplicationInstances", func() {
    22  		var (
    23  			appGUID string
    24  
    25  			instances  map[int]ApplicationInstance
    26  			warnings   Warnings
    27  			executeErr error
    28  		)
    29  
    30  		BeforeEach(func() {
    31  			appGUID = "some-app-guid"
    32  		})
    33  
    34  		JustBeforeEach(func() {
    35  			instances, warnings, executeErr = client.GetApplicationApplicationInstances(appGUID)
    36  		})
    37  
    38  		Context("when the app is found", func() {
    39  			BeforeEach(func() {
    40  				response := `{
    41  					"0": {
    42  						"state": "RUNNING",
    43  						"since": 1403140717.984577,
    44  						"details": "some detail"
    45  					},
    46  					"1": {
    47  						"state": "CRASHED",
    48  						"since": 2514251828.984577,
    49  						"details": "more details"
    50  					}
    51  				}`
    52  
    53  				server.AppendHandlers(
    54  					CombineHandlers(
    55  						VerifyRequest(http.MethodGet, "/v2/apps/some-app-guid/instances"),
    56  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    57  					),
    58  				)
    59  			})
    60  
    61  			It("returns the app instances and warnings", func() {
    62  				Expect(executeErr).ToNot(HaveOccurred())
    63  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
    64  				Expect(instances).To(Equal(
    65  					map[int]ApplicationInstance{
    66  						0: {
    67  							ID:      0,
    68  							State:   constant.ApplicationInstanceRunning,
    69  							Since:   1403140717.984577,
    70  							Details: "some detail",
    71  						},
    72  						1: {
    73  							ID:      1,
    74  							State:   constant.ApplicationInstanceCrashed,
    75  							Since:   2514251828.984577,
    76  							Details: "more details",
    77  						},
    78  					},
    79  				))
    80  			})
    81  		})
    82  
    83  		Context("when the client returns an error", func() {
    84  			BeforeEach(func() {
    85  				response := `{
    86  					"code": 100004,
    87  					"description": "The app could not be found: some-app-guid",
    88  					"error_code": "CF-AppNotFound"
    89  				}`
    90  				server.AppendHandlers(
    91  					CombineHandlers(
    92  						VerifyRequest(http.MethodGet, "/v2/apps/some-app-guid/instances"),
    93  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    94  					),
    95  				)
    96  			})
    97  
    98  			It("returns the error and warnings", func() {
    99  				Expect(executeErr).To(MatchError(ccerror.ResourceNotFoundError{
   100  					Message: "The app could not be found: some-app-guid",
   101  				}))
   102  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   103  			})
   104  		})
   105  	})
   106  })