mirror of https://github.com/hashicorp/consul
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
809 lines
19 KiB
809 lines
19 KiB
11 years ago
|
package agent
|
||
|
|
||
|
import (
|
||
8 years ago
|
"bytes"
|
||
11 years ago
|
"fmt"
|
||
8 years ago
|
"io/ioutil"
|
||
11 years ago
|
"net/http"
|
||
11 years ago
|
"net/http/httptest"
|
||
11 years ago
|
"reflect"
|
||
11 years ago
|
"testing"
|
||
9 years ago
|
|
||
8 years ago
|
"github.com/hashicorp/consul/agent/consul/structs"
|
||
8 years ago
|
"github.com/hashicorp/consul/api"
|
||
8 years ago
|
"github.com/hashicorp/consul/testutil/retry"
|
||
9 years ago
|
"github.com/hashicorp/serf/coordinate"
|
||
11 years ago
|
)
|
||
|
|
||
|
func TestHealthChecksInState(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
t.Run("warning", func(t *testing.T) {
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/health/state/warning?dc=dc1", nil)
|
||
8 years ago
|
retry.Run(t, func(r *retry.R) {
|
||
9 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.HealthChecksInState(resp, req)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
r.Fatal(err)
|
||
9 years ago
|
}
|
||
|
if err := checkIndex(resp); err != nil {
|
||
8 years ago
|
r.Fatal(err)
|
||
9 years ago
|
}
|
||
|
|
||
|
// Should be a non-nil empty list
|
||
|
nodes := obj.(structs.HealthChecks)
|
||
|
if nodes == nil || len(nodes) != 0 {
|
||
8 years ago
|
r.Fatalf("bad: %v", obj)
|
||
9 years ago
|
}
|
||
8 years ago
|
})
|
||
9 years ago
|
})
|
||
|
|
||
8 years ago
|
t.Run("passing", func(t *testing.T) {
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/health/state/passing?dc=dc1", nil)
|
||
8 years ago
|
retry.Run(t, func(r *retry.R) {
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.HealthChecksInState(resp, req)
|
||
11 years ago
|
if err != nil {
|
||
8 years ago
|
r.Fatal(err)
|
||
11 years ago
|
}
|
||
|
if err := checkIndex(resp); err != nil {
|
||
8 years ago
|
r.Fatal(err)
|
||
11 years ago
|
}
|
||
|
|
||
|
// Should be 1 health check for the server
|
||
|
nodes := obj.(structs.HealthChecks)
|
||
|
if len(nodes) != 1 {
|
||
8 years ago
|
r.Fatalf("bad: %v", obj)
|
||
11 years ago
|
}
|
||
8 years ago
|
})
|
||
11 years ago
|
})
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func TestHealthChecksInState_NodeMetaFilter(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
|
|
||
|
args := &structs.RegisterRequest{
|
||
|
Datacenter: "dc1",
|
||
|
Node: "bar",
|
||
|
Address: "127.0.0.1",
|
||
|
NodeMeta: map[string]string{"somekey": "somevalue"},
|
||
|
Check: &structs.HealthCheck{
|
||
|
Node: "bar",
|
||
|
Name: "node check",
|
||
|
Status: api.HealthCritical,
|
||
|
},
|
||
|
}
|
||
|
var out struct{}
|
||
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
req, _ := http.NewRequest("GET", "/v1/health/state/critical?node-meta=somekey:somevalue", nil)
|
||
|
retry.Run(t, func(r *retry.R) {
|
||
|
resp := httptest.NewRecorder()
|
||
|
obj, err := a.srv.HealthChecksInState(resp, req)
|
||
|
if err != nil {
|
||
|
r.Fatal(err)
|
||
8 years ago
|
}
|
||
8 years ago
|
if err := checkIndex(resp); err != nil {
|
||
|
r.Fatal(err)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// Should be 1 health check for the server
|
||
|
nodes := obj.(structs.HealthChecks)
|
||
|
if len(nodes) != 1 {
|
||
|
r.Fatalf("bad: %v", obj)
|
||
|
}
|
||
8 years ago
|
})
|
||
|
}
|
||
|
|
||
9 years ago
|
func TestHealthChecksInState_DistanceSort(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
9 years ago
|
|
||
|
args := &structs.RegisterRequest{
|
||
|
Datacenter: "dc1",
|
||
|
Node: "bar",
|
||
|
Address: "127.0.0.1",
|
||
|
Check: &structs.HealthCheck{
|
||
9 years ago
|
Node: "bar",
|
||
|
Name: "node check",
|
||
8 years ago
|
Status: api.HealthCritical,
|
||
9 years ago
|
},
|
||
|
}
|
||
|
|
||
|
var out struct{}
|
||
8 years ago
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
||
9 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
args.Node, args.Check.Node = "foo", "foo"
|
||
8 years ago
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
||
9 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/health/state/critical?dc=dc1&near=foo", nil)
|
||
9 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.HealthChecksInState(resp, req)
|
||
9 years ago
|
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()),
|
||
|
}
|
||
8 years ago
|
if err := a.RPC("Coordinate.Update", &arg, &out); err != nil {
|
||
9 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
8 years ago
|
// Retry until foo moves to the front of the line.
|
||
|
retry.Run(t, func(r *retry.R) {
|
||
|
resp = httptest.NewRecorder()
|
||
8 years ago
|
obj, err = a.srv.HealthChecksInState(resp, req)
|
||
8 years ago
|
if err != nil {
|
||
|
r.Fatalf("err: %v", err)
|
||
|
}
|
||
|
assertIndex(t, resp)
|
||
|
nodes = obj.(structs.HealthChecks)
|
||
|
if len(nodes) != 2 {
|
||
|
r.Fatalf("bad: %v", nodes)
|
||
|
}
|
||
|
if nodes[0].Node != "foo" {
|
||
|
r.Fatalf("bad: %v", nodes)
|
||
|
}
|
||
|
if nodes[1].Node != "bar" {
|
||
|
r.Fatalf("bad: %v", nodes)
|
||
|
}
|
||
|
})
|
||
9 years ago
|
}
|
||
|
|
||
11 years ago
|
func TestHealthNodeChecks(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
11 years ago
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/health/node/nope?dc=dc1", nil)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.HealthNodeChecks(resp, req)
|
||
11 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
11 years ago
|
assertIndex(t, resp)
|
||
11 years ago
|
|
||
9 years ago
|
// Should be a non-nil empty list
|
||
11 years ago
|
nodes := obj.(structs.HealthChecks)
|
||
9 years ago
|
if nodes == nil || len(nodes) != 0 {
|
||
|
t.Fatalf("bad: %v", obj)
|
||
|
}
|
||
|
|
||
8 years ago
|
req, _ = http.NewRequest("GET", fmt.Sprintf("/v1/health/node/%s?dc=dc1", a.Config.NodeName), nil)
|
||
9 years ago
|
resp = httptest.NewRecorder()
|
||
8 years ago
|
obj, err = a.srv.HealthNodeChecks(resp, req)
|
||
9 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
assertIndex(t, resp)
|
||
|
|
||
|
// Should be 1 health check for the server
|
||
|
nodes = obj.(structs.HealthChecks)
|
||
11 years ago
|
if len(nodes) != 1 {
|
||
|
t.Fatalf("bad: %v", obj)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestHealthServiceChecks(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
11 years ago
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/health/checks/consul?dc=dc1", nil)
|
||
9 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.HealthServiceChecks(resp, req)
|
||
9 years ago
|
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)
|
||
|
}
|
||
|
|
||
11 years ago
|
// Create a service check
|
||
11 years ago
|
args := &structs.RegisterRequest{
|
||
|
Datacenter: "dc1",
|
||
8 years ago
|
Node: a.Config.NodeName,
|
||
11 years ago
|
Address: "127.0.0.1",
|
||
|
Check: &structs.HealthCheck{
|
||
8 years ago
|
Node: a.Config.NodeName,
|
||
11 years ago
|
Name: "consul check",
|
||
|
ServiceID: "consul",
|
||
|
},
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
var out struct{}
|
||
8 years ago
|
if err = a.RPC("Catalog.Register", args, &out); err != nil {
|
||
11 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
8 years ago
|
req, _ = http.NewRequest("GET", "/v1/health/checks/consul?dc=dc1", nil)
|
||
9 years ago
|
resp = httptest.NewRecorder()
|
||
8 years ago
|
obj, err = a.srv.HealthServiceChecks(resp, req)
|
||
11 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
11 years ago
|
assertIndex(t, resp)
|
||
11 years ago
|
|
||
11 years ago
|
// Should be 1 health check for consul
|
||
9 years ago
|
nodes = obj.(structs.HealthChecks)
|
||
11 years ago
|
if len(nodes) != 1 {
|
||
|
t.Fatalf("bad: %v", obj)
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func TestHealthServiceChecks_NodeMetaFilter(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
8 years ago
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/health/checks/consul?dc=dc1&node-meta=somekey:somevalue", nil)
|
||
8 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.HealthServiceChecks(resp, req)
|
||
8 years ago
|
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)
|
||
|
}
|
||
|
|
||
|
// Create a service check
|
||
|
args := &structs.RegisterRequest{
|
||
|
Datacenter: "dc1",
|
||
8 years ago
|
Node: a.Config.NodeName,
|
||
8 years ago
|
Address: "127.0.0.1",
|
||
|
NodeMeta: map[string]string{"somekey": "somevalue"},
|
||
|
Check: &structs.HealthCheck{
|
||
8 years ago
|
Node: a.Config.NodeName,
|
||
8 years ago
|
Name: "consul check",
|
||
|
ServiceID: "consul",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
var out struct{}
|
||
8 years ago
|
if err = a.RPC("Catalog.Register", args, &out); err != nil {
|
||
8 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
8 years ago
|
req, _ = http.NewRequest("GET", "/v1/health/checks/consul?dc=dc1&node-meta=somekey:somevalue", nil)
|
||
8 years ago
|
resp = httptest.NewRecorder()
|
||
8 years ago
|
obj, err = a.srv.HealthServiceChecks(resp, req)
|
||
8 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
assertIndex(t, resp)
|
||
|
|
||
|
// Should be 1 health check for consul
|
||
|
nodes = obj.(structs.HealthChecks)
|
||
|
if len(nodes) != 1 {
|
||
|
t.Fatalf("bad: %v", obj)
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
func TestHealthServiceChecks_DistanceSort(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
9 years ago
|
|
||
|
// Create a service check
|
||
|
args := &structs.RegisterRequest{
|
||
|
Datacenter: "dc1",
|
||
|
Node: "bar",
|
||
|
Address: "127.0.0.1",
|
||
9 years ago
|
Service: &structs.NodeService{
|
||
9 years ago
|
ID: "test",
|
||
|
Service: "test",
|
||
|
},
|
||
9 years ago
|
Check: &structs.HealthCheck{
|
||
9 years ago
|
Node: "bar",
|
||
|
Name: "test check",
|
||
|
ServiceID: "test",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
var out struct{}
|
||
8 years ago
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
||
9 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
args.Node, args.Check.Node = "foo", "foo"
|
||
8 years ago
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
||
9 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/health/checks/test?dc=dc1&near=foo", nil)
|
||
9 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.HealthServiceChecks(resp, req)
|
||
9 years ago
|
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()),
|
||
|
}
|
||
8 years ago
|
if err := a.RPC("Coordinate.Update", &arg, &out); err != nil {
|
||
9 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
8 years ago
|
// Retry until foo has moved to the front of the line.
|
||
|
retry.Run(t, func(r *retry.R) {
|
||
|
resp = httptest.NewRecorder()
|
||
8 years ago
|
obj, err = a.srv.HealthServiceChecks(resp, req)
|
||
8 years ago
|
if err != nil {
|
||
|
r.Fatalf("err: %v", err)
|
||
|
}
|
||
|
assertIndex(t, resp)
|
||
|
nodes = obj.(structs.HealthChecks)
|
||
|
if len(nodes) != 2 {
|
||
|
r.Fatalf("bad: %v", obj)
|
||
|
}
|
||
|
if nodes[0].Node != "foo" {
|
||
|
r.Fatalf("bad: %v", nodes)
|
||
|
}
|
||
|
if nodes[1].Node != "bar" {
|
||
|
r.Fatalf("bad: %v", nodes)
|
||
|
}
|
||
|
})
|
||
9 years ago
|
}
|
||
|
|
||
11 years ago
|
func TestHealthServiceNodes(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
11 years ago
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/health/service/consul?dc=dc1", nil)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.HealthServiceNodes(resp, req)
|
||
11 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
assertIndex(t, resp)
|
||
11 years ago
|
|
||
11 years ago
|
// Should be 1 health check for consul
|
||
|
nodes := obj.(structs.CheckServiceNodes)
|
||
|
if len(nodes) != 1 {
|
||
|
t.Fatalf("bad: %v", obj)
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
req, _ = http.NewRequest("GET", "/v1/health/service/nope?dc=dc1", nil)
|
||
9 years ago
|
resp = httptest.NewRecorder()
|
||
8 years ago
|
obj, err = a.srv.HealthServiceNodes(resp, req)
|
||
9 years ago
|
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{}
|
||
8 years ago
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
||
9 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
8 years ago
|
req, _ = http.NewRequest("GET", "/v1/health/service/test?dc=dc1", nil)
|
||
9 years ago
|
resp = httptest.NewRecorder()
|
||
8 years ago
|
obj, err = a.srv.HealthServiceNodes(resp, req)
|
||
9 years ago
|
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)
|
||
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
8 years ago
|
func TestHealthServiceNodes_NodeMetaFilter(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
8 years ago
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/health/service/consul?dc=dc1&node-meta=somekey:somevalue", nil)
|
||
8 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.HealthServiceNodes(resp, req)
|
||
8 years ago
|
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",
|
||
|
NodeMeta: map[string]string{"somekey": "somevalue"},
|
||
|
Service: &structs.NodeService{
|
||
|
ID: "test",
|
||
|
Service: "test",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
var out struct{}
|
||
8 years ago
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
||
8 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
8 years ago
|
req, _ = http.NewRequest("GET", "/v1/health/service/test?dc=dc1&node-meta=somekey:somevalue", nil)
|
||
8 years ago
|
resp = httptest.NewRecorder()
|
||
8 years ago
|
obj, err = a.srv.HealthServiceNodes(resp, req)
|
||
8 years ago
|
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)
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
func TestHealthServiceNodes_DistanceSort(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
9 years ago
|
|
||
|
// Create a service check
|
||
|
args := &structs.RegisterRequest{
|
||
|
Datacenter: "dc1",
|
||
|
Node: "bar",
|
||
|
Address: "127.0.0.1",
|
||
9 years ago
|
Service: &structs.NodeService{
|
||
9 years ago
|
ID: "test",
|
||
|
Service: "test",
|
||
|
},
|
||
9 years ago
|
Check: &structs.HealthCheck{
|
||
9 years ago
|
Node: "bar",
|
||
|
Name: "test check",
|
||
|
ServiceID: "test",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
var out struct{}
|
||
8 years ago
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
||
9 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
args.Node, args.Check.Node = "foo", "foo"
|
||
8 years ago
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
||
9 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/health/service/test?dc=dc1&near=foo", nil)
|
||
9 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.HealthServiceNodes(resp, req)
|
||
9 years ago
|
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()),
|
||
|
}
|
||
8 years ago
|
if err := a.RPC("Coordinate.Update", &arg, &out); err != nil {
|
||
9 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
8 years ago
|
// Retry until foo has moved to the front of the line.
|
||
|
retry.Run(t, func(r *retry.R) {
|
||
|
resp = httptest.NewRecorder()
|
||
8 years ago
|
obj, err = a.srv.HealthServiceNodes(resp, req)
|
||
8 years ago
|
if err != nil {
|
||
|
r.Fatalf("err: %v", err)
|
||
|
}
|
||
|
assertIndex(t, resp)
|
||
|
nodes = obj.(structs.CheckServiceNodes)
|
||
|
if len(nodes) != 2 {
|
||
|
r.Fatalf("bad: %v", obj)
|
||
|
}
|
||
|
if nodes[0].Node.Node != "foo" {
|
||
|
r.Fatalf("bad: %v", nodes)
|
||
|
}
|
||
|
if nodes[1].Node.Node != "bar" {
|
||
|
r.Fatalf("bad: %v", nodes)
|
||
|
}
|
||
|
})
|
||
9 years ago
|
}
|
||
|
|
||
11 years ago
|
func TestHealthServiceNodes_PassingFilter(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
11 years ago
|
|
||
|
// Create a failing service check
|
||
11 years ago
|
args := &structs.RegisterRequest{
|
||
|
Datacenter: "dc1",
|
||
8 years ago
|
Node: a.Config.NodeName,
|
||
11 years ago
|
Address: "127.0.0.1",
|
||
|
Check: &structs.HealthCheck{
|
||
8 years ago
|
Node: a.Config.NodeName,
|
||
11 years ago
|
Name: "consul check",
|
||
|
ServiceID: "consul",
|
||
8 years ago
|
Status: api.HealthCritical,
|
||
11 years ago
|
},
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
var out struct{}
|
||
8 years ago
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
||
11 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
8 years ago
|
t.Run("bc_no_query_value", func(t *testing.T) {
|
||
|
req, _ := http.NewRequest("GET", "/v1/health/service/consul?passing", nil)
|
||
|
resp := httptest.NewRecorder()
|
||
|
obj, err := a.srv.HealthServiceNodes(resp, req)
|
||
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
assertIndex(t, resp)
|
||
11 years ago
|
|
||
8 years ago
|
// Should be 0 health check for consul
|
||
|
nodes := obj.(structs.CheckServiceNodes)
|
||
|
if len(nodes) != 0 {
|
||
|
t.Fatalf("bad: %v", obj)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
t.Run("passing_true", func(t *testing.T) {
|
||
|
req, _ := http.NewRequest("GET", "/v1/health/service/consul?passing=true", nil)
|
||
|
resp := httptest.NewRecorder()
|
||
|
obj, err := a.srv.HealthServiceNodes(resp, req)
|
||
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
assertIndex(t, resp)
|
||
|
|
||
|
// Should be 0 health check for consul
|
||
|
nodes := obj.(structs.CheckServiceNodes)
|
||
|
if len(nodes) != 0 {
|
||
|
t.Fatalf("bad: %v", obj)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
t.Run("passing_false", func(t *testing.T) {
|
||
|
req, _ := http.NewRequest("GET", "/v1/health/service/consul?passing=false", nil)
|
||
|
resp := httptest.NewRecorder()
|
||
|
obj, err := a.srv.HealthServiceNodes(resp, req)
|
||
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
assertIndex(t, resp)
|
||
|
|
||
8 years ago
|
// Should be 1 consul, it's unhealthy, but we specifically asked for
|
||
|
// everything.
|
||
8 years ago
|
nodes := obj.(structs.CheckServiceNodes)
|
||
|
if len(nodes) != 1 {
|
||
|
t.Fatalf("bad: %v", obj)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
t.Run("passing_bad", func(t *testing.T) {
|
||
|
req, _ := http.NewRequest("GET", "/v1/health/service/consul?passing=nope-nope-nope", nil)
|
||
|
resp := httptest.NewRecorder()
|
||
|
a.srv.HealthServiceNodes(resp, req)
|
||
|
|
||
|
if code := resp.Code; code != 400 {
|
||
|
t.Errorf("bad response code %d, expected %d", code, 400)
|
||
|
}
|
||
|
|
||
|
body, err := ioutil.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
if !bytes.Contains(body, []byte("Invalid value for ?passing")) {
|
||
|
t.Errorf("bad %s", body)
|
||
|
}
|
||
|
})
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
9 years ago
|
func TestHealthServiceNodes_WanTranslation(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
cfg1 := TestConfig()
|
||
|
cfg1.Datacenter = "dc1"
|
||
|
cfg1.TranslateWanAddrs = true
|
||
|
cfg1.ACLDatacenter = ""
|
||
|
a1 := NewTestAgent(t.Name(), cfg1)
|
||
8 years ago
|
defer a1.Shutdown()
|
||
|
|
||
8 years ago
|
cfg2 := TestConfig()
|
||
|
cfg2.Datacenter = "dc2"
|
||
|
cfg2.TranslateWanAddrs = true
|
||
|
cfg2.ACLDatacenter = ""
|
||
|
a2 := NewTestAgent(t.Name(), cfg2)
|
||
8 years ago
|
defer a2.Shutdown()
|
||
8 years ago
|
|
||
|
// Wait for the WAN join.
|
||
8 years ago
|
addr := fmt.Sprintf("127.0.0.1:%d", a1.Config.Ports.SerfWan)
|
||
|
if _, err := a2.JoinWAN([]string{addr}); err != nil {
|
||
8 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
8 years ago
|
retry.Run(t, func(r *retry.R) {
|
||
8 years ago
|
if got, want := len(a1.WANMembers()), 2; got < want {
|
||
8 years ago
|
r.Fatalf("got %d WAN members want at least %d", got, want)
|
||
|
}
|
||
|
})
|
||
9 years ago
|
|
||
8 years ago
|
// Register a node with DC2.
|
||
9 years ago
|
{
|
||
|
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{}
|
||
8 years ago
|
if err := a2.RPC("Catalog.Register", args, &out); err != nil {
|
||
9 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
// Query for a service in DC2 from DC1.
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/health/service/http_wan_translation_test?dc=dc2", nil)
|
||
9 years ago
|
resp1 := httptest.NewRecorder()
|
||
8 years ago
|
obj1, err1 := a1.srv.HealthServiceNodes(resp1, req)
|
||
9 years ago
|
if err1 != nil {
|
||
|
t.Fatalf("err: %v", err1)
|
||
|
}
|
||
|
assertIndex(t, resp1)
|
||
|
|
||
8 years ago
|
// Expect that DC1 gives us a WAN address (since the node is in DC2).
|
||
9 years ago
|
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)
|
||
|
}
|
||
|
|
||
8 years ago
|
// Query DC2 from DC2.
|
||
9 years ago
|
resp2 := httptest.NewRecorder()
|
||
8 years ago
|
obj2, err2 := a2.srv.HealthServiceNodes(resp2, req)
|
||
9 years ago
|
if err2 != nil {
|
||
|
t.Fatalf("err: %v", err2)
|
||
|
}
|
||
|
assertIndex(t, resp2)
|
||
|
|
||
8 years ago
|
// Expect that DC2 gives us a private address (since the node is in DC2).
|
||
9 years ago
|
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)
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
func TestFilterNonPassing(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
11 years ago
|
nodes := structs.CheckServiceNodes{
|
||
|
structs.CheckServiceNode{
|
||
|
Checks: structs.HealthChecks{
|
||
|
&structs.HealthCheck{
|
||
8 years ago
|
Status: api.HealthCritical,
|
||
11 years ago
|
},
|
||
|
&structs.HealthCheck{
|
||
8 years ago
|
Status: api.HealthCritical,
|
||
11 years ago
|
},
|
||
|
},
|
||
|
},
|
||
|
structs.CheckServiceNode{
|
||
|
Checks: structs.HealthChecks{
|
||
|
&structs.HealthCheck{
|
||
8 years ago
|
Status: api.HealthCritical,
|
||
11 years ago
|
},
|
||
|
&structs.HealthCheck{
|
||
8 years ago
|
Status: api.HealthCritical,
|
||
11 years ago
|
},
|
||
|
},
|
||
|
},
|
||
|
structs.CheckServiceNode{
|
||
|
Checks: structs.HealthChecks{
|
||
|
&structs.HealthCheck{
|
||
8 years ago
|
Status: api.HealthPassing,
|
||
11 years ago
|
},
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
out := filterNonPassing(nodes)
|
||
|
if len(out) != 1 && reflect.DeepEqual(out[0], nodes[2]) {
|
||
|
t.Fatalf("bad: %v", out)
|
||
|
}
|
||
|
}
|