Adding tests for checks and services endpoints

pull/19/head
Armon Dadgar 2014-01-20 15:06:44 -10:00
parent cb7541c7af
commit a6e4235b96
2 changed files with 58 additions and 0 deletions

View File

@ -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)

View File

@ -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()