From bcff269238577c534568c782af8fa18578cf8d9d Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Mon, 21 Apr 2014 13:11:05 -0700 Subject: [PATCH] agent: Updating tests for new API --- command/agent/catalog_endpoint_test.go | 36 ++++++++++++-------------- command/agent/health_endpoint_test.go | 33 ++++++++++------------- command/agent/http_test.go | 28 +++++++++++++++++--- command/agent/kvs_endpoint_test.go | 12 ++------- 4 files changed, 57 insertions(+), 52 deletions(-) diff --git a/command/agent/catalog_endpoint_test.go b/command/agent/catalog_endpoint_test.go index 573ddf886..2d6dc39d4 100644 --- a/command/agent/catalog_endpoint_test.go +++ b/command/agent/catalog_endpoint_test.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hashicorp/consul/consul/structs" "net/http" + "net/http/httptest" "os" "testing" "time" @@ -115,14 +116,14 @@ func TestCatalogNodes(t *testing.T) { t.Fatalf("err: %v", err) } - idx, obj, err := srv.CatalogNodes(nil, req) + resp := httptest.NewRecorder() + obj, err := srv.CatalogNodes(resp, req) if err != nil { t.Fatalf("err: %v", err) } - if idx == 0 { - t.Fatalf("bad: %v", idx) - } + // Verify an index is set + assertIndex(t, resp) nodes := obj.(structs.Nodes) if len(nodes) != 2 { @@ -170,7 +171,8 @@ func TestCatalogNodes_Blocking(t *testing.T) { t.Fatalf("err: %v", err) } - idx, obj, err := srv.CatalogNodes(nil, req) + resp := httptest.NewRecorder() + obj, err := srv.CatalogNodes(resp, req) if err != nil { t.Fatalf("err: %v", err) } @@ -180,7 +182,7 @@ func TestCatalogNodes_Blocking(t *testing.T) { t.Fatalf("too fast") } - if idx <= out.Index { + if idx := getIndex(t, resp); idx <= out.Index { t.Fatalf("bad: %v", idx) } @@ -218,14 +220,13 @@ func TestCatalogServices(t *testing.T) { t.Fatalf("err: %v", err) } - idx, obj, err := srv.CatalogServices(nil, req) + resp := httptest.NewRecorder() + obj, err := srv.CatalogServices(resp, req) if err != nil { t.Fatalf("err: %v", err) } - if idx == 0 { - t.Fatalf("bad: %v", idx) - } + assertIndex(t, resp) services := obj.(structs.Services) if len(services) != 2 { @@ -262,14 +263,13 @@ func TestCatalogServiceNodes(t *testing.T) { t.Fatalf("err: %v", err) } - idx, obj, err := srv.CatalogServiceNodes(nil, req) + resp := httptest.NewRecorder() + obj, err := srv.CatalogServiceNodes(resp, req) if err != nil { t.Fatalf("err: %v", err) } - if idx == 0 { - t.Fatalf("bad: %v", idx) - } + assertIndex(t, resp) nodes := obj.(structs.ServiceNodes) if len(nodes) != 1 { @@ -306,14 +306,12 @@ func TestCatalogNodeServices(t *testing.T) { t.Fatalf("err: %v", err) } - idx, obj, err := srv.CatalogNodeServices(nil, req) + resp := httptest.NewRecorder() + obj, err := srv.CatalogNodeServices(resp, req) if err != nil { t.Fatalf("err: %v", err) } - - if idx == 0 { - t.Fatalf("bad: %v", idx) - } + assertIndex(t, resp) services := obj.(*structs.NodeServices) if len(services.Services) != 1 { diff --git a/command/agent/health_endpoint_test.go b/command/agent/health_endpoint_test.go index 1bc9e6a04..b8bf1bef5 100644 --- a/command/agent/health_endpoint_test.go +++ b/command/agent/health_endpoint_test.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hashicorp/consul/consul/structs" "net/http" + "net/http/httptest" "os" "testing" "time" @@ -23,14 +24,12 @@ func TestHealthChecksInState(t *testing.T) { t.Fatalf("err: %v", err) } - idx, obj, err := srv.HealthChecksInState(nil, req) + resp := httptest.NewRecorder() + obj, err := srv.HealthChecksInState(resp, req) if err != nil { t.Fatalf("err: %v", err) } - - if idx == 0 { - t.Fatalf("bad: %v", idx) - } + assertIndex(t, resp) // Should be 1 health check for the server nodes := obj.(structs.HealthChecks) @@ -54,14 +53,12 @@ func TestHealthNodeChecks(t *testing.T) { t.Fatalf("err: %v", err) } - idx, obj, err := srv.HealthNodeChecks(nil, req) + resp := httptest.NewRecorder() + obj, err := srv.HealthNodeChecks(resp, req) if err != nil { t.Fatalf("err: %v", err) } - - if idx == 0 { - t.Fatalf("bad: %v", idx) - } + assertIndex(t, resp) // Should be 1 health check for the server nodes := obj.(structs.HealthChecks) @@ -100,14 +97,12 @@ func TestHealthServiceChecks(t *testing.T) { t.Fatalf("err: %v", err) } - idx, obj, err := srv.HealthServiceChecks(nil, req) + resp := httptest.NewRecorder() + obj, err := srv.HealthServiceChecks(resp, req) if err != nil { t.Fatalf("err: %v", err) } - - if idx == 0 { - t.Fatalf("bad: %v", idx) - } + assertIndex(t, resp) // Should be 1 health check for consul nodes := obj.(structs.HealthChecks) @@ -130,14 +125,12 @@ func TestHealthServiceNodes(t *testing.T) { t.Fatalf("err: %v", err) } - idx, obj, err := srv.HealthServiceNodes(nil, req) + resp := httptest.NewRecorder() + obj, err := srv.HealthServiceNodes(resp, req) if err != nil { t.Fatalf("err: %v", err) } - - if idx == 0 { - t.Fatalf("bad: %v", idx) - } + assertIndex(t, resp) // Should be 1 health check for consul nodes := obj.(structs.CheckServiceNodes) diff --git a/command/agent/http_test.go b/command/agent/http_test.go index 4b97c597a..3beffbf11 100644 --- a/command/agent/http_test.go +++ b/command/agent/http_test.go @@ -9,6 +9,7 @@ import ( "net/http" "net/http/httptest" "os" + "strconv" "testing" "time" ) @@ -69,7 +70,7 @@ func TestContentTypeIsJSON(t *testing.T) { func TestParseWait(t *testing.T) { resp := httptest.NewRecorder() - var b structs.BlockingQuery + var b structs.QueryOptions req, err := http.NewRequest("GET", "/v1/catalog/nodes?wait=60s&index=1000", nil) @@ -91,7 +92,7 @@ func TestParseWait(t *testing.T) { func TestParseWait_InvalidTime(t *testing.T) { resp := httptest.NewRecorder() - var b structs.BlockingQuery + var b structs.QueryOptions req, err := http.NewRequest("GET", "/v1/catalog/nodes?wait=60foo&index=1000", nil) @@ -110,7 +111,7 @@ func TestParseWait_InvalidTime(t *testing.T) { func TestParseWait_InvalidIndex(t *testing.T) { resp := httptest.NewRecorder() - var b structs.BlockingQuery + var b structs.QueryOptions req, err := http.NewRequest("GET", "/v1/catalog/nodes?wait=60s&index=foo", nil) @@ -126,3 +127,24 @@ func TestParseWait_InvalidIndex(t *testing.T) { t.Fatalf("bad code: %v", resp.Code) } } + +// assertIndex tests that X-Consul-Index is set and non-zero +func assertIndex(t *testing.T, resp *httptest.ResponseRecorder) { + header := resp.Header().Get("X-Consul-Index") + if header == "" || header == "0" { + t.Fatalf("Bad: %v", header) + } +} + +// getIndex parses X-Consul-Index +func getIndex(t *testing.T, resp *httptest.ResponseRecorder) uint64 { + header := resp.Header().Get("X-Consul-Index") + if header == "" { + t.Fatalf("Bad: %v", header) + } + val, err := strconv.Atoi(header) + if err != nil { + t.Fatalf("Bad: %v", header) + } + return uint64(val) +} diff --git a/command/agent/kvs_endpoint_test.go b/command/agent/kvs_endpoint_test.go index 6bb476e0a..f28ef17cf 100644 --- a/command/agent/kvs_endpoint_test.go +++ b/command/agent/kvs_endpoint_test.go @@ -57,11 +57,7 @@ func TestKVSEndpoint_PUT_GET_DELETE(t *testing.T) { if err != nil { t.Fatalf("err: %v", err) } - - header := resp.Header().Get("X-Consul-Index") - if header == "" { - t.Fatalf("Bad: %v", header) - } + assertIndex(t, resp) res, ok := obj.(structs.DirEntries) if !ok { @@ -138,11 +134,7 @@ func TestKVSEndpoint_Recurse(t *testing.T) { if err != nil { t.Fatalf("err: %v", err) } - - header := resp.Header().Get("X-Consul-Index") - if header == "" { - t.Fatalf("Bad: %v", header) - } + assertIndex(t, resp) res, ok := obj.(structs.DirEntries) if !ok {