From a6e4235b961aa8a5deab89185c409e8f32f75dff Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Mon, 20 Jan 2014 15:06:44 -1000 Subject: [PATCH] Adding tests for checks and services endpoints --- command/agent/agent_endpoint_test.go | 55 ++++++++++++++++++++++++++++ command/agent/local.go | 3 ++ 2 files changed, 58 insertions(+) diff --git a/command/agent/agent_endpoint_test.go b/command/agent/agent_endpoint_test.go index d898fdf18a..f516f0bbc9 100644 --- a/command/agent/agent_endpoint_test.go +++ b/command/agent/agent_endpoint_test.go @@ -2,6 +2,7 @@ package agent import ( "fmt" + "github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/serf/serf" "net/http" "os" @@ -9,6 +10,60 @@ import ( "time" ) +func TestHTTPAgentServices(t *testing.T) { + dir, srv := makeHTTPServer(t) + defer os.RemoveAll(dir) + defer srv.Shutdown() + defer srv.agent.Shutdown() + + srv1 := &structs.NodeService{ + ID: "mysql", + Service: "mysql", + Tag: "master", + Port: 5000, + } + srv.agent.AddService(srv1) + + obj, err := srv.AgentServices(nil, nil) + if err != nil { + t.Fatalf("Err: %v", err) + } + val := obj.(map[string]*structs.NodeService) + if len(val) != 1 { + t.Fatalf("bad services: %v", obj) + } + if val["mysql"].Port != 5000 { + t.Fatalf("bad service: %v", obj) + } +} + +func TestHTTPAgentChecks(t *testing.T) { + dir, srv := makeHTTPServer(t) + defer os.RemoveAll(dir) + defer srv.Shutdown() + defer srv.agent.Shutdown() + + chk1 := &structs.HealthCheck{ + Node: srv.agent.config.NodeName, + CheckID: "mysql", + Name: "mysql", + Status: structs.HealthPassing, + } + srv.agent.AddCheck(chk1) + + obj, err := srv.AgentChecks(nil, nil) + if err != nil { + t.Fatalf("Err: %v", err) + } + val := obj.(map[string]*structs.HealthCheck) + if len(val) != 1 { + t.Fatalf("bad checks: %v", obj) + } + if val["mysql"].Status != structs.HealthPassing { + t.Fatalf("bad check: %v", obj) + } +} + func TestHTTPAgentMembers(t *testing.T) { dir, srv := makeHTTPServer(t) defer os.RemoveAll(dir) diff --git a/command/agent/local.go b/command/agent/local.go index 12c745e00a..ed55f5e9ed 100644 --- a/command/agent/local.go +++ b/command/agent/local.go @@ -106,6 +106,9 @@ func (a *Agent) Services() map[string]*structs.NodeService { // This entry is persistent and the agent will make a best effort to // ensure it is registered func (a *Agent) AddCheck(check *structs.HealthCheck) { + // Set the node name + check.Node = a.config.NodeName + a.state.Lock() defer a.state.Unlock()