github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/api/app_instances/app_instances_test.go (about)

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