open-consul/consul/health_endpoint_test.go

225 lines
5 KiB
Go
Raw Normal View History

2014-01-08 22:23:52 +00:00
package consul
import (
"github.com/hashicorp/consul/consul/structs"
2014-05-16 22:49:47 +00:00
"github.com/hashicorp/consul/testutil"
2014-01-08 22:23:52 +00:00
"os"
"testing"
)
func TestHealth_ChecksInState(t *testing.T) {
dir1, s1 := testServer(t)
defer os.RemoveAll(dir1)
defer s1.Shutdown()
client := rpcClient(t, s1)
defer client.Close()
testutil.WaitForLeader(t, client.Call, "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{}
if err := client.Call("Catalog.Register", &arg, &out); err != nil {
t.Fatalf("err: %v", err)
}
var out2 structs.IndexedHealthChecks
2014-01-08 22:23:52 +00:00
inState := structs.ChecksInStateRequest{
Datacenter: "dc1",
State: structs.HealthPassing,
}
if err := client.Call("Health.ChecksInState", &inState, &out2); err != nil {
2014-01-08 22:23:52 +00:00
t.Fatalf("err: %v", err)
}
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
// First check is automatically added for the server node
2014-01-16 03:24:16 +00:00
if checks[0].CheckID != SerfCheckID {
2014-01-10 01:22:01 +00:00
t.Fatalf("Bad: %v", checks[0])
}
if checks[1].Name != "memory utilization" {
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()
client := rpcClient(t, s1)
defer client.Close()
testutil.WaitForLeader(t, client.Call, "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{}
if err := client.Call("Catalog.Register", &arg, &out); err != nil {
t.Fatalf("err: %v", err)
}
var out2 structs.IndexedHealthChecks
2014-01-08 22:23:52 +00:00
node := structs.NodeSpecificRequest{
Datacenter: "dc1",
Node: "foo",
}
if err := client.Call("Health.NodeChecks", &node, &out2); err != nil {
2014-01-08 22:23:52 +00:00
t.Fatalf("err: %v", err)
}
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()
client := rpcClient(t, s1)
defer client.Close()
testutil.WaitForLeader(t, client.Call, "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{}
if err := client.Call("Catalog.Register", &arg, &out); err != nil {
t.Fatalf("err: %v", err)
}
var out2 structs.IndexedHealthChecks
2014-01-08 22:23:52 +00:00
node := structs.ServiceSpecificRequest{
Datacenter: "dc1",
ServiceName: "db",
}
if err := client.Call("Health.ServiceChecks", &node, &out2); err != nil {
2014-01-08 22:23:52 +00:00
t.Fatalf("err: %v", err)
}
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)
}
}
func TestHealth_ServiceNodes(t *testing.T) {
dir1, s1 := testServer(t)
defer os.RemoveAll(dir1)
defer s1.Shutdown()
client := rpcClient(t, s1)
defer client.Close()
testutil.WaitForLeader(t, client.Call, "dc1")
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"},
},
Check: &structs.HealthCheck{
Name: "db connect",
Status: structs.HealthPassing,
ServiceID: "db",
},
}
var out struct{}
if err := client.Call("Catalog.Register", &arg, &out); err != nil {
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"},
},
Check: &structs.HealthCheck{
Name: "db connect",
Status: structs.HealthWarning,
ServiceID: "db",
},
}
if err := client.Call("Catalog.Register", &arg, &out); err != nil {
t.Fatalf("err: %v", err)
}
var out2 structs.IndexedCheckServiceNodes
req := structs.ServiceSpecificRequest{
Datacenter: "dc1",
ServiceName: "db",
ServiceTag: "master",
TagFilter: false,
}
if err := client.Call("Health.ServiceNodes", &req, &out2); err != nil {
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])
}
2014-04-03 19:03:10 +00:00
if !strContains(nodes[0].Service.Tags, "master") {
t.Fatalf("Bad: %v", nodes[0])
}
2014-04-03 19:03:10 +00:00
if !strContains(nodes[1].Service.Tags, "slave") {
t.Fatalf("Bad: %v", nodes[1])
}
if nodes[0].Checks[0].Status != structs.HealthPassing {
t.Fatalf("Bad: %v", nodes[0])
}
if nodes[1].Checks[0].Status != structs.HealthWarning {
t.Fatalf("Bad: %v", nodes[1])
}
}