agent: Updating tests for new API

This commit is contained in:
Armon Dadgar 2014-04-21 13:11:05 -07:00
parent 53d3449947
commit bcff269238
4 changed files with 57 additions and 52 deletions

View File

@ -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 {

View File

@ -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)

View File

@ -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)
}

View File

@ -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 {