2014-01-10 23:25:37 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2017-06-09 18:36:00 +00:00
|
|
|
"bytes"
|
2014-01-10 23:25:37 +00:00
|
|
|
"fmt"
|
2017-06-09 18:36:00 +00:00
|
|
|
"io/ioutil"
|
2014-01-10 23:25:37 +00:00
|
|
|
"net/http"
|
2014-04-21 20:11:05 +00:00
|
|
|
"net/http/httptest"
|
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
|
|
|
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2018-08-06 23:46:09 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2017-04-29 16:34:02 +00:00
|
|
|
"github.com/hashicorp/consul/testutil/retry"
|
2015-07-27 21:41:46 +00:00
|
|
|
"github.com/hashicorp/serf/coordinate"
|
2018-03-09 17:52:32 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-06 10:34:28 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2014-01-10 23:25:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHealthChecksInState(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-06-09 18:36:00 +00:00
|
|
|
t.Run("warning", func(t *testing.T) {
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/health/state/warning?dc=dc1", nil)
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2015-11-15 05:05:37 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 18:31:20 +00:00
|
|
|
obj, err := a.srv.HealthChecksInState(resp, req)
|
2015-11-15 05:05:37 +00:00
|
|
|
if err != nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatal(err)
|
2015-11-15 05:05:37 +00:00
|
|
|
}
|
|
|
|
if err := checkIndex(resp); err != nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatal(err)
|
2015-11-15 05:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Should be a non-nil empty list
|
|
|
|
nodes := obj.(structs.HealthChecks)
|
|
|
|
if nodes == nil || len(nodes) != 0 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %v", obj)
|
2015-11-15 05:05:37 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2015-11-15 05:05:37 +00:00
|
|
|
})
|
|
|
|
|
2017-06-09 18:36:00 +00:00
|
|
|
t.Run("passing", func(t *testing.T) {
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/health/state/passing?dc=dc1", nil)
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2014-05-21 19:31:22 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 18:31:20 +00:00
|
|
|
obj, err := a.srv.HealthChecksInState(resp, req)
|
2014-05-21 19:31:22 +00:00
|
|
|
if err != nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatal(err)
|
2014-05-21 19:31:22 +00:00
|
|
|
}
|
|
|
|
if err := checkIndex(resp); err != nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatal(err)
|
2014-05-21 19:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Should be 1 health check for the server
|
|
|
|
nodes := obj.(structs.HealthChecks)
|
|
|
|
if len(nodes) != 1 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %v", obj)
|
2014-05-21 19:31:22 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2014-05-21 19:31:22 +00:00
|
|
|
})
|
2014-01-10 23:25:37 +00:00
|
|
|
}
|
|
|
|
|
2017-01-14 01:08:43 +00:00
|
|
|
func TestHealthChecksInState_NodeMetaFilter(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 18:31:20 +00:00
|
|
|
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)
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
2017-05-21 18:31:20 +00:00
|
|
|
if err := checkIndex(resp); err != nil {
|
|
|
|
r.Fatal(err)
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
// Should be 1 health check for the server
|
|
|
|
nodes := obj.(structs.HealthChecks)
|
|
|
|
if len(nodes) != 1 {
|
|
|
|
r.Fatalf("bad: %v", obj)
|
|
|
|
}
|
2017-01-14 01:08:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-27 21:41:46 +00:00
|
|
|
func TestHealthChecksInState_DistanceSort(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2015-07-27 21:41:46 +00:00
|
|
|
|
|
|
|
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",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthCritical,
|
2015-07-27 21:41:46 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args.Node, args.Check.Node = "foo", "foo"
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/health/state/critical?dc=dc1&near=foo", nil)
|
2015-07-27 21:41:46 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.HealthChecksInState(resp, req)
|
2015-07-27 21:41:46 +00:00
|
|
|
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()),
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Coordinate.Update", &arg, &out); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-05-05 10:14:43 +00:00
|
|
|
// Retry until foo moves to the front of the line.
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
resp = httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err = a.srv.HealthChecksInState(resp, req)
|
2017-05-05 10:14:43 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2015-07-27 21:41:46 +00:00
|
|
|
}
|
|
|
|
|
2014-01-10 23:25:37 +00:00
|
|
|
func TestHealthNodeChecks(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-10 15:58:53 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2014-01-10 23:25:37 +00:00
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/health/node/nope?dc=dc1", nil)
|
2014-04-21 20:11:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.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)
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
req, _ = http.NewRequest("GET", fmt.Sprintf("/v1/health/node/%s?dc=dc1", a.Config.NodeName), nil)
|
2015-11-15 05:05:37 +00:00
|
|
|
resp = httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err = a.srv.HealthNodeChecks(resp, req)
|
2015-11-15 05:05:37 +00:00
|
|
|
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) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-12 13:49:27 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2014-05-07 21:47:16 +00:00
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/health/checks/consul?dc=dc1", nil)
|
2015-11-15 05:05:37 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.HealthServiceChecks(resp, req)
|
2015-11-15 05:05:37 +00:00
|
|
|
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",
|
2017-05-21 07:11:09 +00:00
|
|
|
Node: a.Config.NodeName,
|
2014-01-10 23:25:37 +00:00
|
|
|
Address: "127.0.0.1",
|
|
|
|
Check: &structs.HealthCheck{
|
2017-05-21 07:11:09 +00:00
|
|
|
Node: a.Config.NodeName,
|
2014-01-10 23:25:37 +00:00
|
|
|
Name: "consul check",
|
|
|
|
ServiceID: "consul",
|
|
|
|
},
|
|
|
|
}
|
2014-05-07 21:12:53 +00:00
|
|
|
|
2014-01-10 23:25:37 +00:00
|
|
|
var out struct{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err = a.RPC("Catalog.Register", args, &out); err != nil {
|
2014-01-10 23:25:37 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/health/checks/consul?dc=dc1", nil)
|
2015-11-15 05:05:37 +00:00
|
|
|
resp = httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err = a.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 01:08:43 +00:00
|
|
|
func TestHealthServiceChecks_NodeMetaFilter(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-10 15:58:53 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-01-14 01:08:43 +00:00
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/health/checks/consul?dc=dc1&node-meta=somekey:somevalue", nil)
|
2017-01-14 01:08:43 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.HealthServiceChecks(resp, req)
|
2017-01-14 01:08:43 +00:00
|
|
|
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",
|
2017-05-21 07:11:09 +00:00
|
|
|
Node: a.Config.NodeName,
|
2017-01-14 01:08:43 +00:00
|
|
|
Address: "127.0.0.1",
|
|
|
|
NodeMeta: map[string]string{"somekey": "somevalue"},
|
|
|
|
Check: &structs.HealthCheck{
|
2017-05-21 07:11:09 +00:00
|
|
|
Node: a.Config.NodeName,
|
2017-01-14 01:08:43 +00:00
|
|
|
Name: "consul check",
|
|
|
|
ServiceID: "consul",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err = a.RPC("Catalog.Register", args, &out); err != nil {
|
2017-01-14 01:08:43 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/health/checks/consul?dc=dc1&node-meta=somekey:somevalue", nil)
|
2017-01-14 01:08:43 +00:00
|
|
|
resp = httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err = a.srv.HealthServiceChecks(resp, req)
|
2017-01-14 01:08:43 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-27 21:41:46 +00:00
|
|
|
func TestHealthServiceChecks_DistanceSort(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-12 13:49:27 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2015-07-27 21:41:46 +00:00
|
|
|
|
|
|
|
// 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{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args.Node, args.Check.Node = "foo", "foo"
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/health/checks/test?dc=dc1&near=foo", nil)
|
2015-07-27 21:41:46 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.HealthServiceChecks(resp, req)
|
2015-07-27 21:41:46 +00:00
|
|
|
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()),
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Coordinate.Update", &arg, &out); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-05-05 10:14:43 +00:00
|
|
|
// Retry until foo has moved to the front of the line.
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
resp = httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err = a.srv.HealthServiceChecks(resp, req)
|
2017-05-05 10:14:43 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2015-07-27 21:41:46 +00:00
|
|
|
}
|
|
|
|
|
2014-01-10 23:25:37 +00:00
|
|
|
func TestHealthServiceNodes(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-10 15:58:53 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2014-01-10 23:25:37 +00:00
|
|
|
|
2018-09-06 10:34:28 +00:00
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/health/service/consul?dc=dc1", nil)
|
2014-04-21 20:11:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.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
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/health/service/nope?dc=dc1", nil)
|
2015-11-15 05:05:37 +00:00
|
|
|
resp = httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err = a.srv.HealthServiceNodes(resp, req)
|
2015-11-15 05:05:37 +00:00
|
|
|
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{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2015-11-15 05:05:37 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/health/service/test?dc=dc1", nil)
|
2015-11-15 05:05:37 +00:00
|
|
|
resp = httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err = a.srv.HealthServiceNodes(resp, req)
|
2015-11-15 05:05:37 +00:00
|
|
|
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)
|
|
|
|
}
|
2018-09-06 10:34:28 +00:00
|
|
|
|
|
|
|
// Test caching
|
|
|
|
{
|
|
|
|
// List instances with cache enabled
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/health/service/test?cached", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.HealthServiceNodes(resp, req)
|
|
|
|
require.NoError(err)
|
|
|
|
nodes := obj.(structs.CheckServiceNodes)
|
|
|
|
assert.Len(nodes, 1)
|
|
|
|
|
|
|
|
// Should be a cache miss
|
|
|
|
assert.Equal("MISS", resp.Header().Get("X-Cache"))
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// List instances with cache enabled
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/health/service/test?cached", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.HealthServiceNodes(resp, req)
|
|
|
|
require.NoError(err)
|
|
|
|
nodes := obj.(structs.CheckServiceNodes)
|
|
|
|
assert.Len(nodes, 1)
|
|
|
|
|
|
|
|
// Should be a cache HIT now!
|
|
|
|
assert.Equal("HIT", resp.Header().Get("X-Cache"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure background refresh works
|
|
|
|
{
|
|
|
|
// Register a new instance of the service
|
|
|
|
args2 := args
|
|
|
|
args2.Node = "baz"
|
|
|
|
args2.Address = "127.0.0.2"
|
|
|
|
require.NoError(a.RPC("Catalog.Register", args, &out))
|
|
|
|
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
// List it again
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/health/service/test?cached", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.HealthServiceNodes(resp, req)
|
|
|
|
r.Check(err)
|
|
|
|
|
|
|
|
nodes := obj.(structs.CheckServiceNodes)
|
|
|
|
if len(nodes) != 2 {
|
|
|
|
r.Fatalf("Want 2 nodes")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be a cache hit! The data should've updated in the cache
|
|
|
|
// in the background so this should've been fetched directly from
|
|
|
|
// the cache.
|
|
|
|
if resp.Header().Get("X-Cache") != "HIT" {
|
|
|
|
r.Fatalf("should be a cache hit")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2014-01-10 23:25:37 +00:00
|
|
|
}
|
2014-04-21 23:08:26 +00:00
|
|
|
|
2017-01-14 01:08:43 +00:00
|
|
|
func TestHealthServiceNodes_NodeMetaFilter(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2018-08-06 23:46:09 +00:00
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
2017-01-14 01:08:43 +00:00
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/health/service/consul?dc=dc1&node-meta=somekey:somevalue", nil)
|
2017-01-14 01:08:43 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.HealthServiceNodes(resp, req)
|
2017-01-14 01:08:43 +00:00
|
|
|
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{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2017-01-14 01:08:43 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/health/service/test?dc=dc1&node-meta=somekey:somevalue", nil)
|
2017-01-14 01:08:43 +00:00
|
|
|
resp = httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err = a.srv.HealthServiceNodes(resp, req)
|
2017-01-14 01:08:43 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-27 21:41:46 +00:00
|
|
|
func TestHealthServiceNodes_DistanceSort(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2018-08-06 23:46:09 +00:00
|
|
|
dc := "dc1"
|
2015-07-27 21:41:46 +00:00
|
|
|
// Create a service check
|
|
|
|
args := &structs.RegisterRequest{
|
2018-08-06 23:46:09 +00:00
|
|
|
Datacenter: dc,
|
2015-07-27 21:41:46 +00:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
}
|
2018-08-06 23:46:09 +00:00
|
|
|
testrpc.WaitForLeader(t, a.RPC, dc)
|
2015-07-27 21:41:46 +00:00
|
|
|
var out struct{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args.Node, args.Check.Node = "foo", "foo"
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/health/service/test?dc=dc1&near=foo", nil)
|
2015-07-27 21:41:46 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.HealthServiceNodes(resp, req)
|
2015-07-27 21:41:46 +00:00
|
|
|
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()),
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Coordinate.Update", &arg, &out); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-05-05 10:14:43 +00:00
|
|
|
// Retry until foo has moved to the front of the line.
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
resp = httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err = a.srv.HealthServiceNodes(resp, req)
|
2017-05-05 10:14:43 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2015-07-27 21:41:46 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 23:08:26 +00:00
|
|
|
func TestHealthServiceNodes_PassingFilter(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2014-05-07 21:47:16 +00:00
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
dc := "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{
|
2018-08-06 23:46:09 +00:00
|
|
|
Datacenter: dc,
|
2017-05-21 07:11:09 +00:00
|
|
|
Node: a.Config.NodeName,
|
2014-04-21 23:08:26 +00:00
|
|
|
Address: "127.0.0.1",
|
|
|
|
Check: &structs.HealthCheck{
|
2017-05-21 07:11:09 +00:00
|
|
|
Node: a.Config.NodeName,
|
2014-04-21 23:08:26 +00:00
|
|
|
Name: "consul check",
|
|
|
|
ServiceID: "consul",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthCritical,
|
2014-04-21 23:08:26 +00:00
|
|
|
},
|
|
|
|
}
|
2014-05-07 21:12:53 +00:00
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
testrpc.WaitForLeader(t, a.RPC, dc)
|
2014-04-21 23:08:26 +00:00
|
|
|
var out struct{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2014-04-21 23:08:26 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-06-09 18:36:00 +00:00
|
|
|
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)
|
|
|
|
}
|
2014-05-09 09:38:29 +00:00
|
|
|
|
2017-06-09 18:36:00 +00:00
|
|
|
assertIndex(t, resp)
|
2014-04-21 23:08:26 +00:00
|
|
|
|
2017-06-09 18:36:00 +00:00
|
|
|
// 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)
|
|
|
|
|
2017-06-09 18:51:34 +00:00
|
|
|
// Should be 1 consul, it's unhealthy, but we specifically asked for
|
|
|
|
// everything.
|
2017-06-09 18:36:00 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2014-04-21 23:08:26 +00:00
|
|
|
}
|
2014-07-05 16:49:10 +00:00
|
|
|
|
2016-06-15 18:02:51 +00:00
|
|
|
func TestHealthServiceNodes_WanTranslation(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a1 := NewTestAgent(t, t.Name(), `
|
2017-09-25 18:40:42 +00:00
|
|
|
datacenter = "dc1"
|
|
|
|
translate_wan_addrs = true
|
|
|
|
acl_datacenter = ""
|
|
|
|
`)
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a1.Shutdown()
|
2018-08-06 23:46:09 +00:00
|
|
|
testrpc.WaitForLeader(t, a1.RPC, "dc1")
|
2017-05-21 07:11:09 +00:00
|
|
|
|
2019-02-14 15:59:14 +00:00
|
|
|
a2 := NewTestAgent(t, t.Name(), `
|
2017-09-25 18:40:42 +00:00
|
|
|
datacenter = "dc2"
|
|
|
|
translate_wan_addrs = true
|
|
|
|
acl_datacenter = ""
|
|
|
|
`)
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a2.Shutdown()
|
2018-08-06 23:46:09 +00:00
|
|
|
testrpc.WaitForLeader(t, a2.RPC, "dc2")
|
2016-08-15 22:34:11 +00:00
|
|
|
|
|
|
|
// Wait for the WAN join.
|
2017-09-25 18:40:42 +00:00
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", a1.Config.SerfPortWAN)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a2.JoinWAN([]string{addr}); err != nil {
|
2016-08-15 22:34:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-05-21 07:11:09 +00:00
|
|
|
if got, want := len(a1.WANMembers()), 2; got < want {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("got %d WAN members want at least %d", got, want)
|
|
|
|
}
|
|
|
|
})
|
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{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a2.RPC("Catalog.Register", args, &out); err != nil {
|
2016-06-15 18:02:51 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Query for a service in DC2 from DC1.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/health/service/http_wan_translation_test?dc=dc2", nil)
|
2016-06-15 18:02:51 +00:00
|
|
|
resp1 := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj1, err1 := a1.srv.HealthServiceNodes(resp1, req)
|
2016-06-15 18:02:51 +00:00
|
|
|
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()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj2, err2 := a2.srv.HealthServiceNodes(resp2, req)
|
2016-06-15 18:02:51 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 17:52:32 +00:00
|
|
|
func TestHealthConnectServiceNodes(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
assert := assert.New(t)
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2018-03-09 17:52:32 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Register
|
|
|
|
args := structs.TestRegisterRequestProxy(t)
|
|
|
|
var out struct{}
|
|
|
|
assert.Nil(a.RPC("Catalog.Register", args, &out))
|
|
|
|
|
|
|
|
// Request
|
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf(
|
2018-09-12 16:07:47 +00:00
|
|
|
"/v1/health/connect/%s?dc=dc1", args.Service.Proxy.DestinationServiceName), nil)
|
2018-03-09 17:52:32 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.HealthConnectServiceNodes(resp, req)
|
|
|
|
assert.Nil(err)
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
// Should be a non-nil empty list for checks
|
|
|
|
nodes := obj.(structs.CheckServiceNodes)
|
|
|
|
assert.Len(nodes, 1)
|
|
|
|
assert.Len(nodes[0].Checks, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHealthConnectServiceNodes_PassingFilter(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2018-03-09 17:52:32 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Register
|
|
|
|
args := structs.TestRegisterRequestProxy(t)
|
|
|
|
args.Check = &structs.HealthCheck{
|
|
|
|
Node: args.Node,
|
|
|
|
Name: "check",
|
|
|
|
ServiceID: args.Service.Service,
|
|
|
|
Status: api.HealthCritical,
|
|
|
|
}
|
|
|
|
var out struct{}
|
|
|
|
assert.Nil(t, a.RPC("Catalog.Register", args, &out))
|
|
|
|
|
|
|
|
t.Run("bc_no_query_value", func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf(
|
2018-09-12 16:07:47 +00:00
|
|
|
"/v1/health/connect/%s?passing", args.Service.Proxy.DestinationServiceName), nil)
|
2018-03-09 17:52:32 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.HealthConnectServiceNodes(resp, req)
|
|
|
|
assert.Nil(err)
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
// Should be 0 health check for consul
|
|
|
|
nodes := obj.(structs.CheckServiceNodes)
|
|
|
|
assert.Len(nodes, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("passing_true", func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf(
|
2018-09-12 16:07:47 +00:00
|
|
|
"/v1/health/connect/%s?passing=true", args.Service.Proxy.DestinationServiceName), nil)
|
2018-03-09 17:52:32 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.HealthConnectServiceNodes(resp, req)
|
|
|
|
assert.Nil(err)
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
// Should be 0 health check for consul
|
|
|
|
nodes := obj.(structs.CheckServiceNodes)
|
|
|
|
assert.Len(nodes, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("passing_false", func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf(
|
2018-09-12 16:07:47 +00:00
|
|
|
"/v1/health/connect/%s?passing=false", args.Service.Proxy.DestinationServiceName), nil)
|
2018-03-09 17:52:32 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.HealthConnectServiceNodes(resp, req)
|
|
|
|
assert.Nil(err)
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
2018-03-22 02:54:44 +00:00
|
|
|
// Should be 1
|
2018-03-09 17:52:32 +00:00
|
|
|
nodes := obj.(structs.CheckServiceNodes)
|
|
|
|
assert.Len(nodes, 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("passing_bad", func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf(
|
2018-09-12 16:07:47 +00:00
|
|
|
"/v1/health/connect/%s?passing=nope-nope", args.Service.Proxy.DestinationServiceName), nil)
|
2018-03-09 17:52:32 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
a.srv.HealthConnectServiceNodes(resp, req)
|
|
|
|
assert.Equal(400, resp.Code)
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.True(bytes.Contains(body, []byte("Invalid value for ?passing")))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-05 16:49:10 +00:00
|
|
|
func TestFilterNonPassing(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2014-07-05 16:49:10 +00:00
|
|
|
nodes := structs.CheckServiceNodes{
|
|
|
|
structs.CheckServiceNode{
|
|
|
|
Checks: structs.HealthChecks{
|
|
|
|
&structs.HealthCheck{
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthCritical,
|
2014-07-05 16:49:10 +00:00
|
|
|
},
|
|
|
|
&structs.HealthCheck{
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthCritical,
|
2014-07-05 16:49:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
structs.CheckServiceNode{
|
|
|
|
Checks: structs.HealthChecks{
|
|
|
|
&structs.HealthCheck{
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthCritical,
|
2014-07-05 16:49:10 +00:00
|
|
|
},
|
|
|
|
&structs.HealthCheck{
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthCritical,
|
2014-07-05 16:49:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
structs.CheckServiceNode{
|
|
|
|
Checks: structs.HealthChecks{
|
|
|
|
&structs.HealthCheck{
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2014-07-05 16:49:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
out := filterNonPassing(nodes)
|
|
|
|
if len(out) != 1 && reflect.DeepEqual(out[0], nodes[2]) {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
}
|