From 59b710894d2863c290531149d6c57d21a0c07264 Mon Sep 17 00:00:00 2001 From: James Phillips Date: Thu, 2 Jul 2015 17:21:59 -0700 Subject: [PATCH] Adds tests for HTTP interface. Removes a stray mark. --- command/agent/agent_test.go | 2 + command/agent/catalog_endpoint_test.go | 203 ++++++++++++++++++++++++- consul/rtt.go | 1 - 3 files changed, 203 insertions(+), 3 deletions(-) diff --git a/command/agent/agent_test.go b/command/agent/agent_test.go index d735f60f3..b6237bac6 100644 --- a/command/agent/agent_test.go +++ b/command/agent/agent_test.go @@ -59,6 +59,8 @@ func nextConfig() *Config { cons.RaftConfig.HeartbeatTimeout = 40 * time.Millisecond cons.RaftConfig.ElectionTimeout = 40 * time.Millisecond + cons.DisableCoordinates = false + cons.CoordinateUpdatePeriod = 100 * time.Millisecond return conf } diff --git a/command/agent/catalog_endpoint_test.go b/command/agent/catalog_endpoint_test.go index 5b8b0cc22..5e4d75aab 100644 --- a/command/agent/catalog_endpoint_test.go +++ b/command/agent/catalog_endpoint_test.go @@ -2,13 +2,15 @@ package agent import ( "fmt" - "github.com/hashicorp/consul/consul/structs" - "github.com/hashicorp/consul/testutil" "net/http" "net/http/httptest" "os" "testing" "time" + + "github.com/hashicorp/consul/consul/structs" + "github.com/hashicorp/consul/testutil" + "github.com/hashicorp/serf/coordinate" ) func TestCatalogRegister(t *testing.T) { @@ -204,6 +206,101 @@ func TestCatalogNodes_Blocking(t *testing.T) { } } +func TestCatalogNodes_DistanceSort(t *testing.T) { + dir, srv := makeHTTPServer(t) + defer os.RemoveAll(dir) + defer srv.Shutdown() + defer srv.agent.Shutdown() + + testutil.WaitForLeader(t, srv.agent.RPC, "dc1") + + // Register nodes. + args := &structs.RegisterRequest{ + Datacenter: "dc1", + Node: "foo", + Address: "127.0.0.1", + } + var out struct{} + if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { + t.Fatalf("err: %v", err) + } + + args = &structs.RegisterRequest{ + Datacenter: "dc1", + Node: "bar", + Address: "127.0.0.2", + } + if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { + t.Fatalf("err: %v", err) + } + + // Nobody has coordinates set so this will still return them in the + // order they are indexed. + req, err := http.NewRequest("GET", "/v1/catalog/nodes?dc=dc1&near=foo", nil) + if err != nil { + t.Fatalf("err: %v", err) + } + + resp := httptest.NewRecorder() + obj, err := srv.CatalogNodes(resp, req) + if err != nil { + t.Fatalf("err: %v", err) + } + + assertIndex(t, resp) + nodes := obj.(structs.Nodes) + if len(nodes) != 3 { + t.Fatalf("bad: %v", obj) + } + if nodes[0].Node != "bar" { + t.Fatalf("bad: %v", nodes) + } + if nodes[1].Node != "foo" { + t.Fatalf("bad: %v", nodes) + } + if nodes[2].Node != srv.agent.config.NodeName { + t.Fatalf("bad: %v", nodes) + } + + // Send an update for the node and wait for it to get applied. + arg := structs.CoordinateUpdateRequest{ + Datacenter: "dc1", + Node: "foo", + Coord: coordinate.NewCoordinate(coordinate.DefaultConfig()), + } + if err := srv.agent.RPC("Coordinate.Update", &arg, &out); err != nil { + t.Fatalf("err: %v", err) + } + time.Sleep(200 * time.Millisecond) + + // Query again and now foo should have moved to the front of the line. + req, err = http.NewRequest("GET", "/v1/catalog/nodes?dc=dc1&near=foo", nil) + if err != nil { + t.Fatalf("err: %v", err) + } + + resp = httptest.NewRecorder() + obj, err = srv.CatalogNodes(resp, req) + if err != nil { + t.Fatalf("err: %v", err) + } + + assertIndex(t, resp) + nodes = obj.(structs.Nodes) + if len(nodes) != 3 { + t.Fatalf("bad: %v", obj) + } + if nodes[0].Node != "foo" { + t.Fatalf("bad: %v", nodes) + } + if nodes[1].Node != "bar" { + t.Fatalf("bad: %v", nodes) + } + if nodes[2].Node != srv.agent.config.NodeName { + t.Fatalf("bad: %v", nodes) + } +} + func TestCatalogServices(t *testing.T) { dir, srv := makeHTTPServer(t) defer os.RemoveAll(dir) @@ -289,6 +386,108 @@ func TestCatalogServiceNodes(t *testing.T) { } } +func TestCatalogServiceNodes_DistanceSort(t *testing.T) { + dir, srv := makeHTTPServer(t) + defer os.RemoveAll(dir) + defer srv.Shutdown() + defer srv.agent.Shutdown() + + testutil.WaitForLeader(t, srv.agent.RPC, "dc1") + + // Register nodes. + args := &structs.RegisterRequest{ + Datacenter: "dc1", + Node: "bar", + Address: "127.0.0.1", + Service: &structs.NodeService{ + Service: "api", + Tags: []string{"a"}, + }, + } + var out struct{} + if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { + t.Fatalf("err: %v", err) + } + + req, err := http.NewRequest("GET", "/v1/catalog/service/api?tag=a", nil) + if err != nil { + t.Fatalf("err: %v", err) + } + + args = &structs.RegisterRequest{ + Datacenter: "dc1", + Node: "foo", + Address: "127.0.0.2", + Service: &structs.NodeService{ + Service: "api", + Tags: []string{"a"}, + }, + } + if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { + t.Fatalf("err: %v", err) + } + + // Nobody has coordinates set so this will still return them in the + // order they are indexed. + req, err = http.NewRequest("GET", "/v1/catalog/service/api?tag=a&near=foo", nil) + if err != nil { + t.Fatalf("err: %v", err) + } + + resp := httptest.NewRecorder() + obj, err := srv.CatalogServiceNodes(resp, req) + if err != nil { + t.Fatalf("err: %v", err) + } + + assertIndex(t, resp) + nodes := obj.(structs.ServiceNodes) + if len(nodes) != 2 { + t.Fatalf("bad: %v", obj) + } + if nodes[0].Node != "bar" { + t.Fatalf("bad: %v", nodes) + } + if nodes[1].Node != "foo" { + t.Fatalf("bad: %v", nodes) + } + + // Send an update for the node and wait for it to get applied. + arg := structs.CoordinateUpdateRequest{ + Datacenter: "dc1", + Node: "foo", + Coord: coordinate.NewCoordinate(coordinate.DefaultConfig()), + } + if err := srv.agent.RPC("Coordinate.Update", &arg, &out); err != nil { + t.Fatalf("err: %v", err) + } + time.Sleep(200 * time.Millisecond) + + // Query again and now foo should have moved to the front of the line. + req, err = http.NewRequest("GET", "/v1/catalog/service/api?tag=a&near=foo", nil) + if err != nil { + t.Fatalf("err: %v", err) + } + + resp = httptest.NewRecorder() + obj, err = srv.CatalogServiceNodes(resp, req) + if err != nil { + t.Fatalf("err: %v", err) + } + + assertIndex(t, resp) + nodes = obj.(structs.ServiceNodes) + if len(nodes) != 2 { + t.Fatalf("bad: %v", obj) + } + if nodes[0].Node != "foo" { + t.Fatalf("bad: %v", nodes) + } + if nodes[1].Node != "bar" { + t.Fatalf("bad: %v", nodes) + } +} + func TestCatalogNodeServices(t *testing.T) { dir, srv := makeHTTPServer(t) defer os.RemoveAll(dir) diff --git a/consul/rtt.go b/consul/rtt.go index 68c94920e..e207e0594 100644 --- a/consul/rtt.go +++ b/consul/rtt.go @@ -215,7 +215,6 @@ func getDatacenterDistance(s serfer, dc string) (float64, error) { // Compute the median by sorting and taking the middle item. sort.Float64s(subvec) - fmt.Println("%v", subvec) if len(subvec) > 0 { return subvec[len(subvec)/2], nil }