github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/api/cloudcontroller/ccv2/application_instance_status_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 Status", func() {
    14  	var client *Client
    15  
    16  	BeforeEach(func() {
    17  		client = NewTestClient()
    18  	})
    19  
    20  	Describe("GetApplicationInstanceStatusesByApplication", func() {
    21  		Context("when the app is found", func() {
    22  			BeforeEach(func() {
    23  				response := `{
    24  					"0": {
    25  						"state": "RUNNING",
    26  						"isolation_segment": "some-isolation-segment",
    27  						"stats": {
    28  							"usage": {
    29  								"disk": 66392064,
    30  								"mem": 29880320,
    31  								"cpu": 0.13511219703079957,
    32  								"time": "2014-06-19 22:37:58 +0000"
    33  							},
    34  							"name": "app_name",
    35  							"uris": [
    36  								"app_name.example.com"
    37  							],
    38  							"host": "10.0.0.1",
    39  							"port": 61035,
    40  							"uptime": 65007,
    41  							"mem_quota": 536870912,
    42  							"disk_quota": 1073741824,
    43  							"fds_quota": 16384
    44  						}
    45  					},
    46  					"1": {
    47  						"state": "STARTING",
    48  						"isolation_segment": "some-isolation-segment",
    49  						"stats": {
    50  							"usage": {
    51  								"disk": 66392064,
    52  								"mem": 29880320,
    53  								"cpu": 0.13511219703079957,
    54  								"time": "2014-06-19 22:37:58 +0000"
    55  							},
    56  							"name": "app_name",
    57  							"uris": [
    58  								"app_name.example.com"
    59  							],
    60  							"host": "10.0.0.1",
    61  							"port": 61035,
    62  							"uptime": 65007,
    63  							"mem_quota": 536870912,
    64  							"disk_quota": 1073741824,
    65  							"fds_quota": 16384
    66  						}
    67  					}
    68  				}`
    69  
    70  				server.AppendHandlers(
    71  					CombineHandlers(
    72  						VerifyRequest(http.MethodGet, "/v2/apps/some-app-guid/stats"),
    73  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    74  					),
    75  				)
    76  			})
    77  
    78  			It("returns the app instances and warnings", func() {
    79  				instances, warnings, err := client.GetApplicationInstanceStatusesByApplication("some-app-guid")
    80  				Expect(err).ToNot(HaveOccurred())
    81  				Expect(instances).To(HaveLen(2))
    82  
    83  				Expect(instances[0]).To(Equal(ApplicationInstanceStatus{
    84  					CPU:              0.13511219703079957,
    85  					Disk:             66392064,
    86  					DiskQuota:        1073741824,
    87  					ID:               0,
    88  					IsolationSegment: "some-isolation-segment",
    89  					Memory:           29880320,
    90  					MemoryQuota:      536870912,
    91  					State:            ApplicationInstanceRunning,
    92  					Uptime:           65007,
    93  				},
    94  				))
    95  
    96  				Expect(instances[1]).To(Equal(ApplicationInstanceStatus{
    97  					CPU:              0.13511219703079957,
    98  					Disk:             66392064,
    99  					DiskQuota:        1073741824,
   100  					ID:               1,
   101  					IsolationSegment: "some-isolation-segment",
   102  					Memory:           29880320,
   103  					MemoryQuota:      536870912,
   104  					State:            ApplicationInstanceStarting,
   105  					Uptime:           65007,
   106  				},
   107  				))
   108  
   109  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   110  			})
   111  		})
   112  
   113  		Context("when the client returns an error", func() {
   114  			BeforeEach(func() {
   115  				response := `{
   116  					"code": 100004,
   117  					"description": "The app could not be found: some-app-guid",
   118  					"error_code": "CF-AppNotFound"
   119  				}`
   120  				server.AppendHandlers(
   121  					CombineHandlers(
   122  						VerifyRequest(http.MethodGet, "/v2/apps/some-app-guid/stats"),
   123  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   124  					),
   125  				)
   126  			})
   127  
   128  			It("returns the error and warnings", func() {
   129  				_, warnings, err := client.GetApplicationInstanceStatusesByApplication("some-app-guid")
   130  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{
   131  					Message: "The app could not be found: some-app-guid",
   132  				}))
   133  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   134  			})
   135  		})
   136  	})
   137  })