github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/cloudcontroller/ccv3/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("DeleteInstanceByApplicationProcessTypeAndIndex", 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", 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 Context("when the process exists", func() { 76 BeforeEach(func() { 77 response := `{ 78 "resources": [ 79 { 80 "state": "RUNNING", 81 "usage": { 82 "cpu": 0.01, 83 "mem": 1000000, 84 "disk": 2000000 85 }, 86 "mem_quota": 2000000, 87 "disk_quota": 4000000, 88 "index": 0, 89 "uptime": 123 90 }, 91 { 92 "state": "RUNNING", 93 "usage": { 94 "cpu": 0.02, 95 "mem": 8000000, 96 "disk": 16000000 97 }, 98 "mem_quota": 16000000, 99 "disk_quota": 32000000, 100 "index": 1, 101 "uptime": 456 102 } 103 ] 104 }` 105 server.AppendHandlers( 106 CombineHandlers( 107 VerifyRequest(http.MethodGet, "/v3/processes/some-process-guid/stats"), 108 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"warning-1"}}), 109 ), 110 ) 111 }) 112 113 It("returns a list of instances for the given process and all warnings", func() { 114 processes, warnings, err := client.GetProcessInstances("some-process-guid") 115 Expect(err).ToNot(HaveOccurred()) 116 117 Expect(processes).To(ConsistOf( 118 ProcessInstance{ 119 State: constant.ProcessInstanceRunning, 120 CPU: 0.01, 121 MemoryUsage: 1000000, 122 DiskUsage: 2000000, 123 MemoryQuota: 2000000, 124 DiskQuota: 4000000, 125 Index: 0, 126 Uptime: 123, 127 }, 128 ProcessInstance{ 129 State: constant.ProcessInstanceRunning, 130 CPU: 0.02, 131 MemoryUsage: 8000000, 132 DiskUsage: 16000000, 133 MemoryQuota: 16000000, 134 DiskQuota: 32000000, 135 Index: 1, 136 Uptime: 456, 137 }, 138 )) 139 Expect(warnings).To(ConsistOf("warning-1")) 140 }) 141 }) 142 143 Context("when cloud controller returns an error", func() { 144 BeforeEach(func() { 145 response := `{ 146 "errors": [ 147 { 148 "code": 10010, 149 "detail": "Process not found", 150 "title": "CF-ResourceNotFound" 151 } 152 ] 153 }` 154 server.AppendHandlers( 155 CombineHandlers( 156 VerifyRequest(http.MethodGet, "/v3/processes/some-process-guid/stats"), 157 RespondWith(http.StatusNotFound, response), 158 ), 159 ) 160 }) 161 162 It("returns the error", func() { 163 _, _, err := client.GetProcessInstances("some-process-guid") 164 Expect(err).To(MatchError(ccerror.ProcessNotFoundError{})) 165 }) 166 }) 167 }) 168 169 })