github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+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 Context("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 Context("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 Context("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 }, 101 { 102 "state": "RUNNING", 103 "usage": { 104 "cpu": 0.02, 105 "mem": 8000000, 106 "disk": 16000000 107 }, 108 "mem_quota": 16000000, 109 "disk_quota": 32000000, 110 "index": 1, 111 "uptime": 456 112 } 113 ] 114 }` 115 server.AppendHandlers( 116 CombineHandlers( 117 VerifyRequest(http.MethodGet, "/v3/processes/some-process-guid/stats"), 118 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"warning-1"}}), 119 ), 120 ) 121 }) 122 123 It("returns a list of instances for the given process and all warnings", func() { 124 Expect(executeErr).ToNot(HaveOccurred()) 125 126 Expect(processes).To(ConsistOf( 127 ProcessInstance{ 128 State: constant.ProcessInstanceRunning, 129 CPU: 0.01, 130 MemoryUsage: 1000000, 131 DiskUsage: 2000000, 132 MemoryQuota: 2000000, 133 DiskQuota: 4000000, 134 Index: 0, 135 Uptime: 123, 136 }, 137 ProcessInstance{ 138 State: constant.ProcessInstanceRunning, 139 CPU: 0.02, 140 MemoryUsage: 8000000, 141 DiskUsage: 16000000, 142 MemoryQuota: 16000000, 143 DiskQuota: 32000000, 144 Index: 1, 145 Uptime: 456, 146 }, 147 )) 148 Expect(warnings).To(ConsistOf("warning-1")) 149 }) 150 }) 151 152 Context("when cloud controller returns an error", func() { 153 BeforeEach(func() { 154 response := `{ 155 "errors": [ 156 { 157 "code": 10010, 158 "detail": "Process not found", 159 "title": "CF-ResourceNotFound" 160 } 161 ] 162 }` 163 server.AppendHandlers( 164 CombineHandlers( 165 VerifyRequest(http.MethodGet, "/v3/processes/some-process-guid/stats"), 166 RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"warning-1"}}), 167 ), 168 ) 169 }) 170 171 It("returns the error and all warnings", func() { 172 Expect(executeErr).To(MatchError(ccerror.ProcessNotFoundError{})) 173 Expect(warnings).To(ConsistOf("warning-1")) 174 }) 175 }) 176 }) 177 178 })