github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/api/cloudcontroller/ccv3/instance_test.go (about)

     1  package ccv3_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     8  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/ghttp"
    12  )
    13  
    14  var _ = Describe("Instance", func() {
    15  	var client *Client
    16  
    17  	BeforeEach(func() {
    18  		client = NewTestClient()
    19  	})
    20  
    21  	Describe("GetProcessInstances", func() {
    22  		Context("when the process exists", func() {
    23  			BeforeEach(func() {
    24  				response := fmt.Sprintf(`{
    25  					"resources": [
    26  						{
    27  							"state": "RUNNING",
    28  							"usage": {
    29  								"cpu": 0.01,
    30  								"mem": 1000000,
    31  								"disk": 2000000
    32  							},
    33  							"mem_quota": 2000000,
    34  							"disk_quota": 4000000,
    35  							"index": 0,
    36  							"uptime": 123
    37  						},
    38  						{
    39  							"state": "RUNNING",
    40  							"usage": {
    41  								"cpu": 0.02,
    42  								"mem": 8000000,
    43  								"disk": 16000000
    44  							},
    45  							"mem_quota": 16000000,
    46  							"disk_quota": 32000000,
    47  							"index": 1,
    48  							"uptime": 456
    49  						}
    50  					]
    51  				}`, server.URL())
    52  				server.AppendHandlers(
    53  					CombineHandlers(
    54  						VerifyRequest(http.MethodGet, "/v3/processes/some-process-guid/stats"),
    55  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"warning-1"}}),
    56  					),
    57  				)
    58  			})
    59  
    60  			It("returns a list of instances for the given process and all warnings", func() {
    61  				processes, warnings, err := client.GetProcessInstances("some-process-guid")
    62  				Expect(err).ToNot(HaveOccurred())
    63  
    64  				Expect(processes).To(ConsistOf(
    65  					Instance{
    66  						State:       "RUNNING",
    67  						CPU:         0.01,
    68  						MemoryUsage: 1000000,
    69  						DiskUsage:   2000000,
    70  						MemoryQuota: 2000000,
    71  						DiskQuota:   4000000,
    72  						Index:       0,
    73  						Uptime:      123,
    74  					},
    75  					Instance{
    76  						State:       "RUNNING",
    77  						CPU:         0.02,
    78  						MemoryUsage: 8000000,
    79  						DiskUsage:   16000000,
    80  						MemoryQuota: 16000000,
    81  						DiskQuota:   32000000,
    82  						Index:       1,
    83  						Uptime:      456,
    84  					},
    85  				))
    86  				Expect(warnings).To(ConsistOf("warning-1"))
    87  			})
    88  		})
    89  
    90  		Context("when cloud controller returns an error", func() {
    91  			BeforeEach(func() {
    92  				response := `{
    93  					"errors": [
    94  						{
    95  							"code": 10010,
    96  							"detail": "Process not found",
    97  							"title": "CF-ResourceNotFound"
    98  						}
    99  					]
   100  				}`
   101  				server.AppendHandlers(
   102  					CombineHandlers(
   103  						VerifyRequest(http.MethodGet, "/v3/processes/some-process-guid/stats"),
   104  						RespondWith(http.StatusNotFound, response),
   105  					),
   106  				)
   107  			})
   108  
   109  			It("returns the error", func() {
   110  				_, _, err := client.GetProcessInstances("some-process-guid")
   111  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{Message: "Process not found"}))
   112  			})
   113  		})
   114  	})
   115  
   116  })