mirror of
https://github.com/status-im/consul.git
synced 2025-02-24 11:28:40 +00:00
Adding tests for checks and services endpoints
This commit is contained in:
parent
cb7541c7af
commit
a6e4235b96
@ -2,6 +2,7 @@ package agent
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/hashicorp/consul/consul/structs"
|
||||||
"github.com/hashicorp/serf/serf"
|
"github.com/hashicorp/serf/serf"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -9,6 +10,60 @@ import (
|
|||||||
"time"
|
"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) {
|
func TestHTTPAgentMembers(t *testing.T) {
|
||||||
dir, srv := makeHTTPServer(t)
|
dir, srv := makeHTTPServer(t)
|
||||||
defer os.RemoveAll(dir)
|
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
|
// This entry is persistent and the agent will make a best effort to
|
||||||
// ensure it is registered
|
// ensure it is registered
|
||||||
func (a *Agent) AddCheck(check *structs.HealthCheck) {
|
func (a *Agent) AddCheck(check *structs.HealthCheck) {
|
||||||
|
// Set the node name
|
||||||
|
check.Node = a.config.NodeName
|
||||||
|
|
||||||
a.state.Lock()
|
a.state.Lock()
|
||||||
defer a.state.Unlock()
|
defer a.state.Unlock()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user