From 7b4f7ca6b64fb16892eff28e149cd451c1e3a665 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Wed, 8 Jan 2014 14:23:52 -0800 Subject: [PATCH] Testing the health endpoints --- consul/health_endpoint_test.go | 136 +++++++++++++++++++++++++++++++++ consul/server.go | 1 + 2 files changed, 137 insertions(+) create mode 100644 consul/health_endpoint_test.go diff --git a/consul/health_endpoint_test.go b/consul/health_endpoint_test.go new file mode 100644 index 0000000000..7488a5f7fb --- /dev/null +++ b/consul/health_endpoint_test.go @@ -0,0 +1,136 @@ +package consul + +import ( + "github.com/hashicorp/consul/consul/structs" + "os" + "testing" + "time" +) + +func TestHealth_ChecksInState(t *testing.T) { + dir1, s1 := testServer(t) + defer os.RemoveAll(dir1) + defer s1.Shutdown() + client := rpcClient(t, s1) + defer client.Close() + + // Wait for leader + time.Sleep(100 * time.Millisecond) + + 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 checks structs.HealthChecks + inState := structs.ChecksInStateRequest{ + Datacenter: "dc1", + State: structs.HealthPassing, + } + if err := client.Call("Health.ChecksInState", &inState, &checks); err != nil { + t.Fatalf("err: %v", err) + } + + if len(checks) != 1 { + t.Fatalf("Bad: %v", checks) + } + if checks[0].Name != "memory utilization" { + t.Fatalf("Bad: %v", checks) + } +} + +func TestHealth_NodeChecks(t *testing.T) { + dir1, s1 := testServer(t) + defer os.RemoveAll(dir1) + defer s1.Shutdown() + client := rpcClient(t, s1) + defer client.Close() + + // Wait for leader + time.Sleep(100 * time.Millisecond) + + 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 checks structs.HealthChecks + node := structs.NodeSpecificRequest{ + Datacenter: "dc1", + Node: "foo", + } + if err := client.Call("Health.NodeChecks", &node, &checks); err != nil { + t.Fatalf("err: %v", err) + } + + 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() + + // Wait for leader + time.Sleep(100 * time.Millisecond) + + 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 checks structs.HealthChecks + node := structs.ServiceSpecificRequest{ + Datacenter: "dc1", + ServiceName: "db", + } + if err := client.Call("Health.ServiceChecks", &node, &checks); err != nil { + t.Fatalf("err: %v", err) + } + + if len(checks) != 1 { + t.Fatalf("Bad: %v", checks) + } + if checks[0].Name != "db connect" { + t.Fatalf("Bad: %v", checks) + } +} diff --git a/consul/server.go b/consul/server.go index 879c37be51..02821513a0 100644 --- a/consul/server.go +++ b/consul/server.go @@ -227,6 +227,7 @@ func (s *Server) setupRPC() error { s.rpcServer.Register(&Status{server: s}) s.rpcServer.Register(&Raft{server: s}) s.rpcServer.Register(&Catalog{s}) + s.rpcServer.Register(&Health{s}) list, err := net.ListenTCP("tcp", s.config.RPCAddr) if err != nil {