-
Notifications
You must be signed in to change notification settings - Fork 928
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added API method and test for getting app stats independently
of the instances call. Modified app command so that it uses the stats request instead of stats+instances requests. [#70701706]
- Loading branch information
Anand Gaitonde and Devin Fallak
authored and
Anand Gaitonde and Devin Fallak
committed
Jul 15, 2014
1 parent
417c7ee
commit 87702ed
Showing
11 changed files
with
275 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/cloudfoundry/cli/cf/configuration" | ||
"github.com/cloudfoundry/cli/cf/models" | ||
"github.com/cloudfoundry/cli/cf/net" | ||
) | ||
|
||
type AppStatsRepository interface { | ||
GetStats(appGuid string) (stats []models.AppStatsFields, apiErr error) | ||
} | ||
|
||
type CloudControllerAppStatsRepository struct { | ||
config configuration.Reader | ||
gateway net.Gateway | ||
} | ||
|
||
func NewCloudControllerAppStatsRepository(config configuration.Reader, gateway net.Gateway) (repo CloudControllerAppStatsRepository) { | ||
repo.config = config | ||
repo.gateway = gateway | ||
return | ||
} | ||
|
||
func (repo CloudControllerAppStatsRepository) GetStats(guid string) (stats []models.AppStatsFields, apiErr error) { | ||
path := fmt.Sprintf("%s/v2/apps/%s/stats", repo.config.ApiEndpoint(), guid) | ||
statsResponse := map[string]models.AppStatsFields{} | ||
apiErr = repo.gateway.GetResource(path, &statsResponse) | ||
if apiErr != nil { | ||
return | ||
} | ||
|
||
stats = make([]models.AppStatsFields, len(statsResponse), len(statsResponse)) | ||
for key, value := range statsResponse { | ||
index, err := strconv.Atoi(key) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
stats[index] = value | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package api_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"time" | ||
|
||
testapi "github.com/cloudfoundry/cli/cf/api/fakes" | ||
"github.com/cloudfoundry/cli/cf/models" | ||
"github.com/cloudfoundry/cli/cf/net" | ||
testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" | ||
testnet "github.com/cloudfoundry/cli/testhelpers/net" | ||
|
||
. "github.com/cloudfoundry/cli/cf/api" | ||
. "github.com/cloudfoundry/cli/testhelpers/matchers" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("AppStatsRepo", func() { | ||
It("returns stats for the app, given a guid", func() { | ||
ts, handler, repo := createAppStatsRepo([]testnet.TestRequest{ | ||
appStatsRequest, | ||
}) | ||
defer ts.Close() | ||
appGuid := "my-cool-app-guid" | ||
|
||
stats, err := repo.GetStats(appGuid) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(handler).To(HaveAllRequestsCalled()) | ||
|
||
Expect(len(stats)).To(Equal(2)) | ||
|
||
Expect(stats[1].State).To(Equal(models.InstanceRunning)) | ||
Expect(stats[0].State).To(Equal(models.InstanceFlapping)) | ||
|
||
stats1 := stats[1] | ||
Expect(stats1.Stats.DiskQuota).To(Equal(uint64(10000))) | ||
Expect(stats1.Stats.Usage.Disk).To(Equal(uint64(10000))) | ||
Expect(stats1.Stats.MemQuota).To(Equal(uint64(1024))) | ||
Expect(stats1.Stats.Usage.Mem).To(Equal(uint64(1024))) | ||
Expect(stats1.Stats.Usage.Cpu).To(Equal(0.3)) | ||
}) | ||
}) | ||
|
||
var appStatsRequest = testapi.NewCloudControllerTestRequest(testnet.TestRequest{ | ||
Method: "GET", | ||
Path: "/v2/apps/my-cool-app-guid/stats", | ||
Response: testnet.TestResponse{Status: http.StatusOK, Body: ` | ||
{ | ||
"1":{ | ||
"state": "running", | ||
"stats": { | ||
"disk_quota": 10000, | ||
"mem_quota": 1024, | ||
"usage": { | ||
"cpu": 0.3, | ||
"disk": 10000, | ||
"mem": 1024, | ||
"time": "2014-07-14 23:33:55 +0000" | ||
} | ||
} | ||
}, | ||
"0":{ | ||
"state": "flapping", | ||
"stats": { | ||
"disk_quota": 1073741824, | ||
"mem_quota": 67108864, | ||
"usage": { | ||
"cpu": 3.659571249238058e-05, | ||
"disk": 56037376, | ||
"mem": 19218432, | ||
"time": "2014-07-14 23:33:55 +0000" | ||
} | ||
} | ||
} | ||
}`}}) | ||
|
||
func createAppStatsRepo(requests []testnet.TestRequest) (ts *httptest.Server, handler *testnet.TestHandler, repo AppStatsRepository) { | ||
ts, handler = testnet.NewServer(requests) | ||
space := models.SpaceFields{} | ||
space.Guid = "my-space-guid" | ||
configRepo := testconfig.NewRepositoryWithDefaults() | ||
configRepo.SetApiEndpoint(ts.URL) | ||
gateway := net.NewCloudControllerGateway(configRepo, time.Now) | ||
repo = NewCloudControllerAppStatsRepository(configRepo, gateway) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package fakes | ||
|
||
import ( | ||
"github.com/cloudfoundry/cli/cf/errors" | ||
"github.com/cloudfoundry/cli/cf/models" | ||
) | ||
|
||
type FakeAppStatsRepo struct { | ||
GetStatsAppGuid string | ||
GetStatsResponses [][]models.AppStatsFields | ||
GetStatsErrorCodes []string | ||
} | ||
|
||
func (repo *FakeAppStatsRepo) GetStats(appGuid string) (stats []models.AppStatsFields, apiErr error) { | ||
repo.GetStatsAppGuid = appGuid | ||
|
||
if len(repo.GetStatsResponses) > 0 { | ||
stats = repo.GetStatsResponses[0] | ||
|
||
if len(repo.GetStatsResponses) > 1 { | ||
repo.GetStatsResponses = repo.GetStatsResponses[1:] | ||
} | ||
} | ||
|
||
if len(repo.GetStatsErrorCodes) > 0 { | ||
errorCode := repo.GetStatsErrorCodes[0] | ||
|
||
// don't slice away the last one if this is all we have | ||
if len(repo.GetStatsErrorCodes) > 1 { | ||
repo.GetStatsErrorCodes = repo.GetStatsErrorCodes[1:] | ||
} | ||
|
||
if errorCode != "" { | ||
apiErr = errors.NewHttpError(400, errorCode, "Error staging app") | ||
} | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.