github.com/cloudfoundry/cli@v7.1.0+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/cf/util/testhelpers/configuration"
    14  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    15  	testnet "code.cloudfoundry.org/cli/cf/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  
    70  			app2 := apps[1]
    71  			Expect(app2.Name).To(Equal("app2"))
    72  			Expect(app2.Command).To(Equal(""))
    73  			Expect(app2.GUID).To(Equal("app-2-guid"))
    74  			Expect(len(app2.Routes)).To(Equal(2))
    75  			Expect(app2.Routes[0].URL()).To(Equal("app2.cfapps.io"))
    76  			Expect(app2.Routes[1].URL()).To(Equal("foo.cfapps.io"))
    77  
    78  			Expect(app2.State).To(Equal("started"))
    79  			Expect(app2.InstanceCount).To(Equal(3))
    80  			Expect(app2.RunningInstances).To(Equal(1))
    81  			Expect(app2.Memory).To(Equal(int64(512)))
    82  			Expect(app2.PackageUpdatedAt.Format("2006-01-02T15:04:05Z07:00")).To(Equal("2012-10-24T19:55:00Z"))
    83  
    84  			nullUpdateAtApp := apps[2]
    85  			Expect(nullUpdateAtApp.PackageUpdatedAt).To(BeNil())
    86  		})
    87  	})
    88  
    89  	Describe("GetSummary()", func() {
    90  		BeforeEach(func() {
    91  			getAppSummaryRequest := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
    92  				Method: "GET",
    93  				Path:   "/v2/apps/app1-guid/summary",
    94  				Response: testnet.TestResponse{
    95  					Status: http.StatusOK,
    96  					Body:   getAppSummaryResponseBody,
    97  				},
    98  			})
    99  
   100  			testServer, handler = testnet.NewServer([]testnet.TestRequest{getAppSummaryRequest})
   101  			configRepo := testconfig.NewRepositoryWithDefaults()
   102  			configRepo.SetAPIEndpoint(testServer.URL)
   103  			gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
   104  			repo = NewCloudControllerAppSummaryRepository(configRepo, gateway)
   105  		})
   106  
   107  		AfterEach(func() {
   108  			testServer.Close()
   109  		})
   110  
   111  		It("returns the app summary", func() {
   112  			app, apiErr := repo.GetSummary("app1-guid")
   113  			Expect(handler).To(HaveAllRequestsCalled())
   114  
   115  			Expect(apiErr).NotTo(HaveOccurred())
   116  
   117  			Expect(app.Name).To(Equal("app1"))
   118  			Expect(app.GUID).To(Equal("app-1-guid"))
   119  			Expect(app.BuildpackURL).To(Equal("go_buildpack"))
   120  			Expect(len(app.Routes)).To(Equal(1))
   121  			Expect(app.Routes[0].URL()).To(Equal("app1.cfapps.io"))
   122  
   123  			Expect(app.State).To(Equal("started"))
   124  			Expect(app.Command).To(Equal("start_command"))
   125  			Expect(app.InstanceCount).To(Equal(1))
   126  			Expect(app.RunningInstances).To(Equal(1))
   127  			Expect(app.Memory).To(Equal(int64(128)))
   128  			Expect(app.PackageUpdatedAt.Format("2006-01-02T15:04:05Z07:00")).To(Equal("2014-10-24T19:54:00Z"))
   129  			Expect(app.StackGUID).To(Equal("the-stack-guid"))
   130  			Expect(app.HealthCheckType).To(Equal("some-health-check-type"))
   131  			Expect(app.HealthCheckHTTPEndpoint).To(Equal("/some-endpoint"))
   132  		})
   133  	})
   134  
   135  })
   136  
   137  const getAppSummariesResponseBody string = `
   138  {
   139    "apps":[
   140      {
   141        "guid":"app-1-guid",
   142        "routes":[
   143          {
   144            "guid":"route-1-guid",
   145            "host":"app1",
   146            "domain":{
   147              "guid":"domain-1-guid",
   148              "name":"cfapps.io"
   149            }
   150          }
   151        ],
   152        "running_instances":1,
   153        "name":"app1",
   154        "memory":128,
   155  			"command": "start_command",
   156        "instances":1,
   157  			"buildpack":"go_buildpack",
   158        "state":"STARTED",
   159        "service_names":[
   160        	"my-service-instance"
   161        ],
   162  			"package_updated_at":"2014-10-24T19:54:00+00:00",
   163  			"ports":[
   164  				8080,
   165  				9090
   166  			]
   167      },{
   168        "guid":"app-2-guid",
   169        "routes":[
   170          {
   171            "guid":"route-2-guid",
   172            "host":"app2",
   173            "domain":{
   174              "guid":"domain-1-guid",
   175              "name":"cfapps.io"
   176            }
   177          },
   178          {
   179            "guid":"route-2-guid",
   180            "host":"foo",
   181            "domain":{
   182              "guid":"domain-1-guid",
   183              "name":"cfapps.io"
   184            }
   185          }
   186        ],
   187        "running_instances":1,
   188        "name":"app2",
   189        "memory":512,
   190        "instances":3,
   191        "state":"STARTED",
   192        "service_names":[
   193        	"my-service-instance"
   194        ],
   195  			"package_updated_at":"2012-10-24T19:55:00+00:00",
   196  			"ports":null
   197      },{
   198        "guid":"app-with-null-updated-at-guid",
   199        "routes":[
   200          {
   201            "guid":"route-3-guid",
   202            "host":"app3",
   203            "domain":{
   204              "guid":"domain-3-guid",
   205              "name":"cfapps.io"
   206            }
   207          }
   208        ],
   209        "running_instances":1,
   210        "name":"app-with-null-updated-at",
   211  			"memory":512,
   212        "instances":3,
   213        "state":"STARTED",
   214        "service_names":[
   215        	"my-service-instance"
   216        ],
   217  			"package_updated_at":null,
   218  			"ports":null
   219      }
   220    ]
   221  }`
   222  
   223  const getAppSummaryResponseBody string = `
   224  {
   225  		"guid":"app-1-guid",
   226  		"routes":[
   227  			{
   228  				"guid":"route-1-guid",
   229  				"host":"app1",
   230  				"domain":{
   231  					"guid":"domain-1-guid",
   232  					"name":"cfapps.io"
   233  				}
   234  			}
   235  		],
   236  		"running_instances":1,
   237  		"name":"app1",
   238  		"stack_guid":"the-stack-guid",
   239  		"memory":128,
   240  		"command": "start_command",
   241  		"instances":1,
   242  		"buildpack":"go_buildpack",
   243  		"state":"STARTED",
   244  		"service_names":[
   245  			"my-service-instance"
   246  		],
   247  		"package_updated_at":"2014-10-24T19:54:00+00:00",
   248  		"health_check_type":"some-health-check-type",
   249  		"health_check_http_endpoint":"/some-endpoint"
   250  }`