2014-01-08 22:23:52 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
2015-07-27 21:41:46 +00:00
|
|
|
"time"
|
2015-10-13 23:43:52 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
|
|
|
"github.com/hashicorp/consul/testutil"
|
|
|
|
"github.com/hashicorp/net-rpc-msgpackrpc"
|
2014-01-08 22:23:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHealth_ChecksInState(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-01-08 22:23:52 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2014-01-08 22:23:52 +00:00
|
|
|
|
|
|
|
arg := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
Name: "memory utilization",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out struct{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out); err != nil {
|
2014-01-08 22:23:52 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-02-05 21:30:18 +00:00
|
|
|
var out2 structs.IndexedHealthChecks
|
2014-01-08 22:23:52 +00:00
|
|
|
inState := structs.ChecksInStateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
State: structs.HealthPassing,
|
|
|
|
}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Health.ChecksInState", &inState, &out2); err != nil {
|
2014-01-08 22:23:52 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-02-05 21:30:18 +00:00
|
|
|
checks := out2.HealthChecks
|
2014-01-10 01:22:01 +00:00
|
|
|
if len(checks) != 2 {
|
2014-01-08 22:23:52 +00:00
|
|
|
t.Fatalf("Bad: %v", checks)
|
|
|
|
}
|
2014-01-10 01:22:01 +00:00
|
|
|
|
2015-10-12 07:42:09 +00:00
|
|
|
// Serf check is automatically added
|
|
|
|
if checks[0].Name != "memory utilization" {
|
2014-01-10 01:22:01 +00:00
|
|
|
t.Fatalf("Bad: %v", checks[0])
|
|
|
|
}
|
2015-10-12 07:42:09 +00:00
|
|
|
if checks[1].CheckID != SerfCheckID {
|
2014-01-10 01:22:01 +00:00
|
|
|
t.Fatalf("Bad: %v", checks[1])
|
2014-01-08 22:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-27 21:41:46 +00:00
|
|
|
func TestHealth_ChecksInState_DistanceSort(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-15 23:07:16 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2015-07-27 21:41:46 +00:00
|
|
|
|
2015-10-15 23:07:16 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2015-10-23 22:19:14 +00:00
|
|
|
if err := s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.2"}); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
if err := s1.fsm.State().EnsureNode(2, &structs.Node{Node: "bar", Address: "127.0.0.3"}); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
updates := structs.Coordinates{
|
2015-07-27 21:41:46 +00:00
|
|
|
{"foo", generateCoordinate(1 * time.Millisecond)},
|
|
|
|
{"bar", generateCoordinate(2 * time.Millisecond)},
|
|
|
|
}
|
|
|
|
if err := s1.fsm.State().CoordinateBatchUpdate(3, updates); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arg := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
Name: "memory utilization",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arg.Node = "bar"
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query relative to foo to make sure it shows up first in the list.
|
|
|
|
var out2 structs.IndexedHealthChecks
|
|
|
|
inState := structs.ChecksInStateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
State: structs.HealthPassing,
|
2015-07-29 23:33:25 +00:00
|
|
|
Source: structs.QuerySource{
|
2015-07-27 21:41:46 +00:00
|
|
|
Datacenter: "dc1",
|
2015-07-29 23:33:25 +00:00
|
|
|
Node: "foo",
|
2015-07-27 21:41:46 +00:00
|
|
|
},
|
|
|
|
}
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Health.ChecksInState", &inState, &out2); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
checks := out2.HealthChecks
|
|
|
|
if len(checks) != 3 {
|
|
|
|
t.Fatalf("Bad: %v", checks)
|
|
|
|
}
|
|
|
|
if checks[0].Node != "foo" {
|
|
|
|
t.Fatalf("Bad: %v", checks[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now query relative to bar to make sure it shows up first.
|
|
|
|
inState.Source.Node = "bar"
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Health.ChecksInState", &inState, &out2); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
checks = out2.HealthChecks
|
|
|
|
if len(checks) != 3 {
|
|
|
|
t.Fatalf("Bad: %v", checks)
|
|
|
|
}
|
|
|
|
if checks[0].Node != "bar" {
|
|
|
|
t.Fatalf("Bad: %v", checks[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-08 22:23:52 +00:00
|
|
|
func TestHealth_NodeChecks(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-01-08 22:23:52 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2014-01-08 22:23:52 +00:00
|
|
|
|
|
|
|
arg := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
Name: "memory utilization",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out struct{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out); err != nil {
|
2014-01-08 22:23:52 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-02-05 21:30:18 +00:00
|
|
|
var out2 structs.IndexedHealthChecks
|
2014-01-08 22:23:52 +00:00
|
|
|
node := structs.NodeSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Health.NodeChecks", &node, &out2); err != nil {
|
2014-01-08 22:23:52 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-02-05 21:30:18 +00:00
|
|
|
checks := out2.HealthChecks
|
2014-01-08 22:23:52 +00:00
|
|
|
if len(checks) != 1 {
|
|
|
|
t.Fatalf("Bad: %v", checks)
|
|
|
|
}
|
|
|
|
if checks[0].Name != "memory utilization" {
|
|
|
|
t.Fatalf("Bad: %v", checks)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHealth_ServiceChecks(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-01-08 22:23:52 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2014-01-08 22:23:52 +00:00
|
|
|
|
|
|
|
arg := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "db",
|
|
|
|
Service: "db",
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
Name: "db connect",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "db",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out struct{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out); err != nil {
|
2014-01-08 22:23:52 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-02-05 21:30:18 +00:00
|
|
|
var out2 structs.IndexedHealthChecks
|
2014-01-08 22:23:52 +00:00
|
|
|
node := structs.ServiceSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
ServiceName: "db",
|
|
|
|
}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Health.ServiceChecks", &node, &out2); err != nil {
|
2014-01-08 22:23:52 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-02-05 21:30:18 +00:00
|
|
|
checks := out2.HealthChecks
|
2014-01-08 22:23:52 +00:00
|
|
|
if len(checks) != 1 {
|
|
|
|
t.Fatalf("Bad: %v", checks)
|
|
|
|
}
|
|
|
|
if checks[0].Name != "db connect" {
|
|
|
|
t.Fatalf("Bad: %v", checks)
|
|
|
|
}
|
|
|
|
}
|
2014-01-08 23:06:13 +00:00
|
|
|
|
2015-07-27 21:41:46 +00:00
|
|
|
func TestHealth_ServiceChecks_DistanceSort(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-15 23:07:16 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2015-07-27 21:41:46 +00:00
|
|
|
|
2015-10-15 23:07:16 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2015-10-23 22:19:14 +00:00
|
|
|
if err := s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.2"}); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
if err := s1.fsm.State().EnsureNode(2, &structs.Node{Node: "bar", Address: "127.0.0.3"}); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
updates := structs.Coordinates{
|
2015-07-27 21:41:46 +00:00
|
|
|
{"foo", generateCoordinate(1 * time.Millisecond)},
|
|
|
|
{"bar", generateCoordinate(2 * time.Millisecond)},
|
|
|
|
}
|
|
|
|
if err := s1.fsm.State().CoordinateBatchUpdate(3, updates); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arg := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "db",
|
|
|
|
Service: "db",
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
Name: "db connect",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "db",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arg.Node = "bar"
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query relative to foo to make sure it shows up first in the list.
|
|
|
|
var out2 structs.IndexedHealthChecks
|
|
|
|
node := structs.ServiceSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
ServiceName: "db",
|
2015-07-29 23:33:25 +00:00
|
|
|
Source: structs.QuerySource{
|
2015-07-27 21:41:46 +00:00
|
|
|
Datacenter: "dc1",
|
2015-07-29 23:33:25 +00:00
|
|
|
Node: "foo",
|
2015-07-27 21:41:46 +00:00
|
|
|
},
|
|
|
|
}
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Health.ServiceChecks", &node, &out2); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
checks := out2.HealthChecks
|
|
|
|
if len(checks) != 2 {
|
|
|
|
t.Fatalf("Bad: %v", checks)
|
|
|
|
}
|
|
|
|
if checks[0].Node != "foo" {
|
|
|
|
t.Fatalf("Bad: %v", checks)
|
|
|
|
}
|
|
|
|
if checks[1].Node != "bar" {
|
|
|
|
t.Fatalf("Bad: %v", checks)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now query relative to bar to make sure it shows up first.
|
|
|
|
node.Source.Node = "bar"
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Health.ServiceChecks", &node, &out2); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
checks = out2.HealthChecks
|
|
|
|
if len(checks) != 2 {
|
|
|
|
t.Fatalf("Bad: %v", checks)
|
|
|
|
}
|
|
|
|
if checks[0].Node != "bar" {
|
|
|
|
t.Fatalf("Bad: %v", checks)
|
|
|
|
}
|
|
|
|
if checks[1].Node != "foo" {
|
|
|
|
t.Fatalf("Bad: %v", checks)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-08 23:06:13 +00:00
|
|
|
func TestHealth_ServiceNodes(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-01-08 23:06:13 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2014-01-08 23:06:13 +00:00
|
|
|
|
|
|
|
arg := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "db",
|
|
|
|
Service: "db",
|
2014-04-03 19:03:10 +00:00
|
|
|
Tags: []string{"master"},
|
2014-01-08 23:06:13 +00:00
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
Name: "db connect",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "db",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out struct{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out); err != nil {
|
2014-01-08 23:06:13 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arg = structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "db",
|
|
|
|
Service: "db",
|
2014-04-03 19:03:10 +00:00
|
|
|
Tags: []string{"slave"},
|
2014-01-08 23:06:13 +00:00
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
Name: "db connect",
|
|
|
|
Status: structs.HealthWarning,
|
|
|
|
ServiceID: "db",
|
|
|
|
},
|
|
|
|
}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out); err != nil {
|
2014-01-08 23:06:13 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-02-05 21:30:18 +00:00
|
|
|
var out2 structs.IndexedCheckServiceNodes
|
2014-01-08 23:06:13 +00:00
|
|
|
req := structs.ServiceSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
ServiceName: "db",
|
|
|
|
ServiceTag: "master",
|
|
|
|
TagFilter: false,
|
|
|
|
}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Health.ServiceNodes", &req, &out2); err != nil {
|
2014-01-08 23:06:13 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-02-05 21:30:18 +00:00
|
|
|
nodes := out2.Nodes
|
2014-01-08 23:06:13 +00:00
|
|
|
if len(nodes) != 2 {
|
|
|
|
t.Fatalf("Bad: %v", nodes)
|
|
|
|
}
|
2015-10-12 07:42:09 +00:00
|
|
|
if nodes[0].Node.Node != "bar" {
|
2014-01-08 23:06:13 +00:00
|
|
|
t.Fatalf("Bad: %v", nodes[0])
|
|
|
|
}
|
2015-10-12 07:42:09 +00:00
|
|
|
if nodes[1].Node.Node != "foo" {
|
2014-01-08 23:06:13 +00:00
|
|
|
t.Fatalf("Bad: %v", nodes[1])
|
|
|
|
}
|
2015-10-12 07:42:09 +00:00
|
|
|
if !strContains(nodes[0].Service.Tags, "slave") {
|
2014-01-08 23:06:13 +00:00
|
|
|
t.Fatalf("Bad: %v", nodes[0])
|
|
|
|
}
|
2015-10-12 07:42:09 +00:00
|
|
|
if !strContains(nodes[1].Service.Tags, "master") {
|
2014-01-08 23:06:13 +00:00
|
|
|
t.Fatalf("Bad: %v", nodes[1])
|
|
|
|
}
|
2015-10-12 07:42:09 +00:00
|
|
|
if nodes[0].Checks[0].Status != structs.HealthWarning {
|
2014-01-08 23:06:13 +00:00
|
|
|
t.Fatalf("Bad: %v", nodes[0])
|
|
|
|
}
|
2015-10-12 07:42:09 +00:00
|
|
|
if nodes[1].Checks[0].Status != structs.HealthPassing {
|
2014-01-08 23:06:13 +00:00
|
|
|
t.Fatalf("Bad: %v", nodes[1])
|
|
|
|
}
|
|
|
|
}
|
2015-06-11 16:39:35 +00:00
|
|
|
|
2015-07-27 21:41:46 +00:00
|
|
|
func TestHealth_ServiceNodes_DistanceSort(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-15 23:07:16 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2015-07-27 21:41:46 +00:00
|
|
|
|
2015-10-15 23:07:16 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2015-10-23 22:19:14 +00:00
|
|
|
if err := s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.2"}); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
if err := s1.fsm.State().EnsureNode(2, &structs.Node{Node: "bar", Address: "127.0.0.3"}); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
updates := structs.Coordinates{
|
2015-07-27 21:41:46 +00:00
|
|
|
{"foo", generateCoordinate(1 * time.Millisecond)},
|
|
|
|
{"bar", generateCoordinate(2 * time.Millisecond)},
|
|
|
|
}
|
|
|
|
if err := s1.fsm.State().CoordinateBatchUpdate(3, updates); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arg := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "db",
|
|
|
|
Service: "db",
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
Name: "db connect",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "db",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arg.Node = "bar"
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query relative to foo to make sure it shows up first in the list.
|
|
|
|
var out2 structs.IndexedCheckServiceNodes
|
|
|
|
req := structs.ServiceSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
ServiceName: "db",
|
2015-07-29 23:33:25 +00:00
|
|
|
Source: structs.QuerySource{
|
2015-07-27 21:41:46 +00:00
|
|
|
Datacenter: "dc1",
|
2015-07-29 23:33:25 +00:00
|
|
|
Node: "foo",
|
2015-07-27 21:41:46 +00:00
|
|
|
},
|
|
|
|
}
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Health.ServiceNodes", &req, &out2); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
nodes := out2.Nodes
|
|
|
|
if len(nodes) != 2 {
|
|
|
|
t.Fatalf("Bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[0].Node.Node != "foo" {
|
|
|
|
t.Fatalf("Bad: %v", nodes[0])
|
|
|
|
}
|
|
|
|
if nodes[1].Node.Node != "bar" {
|
|
|
|
t.Fatalf("Bad: %v", nodes[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now query relative to bar to make sure it shows up first.
|
|
|
|
req.Source.Node = "bar"
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Health.ServiceNodes", &req, &out2); err != nil {
|
2015-07-27 21:41:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
nodes = out2.Nodes
|
|
|
|
if len(nodes) != 2 {
|
|
|
|
t.Fatalf("Bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[0].Node.Node != "bar" {
|
|
|
|
t.Fatalf("Bad: %v", nodes[0])
|
|
|
|
}
|
|
|
|
if nodes[1].Node.Node != "foo" {
|
|
|
|
t.Fatalf("Bad: %v", nodes[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-11 20:23:49 +00:00
|
|
|
func TestHealth_NodeChecks_FilterACL(t *testing.T) {
|
2015-10-13 23:43:52 +00:00
|
|
|
dir, token, srv, codec := testACLFilterServer(t)
|
2015-06-11 16:39:35 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
defer codec.Close()
|
2015-06-11 16:39:35 +00:00
|
|
|
|
2015-06-11 20:23:49 +00:00
|
|
|
opt := structs.NodeSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: srv.config.NodeName,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: token},
|
2015-06-11 16:39:35 +00:00
|
|
|
}
|
2015-06-11 20:23:49 +00:00
|
|
|
reply := structs.IndexedHealthChecks{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Health.NodeChecks", &opt, &reply); err != nil {
|
2015-06-11 20:23:49 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
2015-06-11 16:39:35 +00:00
|
|
|
}
|
2015-06-11 20:23:49 +00:00
|
|
|
found := false
|
|
|
|
for _, chk := range reply.HealthChecks {
|
|
|
|
switch chk.ServiceName {
|
|
|
|
case "foo":
|
|
|
|
found = true
|
|
|
|
case "bar":
|
|
|
|
t.Fatalf("bad: %#v", reply.HealthChecks)
|
|
|
|
}
|
2015-06-11 16:39:35 +00:00
|
|
|
}
|
2015-06-11 20:23:49 +00:00
|
|
|
if !found {
|
|
|
|
t.Fatalf("bad: %#v", reply.HealthChecks)
|
2015-06-11 16:39:35 +00:00
|
|
|
}
|
2015-06-11 20:23:49 +00:00
|
|
|
}
|
2015-06-11 16:39:35 +00:00
|
|
|
|
2015-06-11 20:23:49 +00:00
|
|
|
func TestHealth_ServiceChecks_FilterACL(t *testing.T) {
|
2015-10-13 23:43:52 +00:00
|
|
|
dir, token, srv, codec := testACLFilterServer(t)
|
2015-06-11 20:23:49 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
defer codec.Close()
|
2015-06-11 20:23:49 +00:00
|
|
|
|
|
|
|
opt := structs.ServiceSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
ServiceName: "foo",
|
|
|
|
QueryOptions: structs.QueryOptions{Token: token},
|
2015-06-11 16:39:35 +00:00
|
|
|
}
|
2015-06-11 20:23:49 +00:00
|
|
|
reply := structs.IndexedHealthChecks{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Health.ServiceChecks", &opt, &reply); err != nil {
|
2015-06-11 16:39:35 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-06-11 20:23:49 +00:00
|
|
|
found := false
|
|
|
|
for _, chk := range reply.HealthChecks {
|
|
|
|
if chk.ServiceName == "foo" {
|
|
|
|
found = true
|
|
|
|
break
|
2015-06-11 16:39:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-11 20:23:49 +00:00
|
|
|
if !found {
|
|
|
|
t.Fatalf("bad: %#v", reply.HealthChecks)
|
|
|
|
}
|
2015-06-11 16:39:35 +00:00
|
|
|
|
2015-06-11 20:23:49 +00:00
|
|
|
opt.ServiceName = "bar"
|
|
|
|
reply = structs.IndexedHealthChecks{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Health.ServiceChecks", &opt, &reply); err != nil {
|
2015-06-11 20:23:49 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if len(reply.HealthChecks) != 0 {
|
|
|
|
t.Fatalf("bad: %#v", reply.HealthChecks)
|
2015-06-11 16:39:35 +00:00
|
|
|
}
|
2015-06-11 20:23:49 +00:00
|
|
|
}
|
2015-06-11 16:39:35 +00:00
|
|
|
|
2015-06-11 20:23:49 +00:00
|
|
|
func TestHealth_ServiceNodes_FilterACL(t *testing.T) {
|
2015-10-13 23:43:52 +00:00
|
|
|
dir, token, srv, codec := testACLFilterServer(t)
|
2015-06-11 20:23:49 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
defer codec.Close()
|
2015-06-11 16:39:35 +00:00
|
|
|
|
2015-06-11 20:23:49 +00:00
|
|
|
opt := structs.ServiceSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
ServiceName: "foo",
|
|
|
|
QueryOptions: structs.QueryOptions{Token: token},
|
|
|
|
}
|
|
|
|
reply := structs.IndexedCheckServiceNodes{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Health.ServiceNodes", &opt, &reply); err != nil {
|
2015-06-11 20:23:49 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if len(reply.Nodes) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", reply.Nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
opt.ServiceName = "bar"
|
|
|
|
reply = structs.IndexedCheckServiceNodes{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Health.ServiceNodes", &opt, &reply); err != nil {
|
2015-06-11 20:23:49 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if len(reply.Nodes) != 0 {
|
|
|
|
t.Fatalf("bad: %#v", reply.Nodes)
|
2015-06-11 16:39:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-27 23:57:56 +00:00
|
|
|
|
|
|
|
func TestHealth_ChecksInState_FilterACL(t *testing.T) {
|
2015-10-13 23:43:52 +00:00
|
|
|
dir, token, srv, codec := testACLFilterServer(t)
|
2015-07-27 23:57:56 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
defer codec.Close()
|
2015-07-27 23:57:56 +00:00
|
|
|
|
|
|
|
opt := structs.ChecksInStateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
State: structs.HealthPassing,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: token},
|
|
|
|
}
|
|
|
|
reply := structs.IndexedHealthChecks{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Health.ChecksInState", &opt, &reply); err != nil {
|
2015-07-27 23:57:56 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
|
|
|
for _, chk := range reply.HealthChecks {
|
|
|
|
switch chk.ServiceName {
|
|
|
|
case "foo":
|
|
|
|
found = true
|
|
|
|
case "bar":
|
|
|
|
t.Fatalf("bad service 'bar': %#v", reply.HealthChecks)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("missing service 'foo': %#v", reply.HealthChecks)
|
|
|
|
}
|
|
|
|
}
|