github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/cf/api/appinstances/app_instances_test.go (about)

     1  package appinstances_test
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"time"
     7  
     8  	"code.cloudfoundry.org/cli/cf/api/apifakes"
     9  	"code.cloudfoundry.org/cli/cf/models"
    10  	"code.cloudfoundry.org/cli/cf/net"
    11  	"code.cloudfoundry.org/cli/cf/terminal/terminalfakes"
    12  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    13  	testnet "code.cloudfoundry.org/cli/util/testhelpers/net"
    14  
    15  	. "code.cloudfoundry.org/cli/cf/api/appinstances"
    16  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
    17  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("AppInstancesRepo", func() {
    23  	Describe("Getting the instances for an application", func() {
    24  		It("returns instances of the app, given a guid", func() {
    25  			ts, handler, repo := createAppInstancesRepo([]testnet.TestRequest{
    26  				appInstancesRequest,
    27  				appStatsRequest,
    28  			})
    29  			defer ts.Close()
    30  			appGUID := "my-cool-app-guid"
    31  
    32  			instances, err := repo.GetInstances(appGUID)
    33  			Expect(err).NotTo(HaveOccurred())
    34  			Expect(handler).To(HaveAllRequestsCalled())
    35  
    36  			Expect(len(instances)).To(Equal(2))
    37  
    38  			Expect(instances[0].State).To(Equal(models.InstanceRunning))
    39  			Expect(instances[1].State).To(Equal(models.InstanceStarting))
    40  			Expect(instances[1].Details).To(Equal("insufficient resources"))
    41  
    42  			instance0 := instances[0]
    43  			Expect(instance0.Since).To(Equal(time.Unix(1379522342, 0)))
    44  			Expect(instance0.DiskQuota).To(Equal(int64(1073741824)))
    45  			Expect(instance0.DiskUsage).To(Equal(int64(56037376)))
    46  			Expect(instance0.MemQuota).To(Equal(int64(67108864)))
    47  			Expect(instance0.MemUsage).To(Equal(int64(19218432)))
    48  			Expect(instance0.CPUUsage).To(Equal(3.659571249238058e-05))
    49  		})
    50  	})
    51  
    52  	Describe("Deleting an instance for an application", func() {
    53  		It("returns no error if the response is successful", func() {
    54  			ts, handler, repo := createAppInstancesRepo([]testnet.TestRequest{
    55  				deleteInstanceRequest,
    56  			})
    57  			defer ts.Close()
    58  			appGUID := "my-cool-app-guid"
    59  
    60  			err := repo.DeleteInstance(appGUID, 0)
    61  			Expect(err).NotTo(HaveOccurred())
    62  			Expect(handler).To(HaveAllRequestsCalled())
    63  		})
    64  
    65  		It("returns the error if the response is unsuccessful", func() {
    66  			ts, handler, repo := createAppInstancesRepo([]testnet.TestRequest{
    67  				deleteInstanceFromUnkownApp,
    68  			})
    69  			defer ts.Close()
    70  			appGUID := "some-wrong-app-guid"
    71  
    72  			err := repo.DeleteInstance(appGUID, 0)
    73  			Expect(err).To(HaveOccurred())
    74  			Expect(handler).To(HaveAllRequestsCalled())
    75  
    76  		})
    77  	})
    78  })
    79  
    80  var appStatsRequest = apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
    81  	Method: "GET",
    82  	Path:   "/v2/apps/my-cool-app-guid/stats",
    83  	Response: testnet.TestResponse{Status: http.StatusOK, Body: `
    84  {
    85    "1":{
    86      "stats": {
    87          "disk_quota": 10000,
    88          "mem_quota": 1024,
    89          "usage": {
    90              "cpu": 0.3,
    91              "disk": 10000,
    92              "mem": 1024
    93          }
    94      }
    95    },
    96    "0":{
    97      "stats": {
    98          "disk_quota": 1073741824,
    99          "mem_quota": 67108864,
   100          "usage": {
   101              "cpu": 3.659571249238058e-05,
   102              "disk": 56037376,
   103              "mem": 19218432
   104          }
   105      }
   106    }
   107  }`}})
   108  
   109  var appInstancesRequest = apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   110  	Method: "GET",
   111  	Path:   "/v2/apps/my-cool-app-guid/instances",
   112  	Response: testnet.TestResponse{Status: http.StatusOK, Body: `
   113  {
   114    "1": {
   115      "state": "STARTING",
   116      "details": "insufficient resources",
   117      "since": 1379522342.6783738
   118    },
   119    "0": {
   120      "state": "RUNNING",
   121      "since": 1379522342.6783738
   122    }
   123  }`}})
   124  
   125  var deleteInstanceRequest = apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   126  	Method:   "DELETE",
   127  	Path:     "/v2/apps/my-cool-app-guid/instances/0",
   128  	Response: testnet.TestResponse{Status: http.StatusNoContent, Body: `{}`},
   129  })
   130  
   131  var deleteInstanceFromUnkownApp = apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   132  	Method:   "DELETE",
   133  	Path:     "/v2/apps/some-wrong-app-guid/instances/0",
   134  	Response: testnet.TestResponse{Status: http.StatusNotFound, Body: `{}`},
   135  })
   136  
   137  func createAppInstancesRepo(requests []testnet.TestRequest) (ts *httptest.Server, handler *testnet.TestHandler, repo Repository) {
   138  	ts, handler = testnet.NewServer(requests)
   139  	space := models.SpaceFields{}
   140  	space.GUID = "my-space-guid"
   141  	configRepo := testconfig.NewRepositoryWithDefaults()
   142  	configRepo.SetAPIEndpoint(ts.URL)
   143  	gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
   144  	repo = NewCloudControllerAppInstancesRepository(configRepo, gateway)
   145  	return
   146  }