github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/api/cloudcontroller/ccv3/process_instance_test.go (about)

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