Skip to content

Commit

Permalink
Draw container IDs in /containers from both container and app metrics (
Browse files Browse the repository at this point in the history
…#116)

* Update signature of test setup method

This will allow the same method to be used without sending all the test
data.

* Populate /containers from app and container metrics

This commit adds a test which supplies node and app metrics, but no
container metrics. This fails without the accompanying fix, which draws
app and container metrics from the store and extracts the set of unique
container IDs from them.

* Restore accidentally-removed build flag

I would normally squash this fix in, but a downstream DC/OS PR is
depending on a specific commit hash, so I'm not going to do so. It will
get squashed when we merge.

* Rename allTestData to allTestMessages

* Improve readability of containers test case
  • Loading branch information
philipnrmn authored Aug 28, 2017
1 parent 47994c8 commit c930517
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
8 changes: 7 additions & 1 deletion producers/http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ func nodeHandler(p *producerImpl) http.HandlerFunc {
func containersHandler(p *producerImpl) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cm := []string{}
containerMetrics, err := p.store.GetByRegex(regexp.QuoteMeta(producers.ContainerMetricPrefix) + ".*")
containerMetrics, err := p.store.GetByRegex(
fmt.Sprintf(
"(%s|%s).*",
regexp.QuoteMeta(producers.ContainerMetricPrefix),
regexp.QuoteMeta(producers.AppMetricPrefix),
),
)
if err != nil {
httpLog.Errorf("/v0/containers - %s", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
43 changes: 33 additions & 10 deletions producers/http/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,15 @@ var (
},
Timestamp: testTime.UTC().Unix(),
}

allTestMessages = []producers.MetricsMessage{
testNodeData,
testContainerData,
testAppData,
}
)

func setup() int {
func setup(messages []producers.MetricsMessage) int {
port, err := getEphemeralPort()
if err != nil {
panic(err)
Expand All @@ -102,17 +108,17 @@ func setup() int {
go pi.Run()
time.Sleep(1 * time.Second) // give the http server a chance to start before querying it

pc <- testNodeData
pc <- testContainerData
pc <- testAppData
for _, m := range messages {
pc <- m
}

return port
}

func TestNodeHandler(t *testing.T) {
Convey("When querying the /v0/node endpoint", t, func() {
Convey("Should return metrics in the expected structure", func() {
port := setup()
port := setup(allTestMessages)
resp, err := http.Get(urlBuilder("localhost", port, "/v0/node"))
if err != nil {
panic(err)
Expand All @@ -138,7 +144,7 @@ func TestNodeHandler(t *testing.T) {
func TestContainersHandler(t *testing.T) {
Convey("When querying the /v0/containers endpoint", t, func() {
Convey("Should return container IDs in the expected structure", func() {
port := setup()
port := setup(allTestMessages)
resp, err := http.Get(urlBuilder("localhost", port, "/v0/containers"))
if err != nil {
panic(err)
Expand All @@ -157,13 +163,30 @@ func TestContainersHandler(t *testing.T) {
So(resp.StatusCode, ShouldEqual, http.StatusOK)
So(strings.TrimSpace(string(got)), ShouldEqual, strings.TrimSpace(string(expected)))
})
Convey("Should return container IDs when app metrics, but not container metrics, are present", func() {
port := setup([]producers.MetricsMessage{testNodeData, testAppData})
resp, err := http.Get(urlBuilder("localhost", port, "/v0/containers"))
if err != nil {
panic(err)
}
defer resp.Body.Close()

// Note that we didn't supply testContainerData in `setup` above.
var containerIDs []string
if err := json.NewDecoder(resp.Body).Decode(&containerIDs); err != nil {
panic(err)
}

So(resp.StatusCode, ShouldEqual, http.StatusOK)
So(containerIDs, ShouldResemble, []string{testContainerData.Dimensions.ContainerID})
})
})
}

func TestContainerHandler(t *testing.T) {
Convey("When querying the /v0/containers/{id} endpoint", t, func() {
Convey("Should return container metrics for the container ID given", func() {
port := setup()
port := setup(allTestMessages)
resp, err := http.Get(urlBuilder("localhost", port, "/v0/containers/foo-container"))
if err != nil {
panic(err)
Expand All @@ -188,7 +211,7 @@ func TestContainerHandler(t *testing.T) {
func TestContainerAppHandler(t *testing.T) {
Convey("When querying the /v0/containers/{id}/app endpoint", t, func() {
Convey("Should return app metrics in the expected structure", func() {
port := setup()
port := setup(allTestMessages)
resp, err := http.Get(urlBuilder("localhost", port, "/v0/containers/foo-container/app"))
if err != nil {
panic(err)
Expand All @@ -213,7 +236,7 @@ func TestContainerAppHandler(t *testing.T) {
func TestContainerAppMetricHandler(t *testing.T) {
Convey("When querying the /v0/containers/{id}/app/{metric-id} endpoint", t, func() {
Convey("Should return app metrics in the expected structure", func() {
port := setup()
port := setup(allTestMessages)
resp, err := http.Get(urlBuilder("localhost", port, "/v0/containers/foo-container/app/some-app-metric"))
if err != nil {
panic(err)
Expand All @@ -238,7 +261,7 @@ func TestContainerAppMetricHandler(t *testing.T) {
func TestPingHandler(t *testing.T) {
Convey("When querying the /v0/ping endpoint", t, func() {
Convey("Should return a message and a timestamp", func() {
port := setup()
port := setup(allTestMessages)
resp, err := http.Get(urlBuilder("localhost", port, "/v0/ping"))
if err != nil {
panic(err)
Expand Down

0 comments on commit c930517

Please sign in to comment.