2015-01-06 18:40:00 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-01-21 19:08:57 +00:00
|
|
|
"strings"
|
2015-01-06 18:40:00 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAgent_Self(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
info, err := agent.Self()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
name := info["Config"]["NodeName"]
|
|
|
|
if name == "" {
|
|
|
|
t.Fatalf("bad: %v", info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_Members(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
members, err := agent.Members(false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(members) != 1 {
|
|
|
|
t.Fatalf("bad: %v", members)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_Services(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
reg := &AgentServiceRegistration{
|
2015-01-22 21:42:22 +00:00
|
|
|
Name: "foo",
|
|
|
|
Tags: []string{"bar", "baz"},
|
|
|
|
Port: 8000,
|
2015-01-06 18:40:00 +00:00
|
|
|
Check: &AgentServiceCheck{
|
|
|
|
TTL: "15s",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-12 00:53:48 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
chk, ok := checks["service:foo"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing check: %v", checks)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks should default to critical
|
|
|
|
if chk.Status != "critical" {
|
|
|
|
t.Fatalf("Bad: %#v", chk)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := agent.ServiceDeregister("foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_Services_CheckPassing(t *testing.T) {
|
2015-05-12 01:53:09 +00:00
|
|
|
t.Parallel()
|
2015-04-12 00:53:48 +00:00
|
|
|
c, s := makeClient(t)
|
2015-05-12 01:53:09 +00:00
|
|
|
defer s.Stop()
|
2015-04-12 00:53:48 +00:00
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
Tags: []string{"bar", "baz"},
|
|
|
|
Port: 8000,
|
|
|
|
Check: &AgentServiceCheck{
|
|
|
|
TTL: "15s",
|
|
|
|
Status: "passing",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
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)
|
|
|
|
}
|
2015-04-12 00:53:48 +00:00
|
|
|
chk, ok := checks["service:foo"]
|
|
|
|
if !ok {
|
2015-01-06 18:40:00 +00:00
|
|
|
t.Fatalf("missing check: %v", checks)
|
|
|
|
}
|
|
|
|
|
2015-04-12 00:53:48 +00:00
|
|
|
if chk.Status != "passing" {
|
|
|
|
t.Fatalf("Bad: %#v", chk)
|
|
|
|
}
|
2015-01-06 18:40:00 +00:00
|
|
|
if err := agent.ServiceDeregister("foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-12 00:53:48 +00:00
|
|
|
func TestAgent_Services_CheckBadStatus(t *testing.T) {
|
2015-05-12 01:53:09 +00:00
|
|
|
t.Parallel()
|
2015-04-12 00:53:48 +00:00
|
|
|
c, s := makeClient(t)
|
2015-05-12 01:53:09 +00:00
|
|
|
defer s.Stop()
|
2015-04-12 00:53:48 +00:00
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
Tags: []string{"bar", "baz"},
|
|
|
|
Port: 8000,
|
|
|
|
Check: &AgentServiceCheck{
|
|
|
|
TTL: "15s",
|
|
|
|
Status: "fluffy",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err == nil {
|
|
|
|
t.Fatalf("bad status accepted")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 10:50:20 +00:00
|
|
|
func TestAgent_ServiceAddress(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-22 10:50:20 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-22 10:50:20 +00:00
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
reg1 := &AgentServiceRegistration{
|
|
|
|
Name: "foo1",
|
|
|
|
Port: 8000,
|
|
|
|
Address: "192.168.0.42",
|
|
|
|
}
|
|
|
|
reg2 := &AgentServiceRegistration{
|
2015-01-22 21:42:22 +00:00
|
|
|
Name: "foo2",
|
|
|
|
Port: 8000,
|
2015-01-22 10:50:20 +00:00
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg1); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg2); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
services, err := agent.Services()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-01-22 21:42:22 +00:00
|
|
|
|
2015-01-22 10:50:20 +00:00
|
|
|
if _, ok := services["foo1"]; !ok {
|
|
|
|
t.Fatalf("missing service: %v", services)
|
|
|
|
}
|
|
|
|
if _, ok := services["foo2"]; !ok {
|
|
|
|
t.Fatalf("missing service: %v", services)
|
|
|
|
}
|
|
|
|
|
|
|
|
if services["foo1"].Address != "192.168.0.42" {
|
|
|
|
t.Fatalf("missing Address field in service foo1: %v", services)
|
|
|
|
}
|
|
|
|
if services["foo2"].Address != "" {
|
|
|
|
t.Fatalf("missing Address field in service foo2: %v", services)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := agent.ServiceDeregister("foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-16 19:45:29 +00:00
|
|
|
func TestAgent_EnableTagOverride(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
reg1 := &AgentServiceRegistration{
|
|
|
|
Name: "foo1",
|
|
|
|
Port: 8000,
|
|
|
|
Address: "192.168.0.42",
|
|
|
|
EnableTagOverride: true,
|
|
|
|
}
|
|
|
|
reg2 := &AgentServiceRegistration{
|
|
|
|
Name: "foo2",
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg1); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg2); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
services, err := agent.Services()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := services["foo1"]; !ok {
|
|
|
|
t.Fatalf("missing service: %v", services)
|
|
|
|
}
|
|
|
|
if services["foo1"].EnableTagOverride != true {
|
|
|
|
t.Fatalf("tag override not set on service foo1: %v", services)
|
|
|
|
}
|
|
|
|
if _, ok := services["foo2"]; !ok {
|
|
|
|
t.Fatalf("missing service: %v", services)
|
|
|
|
}
|
|
|
|
if services["foo2"].EnableTagOverride != false {
|
|
|
|
t.Fatalf("tag override set on service foo2: %v", services)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 03:18:46 +00:00
|
|
|
func TestAgent_Services_MultipleChecks(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-14 03:18:46 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-14 03:18:46 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
func TestAgent_SetTTLStatus(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
Check: &AgentServiceCheck{
|
|
|
|
TTL: "15s",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-03-04 23:18:25 +00:00
|
|
|
verify := func(status, output string) {
|
|
|
|
checks, err := agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
chk, ok := checks["service:foo"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing check: %v", checks)
|
|
|
|
}
|
|
|
|
if chk.Status != status {
|
|
|
|
t.Fatalf("Bad: %#v", chk)
|
|
|
|
}
|
|
|
|
if chk.Output != output {
|
|
|
|
t.Fatalf("Bad: %#v", chk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := agent.WarnTTL("service:foo", "foo"); err != nil {
|
2015-01-06 18:40:00 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-03-04 23:18:25 +00:00
|
|
|
verify("warning", "foo")
|
2015-01-06 18:40:00 +00:00
|
|
|
|
2016-03-04 23:18:25 +00:00
|
|
|
if err := agent.PassTTL("service:foo", "bar"); err != nil {
|
2015-01-06 18:40:00 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-03-04 23:18:25 +00:00
|
|
|
verify("passing", "bar")
|
|
|
|
|
|
|
|
if err := agent.FailTTL("service:foo", "baz"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
2016-03-04 23:18:25 +00:00
|
|
|
verify("critical", "baz")
|
|
|
|
|
|
|
|
if err := agent.UpdateTTL("service:foo", "foo", "warn"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
2016-03-04 23:18:25 +00:00
|
|
|
verify("warning", "foo")
|
|
|
|
|
|
|
|
if err := agent.UpdateTTL("service:foo", "bar", "pass"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
verify("passing", "bar")
|
|
|
|
|
|
|
|
if err := agent.UpdateTTL("service:foo", "baz", "fail"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
verify("critical", "baz")
|
|
|
|
|
|
|
|
if err := agent.UpdateTTL("service:foo", "foo", "warning"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
verify("warning", "foo")
|
|
|
|
|
|
|
|
if err := agent.UpdateTTL("service:foo", "bar", "passing"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
verify("passing", "bar")
|
|
|
|
|
|
|
|
if err := agent.UpdateTTL("service:foo", "baz", "critical"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
2016-03-04 23:18:25 +00:00
|
|
|
verify("critical", "baz")
|
2015-01-06 18:40:00 +00:00
|
|
|
|
|
|
|
if err := agent.ServiceDeregister("foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_Checks(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
reg := &AgentCheckRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
2015-04-12 00:53:48 +00:00
|
|
|
chk, ok := checks["foo"]
|
|
|
|
if !ok {
|
2015-01-06 18:40:00 +00:00
|
|
|
t.Fatalf("missing check: %v", checks)
|
|
|
|
}
|
2015-04-12 00:53:48 +00:00
|
|
|
if chk.Status != "critical" {
|
|
|
|
t.Fatalf("check not critical: %v", chk)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := agent.CheckDeregister("foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_CheckStartPassing(t *testing.T) {
|
2015-05-12 01:53:09 +00:00
|
|
|
t.Parallel()
|
2015-04-12 00:53:48 +00:00
|
|
|
c, s := makeClient(t)
|
2015-05-12 01:53:09 +00:00
|
|
|
defer s.Stop()
|
2015-04-12 00:53:48 +00:00
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
reg := &AgentCheckRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
AgentServiceCheck: AgentServiceCheck{
|
|
|
|
Status: "passing",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
chk, ok := checks["foo"]
|
|
|
|
if !ok {
|
2015-01-06 18:40:00 +00:00
|
|
|
t.Fatalf("missing check: %v", checks)
|
|
|
|
}
|
2015-04-12 00:53:48 +00:00
|
|
|
if chk.Status != "passing" {
|
|
|
|
t.Fatalf("check not passing: %v", chk)
|
|
|
|
}
|
2015-01-06 18:40:00 +00:00
|
|
|
|
|
|
|
if err := agent.CheckDeregister("foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 03:18:46 +00:00
|
|
|
func TestAgent_Checks_serviceBound(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-14 03:18:46 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-14 03:18:46 +00:00
|
|
|
|
|
|
|
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 {
|
2015-11-18 15:40:02 +00:00
|
|
|
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_Checks_Docker(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
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",
|
|
|
|
AgentServiceCheck: AgentServiceCheck{
|
|
|
|
DockerContainerID: "f972c95ebf0e",
|
|
|
|
Script: "/bin/true",
|
|
|
|
Shell: "/bin/bash",
|
|
|
|
Interval: "10s",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := agent.CheckRegister(reg); err != nil {
|
2015-01-14 03:18:46 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
func TestAgent_Join(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
info, err := agent.Self()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join ourself
|
|
|
|
addr := info["Config"]["AdvertiseAddr"].(string)
|
|
|
|
err = agent.Join(addr, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_ForceLeave(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
// Eject somebody
|
|
|
|
err := agent.ForceLeave("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2015-01-21 18:51:26 +00:00
|
|
|
|
|
|
|
func TestServiceMaintenance(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-21 18:51:26 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-21 18:51:26 +00:00
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
// First register a service
|
|
|
|
serviceReg := &AgentServiceRegistration{
|
|
|
|
Name: "redis",
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(serviceReg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable maintenance mode
|
2015-01-21 21:02:47 +00:00
|
|
|
if err := agent.EnableServiceMaintenance("redis", "broken"); err != nil {
|
2015-01-21 18:51:26 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a critical check was added
|
|
|
|
checks, err := agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
for _, check := range checks {
|
2015-01-21 19:08:57 +00:00
|
|
|
if strings.Contains(check.CheckID, "maintenance") {
|
2015-01-21 18:51:26 +00:00
|
|
|
found = true
|
2015-01-21 21:02:47 +00:00
|
|
|
if check.Status != "critical" || check.Notes != "broken" {
|
2015-01-21 18:51:26 +00:00
|
|
|
t.Fatalf("bad: %#v", checks)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("bad: %#v", checks)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable maintenance mode
|
|
|
|
if err := agent.DisableServiceMaintenance("redis"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the critical health check was removed
|
|
|
|
checks, err = agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
for _, check := range checks {
|
2015-01-21 19:08:57 +00:00
|
|
|
if strings.Contains(check.CheckID, "maintenance") {
|
|
|
|
t.Fatalf("should have removed health check")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeMaintenance(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-21 19:08:57 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-21 19:08:57 +00:00
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
// Enable maintenance mode
|
2015-01-21 21:02:47 +00:00
|
|
|
if err := agent.EnableNodeMaintenance("broken"); err != nil {
|
2015-01-21 19:08:57 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that a critical check was added
|
|
|
|
checks, err := agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
for _, check := range checks {
|
|
|
|
if strings.Contains(check.CheckID, "maintenance") {
|
|
|
|
found = true
|
2015-01-21 21:02:47 +00:00
|
|
|
if check.Status != "critical" || check.Notes != "broken" {
|
2015-01-21 19:08:57 +00:00
|
|
|
t.Fatalf("bad: %#v", checks)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("bad: %#v", checks)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable maintenance mode
|
|
|
|
if err := agent.DisableNodeMaintenance(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the check was removed
|
|
|
|
checks, err = agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
for _, check := range checks {
|
|
|
|
if strings.Contains(check.CheckID, "maintenance") {
|
2015-01-21 18:51:26 +00:00
|
|
|
t.Fatalf("should have removed health check")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|