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