2014-01-10 23:25:37 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2014-04-21 20:11:05 +00:00
|
|
|
"net/http/httptest"
|
2014-01-10 23:25:37 +00:00
|
|
|
"os"
|
2014-07-05 16:49:10 +00:00
|
|
|
"reflect"
|
2014-01-10 23:25:37 +00:00
|
|
|
"testing"
|
2015-07-27 21:41:46 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
|
|
|
"github.com/hashicorp/consul/testutil"
|
|
|
|
"github.com/hashicorp/serf/coordinate"
|
2014-01-10 23:25:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHealthChecksInState(t *testing.T) {
|
2015-11-15 05:05:37 +00:00
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
|
|
|
req, err := http.NewRequest("GET", "/v1/health/state/warning?dc=dc1", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.HealthChecksInState(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if err := checkIndex(resp); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be a non-nil empty list
|
|
|
|
nodes := obj.(structs.HealthChecks)
|
|
|
|
if nodes == nil || len(nodes) != 0 {
|
|
|
|
return false, fmt.Errorf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) { t.Fatalf("err: %v", err) })
|
|
|
|
})
|
|
|
|
|
2014-05-21 19:31:22 +00:00
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
|
|
|
req, err := http.NewRequest("GET", "/v1/health/state/passing?dc=dc1", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.HealthChecksInState(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if err := checkIndex(resp); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be 1 health check for the server
|
|
|
|
nodes := obj.(structs.HealthChecks)
|
|
|
|
if len(nodes) != 1 {
|
|
|
|
return false, fmt.Errorf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) { t.Fatalf("err: %v", err) })
|
|
|
|
})
|
2014-01-10 23:25:37 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 21:41:46 +00:00
|
|
|
func TestHealthChecksInState_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")
|
|
|
|
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Check: &structs.HealthCheck{
|
2015-07-29 23:33:25 +00:00
|
|
|
Node: "bar",
|
|
|
|
Name: "node check",
|
|
|
|
Status: structs.HealthCritical,
|
2015-07-27 21:41:46 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args.Node, args.Check.Node = "foo", "foo"
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/v1/health/state/critical?dc=dc1&near=foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.HealthChecksInState(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
assertIndex(t, resp)
|
|
|
|
nodes := obj.(structs.HealthChecks)
|
|
|
|
if len(nodes) != 2 {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-10-25 20:46:54 +00:00
|
|
|
// Retry until foo moves to the front of the line.
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
obj, err = srv.HealthChecksInState(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("err: %v", err)
|
|
|
|
}
|
|
|
|
assertIndex(t, resp)
|
|
|
|
nodes = obj.(structs.HealthChecks)
|
|
|
|
if len(nodes) != 2 {
|
|
|
|
return false, fmt.Errorf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[0].Node != "foo" {
|
|
|
|
return false, fmt.Errorf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[1].Node != "bar" {
|
|
|
|
return false, fmt.Errorf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("failed to get sorted service nodes: %v", err)
|
|
|
|
})
|
2015-07-27 21:41:46 +00:00
|
|
|
}
|
|
|
|
|
2014-01-10 23:25:37 +00:00
|
|
|
func TestHealthNodeChecks(t *testing.T) {
|
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
2014-01-10 23:25:37 +00:00
|
|
|
|
2015-11-15 05:05:37 +00:00
|
|
|
req, err := http.NewRequest("GET", "/v1/health/node/nope?dc=dc1", nil)
|
2014-01-10 23:25:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-21 20:11:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.HealthNodeChecks(resp, req)
|
2014-01-10 23:25:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-04-21 20:11:05 +00:00
|
|
|
assertIndex(t, resp)
|
2014-02-05 22:36:13 +00:00
|
|
|
|
2015-11-15 05:05:37 +00:00
|
|
|
// Should be a non-nil empty list
|
2014-01-10 23:25:37 +00:00
|
|
|
nodes := obj.(structs.HealthChecks)
|
2015-11-15 05:05:37 +00:00
|
|
|
if nodes == nil || len(nodes) != 0 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err = http.NewRequest("GET",
|
|
|
|
fmt.Sprintf("/v1/health/node/%s?dc=dc1", srv.agent.config.NodeName), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
obj, err = srv.HealthNodeChecks(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
// Should be 1 health check for the server
|
|
|
|
nodes = obj.(structs.HealthChecks)
|
2014-01-10 23:25:37 +00:00
|
|
|
if len(nodes) != 1 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHealthServiceChecks(t *testing.T) {
|
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
2014-05-07 21:47:16 +00:00
|
|
|
|
2015-11-15 05:05:37 +00:00
|
|
|
req, err := http.NewRequest("GET", "/v1/health/checks/consul?dc=dc1", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.HealthServiceChecks(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
// Should be a non-nil empty list
|
|
|
|
nodes := obj.(structs.HealthChecks)
|
|
|
|
if nodes == nil || len(nodes) != 0 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
|
2014-05-07 21:47:16 +00:00
|
|
|
// Create a service check
|
2014-01-10 23:25:37 +00:00
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: srv.agent.config.NodeName,
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
Node: srv.agent.config.NodeName,
|
|
|
|
Name: "consul check",
|
|
|
|
ServiceID: "consul",
|
|
|
|
},
|
|
|
|
}
|
2014-05-07 21:12:53 +00:00
|
|
|
|
2014-01-10 23:25:37 +00:00
|
|
|
var out struct{}
|
2015-11-15 05:05:37 +00:00
|
|
|
if err = srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
2014-01-10 23:25:37 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-11-15 05:05:37 +00:00
|
|
|
req, err = http.NewRequest("GET", "/v1/health/checks/consul?dc=dc1", nil)
|
2014-01-10 23:25:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-11-15 05:05:37 +00:00
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
obj, err = srv.HealthServiceChecks(resp, req)
|
2014-01-10 23:25:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-04-21 20:11:05 +00:00
|
|
|
assertIndex(t, resp)
|
2014-02-05 22:36:13 +00:00
|
|
|
|
2014-01-10 23:25:37 +00:00
|
|
|
// Should be 1 health check for consul
|
2015-11-15 05:05:37 +00:00
|
|
|
nodes = obj.(structs.HealthChecks)
|
2014-01-10 23:25:37 +00:00
|
|
|
if len(nodes) != 1 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-27 21:41:46 +00:00
|
|
|
func TestHealthServiceChecks_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")
|
|
|
|
|
|
|
|
// Create a service check
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "127.0.0.1",
|
2015-07-29 23:33:25 +00:00
|
|
|
Service: &structs.NodeService{
|
2015-07-27 21:41:46 +00:00
|
|
|
ID: "test",
|
|
|
|
Service: "test",
|
|
|
|
},
|
2015-07-29 23:33:25 +00:00
|
|
|
Check: &structs.HealthCheck{
|
2015-07-27 21:41:46 +00:00
|
|
|
Node: "bar",
|
|
|
|
Name: "test check",
|
|
|
|
ServiceID: "test",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args.Node, args.Check.Node = "foo", "foo"
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/v1/health/checks/test?dc=dc1&near=foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.HealthServiceChecks(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
assertIndex(t, resp)
|
|
|
|
nodes := obj.(structs.HealthChecks)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-10-25 20:46:54 +00:00
|
|
|
// Retry until foo has moved to the front of the line.
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
obj, err = srv.HealthServiceChecks(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("err: %v", err)
|
|
|
|
}
|
|
|
|
assertIndex(t, resp)
|
|
|
|
nodes = obj.(structs.HealthChecks)
|
|
|
|
if len(nodes) != 2 {
|
|
|
|
return false, fmt.Errorf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
if nodes[0].Node != "foo" {
|
|
|
|
return false, fmt.Errorf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[1].Node != "bar" {
|
|
|
|
return false, fmt.Errorf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("failed to get sorted service checks: %v", err)
|
|
|
|
})
|
2015-07-27 21:41:46 +00:00
|
|
|
}
|
|
|
|
|
2014-01-10 23:25:37 +00:00
|
|
|
func TestHealthServiceNodes(t *testing.T) {
|
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
2014-01-10 23:25:37 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/v1/health/service/consul?dc=dc1", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-21 20:11:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.HealthServiceNodes(resp, req)
|
2014-01-10 23:25:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-05-09 09:38:29 +00:00
|
|
|
|
2014-04-21 20:11:05 +00:00
|
|
|
assertIndex(t, resp)
|
2014-02-05 22:36:13 +00:00
|
|
|
|
2014-01-10 23:25:37 +00:00
|
|
|
// Should be 1 health check for consul
|
|
|
|
nodes := obj.(structs.CheckServiceNodes)
|
|
|
|
if len(nodes) != 1 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
2015-11-15 05:05:37 +00:00
|
|
|
|
|
|
|
req, err = http.NewRequest("GET", "/v1/health/service/nope?dc=dc1", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
obj, err = srv.HealthServiceNodes(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
// Should be a non-nil empty list
|
|
|
|
nodes = obj.(structs.CheckServiceNodes)
|
|
|
|
if nodes == nil || len(nodes) != 0 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "test",
|
|
|
|
Service: "test",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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/health/service/test?dc=dc1", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
obj, err = srv.HealthServiceNodes(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
// Should be a non-nil empty list for checks
|
|
|
|
nodes = obj.(structs.CheckServiceNodes)
|
|
|
|
if len(nodes) != 1 || nodes[0].Checks == nil || len(nodes[0].Checks) != 0 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
2014-01-10 23:25:37 +00:00
|
|
|
}
|
2014-04-21 23:08:26 +00:00
|
|
|
|
2015-07-27 21:41:46 +00:00
|
|
|
func TestHealthServiceNodes_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")
|
|
|
|
|
|
|
|
// Create a service check
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "127.0.0.1",
|
2015-07-29 23:33:25 +00:00
|
|
|
Service: &structs.NodeService{
|
2015-07-27 21:41:46 +00:00
|
|
|
ID: "test",
|
|
|
|
Service: "test",
|
|
|
|
},
|
2015-07-29 23:33:25 +00:00
|
|
|
Check: &structs.HealthCheck{
|
2015-07-27 21:41:46 +00:00
|
|
|
Node: "bar",
|
|
|
|
Name: "test check",
|
|
|
|
ServiceID: "test",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args.Node, args.Check.Node = "foo", "foo"
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/v1/health/service/test?dc=dc1&near=foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.HealthServiceNodes(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
assertIndex(t, resp)
|
|
|
|
nodes := obj.(structs.CheckServiceNodes)
|
|
|
|
if len(nodes) != 2 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
if nodes[0].Node.Node != "bar" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[1].Node.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)
|
|
|
|
}
|
|
|
|
|
2016-10-25 20:46:54 +00:00
|
|
|
// Retry until foo has moved to the front of the line.
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
obj, err = srv.HealthServiceNodes(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("err: %v", err)
|
|
|
|
}
|
|
|
|
assertIndex(t, resp)
|
|
|
|
nodes = obj.(structs.CheckServiceNodes)
|
|
|
|
if len(nodes) != 2 {
|
|
|
|
return false, fmt.Errorf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
if nodes[0].Node.Node != "foo" {
|
|
|
|
return false, fmt.Errorf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[1].Node.Node != "bar" {
|
|
|
|
return false, fmt.Errorf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("failed to get sorted service nodes: %v", err)
|
|
|
|
})
|
2015-07-27 21:41:46 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 23:08:26 +00:00
|
|
|
func TestHealthServiceNodes_PassingFilter(t *testing.T) {
|
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
2014-05-07 21:47:16 +00:00
|
|
|
|
|
|
|
// Create a failing service check
|
2014-04-21 23:08:26 +00:00
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: srv.agent.config.NodeName,
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
Node: srv.agent.config.NodeName,
|
|
|
|
Name: "consul check",
|
|
|
|
ServiceID: "consul",
|
|
|
|
Status: structs.HealthCritical,
|
|
|
|
},
|
|
|
|
}
|
2014-05-07 21:12:53 +00:00
|
|
|
|
2014-04-21 23:08:26 +00:00
|
|
|
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/health/service/consul?passing", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.HealthServiceNodes(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-05-09 09:38:29 +00:00
|
|
|
|
2014-04-21 23:08:26 +00:00
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
// Should be 0 health check for consul
|
|
|
|
nodes := obj.(structs.CheckServiceNodes)
|
|
|
|
if len(nodes) != 0 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
2014-07-05 16:49:10 +00:00
|
|
|
|
2016-06-15 18:02:51 +00:00
|
|
|
func TestHealthServiceNodes_WanTranslation(t *testing.T) {
|
2016-08-15 22:34:11 +00:00
|
|
|
dir1, srv1 := makeHTTPServerWithConfig(t,
|
|
|
|
func(c *Config) {
|
|
|
|
c.Datacenter = "dc1"
|
|
|
|
c.TranslateWanAddrs = true
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer srv1.Shutdown()
|
|
|
|
defer srv1.agent.Shutdown()
|
|
|
|
testutil.WaitForLeader(t, srv1.agent.RPC, "dc1")
|
|
|
|
|
|
|
|
dir2, srv2 := makeHTTPServerWithConfig(t,
|
|
|
|
func(c *Config) {
|
|
|
|
c.Datacenter = "dc2"
|
|
|
|
c.TranslateWanAddrs = true
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer srv2.Shutdown()
|
|
|
|
defer srv2.agent.Shutdown()
|
|
|
|
testutil.WaitForLeader(t, srv2.agent.RPC, "dc2")
|
|
|
|
|
|
|
|
// Wait for the WAN join.
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
srv1.agent.config.Ports.SerfWan)
|
|
|
|
if _, err := srv2.agent.JoinWAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
testutil.WaitForResult(
|
|
|
|
func() (bool, error) {
|
|
|
|
return len(srv1.agent.WANMembers()) > 1, nil
|
|
|
|
},
|
|
|
|
func(err error) {
|
|
|
|
t.Fatalf("Failed waiting for WAN join: %v", err)
|
|
|
|
})
|
2016-06-15 18:02:51 +00:00
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Register a node with DC2.
|
2016-06-15 18:02:51 +00:00
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc2",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
TaggedAddresses: map[string]string{
|
|
|
|
"wan": "127.0.0.2",
|
|
|
|
},
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "http_wan_translation_test",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv2.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Query for a service in DC2 from DC1.
|
2016-06-15 18:02:51 +00:00
|
|
|
req, err := http.NewRequest("GET", "/v1/health/service/http_wan_translation_test?dc=dc2", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp1 := httptest.NewRecorder()
|
|
|
|
obj1, err1 := srv1.HealthServiceNodes(resp1, req)
|
|
|
|
if err1 != nil {
|
|
|
|
t.Fatalf("err: %v", err1)
|
|
|
|
}
|
|
|
|
assertIndex(t, resp1)
|
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Expect that DC1 gives us a WAN address (since the node is in DC2).
|
2016-06-15 18:02:51 +00:00
|
|
|
nodes1 := obj1.(structs.CheckServiceNodes)
|
|
|
|
if len(nodes1) != 1 {
|
|
|
|
t.Fatalf("bad: %v", obj1)
|
|
|
|
}
|
|
|
|
node1 := nodes1[0].Node
|
|
|
|
if node1.Address != "127.0.0.2" {
|
|
|
|
t.Fatalf("bad: %v", node1)
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Query DC2 from DC2.
|
2016-06-15 18:02:51 +00:00
|
|
|
resp2 := httptest.NewRecorder()
|
|
|
|
obj2, err2 := srv2.HealthServiceNodes(resp2, req)
|
|
|
|
if err2 != nil {
|
|
|
|
t.Fatalf("err: %v", err2)
|
|
|
|
}
|
|
|
|
assertIndex(t, resp2)
|
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Expect that DC2 gives us a private address (since the node is in DC2).
|
2016-06-15 18:02:51 +00:00
|
|
|
nodes2 := obj2.(structs.CheckServiceNodes)
|
|
|
|
if len(nodes2) != 1 {
|
|
|
|
t.Fatalf("bad: %v", obj2)
|
|
|
|
}
|
|
|
|
node2 := nodes2[0].Node
|
|
|
|
if node2.Address != "127.0.0.1" {
|
|
|
|
t.Fatalf("bad: %v", node2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-05 16:49:10 +00:00
|
|
|
func TestFilterNonPassing(t *testing.T) {
|
|
|
|
nodes := structs.CheckServiceNodes{
|
|
|
|
structs.CheckServiceNode{
|
|
|
|
Checks: structs.HealthChecks{
|
|
|
|
&structs.HealthCheck{
|
|
|
|
Status: structs.HealthCritical,
|
|
|
|
},
|
|
|
|
&structs.HealthCheck{
|
|
|
|
Status: structs.HealthCritical,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
structs.CheckServiceNode{
|
|
|
|
Checks: structs.HealthChecks{
|
|
|
|
&structs.HealthCheck{
|
|
|
|
Status: structs.HealthCritical,
|
|
|
|
},
|
|
|
|
&structs.HealthCheck{
|
|
|
|
Status: structs.HealthCritical,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
structs.CheckServiceNode{
|
|
|
|
Checks: structs.HealthChecks{
|
|
|
|
&structs.HealthCheck{
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
out := filterNonPassing(nodes)
|
|
|
|
if len(out) != 1 && reflect.DeepEqual(out[0], nodes[2]) {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
}
|