github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+incompatible/api/cloudcontroller/ccv3/process_instance_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/api/cloudcontroller/ccv3/constant"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/ghttp"
    12  )
    13  
    14  var _ = Describe("ProcessInstance", func() {
    15  	var client *Client
    16  
    17  	BeforeEach(func() {
    18  		client = NewTestClient()
    19  	})
    20  
    21  	Describe("DeleteApplicationProcessInstance", func() {
    22  		var (
    23  			warnings   Warnings
    24  			executeErr error
    25  		)
    26  
    27  		JustBeforeEach(func() {
    28  			warnings, executeErr = client.DeleteApplicationProcessInstance("some-app-guid", "some-process-type", 666)
    29  		})
    30  
    31  		When("the cloud controller returns an error", func() {
    32  			BeforeEach(func() {
    33  				response := `{
    34  					"errors": [
    35  						{
    36  							"code": 10010,
    37  							"detail": "Process not found",
    38  							"title": "CF-ResourceNotFound"
    39  						}
    40  					]
    41  				}`
    42  
    43  				server.AppendHandlers(
    44  					CombineHandlers(
    45  						VerifyRequest(http.MethodDelete, "/v3/apps/some-app-guid/processes/some-process-type/instances/666"),
    46  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"warning-1"}}),
    47  					),
    48  				)
    49  			})
    50  
    51  			It("returns the error and all warnings", func() {
    52  				Expect(executeErr).To(MatchError(ccerror.ProcessNotFoundError{}))
    53  				Expect(warnings).To(ConsistOf("warning-1"))
    54  			})
    55  		})
    56  
    57  		When("the delete is successful", func() {
    58  			BeforeEach(func() {
    59  				server.AppendHandlers(
    60  					CombineHandlers(
    61  						VerifyRequest(http.MethodDelete, "/v3/apps/some-app-guid/processes/some-process-type/instances/666"),
    62  						RespondWith(http.StatusNoContent, "", http.Header{"X-Cf-Warnings": {"warning-1"}}),
    63  					),
    64  				)
    65  			})
    66  
    67  			It("returns all warnings", func() {
    68  				Expect(executeErr).ToNot(HaveOccurred())
    69  				Expect(warnings).To(ConsistOf("warning-1"))
    70  			})
    71  		})
    72  	})
    73  
    74  	Describe("GetProcessInstances", func() {
    75  		var (
    76  			processes  []ProcessInstance
    77  			warnings   Warnings
    78  			executeErr error
    79  		)
    80  
    81  		JustBeforeEach(func() {
    82  			processes, warnings, executeErr = client.GetProcessInstances("some-process-guid")
    83  		})
    84  
    85  		When("the process exists", func() {
    86  			BeforeEach(func() {
    87  				response := `{
    88  					"resources": [
    89  						{
    90  							"state": "RUNNING",
    91  							"usage": {
    92  								"cpu": 0.01,
    93  								"mem": 1000000,
    94  								"disk": 2000000
    95  							},
    96  							"mem_quota": 2000000,
    97  							"disk_quota": 4000000,
    98  							"index": 0,
    99  							"uptime": 123,
   100  							"details": "some details"
   101  						},
   102  						{
   103  							"state": "RUNNING",
   104  							"usage": {
   105  								"cpu": 0.02,
   106  								"mem": 8000000,
   107  								"disk": 16000000
   108  							},
   109  							"mem_quota": 16000000,
   110  							"disk_quota": 32000000,
   111  							"index": 1,
   112  							"uptime": 456
   113  						}
   114  					]
   115  				}`
   116  				server.AppendHandlers(
   117  					CombineHandlers(
   118  						VerifyRequest(http.MethodGet, "/v3/processes/some-process-guid/stats"),
   119  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   120  					),
   121  				)
   122  			})
   123  
   124  			It("returns a list of instances for the given process and all warnings", func() {
   125  				Expect(executeErr).ToNot(HaveOccurred())
   126  
   127  				Expect(processes).To(ConsistOf(
   128  					ProcessInstance{
   129  						State:       constant.ProcessInstanceRunning,
   130  						CPU:         0.01,
   131  						MemoryUsage: 1000000,
   132  						DiskUsage:   2000000,
   133  						MemoryQuota: 2000000,
   134  						DiskQuota:   4000000,
   135  						Index:       0,
   136  						Uptime:      123,
   137  						Details:     "some details",
   138  					},
   139  					ProcessInstance{
   140  						State:       constant.ProcessInstanceRunning,
   141  						CPU:         0.02,
   142  						MemoryUsage: 8000000,
   143  						DiskUsage:   16000000,
   144  						MemoryQuota: 16000000,
   145  						DiskQuota:   32000000,
   146  						Index:       1,
   147  						Uptime:      456,
   148  					},
   149  				))
   150  				Expect(warnings).To(ConsistOf("warning-1"))
   151  			})
   152  		})
   153  
   154  		When("cloud controller returns an error", func() {
   155  			BeforeEach(func() {
   156  				response := `{
   157  					"errors": [
   158  						{
   159  							"code": 10010,
   160  							"detail": "Process not found",
   161  							"title": "CF-ResourceNotFound"
   162  						}
   163  					]
   164  				}`
   165  				server.AppendHandlers(
   166  					CombineHandlers(
   167  						VerifyRequest(http.MethodGet, "/v3/processes/some-process-guid/stats"),
   168  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   169  					),
   170  				)
   171  			})
   172  
   173  			It("returns the error and all warnings", func() {
   174  				Expect(executeErr).To(MatchError(ccerror.ProcessNotFoundError{}))
   175  				Expect(warnings).To(ConsistOf("warning-1"))
   176  			})
   177  		})
   178  	})
   179  
   180  })