2013-12-23 21:52:10 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2014-11-24 08:36:03 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2013-12-23 21:52:10 +00:00
|
|
|
"fmt"
|
2013-12-30 22:42:41 +00:00
|
|
|
"io"
|
2013-12-23 21:52:10 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-09-18 05:31:32 +00:00
|
|
|
"path/filepath"
|
2014-11-25 03:24:32 +00:00
|
|
|
"reflect"
|
2013-12-23 21:52:10 +00:00
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2014-11-19 19:51:25 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/consul"
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
|
|
|
"github.com/hashicorp/consul/testutil"
|
2013-12-23 21:52:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var offset uint64
|
|
|
|
|
|
|
|
func nextConfig() *Config {
|
2014-04-11 22:46:55 +00:00
|
|
|
idx := int(atomic.AddUint64(&offset, 1))
|
2013-12-23 21:52:10 +00:00
|
|
|
conf := DefaultConfig()
|
|
|
|
|
2014-01-21 00:22:59 +00:00
|
|
|
conf.AdvertiseAddr = "127.0.0.1"
|
2013-12-25 00:53:30 +00:00
|
|
|
conf.Bootstrap = true
|
2013-12-24 00:20:51 +00:00
|
|
|
conf.Datacenter = "dc1"
|
2013-12-30 22:42:41 +00:00
|
|
|
conf.NodeName = fmt.Sprintf("Node %d", idx)
|
2014-04-11 22:46:55 +00:00
|
|
|
conf.BindAddr = "127.0.0.1"
|
2014-11-19 19:51:25 +00:00
|
|
|
conf.Ports.DNS = 19000 + idx
|
|
|
|
conf.Ports.HTTP = 18800 + idx
|
|
|
|
conf.Ports.RPC = 18600 + idx
|
2014-04-11 22:46:55 +00:00
|
|
|
conf.Ports.SerfLan = 18200 + idx
|
2014-11-19 19:51:25 +00:00
|
|
|
conf.Ports.SerfWan = 18400 + idx
|
|
|
|
conf.Ports.Server = 18000 + idx
|
2013-12-23 21:52:10 +00:00
|
|
|
conf.Server = true
|
2014-08-12 21:48:36 +00:00
|
|
|
conf.ACLDatacenter = "dc1"
|
2014-08-19 21:28:49 +00:00
|
|
|
conf.ACLMasterToken = "root"
|
2013-12-23 21:52:10 +00:00
|
|
|
|
|
|
|
cons := consul.DefaultConfig()
|
|
|
|
conf.ConsulConfig = cons
|
|
|
|
|
2014-01-06 21:21:48 +00:00
|
|
|
cons.SerfLANConfig.MemberlistConfig.SuspicionMult = 3
|
2013-12-30 22:42:41 +00:00
|
|
|
cons.SerfLANConfig.MemberlistConfig.ProbeTimeout = 100 * time.Millisecond
|
|
|
|
cons.SerfLANConfig.MemberlistConfig.ProbeInterval = 100 * time.Millisecond
|
2013-12-23 21:52:10 +00:00
|
|
|
cons.SerfLANConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond
|
|
|
|
|
2014-01-06 21:21:48 +00:00
|
|
|
cons.SerfWANConfig.MemberlistConfig.SuspicionMult = 3
|
2013-12-30 22:42:41 +00:00
|
|
|
cons.SerfWANConfig.MemberlistConfig.ProbeTimeout = 100 * time.Millisecond
|
|
|
|
cons.SerfWANConfig.MemberlistConfig.ProbeInterval = 100 * time.Millisecond
|
2013-12-23 21:52:10 +00:00
|
|
|
cons.SerfWANConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond
|
|
|
|
|
2014-05-30 18:57:39 +00:00
|
|
|
cons.RaftConfig.LeaderLeaseTimeout = 20 * time.Millisecond
|
2013-12-23 21:52:10 +00:00
|
|
|
cons.RaftConfig.HeartbeatTimeout = 40 * time.Millisecond
|
|
|
|
cons.RaftConfig.ElectionTimeout = 40 * time.Millisecond
|
|
|
|
|
|
|
|
return conf
|
|
|
|
}
|
|
|
|
|
2013-12-30 22:42:41 +00:00
|
|
|
func makeAgentLog(t *testing.T, conf *Config, l io.Writer) (string, *Agent) {
|
2013-12-23 21:52:10 +00:00
|
|
|
dir, err := ioutil.TempDir("", "agent")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(fmt.Sprintf("err: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.DataDir = dir
|
2013-12-30 22:42:41 +00:00
|
|
|
agent, err := Create(conf, l)
|
2013-12-23 21:52:10 +00:00
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
t.Fatalf(fmt.Sprintf("err: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir, agent
|
|
|
|
}
|
|
|
|
|
2014-09-18 05:31:32 +00:00
|
|
|
func makeAgentKeyring(t *testing.T, conf *Config, key string) (string, *Agent) {
|
|
|
|
dir, err := ioutil.TempDir("", "agent")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.DataDir = dir
|
|
|
|
|
2014-10-04 20:43:10 +00:00
|
|
|
fileLAN := filepath.Join(dir, serfLANKeyring)
|
2014-10-10 18:13:30 +00:00
|
|
|
if err := initKeyring(fileLAN, key); err != nil {
|
2014-09-18 05:31:32 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-10-04 20:43:10 +00:00
|
|
|
fileWAN := filepath.Join(dir, serfWANKeyring)
|
2014-10-10 18:13:30 +00:00
|
|
|
if err := initKeyring(fileWAN, key); err != nil {
|
2014-09-18 05:31:32 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
agent, err := Create(conf, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir, agent
|
|
|
|
}
|
|
|
|
|
2013-12-30 22:42:41 +00:00
|
|
|
func makeAgent(t *testing.T, conf *Config) (string, *Agent) {
|
|
|
|
return makeAgentLog(t, conf, nil)
|
|
|
|
}
|
|
|
|
|
2013-12-23 21:52:10 +00:00
|
|
|
func TestAgentStartStop(t *testing.T) {
|
|
|
|
dir, agent := makeAgent(t, nextConfig())
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
if err := agent.Leave(); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if err := agent.Shutdown(); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-agent.ShutdownCh():
|
|
|
|
default:
|
|
|
|
t.Fatalf("should be closed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 01:24:15 +00:00
|
|
|
func TestAgent_RPCPing(t *testing.T) {
|
2013-12-23 21:52:10 +00:00
|
|
|
dir, agent := makeAgent(t, nextConfig())
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := agent.RPC("Status.Ping", struct{}{}, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2014-01-30 21:39:02 +00:00
|
|
|
|
|
|
|
func TestAgent_AddService(t *testing.T) {
|
|
|
|
dir, agent := makeAgent(t, nextConfig())
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
srv := &structs.NodeService{
|
|
|
|
ID: "redis",
|
|
|
|
Service: "redis",
|
2014-04-03 19:12:23 +00:00
|
|
|
Tags: []string{"foo"},
|
2014-01-30 21:39:02 +00:00
|
|
|
Port: 8000,
|
|
|
|
}
|
2014-11-07 02:24:04 +00:00
|
|
|
chk := &CheckType{
|
|
|
|
TTL: time.Minute,
|
|
|
|
Notes: "redis health check",
|
|
|
|
}
|
2014-11-25 03:24:32 +00:00
|
|
|
err := agent.AddService(srv, chk, false)
|
2014-01-30 21:39:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a state mapping
|
|
|
|
if _, ok := agent.state.Services()["redis"]; !ok {
|
|
|
|
t.Fatalf("missing redis service")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
|
|
|
if _, ok := agent.state.Checks()["service:redis"]; !ok {
|
|
|
|
t.Fatalf("missing redis check")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a TTL is setup
|
|
|
|
if _, ok := agent.checkTTLs["service:redis"]; !ok {
|
|
|
|
t.Fatalf("missing redis check ttl")
|
|
|
|
}
|
2014-11-07 02:24:04 +00:00
|
|
|
|
|
|
|
// Ensure the notes are passed through
|
|
|
|
if agent.state.Checks()["service:redis"].Notes == "" {
|
|
|
|
t.Fatalf("missing redis check notes")
|
|
|
|
}
|
2014-01-30 21:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_RemoveService(t *testing.T) {
|
|
|
|
dir, agent := makeAgent(t, nextConfig())
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
// Remove a service that doesn't exist
|
2014-11-26 07:58:02 +00:00
|
|
|
if err := agent.RemoveService("redis", false); err != nil {
|
2014-01-30 21:39:02 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-10-14 22:05:41 +00:00
|
|
|
// Remove the consul service
|
2014-11-26 07:58:02 +00:00
|
|
|
if err := agent.RemoveService("consul", false); err == nil {
|
2014-10-14 22:05:41 +00:00
|
|
|
t.Fatalf("should have errored")
|
|
|
|
}
|
|
|
|
|
2014-01-30 21:39:02 +00:00
|
|
|
srv := &structs.NodeService{
|
|
|
|
ID: "redis",
|
|
|
|
Service: "redis",
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
chk := &CheckType{TTL: time.Minute}
|
2014-11-25 03:24:32 +00:00
|
|
|
if err := agent.AddService(srv, chk, false); err != nil {
|
2014-01-30 21:39:02 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the service
|
2014-11-26 07:58:02 +00:00
|
|
|
if err := agent.RemoveService("redis", false); err != nil {
|
2014-01-30 21:39:02 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a state mapping
|
|
|
|
if _, ok := agent.state.Services()["redis"]; ok {
|
|
|
|
t.Fatalf("have redis service")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
|
|
|
if _, ok := agent.state.Checks()["service:redis"]; ok {
|
|
|
|
t.Fatalf("have redis check")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a TTL is setup
|
|
|
|
if _, ok := agent.checkTTLs["service:redis"]; ok {
|
|
|
|
t.Fatalf("have redis check ttl")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_AddCheck(t *testing.T) {
|
|
|
|
dir, agent := makeAgent(t, nextConfig())
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
health := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "mem",
|
|
|
|
Name: "memory util",
|
2014-10-15 17:14:46 +00:00
|
|
|
Status: structs.HealthCritical,
|
2014-01-30 21:39:02 +00:00
|
|
|
}
|
|
|
|
chk := &CheckType{
|
|
|
|
Script: "exit 0",
|
|
|
|
Interval: 15 * time.Second,
|
|
|
|
}
|
2014-11-25 03:24:32 +00:00
|
|
|
err := agent.AddCheck(health, chk, false)
|
2014-01-30 21:39:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
|
|
|
if _, ok := agent.state.Checks()["mem"]; !ok {
|
|
|
|
t.Fatalf("missing mem check")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a TTL is setup
|
|
|
|
if _, ok := agent.checkMonitors["mem"]; !ok {
|
|
|
|
t.Fatalf("missing mem monitor")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-21 21:42:42 +00:00
|
|
|
func TestAgent_AddCheck_MinInterval(t *testing.T) {
|
|
|
|
dir, agent := makeAgent(t, nextConfig())
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
health := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "mem",
|
|
|
|
Name: "memory util",
|
2014-10-15 17:14:46 +00:00
|
|
|
Status: structs.HealthCritical,
|
2014-04-21 21:42:42 +00:00
|
|
|
}
|
|
|
|
chk := &CheckType{
|
|
|
|
Script: "exit 0",
|
|
|
|
Interval: time.Microsecond,
|
|
|
|
}
|
2014-11-25 03:24:32 +00:00
|
|
|
err := agent.AddCheck(health, chk, false)
|
2014-04-21 21:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
|
|
|
if _, ok := agent.state.Checks()["mem"]; !ok {
|
|
|
|
t.Fatalf("missing mem check")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a TTL is setup
|
|
|
|
if mon, ok := agent.checkMonitors["mem"]; !ok {
|
|
|
|
t.Fatalf("missing mem monitor")
|
|
|
|
} else if mon.Interval != MinInterval {
|
|
|
|
t.Fatalf("bad mem monitor interval")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-30 21:39:02 +00:00
|
|
|
func TestAgent_RemoveCheck(t *testing.T) {
|
|
|
|
dir, agent := makeAgent(t, nextConfig())
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
// Remove check that doesn't exist
|
2014-11-26 07:58:02 +00:00
|
|
|
if err := agent.RemoveCheck("mem", false); err != nil {
|
2014-01-30 21:39:02 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
health := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "mem",
|
|
|
|
Name: "memory util",
|
2014-10-15 17:14:46 +00:00
|
|
|
Status: structs.HealthCritical,
|
2014-01-30 21:39:02 +00:00
|
|
|
}
|
|
|
|
chk := &CheckType{
|
|
|
|
Script: "exit 0",
|
|
|
|
Interval: 15 * time.Second,
|
|
|
|
}
|
2014-11-25 03:24:32 +00:00
|
|
|
err := agent.AddCheck(health, chk, false)
|
2014-01-30 21:39:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove check
|
2014-11-26 07:58:02 +00:00
|
|
|
if err := agent.RemoveCheck("mem", false); err != nil {
|
2014-01-30 21:39:02 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
|
|
|
if _, ok := agent.state.Checks()["mem"]; ok {
|
|
|
|
t.Fatalf("have mem check")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a TTL is setup
|
|
|
|
if _, ok := agent.checkMonitors["mem"]; ok {
|
|
|
|
t.Fatalf("have mem monitor")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_UpdateCheck(t *testing.T) {
|
|
|
|
dir, agent := makeAgent(t, nextConfig())
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
health := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "mem",
|
|
|
|
Name: "memory util",
|
2014-10-15 17:14:46 +00:00
|
|
|
Status: structs.HealthCritical,
|
2014-01-30 21:39:02 +00:00
|
|
|
}
|
|
|
|
chk := &CheckType{
|
|
|
|
TTL: 15 * time.Second,
|
|
|
|
}
|
2014-11-25 03:24:32 +00:00
|
|
|
err := agent.AddCheck(health, chk, false)
|
2014-01-30 21:39:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove check
|
|
|
|
if err := agent.UpdateCheck("mem", structs.HealthPassing, "foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
|
|
|
status := agent.state.Checks()["mem"]
|
|
|
|
if status.Status != structs.HealthPassing {
|
|
|
|
t.Fatalf("bad: %v", status)
|
|
|
|
}
|
2014-04-21 23:20:22 +00:00
|
|
|
if status.Output != "foo" {
|
2014-01-30 21:39:02 +00:00
|
|
|
t.Fatalf("bad: %v", status)
|
|
|
|
}
|
|
|
|
}
|
2014-10-14 22:05:41 +00:00
|
|
|
|
|
|
|
func TestAgent_ConsulService(t *testing.T) {
|
|
|
|
dir, agent := makeAgent(t, nextConfig())
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
2014-10-17 21:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, agent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Consul service is registered
|
2014-10-14 22:05:41 +00:00
|
|
|
services := agent.state.Services()
|
2014-10-14 22:42:49 +00:00
|
|
|
if _, ok := services[consul.ConsulServiceID]; !ok {
|
|
|
|
t.Fatalf("%s service should be registered", consul.ConsulServiceID)
|
2014-10-14 22:05:41 +00:00
|
|
|
}
|
2014-10-17 21:43:52 +00:00
|
|
|
|
|
|
|
// Perform anti-entropy on consul service
|
|
|
|
if err := agent.state.syncService(consul.ConsulServiceID); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consul service should be in sync
|
|
|
|
if !agent.state.serviceStatus[consul.ConsulServiceID].inSync {
|
|
|
|
t.Fatalf("%s service should be in sync", consul.ConsulServiceID)
|
|
|
|
}
|
2014-10-14 22:05:41 +00:00
|
|
|
}
|
2014-11-24 08:36:03 +00:00
|
|
|
|
|
|
|
func TestAgent_PersistService(t *testing.T) {
|
|
|
|
config := nextConfig()
|
2014-12-05 18:36:01 +00:00
|
|
|
config.Server = false
|
2014-11-24 08:36:03 +00:00
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
2014-12-05 18:36:01 +00:00
|
|
|
defer agent.Shutdown()
|
2014-11-24 08:36:03 +00:00
|
|
|
|
|
|
|
svc := &structs.NodeService{
|
|
|
|
ID: "redis",
|
|
|
|
Service: "redis",
|
|
|
|
Tags: []string{"foo"},
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
|
2015-01-08 03:11:21 +00:00
|
|
|
file := filepath.Join(agent.config.DataDir, servicesDir, stringHash(svc.ID))
|
2014-11-25 03:24:32 +00:00
|
|
|
|
|
|
|
// Check is not persisted unless requested
|
|
|
|
if err := agent.AddService(svc, nil, false); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(file); err == nil {
|
|
|
|
t.Fatalf("should not persist")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Persists to file if requested
|
|
|
|
if err := agent.AddService(svc, nil, true); err != nil {
|
2014-11-24 08:36:03 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(file); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected, err := json.Marshal(svc)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
content, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(expected, content) {
|
|
|
|
t.Fatalf("bad: %s", string(content))
|
|
|
|
}
|
|
|
|
agent.Shutdown()
|
|
|
|
|
|
|
|
// Should load it back during later start
|
|
|
|
agent2, err := Create(config, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer agent2.Shutdown()
|
|
|
|
|
|
|
|
if _, ok := agent2.state.services[svc.ID]; !ok {
|
|
|
|
t.Fatalf("bad: %#v", agent2.state.services)
|
|
|
|
}
|
2014-11-26 07:58:02 +00:00
|
|
|
}
|
2014-11-24 08:36:03 +00:00
|
|
|
|
2014-11-26 07:58:02 +00:00
|
|
|
func TestAgent_PurgeService(t *testing.T) {
|
|
|
|
config := nextConfig()
|
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
2014-12-05 18:36:01 +00:00
|
|
|
defer agent.Shutdown()
|
2014-11-26 07:58:02 +00:00
|
|
|
|
|
|
|
svc := &structs.NodeService{
|
|
|
|
ID: "redis",
|
|
|
|
Service: "redis",
|
|
|
|
Tags: []string{"foo"},
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
|
2015-01-08 03:11:21 +00:00
|
|
|
file := filepath.Join(agent.config.DataDir, servicesDir, stringHash(svc.ID))
|
2014-11-26 07:58:02 +00:00
|
|
|
if err := agent.AddService(svc, nil, true); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not removed
|
|
|
|
if err := agent.RemoveService(svc.ID, false); err != nil {
|
2014-11-24 08:36:03 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-11-26 07:58:02 +00:00
|
|
|
if _, err := os.Stat(file); err != nil {
|
2014-11-24 08:36:03 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-11-26 07:58:02 +00:00
|
|
|
|
|
|
|
// Removed
|
|
|
|
if err := agent.RemoveService(svc.ID, true); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(file); !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
2014-11-24 08:36:03 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 03:24:32 +00:00
|
|
|
func TestAgent_PurgeServiceOnDuplicate(t *testing.T) {
|
|
|
|
config := nextConfig()
|
2014-12-05 18:36:01 +00:00
|
|
|
config.Server = false
|
2014-11-25 03:24:32 +00:00
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
2014-12-05 18:36:01 +00:00
|
|
|
defer agent.Shutdown()
|
2014-11-25 03:24:32 +00:00
|
|
|
|
|
|
|
svc1 := &structs.NodeService{
|
|
|
|
ID: "redis",
|
|
|
|
Service: "redis",
|
|
|
|
Tags: []string{"foo"},
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
|
|
|
|
// First persist the service
|
|
|
|
if err := agent.AddService(svc1, nil, true); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
agent.Shutdown()
|
|
|
|
|
|
|
|
// Try bringing the agent back up with the service already
|
|
|
|
// existing in the config
|
|
|
|
svc2 := &ServiceDefinition{
|
|
|
|
ID: "redis",
|
|
|
|
Name: "redis",
|
|
|
|
Tags: []string{"bar"},
|
|
|
|
Port: 9000,
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Services = []*ServiceDefinition{svc2}
|
|
|
|
agent2, err := Create(config, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer agent2.Shutdown()
|
|
|
|
|
2015-01-08 03:11:21 +00:00
|
|
|
file := filepath.Join(agent.config.DataDir, servicesDir, stringHash(svc1.ID))
|
2014-11-25 03:24:32 +00:00
|
|
|
if _, err := os.Stat(file); err == nil {
|
|
|
|
t.Fatalf("should have removed persisted service")
|
|
|
|
}
|
|
|
|
result, ok := agent2.state.services[svc2.ID]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing service registration")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(result.Tags, svc2.Tags) || result.Port != svc2.Port {
|
|
|
|
t.Fatalf("bad: %#v", result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-24 08:36:03 +00:00
|
|
|
func TestAgent_PersistCheck(t *testing.T) {
|
|
|
|
config := nextConfig()
|
2014-12-05 18:36:01 +00:00
|
|
|
config.Server = false
|
2014-11-24 08:36:03 +00:00
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
2014-12-05 18:36:01 +00:00
|
|
|
defer agent.Shutdown()
|
2014-11-24 08:36:03 +00:00
|
|
|
|
|
|
|
check := &structs.HealthCheck{
|
|
|
|
Node: config.NodeName,
|
|
|
|
CheckID: "service:redis1",
|
|
|
|
Name: "redischeck",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "redis",
|
|
|
|
ServiceName: "redis",
|
|
|
|
}
|
2014-11-29 20:25:01 +00:00
|
|
|
chkType := &CheckType{
|
|
|
|
Script: "/bin/true",
|
|
|
|
Interval: 10 * time.Second,
|
|
|
|
}
|
2014-11-24 08:36:03 +00:00
|
|
|
|
2015-01-08 03:11:21 +00:00
|
|
|
file := filepath.Join(agent.config.DataDir, checksDir, stringHash(check.CheckID))
|
2014-11-25 03:24:32 +00:00
|
|
|
|
|
|
|
// Not persisted if not requested
|
2014-11-29 20:25:01 +00:00
|
|
|
if err := agent.AddCheck(check, chkType, false); err != nil {
|
2014-11-25 03:24:32 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(file); err == nil {
|
|
|
|
t.Fatalf("should not persist")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should persist if requested
|
2014-11-29 20:25:01 +00:00
|
|
|
if err := agent.AddCheck(check, chkType, true); err != nil {
|
2014-11-24 08:36:03 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(file); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-11-29 20:25:01 +00:00
|
|
|
p := persistedCheck{check, chkType}
|
|
|
|
expected, err := json.Marshal(p)
|
2014-11-24 08:36:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
content, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(expected, content) {
|
|
|
|
t.Fatalf("bad: %s", string(content))
|
|
|
|
}
|
|
|
|
agent.Shutdown()
|
|
|
|
|
|
|
|
// Should load it back during later start
|
|
|
|
agent2, err := Create(config, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer agent2.Shutdown()
|
|
|
|
|
2014-11-29 20:25:01 +00:00
|
|
|
result, ok := agent2.state.checks[p.Check.CheckID]
|
2014-11-24 09:15:18 +00:00
|
|
|
if !ok {
|
2014-11-24 08:36:03 +00:00
|
|
|
t.Fatalf("bad: %#v", agent2.state.checks)
|
|
|
|
}
|
2014-11-24 09:15:18 +00:00
|
|
|
if result.Status != structs.HealthCritical {
|
|
|
|
t.Fatalf("bad: %#v", result)
|
|
|
|
}
|
2014-11-29 20:25:01 +00:00
|
|
|
|
|
|
|
// Should have restored the monitor
|
|
|
|
if _, ok := agent2.checkMonitors[p.Check.CheckID]; !ok {
|
|
|
|
t.Fatalf("bad: %#v", agent2.checkMonitors)
|
|
|
|
}
|
2014-11-26 07:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_PurgeCheck(t *testing.T) {
|
|
|
|
config := nextConfig()
|
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
2014-12-05 18:36:01 +00:00
|
|
|
defer agent.Shutdown()
|
2014-11-26 07:58:02 +00:00
|
|
|
|
|
|
|
check := &structs.HealthCheck{
|
|
|
|
Node: config.NodeName,
|
|
|
|
CheckID: "service:redis1",
|
|
|
|
Name: "redischeck",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "redis",
|
|
|
|
ServiceName: "redis",
|
|
|
|
}
|
2014-11-24 08:36:03 +00:00
|
|
|
|
2015-01-08 03:11:21 +00:00
|
|
|
file := filepath.Join(agent.config.DataDir, checksDir, stringHash(check.CheckID))
|
2014-11-26 07:58:02 +00:00
|
|
|
if err := agent.AddCheck(check, nil, true); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not removed
|
|
|
|
if err := agent.RemoveCheck(check.CheckID, false); err != nil {
|
2014-11-24 08:36:03 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-11-26 07:58:02 +00:00
|
|
|
if _, err := os.Stat(file); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removed
|
|
|
|
if err := agent.RemoveCheck(check.CheckID, true); err != nil {
|
2014-11-24 08:36:03 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-11-26 07:58:02 +00:00
|
|
|
if _, err := os.Stat(file); !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
2014-11-24 08:36:03 +00:00
|
|
|
}
|
2014-11-25 03:24:32 +00:00
|
|
|
|
|
|
|
func TestAgent_PurgeCheckOnDuplicate(t *testing.T) {
|
|
|
|
config := nextConfig()
|
2014-12-05 18:36:01 +00:00
|
|
|
config.Server = false
|
2014-11-25 03:24:32 +00:00
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
2014-12-05 18:36:01 +00:00
|
|
|
defer agent.Shutdown()
|
2014-11-25 03:24:32 +00:00
|
|
|
|
|
|
|
check1 := &structs.HealthCheck{
|
|
|
|
Node: config.NodeName,
|
|
|
|
CheckID: "service:redis1",
|
|
|
|
Name: "redischeck",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "redis",
|
|
|
|
ServiceName: "redis",
|
|
|
|
}
|
|
|
|
|
|
|
|
// First persist the check
|
|
|
|
if err := agent.AddCheck(check1, nil, true); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
agent.Shutdown()
|
|
|
|
|
|
|
|
// Start again with the check registered in config
|
|
|
|
check2 := &CheckDefinition{
|
|
|
|
ID: "service:redis1",
|
|
|
|
Name: "redischeck",
|
|
|
|
Notes: "my cool notes",
|
|
|
|
CheckType: CheckType{
|
|
|
|
Script: "/bin/check-redis.py",
|
|
|
|
Interval: 30 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Checks = []*CheckDefinition{check2}
|
|
|
|
agent2, err := Create(config, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer agent2.Shutdown()
|
|
|
|
|
2015-01-08 03:11:21 +00:00
|
|
|
file := filepath.Join(agent.config.DataDir, checksDir, stringHash(check1.CheckID))
|
2014-11-25 03:24:32 +00:00
|
|
|
if _, err := os.Stat(file); err == nil {
|
|
|
|
t.Fatalf("should have removed persisted check")
|
|
|
|
}
|
|
|
|
result, ok := agent2.state.checks[check2.ID]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing check registration")
|
|
|
|
}
|
|
|
|
expected := check2.HealthCheck(config.NodeName)
|
|
|
|
if !reflect.DeepEqual(expected, result) {
|
|
|
|
t.Fatalf("bad: %#v", result)
|
|
|
|
}
|
|
|
|
}
|
2015-01-08 06:26:40 +00:00
|
|
|
|
|
|
|
func TestAgent_unloadChecks(t *testing.T) {
|
|
|
|
config := nextConfig()
|
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
check1 := &structs.HealthCheck{
|
|
|
|
Node: config.NodeName,
|
|
|
|
CheckID: "service:redis1",
|
|
|
|
Name: "redischeck",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "redis",
|
|
|
|
ServiceName: "redis",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the check
|
|
|
|
if err := agent.AddCheck(check1, nil, false); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
for check, _ := range agent.state.Checks() {
|
|
|
|
if check == check1.CheckID {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("check should have been registered")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unload all of the checks
|
|
|
|
if err := agent.unloadChecks(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it was unloaded
|
|
|
|
for check, _ := range agent.state.Checks() {
|
|
|
|
if check == check1.CheckID {
|
|
|
|
t.Fatalf("should have unloaded checks")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_unloadServices(t *testing.T) {
|
|
|
|
config := nextConfig()
|
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
svc := &structs.NodeService{
|
|
|
|
ID: "redis",
|
|
|
|
Service: "redis",
|
|
|
|
Tags: []string{"foo"},
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the service
|
|
|
|
if err := agent.AddService(svc, nil, false); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
for id, _ := range agent.state.Services() {
|
|
|
|
if id == svc.ID {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("should have registered service")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unload all services
|
|
|
|
if err := agent.unloadServices(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it was unloaded and the consul service remains
|
|
|
|
found = false
|
|
|
|
for id, _ := range agent.state.Services() {
|
|
|
|
if id == svc.ID {
|
|
|
|
t.Fatalf("should have unloaded services")
|
|
|
|
}
|
|
|
|
if id == consul.ConsulServiceID {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("consul service should not be removed")
|
|
|
|
}
|
|
|
|
}
|
2015-01-15 08:25:36 +00:00
|
|
|
|
2015-01-15 19:26:14 +00:00
|
|
|
func TestAgent_ServiceMaintenanceMode(t *testing.T) {
|
2015-01-15 08:25:36 +00:00
|
|
|
config := nextConfig()
|
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
svc := &structs.NodeService{
|
|
|
|
ID: "redis",
|
|
|
|
Service: "redis",
|
|
|
|
Tags: []string{"foo"},
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the service
|
|
|
|
if err := agent.AddService(svc, nil, false); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enter maintenance mode for the service
|
|
|
|
if err := agent.EnableServiceMaintenance("redis"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the critical health check was added
|
2015-01-15 19:26:14 +00:00
|
|
|
if _, ok := agent.state.Checks()[serviceMaintCheckID]; !ok {
|
|
|
|
t.Fatalf("should have registered critical maintenance check")
|
2015-01-15 08:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Leave maintenance mode
|
|
|
|
if err := agent.DisableServiceMaintenance("redis"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the check was deregistered
|
2015-01-15 19:26:14 +00:00
|
|
|
if _, ok := agent.state.Checks()[serviceMaintCheckID]; ok {
|
|
|
|
t.Fatalf("should have deregistered maintenance check")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_NodeMaintenanceMode(t *testing.T) {
|
|
|
|
config := nextConfig()
|
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
// Enter maintenance mode for the node
|
|
|
|
agent.EnableNodeMaintenance()
|
|
|
|
|
|
|
|
// Make sure the critical health check was added
|
|
|
|
if _, ok := agent.state.Checks()[nodeMaintCheckID]; !ok {
|
|
|
|
t.Fatalf("should have registered critical node check")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Leave maintenance mode
|
|
|
|
agent.DisableNodeMaintenance()
|
|
|
|
|
|
|
|
// Ensure the check was deregistered
|
|
|
|
if _, ok := agent.state.Checks()[nodeMaintCheckID]; ok {
|
|
|
|
t.Fatalf("should have deregistered critical node check")
|
2015-01-15 08:25:36 +00:00
|
|
|
}
|
|
|
|
}
|