mirror of https://github.com/hashicorp/consul
Adding tests for checks and services endpoints
parent
cb7541c7af
commit
a6e4235b96
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in New Issue