github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/app_summary_test.go (about) 1 package api_test 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "time" 7 8 . "code.cloudfoundry.org/cli/cf/api" 9 "code.cloudfoundry.org/cli/cf/api/apifakes" 10 "code.cloudfoundry.org/cli/cf/net" 11 "code.cloudfoundry.org/cli/cf/terminal/terminalfakes" 12 "code.cloudfoundry.org/cli/cf/trace/tracefakes" 13 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 14 . "code.cloudfoundry.org/cli/util/testhelpers/matchers" 15 testnet "code.cloudfoundry.org/cli/util/testhelpers/net" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 ) 19 20 var _ = Describe("AppSummaryRepository", func() { 21 var ( 22 testServer *httptest.Server 23 handler *testnet.TestHandler 24 repo AppSummaryRepository 25 ) 26 27 Describe("GetSummariesInCurrentSpace()", func() { 28 BeforeEach(func() { 29 getAppSummariesRequest := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{ 30 Method: "GET", 31 Path: "/v2/spaces/my-space-guid/summary", 32 Response: testnet.TestResponse{ 33 Status: http.StatusOK, 34 Body: getAppSummariesResponseBody, 35 }, 36 }) 37 38 testServer, handler = testnet.NewServer([]testnet.TestRequest{getAppSummariesRequest}) 39 configRepo := testconfig.NewRepositoryWithDefaults() 40 configRepo.SetAPIEndpoint(testServer.URL) 41 gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "") 42 repo = NewCloudControllerAppSummaryRepository(configRepo, gateway) 43 }) 44 45 AfterEach(func() { 46 testServer.Close() 47 }) 48 49 It("returns a slice of app summaries for each instance", func() { 50 apps, apiErr := repo.GetSummariesInCurrentSpace() 51 Expect(handler).To(HaveAllRequestsCalled()) 52 53 Expect(apiErr).NotTo(HaveOccurred()) 54 Expect(3).To(Equal(len(apps))) 55 56 app1 := apps[0] 57 Expect(app1.Name).To(Equal("app1")) 58 Expect(app1.GUID).To(Equal("app-1-guid")) 59 Expect(app1.BuildpackURL).To(Equal("go_buildpack")) 60 Expect(len(app1.Routes)).To(Equal(1)) 61 Expect(app1.Routes[0].URL()).To(Equal("app1.cfapps.io")) 62 63 Expect(app1.State).To(Equal("started")) 64 Expect(app1.Command).To(Equal("start_command")) 65 Expect(app1.InstanceCount).To(Equal(1)) 66 Expect(app1.RunningInstances).To(Equal(1)) 67 Expect(app1.Memory).To(Equal(int64(128))) 68 Expect(app1.PackageUpdatedAt.Format("2006-01-02T15:04:05Z07:00")).To(Equal("2014-10-24T19:54:00Z")) 69 Expect(app1.AppPorts).To(Equal([]int{8080, 9090})) 70 71 app2 := apps[1] 72 Expect(app2.Name).To(Equal("app2")) 73 Expect(app2.Command).To(Equal("")) 74 Expect(app2.GUID).To(Equal("app-2-guid")) 75 Expect(len(app2.Routes)).To(Equal(2)) 76 Expect(app2.Routes[0].URL()).To(Equal("app2.cfapps.io")) 77 Expect(app2.Routes[1].URL()).To(Equal("foo.cfapps.io")) 78 Expect(app2.AppPorts).To(HaveLen(0)) 79 80 Expect(app2.State).To(Equal("started")) 81 Expect(app2.InstanceCount).To(Equal(3)) 82 Expect(app2.RunningInstances).To(Equal(1)) 83 Expect(app2.Memory).To(Equal(int64(512))) 84 Expect(app2.PackageUpdatedAt.Format("2006-01-02T15:04:05Z07:00")).To(Equal("2012-10-24T19:55:00Z")) 85 86 nullUpdateAtApp := apps[2] 87 Expect(nullUpdateAtApp.PackageUpdatedAt).To(BeNil()) 88 }) 89 }) 90 91 Describe("GetSummary()", func() { 92 BeforeEach(func() { 93 getAppSummaryRequest := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{ 94 Method: "GET", 95 Path: "/v2/apps/app1-guid/summary", 96 Response: testnet.TestResponse{ 97 Status: http.StatusOK, 98 Body: getAppSummaryResponseBody, 99 }, 100 }) 101 102 testServer, handler = testnet.NewServer([]testnet.TestRequest{getAppSummaryRequest}) 103 configRepo := testconfig.NewRepositoryWithDefaults() 104 configRepo.SetAPIEndpoint(testServer.URL) 105 gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "") 106 repo = NewCloudControllerAppSummaryRepository(configRepo, gateway) 107 }) 108 109 AfterEach(func() { 110 testServer.Close() 111 }) 112 113 It("returns the app summary", func() { 114 app, apiErr := repo.GetSummary("app1-guid") 115 Expect(handler).To(HaveAllRequestsCalled()) 116 117 Expect(apiErr).NotTo(HaveOccurred()) 118 119 Expect(app.Name).To(Equal("app1")) 120 Expect(app.GUID).To(Equal("app-1-guid")) 121 Expect(app.BuildpackURL).To(Equal("go_buildpack")) 122 Expect(len(app.Routes)).To(Equal(1)) 123 Expect(app.Routes[0].URL()).To(Equal("app1.cfapps.io")) 124 125 Expect(app.State).To(Equal("started")) 126 Expect(app.Command).To(Equal("start_command")) 127 Expect(app.InstanceCount).To(Equal(1)) 128 Expect(app.RunningInstances).To(Equal(1)) 129 Expect(app.Memory).To(Equal(int64(128))) 130 Expect(app.PackageUpdatedAt.Format("2006-01-02T15:04:05Z07:00")).To(Equal("2014-10-24T19:54:00Z")) 131 Expect(app.StackGUID).To(Equal("the-stack-guid")) 132 Expect(app.HealthCheckType).To(Equal("some-health-check-type")) 133 Expect(app.HealthCheckHTTPEndpoint).To(Equal("/some-endpoint")) 134 }) 135 }) 136 137 }) 138 139 const getAppSummariesResponseBody string = ` 140 { 141 "apps":[ 142 { 143 "guid":"app-1-guid", 144 "routes":[ 145 { 146 "guid":"route-1-guid", 147 "host":"app1", 148 "domain":{ 149 "guid":"domain-1-guid", 150 "name":"cfapps.io" 151 } 152 } 153 ], 154 "running_instances":1, 155 "name":"app1", 156 "memory":128, 157 "command": "start_command", 158 "instances":1, 159 "buildpack":"go_buildpack", 160 "state":"STARTED", 161 "service_names":[ 162 "my-service-instance" 163 ], 164 "package_updated_at":"2014-10-24T19:54:00+00:00", 165 "ports":[ 166 8080, 167 9090 168 ] 169 },{ 170 "guid":"app-2-guid", 171 "routes":[ 172 { 173 "guid":"route-2-guid", 174 "host":"app2", 175 "domain":{ 176 "guid":"domain-1-guid", 177 "name":"cfapps.io" 178 } 179 }, 180 { 181 "guid":"route-2-guid", 182 "host":"foo", 183 "domain":{ 184 "guid":"domain-1-guid", 185 "name":"cfapps.io" 186 } 187 } 188 ], 189 "running_instances":1, 190 "name":"app2", 191 "memory":512, 192 "instances":3, 193 "state":"STARTED", 194 "service_names":[ 195 "my-service-instance" 196 ], 197 "package_updated_at":"2012-10-24T19:55:00+00:00", 198 "ports":null 199 },{ 200 "guid":"app-with-null-updated-at-guid", 201 "routes":[ 202 { 203 "guid":"route-3-guid", 204 "host":"app3", 205 "domain":{ 206 "guid":"domain-3-guid", 207 "name":"cfapps.io" 208 } 209 } 210 ], 211 "running_instances":1, 212 "name":"app-with-null-updated-at", 213 "memory":512, 214 "instances":3, 215 "state":"STARTED", 216 "service_names":[ 217 "my-service-instance" 218 ], 219 "package_updated_at":null, 220 "ports":null 221 } 222 ] 223 }` 224 225 const getAppSummaryResponseBody string = ` 226 { 227 "guid":"app-1-guid", 228 "routes":[ 229 { 230 "guid":"route-1-guid", 231 "host":"app1", 232 "domain":{ 233 "guid":"domain-1-guid", 234 "name":"cfapps.io" 235 } 236 } 237 ], 238 "running_instances":1, 239 "name":"app1", 240 "stack_guid":"the-stack-guid", 241 "memory":128, 242 "command": "start_command", 243 "instances":1, 244 "buildpack":"go_buildpack", 245 "state":"STARTED", 246 "service_names":[ 247 "my-service-instance" 248 ], 249 "package_updated_at":"2014-10-24T19:54:00+00:00", 250 "health_check_type":"some-health-check-type", 251 "health_check_http_endpoint":"/some-endpoint" 252 }`