api: support multiple checks during service/check registration

This commit is contained in:
Ryan Uber 2015-01-13 19:18:46 -08:00
parent 2a7211cd5d
commit 197a5a9a9a
2 changed files with 92 additions and 8 deletions

View File

@ -46,6 +46,7 @@ type AgentServiceRegistration struct {
Tags []string `json:",omitempty"` Tags []string `json:",omitempty"`
Port int `json:",omitempty"` Port int `json:",omitempty"`
Check *AgentServiceCheck Check *AgentServiceCheck
Checks AgentServiceChecks
} }
// AgentCheckRegistration is used to register a new check // AgentCheckRegistration is used to register a new check
@ -53,6 +54,7 @@ type AgentCheckRegistration struct {
ID string `json:",omitempty"` ID string `json:",omitempty"`
Name string `json:",omitempty"` Name string `json:",omitempty"`
Notes string `json:",omitempty"` Notes string `json:",omitempty"`
ServiceID string `json:",omitempty"`
AgentServiceCheck AgentServiceCheck
} }
@ -63,6 +65,7 @@ type AgentServiceCheck struct {
Interval string `json:",omitempty"` Interval string `json:",omitempty"`
TTL string `json:",omitempty"` TTL string `json:",omitempty"`
} }
type AgentServiceChecks []*AgentServiceCheck
// Agent can be used to query the Agent endpoints // Agent can be used to query the Agent endpoints
type Agent struct { type Agent struct {

View File

@ -76,6 +76,49 @@ func TestAgent_Services(t *testing.T) {
} }
} }
func TestAgent_Services_MultipleChecks(t *testing.T) {
c, s := makeClient(t)
defer s.stop()
agent := c.Agent()
reg := &AgentServiceRegistration{
Name: "foo",
Tags: []string{"bar", "baz"},
Port: 8000,
Checks: AgentServiceChecks{
&AgentServiceCheck{
TTL: "15s",
},
&AgentServiceCheck{
TTL: "30s",
},
},
}
if err := agent.ServiceRegister(reg); err != nil {
t.Fatalf("err: %v", err)
}
services, err := agent.Services()
if err != nil {
t.Fatalf("err: %v", err)
}
if _, ok := services["foo"]; !ok {
t.Fatalf("missing service: %v", services)
}
checks, err := agent.Checks()
if err != nil {
t.Fatalf("err: %v", err)
}
if _, ok := checks["service:foo:1"]; !ok {
t.Fatalf("missing check: %v", checks)
}
if _, ok := checks["service:foo:2"]; !ok {
t.Fatalf("missing check: %v", checks)
}
}
func TestAgent_SetTTLStatus(t *testing.T) { func TestAgent_SetTTLStatus(t *testing.T) {
c, s := makeClient(t) c, s := makeClient(t)
defer s.stop() defer s.stop()
@ -143,6 +186,44 @@ func TestAgent_Checks(t *testing.T) {
} }
} }
func TestAgent_Checks_serviceBound(t *testing.T) {
c, s := makeClient(t)
defer s.stop()
agent := c.Agent()
// First register a service
serviceReg := &AgentServiceRegistration{
Name: "redis",
}
if err := agent.ServiceRegister(serviceReg); err != nil {
t.Fatalf("err: %v", err)
}
// Register a check bound to the service
reg := &AgentCheckRegistration{
Name: "redischeck",
ServiceID: "redis",
}
reg.TTL = "15s"
if err := agent.CheckRegister(reg); err != nil {
t.Fatalf("err: %v", err)
}
checks, err := agent.Checks()
if err != nil {
t.Fatalf("err: %v", err)
}
check, ok := checks["redischeck"]
if !ok {
t.Fatalf("missing check: %v", checks)
}
if check.ServiceID != "redis" {
t.Fatalf("missing service association for check: %v", check)
}
}
func TestAgent_Join(t *testing.T) { func TestAgent_Join(t *testing.T) {
c, s := makeClient(t) c, s := makeClient(t)
defer s.stop() defer s.stop()