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"
|
2015-06-05 11:44:42 +00:00
|
|
|
"net"
|
2013-12-23 21:52:10 +00:00
|
|
|
"os"
|
2014-09-18 05:31:32 +00:00
|
|
|
"path/filepath"
|
2014-11-25 03:24:32 +00:00
|
|
|
"reflect"
|
2017-01-24 02:11:13 +00:00
|
|
|
"strings"
|
2013-12-23 21:52:10 +00:00
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2014-11-19 19:51:25 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2014-11-19 19:51:25 +00:00
|
|
|
"github.com/hashicorp/consul/consul"
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2016-11-28 21:08:31 +00:00
|
|
|
"github.com/hashicorp/consul/logger"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2017-01-18 06:20:11 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2017-03-21 23:36:44 +00:00
|
|
|
"github.com/hashicorp/consul/version"
|
2017-01-18 06:20:11 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
2016-08-25 00:33:53 +00:00
|
|
|
"github.com/hashicorp/raft"
|
2013-12-23 21:52:10 +00:00
|
|
|
)
|
|
|
|
|
2016-06-20 21:33:46 +00:00
|
|
|
const (
|
|
|
|
basePortNumber = 10000
|
|
|
|
|
|
|
|
portOffsetDNS = iota
|
|
|
|
portOffsetHTTP
|
|
|
|
portOffsetSerfLan
|
|
|
|
portOffsetSerfWan
|
|
|
|
portOffsetServer
|
|
|
|
|
|
|
|
// Must be last in list
|
|
|
|
numPortsPerIndex
|
|
|
|
)
|
|
|
|
|
2017-03-21 23:36:44 +00:00
|
|
|
func init() {
|
|
|
|
version.Version = "0.8.0"
|
|
|
|
}
|
|
|
|
|
2016-06-20 21:33:46 +00:00
|
|
|
var offset uint64 = basePortNumber
|
2013-12-23 21:52:10 +00:00
|
|
|
|
|
|
|
func nextConfig() *Config {
|
2016-06-20 21:33:46 +00:00
|
|
|
idx := int(atomic.AddUint64(&offset, numPortsPerIndex))
|
2013-12-23 21:52:10 +00:00
|
|
|
conf := DefaultConfig()
|
|
|
|
|
2017-03-27 08:28:54 +00:00
|
|
|
nodeID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-03-21 23:36:44 +00:00
|
|
|
conf.Version = version.Version
|
2015-08-26 03:22:22 +00:00
|
|
|
conf.VersionPrerelease = "c.d"
|
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)
|
2017-03-27 08:28:54 +00:00
|
|
|
conf.NodeID = types.NodeID(nodeID)
|
2014-04-11 22:46:55 +00:00
|
|
|
conf.BindAddr = "127.0.0.1"
|
2016-06-20 21:33:46 +00:00
|
|
|
conf.Ports.DNS = basePortNumber + idx + portOffsetDNS
|
|
|
|
conf.Ports.HTTP = basePortNumber + idx + portOffsetHTTP
|
|
|
|
conf.Ports.SerfLan = basePortNumber + idx + portOffsetSerfLan
|
|
|
|
conf.Ports.SerfWan = basePortNumber + idx + portOffsetSerfWan
|
|
|
|
conf.Ports.Server = basePortNumber + idx + portOffsetServer
|
2013-12-23 21:52:10 +00:00
|
|
|
conf.Server = true
|
2016-12-14 07:21:14 +00:00
|
|
|
conf.ACLEnforceVersion8 = Bool(false)
|
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
|
|
|
|
|
2015-07-03 00:21:59 +00:00
|
|
|
cons.CoordinateUpdatePeriod = 100 * time.Millisecond
|
2017-05-04 22:04:30 +00:00
|
|
|
cons.ServerHealthInterval = 10 * time.Millisecond
|
2013-12-23 21:52:10 +00:00
|
|
|
return conf
|
|
|
|
}
|
|
|
|
|
2016-11-28 21:08:31 +00:00
|
|
|
func makeAgentLog(t *testing.T, conf *Config, l io.Writer, writer *logger.LogWriter) (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
|
2016-11-30 18:29:42 +00:00
|
|
|
agent, err := Create(conf, l, writer, nil)
|
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)
|
|
|
|
}
|
|
|
|
|
2016-11-30 18:29:42 +00:00
|
|
|
agent, err := Create(conf, nil, nil, nil)
|
2014-09-18 05:31:32 +00:00
|
|
|
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) {
|
2016-11-28 21:08:31 +00:00
|
|
|
return makeAgentLog(t, conf, nil, nil)
|
2013-12-30 22:42:41 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 19:58:58 +00:00
|
|
|
func externalIP() (string, error) {
|
|
|
|
addrs, err := net.InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Unable to lookup network interfaces: %v", err)
|
|
|
|
}
|
|
|
|
for _, a := range addrs {
|
|
|
|
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
|
|
|
if ipnet.IP.To4() != nil {
|
|
|
|
return ipnet.IP.String(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("Unable to find a non-loopback interface")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-11-03 19:58:58 +00:00
|
|
|
func TestAgent_CheckSerfBindAddrsSettings(t *testing.T) {
|
|
|
|
c := nextConfig()
|
|
|
|
ip, err := externalIP()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to get a non-loopback IP: %v", err)
|
|
|
|
}
|
|
|
|
c.SerfLanBindAddr = ip
|
|
|
|
c.SerfWanBindAddr = ip
|
|
|
|
dir, agent := makeAgent(t, c)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
2017-05-03 21:47:25 +00:00
|
|
|
serfWanBind := consulConfig(agent).SerfWANConfig.MemberlistConfig.BindAddr
|
2016-11-03 19:58:58 +00:00
|
|
|
if serfWanBind != ip {
|
|
|
|
t.Fatalf("SerfWanBindAddr is should be a non-loopback IP not %s", serfWanBind)
|
|
|
|
}
|
|
|
|
|
2017-05-03 21:47:25 +00:00
|
|
|
serfLanBind := consulConfig(agent).SerfLANConfig.MemberlistConfig.BindAddr
|
2016-11-03 19:58:58 +00:00
|
|
|
if serfLanBind != ip {
|
|
|
|
t.Fatalf("SerfLanBindAddr is should be a non-loopback IP not %s", serfWanBind)
|
|
|
|
}
|
|
|
|
}
|
2015-06-05 11:44:42 +00:00
|
|
|
func TestAgent_CheckAdvertiseAddrsSettings(t *testing.T) {
|
|
|
|
c := nextConfig()
|
|
|
|
c.AdvertiseAddrs.SerfLan, _ = net.ResolveTCPAddr("tcp", "127.0.0.42:1233")
|
|
|
|
c.AdvertiseAddrs.SerfWan, _ = net.ResolveTCPAddr("tcp", "127.0.0.43:1234")
|
|
|
|
c.AdvertiseAddrs.RPC, _ = net.ResolveTCPAddr("tcp", "127.0.0.44:1235")
|
|
|
|
dir, agent := makeAgent(t, c)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
2017-05-03 21:47:25 +00:00
|
|
|
serfLanAddr := consulConfig(agent).SerfLANConfig.MemberlistConfig.AdvertiseAddr
|
2015-06-05 11:44:42 +00:00
|
|
|
if serfLanAddr != "127.0.0.42" {
|
|
|
|
t.Fatalf("SerfLan is not properly set to '127.0.0.42': %s", serfLanAddr)
|
|
|
|
}
|
2017-05-03 21:47:25 +00:00
|
|
|
serfLanPort := consulConfig(agent).SerfLANConfig.MemberlistConfig.AdvertisePort
|
2015-06-05 11:44:42 +00:00
|
|
|
if serfLanPort != 1233 {
|
2015-07-14 18:42:51 +00:00
|
|
|
t.Fatalf("SerfLan is not properly set to '1233': %d", serfLanPort)
|
2015-06-05 11:44:42 +00:00
|
|
|
}
|
2017-05-03 21:47:25 +00:00
|
|
|
serfWanAddr := consulConfig(agent).SerfWANConfig.MemberlistConfig.AdvertiseAddr
|
2015-06-05 11:44:42 +00:00
|
|
|
if serfWanAddr != "127.0.0.43" {
|
|
|
|
t.Fatalf("SerfWan is not properly set to '127.0.0.43': %s", serfWanAddr)
|
|
|
|
}
|
2017-05-03 21:47:25 +00:00
|
|
|
serfWanPort := consulConfig(agent).SerfWANConfig.MemberlistConfig.AdvertisePort
|
2015-06-05 11:44:42 +00:00
|
|
|
if serfWanPort != 1234 {
|
2015-07-14 18:42:51 +00:00
|
|
|
t.Fatalf("SerfWan is not properly set to '1234': %d", serfWanPort)
|
2015-06-05 11:44:42 +00:00
|
|
|
}
|
2017-05-03 21:47:25 +00:00
|
|
|
rpc := consulConfig(agent).RPCAdvertise
|
2015-06-05 11:44:42 +00:00
|
|
|
if rpc != c.AdvertiseAddrs.RPC {
|
|
|
|
t.Fatalf("RPC is not properly set to %v: %s", c.AdvertiseAddrs.RPC, rpc)
|
|
|
|
}
|
2016-02-07 18:37:34 +00:00
|
|
|
expected := map[string]string{
|
2016-08-16 17:49:03 +00:00
|
|
|
"lan": agent.config.AdvertiseAddr,
|
2016-02-07 18:37:34 +00:00
|
|
|
"wan": agent.config.AdvertiseAddrWan,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(agent.config.TaggedAddresses, expected) {
|
|
|
|
t.Fatalf("Tagged addresses not set up properly: %v", agent.config.TaggedAddresses)
|
2015-12-24 00:07:27 +00:00
|
|
|
}
|
2015-06-05 11:44:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-25 00:33:53 +00:00
|
|
|
func TestAgent_CheckPerformanceSettings(t *testing.T) {
|
|
|
|
// Try a default config.
|
|
|
|
{
|
|
|
|
c := nextConfig()
|
|
|
|
c.ConsulConfig = nil
|
|
|
|
dir, agent := makeAgent(t, c)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
raftMult := time.Duration(consul.DefaultRaftMultiplier)
|
2017-05-03 21:47:25 +00:00
|
|
|
r := consulConfig(agent).RaftConfig
|
2016-08-25 00:33:53 +00:00
|
|
|
def := raft.DefaultConfig()
|
|
|
|
if r.HeartbeatTimeout != raftMult*def.HeartbeatTimeout ||
|
|
|
|
r.ElectionTimeout != raftMult*def.ElectionTimeout ||
|
|
|
|
r.LeaderLeaseTimeout != raftMult*def.LeaderLeaseTimeout {
|
|
|
|
t.Fatalf("bad: %#v", *r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try a multiplier.
|
|
|
|
{
|
|
|
|
c := nextConfig()
|
|
|
|
c.Performance.RaftMultiplier = 99
|
|
|
|
dir, agent := makeAgent(t, c)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
const raftMult time.Duration = 99
|
2017-05-03 21:47:25 +00:00
|
|
|
r := consulConfig(agent).RaftConfig
|
2016-08-25 00:33:53 +00:00
|
|
|
def := raft.DefaultConfig()
|
|
|
|
if r.HeartbeatTimeout != raftMult*def.HeartbeatTimeout ||
|
|
|
|
r.ElectionTimeout != raftMult*def.ElectionTimeout ||
|
|
|
|
r.LeaderLeaseTimeout != raftMult*def.LeaderLeaseTimeout {
|
|
|
|
t.Fatalf("bad: %#v", *r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-11 05:46:07 +00:00
|
|
|
func TestAgent_ReconnectConfigSettings(t *testing.T) {
|
|
|
|
c := nextConfig()
|
|
|
|
func() {
|
|
|
|
dir, agent := makeAgent(t, c)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
2017-05-03 21:47:25 +00:00
|
|
|
lan := consulConfig(agent).SerfLANConfig.ReconnectTimeout
|
2016-04-11 05:46:07 +00:00
|
|
|
if lan != 3*24*time.Hour {
|
|
|
|
t.Fatalf("bad: %s", lan.String())
|
|
|
|
}
|
|
|
|
|
2017-05-03 21:47:25 +00:00
|
|
|
wan := consulConfig(agent).SerfWANConfig.ReconnectTimeout
|
2016-04-11 05:46:07 +00:00
|
|
|
if wan != 3*24*time.Hour {
|
|
|
|
t.Fatalf("bad: %s", wan.String())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-10-25 20:46:54 +00:00
|
|
|
c = nextConfig()
|
2016-04-11 06:31:16 +00:00
|
|
|
c.ReconnectTimeoutLan = 24 * time.Hour
|
|
|
|
c.ReconnectTimeoutWan = 36 * time.Hour
|
2016-04-11 05:46:07 +00:00
|
|
|
func() {
|
|
|
|
dir, agent := makeAgent(t, c)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
2017-05-03 21:47:25 +00:00
|
|
|
lan := consulConfig(agent).SerfLANConfig.ReconnectTimeout
|
2016-04-11 06:31:16 +00:00
|
|
|
if lan != 24*time.Hour {
|
2016-04-11 05:46:07 +00:00
|
|
|
t.Fatalf("bad: %s", lan.String())
|
|
|
|
}
|
|
|
|
|
2017-05-03 21:47:25 +00:00
|
|
|
wan := consulConfig(agent).SerfWANConfig.ReconnectTimeout
|
2016-04-11 06:31:16 +00:00
|
|
|
if wan != 36*time.Hour {
|
2016-04-11 05:46:07 +00:00
|
|
|
t.Fatalf("bad: %s", wan.String())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2017-04-13 05:05:38 +00:00
|
|
|
func TestAgent_setupNodeID(t *testing.T) {
|
2017-01-18 06:20:11 +00:00
|
|
|
c := nextConfig()
|
2017-03-27 08:28:54 +00:00
|
|
|
c.NodeID = ""
|
2017-01-18 06:20:11 +00:00
|
|
|
dir, agent := makeAgent(t, c)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
// The auto-assigned ID should be valid.
|
2017-05-03 21:47:25 +00:00
|
|
|
id := consulConfig(agent).NodeID
|
2017-01-18 06:20:11 +00:00
|
|
|
if _, err := uuid.ParseUUID(string(id)); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-01-18 17:52:34 +00:00
|
|
|
// Running again should get the same ID (persisted in the file).
|
|
|
|
c.NodeID = ""
|
|
|
|
if err := agent.setupNodeID(c); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-05-03 21:47:25 +00:00
|
|
|
if newID := consulConfig(agent).NodeID; id != newID {
|
2017-01-18 17:52:34 +00:00
|
|
|
t.Fatalf("bad: %q vs %q", id, newID)
|
|
|
|
}
|
|
|
|
|
2017-01-18 06:20:11 +00:00
|
|
|
// Set an invalid ID via config.
|
|
|
|
c.NodeID = types.NodeID("nope")
|
|
|
|
err := agent.setupNodeID(c)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "uuid string is wrong length") {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a valid ID via config.
|
|
|
|
newID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-03-14 02:51:56 +00:00
|
|
|
c.NodeID = types.NodeID(strings.ToUpper(newID))
|
2017-01-18 06:20:11 +00:00
|
|
|
if err := agent.setupNodeID(c); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-05-03 21:47:25 +00:00
|
|
|
if id := consulConfig(agent).NodeID; string(id) != newID {
|
2017-01-18 06:20:11 +00:00
|
|
|
t.Fatalf("bad: %q vs. %q", id, newID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set an invalid ID via the file.
|
|
|
|
fileID := filepath.Join(c.DataDir, "node-id")
|
|
|
|
if err := ioutil.WriteFile(fileID, []byte("adf4238a!882b!9ddc!4a9d!5b6758e4159e"), 0600); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
c.NodeID = ""
|
|
|
|
err = agent.setupNodeID(c)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "uuid is improperly formatted") {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a valid ID via the file.
|
2017-03-14 02:51:56 +00:00
|
|
|
if err := ioutil.WriteFile(fileID, []byte("ADF4238a-882b-9ddc-4a9d-5b6758e4159e"), 0600); err != nil {
|
2017-01-18 06:20:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
c.NodeID = ""
|
|
|
|
if err := agent.setupNodeID(c); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-05-03 21:47:25 +00:00
|
|
|
if id := consulConfig(agent).NodeID; string(id) != "adf4238a-882b-9ddc-4a9d-5b6758e4159e" {
|
2017-01-18 06:20:11 +00:00
|
|
|
t.Fatalf("bad: %q vs. %q", id, newID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 05:05:38 +00:00
|
|
|
func TestAgent_makeNodeID(t *testing.T) {
|
|
|
|
c := nextConfig()
|
|
|
|
c.NodeID = ""
|
|
|
|
dir, agent := makeAgent(t, c)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
// We should get a valid host-based ID initially.
|
|
|
|
id, err := agent.makeNodeID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := uuid.ParseUUID(string(id)); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calling again should yield the same ID since it's host-based.
|
|
|
|
another, err := agent.makeNodeID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if id != another {
|
|
|
|
t.Fatalf("bad: %s vs %s", id, another)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn off host-based IDs and try again. We should get a random ID.
|
|
|
|
agent.config.DisableHostNodeID = true
|
|
|
|
another, err = agent.makeNodeID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if id == another {
|
|
|
|
t.Fatalf("bad: %s vs %s", id, another)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2015-01-14 01:52:17 +00:00
|
|
|
// Service registration with a single check
|
|
|
|
{
|
|
|
|
srv := &structs.NodeService{
|
|
|
|
ID: "redis",
|
|
|
|
Service: "redis",
|
|
|
|
Tags: []string{"foo"},
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
chkTypes := CheckTypes{
|
|
|
|
&CheckType{
|
|
|
|
TTL: time.Minute,
|
|
|
|
Notes: "redis heath check 2",
|
|
|
|
},
|
|
|
|
}
|
2015-05-05 00:36:17 +00:00
|
|
|
err := agent.AddService(srv, chkTypes, false, "")
|
2015-01-14 01:52:17 +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 the check is registered
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the notes are passed through
|
|
|
|
if agent.state.Checks()["service:redis"].Notes == "" {
|
|
|
|
t.Fatalf("missing redis check notes")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Service registration with multiple checks
|
|
|
|
{
|
|
|
|
srv := &structs.NodeService{
|
|
|
|
ID: "memcache",
|
|
|
|
Service: "memcache",
|
|
|
|
Tags: []string{"bar"},
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
chkTypes := CheckTypes{
|
|
|
|
&CheckType{
|
|
|
|
TTL: time.Minute,
|
|
|
|
Notes: "memcache health check 1",
|
|
|
|
},
|
|
|
|
&CheckType{
|
|
|
|
TTL: time.Second,
|
|
|
|
Notes: "memcache heath check 2",
|
|
|
|
},
|
|
|
|
}
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := agent.AddService(srv, chkTypes, false, ""); err != nil {
|
2015-01-14 01:52:17 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a state mapping
|
|
|
|
if _, ok := agent.state.Services()["memcache"]; !ok {
|
|
|
|
t.Fatalf("missing memcache service")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure both checks were added
|
|
|
|
if _, ok := agent.state.Checks()["service:memcache:1"]; !ok {
|
|
|
|
t.Fatalf("missing memcache:1 check")
|
|
|
|
}
|
|
|
|
if _, ok := agent.state.Checks()["service:memcache:2"]; !ok {
|
|
|
|
t.Fatalf("missing memcache:2 check")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a TTL is setup
|
|
|
|
if _, ok := agent.checkTTLs["service:memcache:1"]; !ok {
|
|
|
|
t.Fatalf("missing memcache:1 check ttl")
|
|
|
|
}
|
|
|
|
if _, ok := agent.checkTTLs["service:memcache:2"]; !ok {
|
|
|
|
t.Fatalf("missing memcache:2 check ttl")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the notes are passed through
|
|
|
|
if agent.state.Checks()["service:memcache:1"].Notes == "" {
|
|
|
|
t.Fatalf("missing redis check notes")
|
|
|
|
}
|
|
|
|
if agent.state.Checks()["service:memcache:2"].Notes == "" {
|
|
|
|
t.Fatalf("missing redis check notes")
|
|
|
|
}
|
2014-11-07 02:24:04 +00:00
|
|
|
}
|
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")
|
|
|
|
}
|
|
|
|
|
2015-01-27 09:10:56 +00:00
|
|
|
// Remove without an ID
|
|
|
|
if err := agent.RemoveService("", false); err == nil {
|
|
|
|
t.Fatalf("should have errored")
|
|
|
|
}
|
|
|
|
|
2015-01-14 01:52:17 +00:00
|
|
|
// Removing a service with a single check works
|
|
|
|
{
|
|
|
|
srv := &structs.NodeService{
|
|
|
|
ID: "memcache",
|
|
|
|
Service: "memcache",
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
chkTypes := CheckTypes{&CheckType{TTL: time.Minute}}
|
|
|
|
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := agent.AddService(srv, chkTypes, false, ""); err != nil {
|
2015-01-14 01:52:17 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-07 22:30:01 +00:00
|
|
|
// Add a check after the fact with a specific check ID
|
|
|
|
check := &CheckDefinition{
|
|
|
|
ID: "check2",
|
|
|
|
Name: "check2",
|
|
|
|
ServiceID: "memcache",
|
|
|
|
CheckType: CheckType{TTL: time.Minute},
|
|
|
|
}
|
|
|
|
hc := check.HealthCheck("node1")
|
|
|
|
if err := agent.AddCheck(hc, &check.CheckType, false, ""); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-01-14 01:52:17 +00:00
|
|
|
if err := agent.RemoveService("memcache", false); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if _, ok := agent.state.Checks()["service:memcache"]; ok {
|
|
|
|
t.Fatalf("have memcache check")
|
2015-05-07 22:30:01 +00:00
|
|
|
}
|
|
|
|
if _, ok := agent.state.Checks()["check2"]; ok {
|
|
|
|
t.Fatalf("have check2 check")
|
2015-01-14 01:52:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removing a service with multiple checks works
|
|
|
|
{
|
|
|
|
srv := &structs.NodeService{
|
|
|
|
ID: "redis",
|
|
|
|
Service: "redis",
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
chkTypes := CheckTypes{
|
|
|
|
&CheckType{TTL: time.Minute},
|
|
|
|
&CheckType{TTL: 30 * time.Second},
|
|
|
|
}
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := agent.AddService(srv, chkTypes, false, ""); err != nil {
|
2015-01-14 01:52:17 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the service
|
|
|
|
if err := agent.RemoveService("redis", false); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a state mapping
|
|
|
|
if _, ok := agent.state.Services()["redis"]; ok {
|
|
|
|
t.Fatalf("have redis service")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure checks were removed
|
|
|
|
if _, ok := agent.state.Checks()["service:redis:1"]; ok {
|
|
|
|
t.Fatalf("check redis:1 should be removed")
|
|
|
|
}
|
|
|
|
if _, ok := agent.state.Checks()["service:redis:2"]; ok {
|
|
|
|
t.Fatalf("check redis:2 should be removed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a TTL is setup
|
|
|
|
if _, ok := agent.checkTTLs["service:redis:1"]; ok {
|
|
|
|
t.Fatalf("check ttl for redis:1 should be removed")
|
|
|
|
}
|
|
|
|
if _, ok := agent.checkTTLs["service:redis:2"]; ok {
|
|
|
|
t.Fatalf("check ttl for redis:2 should be removed")
|
|
|
|
}
|
2014-01-30 21:39:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthCritical,
|
2014-01-30 21:39:02 +00:00
|
|
|
}
|
|
|
|
chk := &CheckType{
|
|
|
|
Script: "exit 0",
|
|
|
|
Interval: 15 * time.Second,
|
|
|
|
}
|
2015-05-05 00:36:17 +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
|
2015-04-12 00:53:48 +00:00
|
|
|
sChk, ok := agent.state.Checks()["mem"]
|
|
|
|
if !ok {
|
2014-01-30 21:39:02 +00:00
|
|
|
t.Fatalf("missing mem check")
|
|
|
|
}
|
|
|
|
|
2015-04-12 00:53:48 +00:00
|
|
|
// Ensure our check is in the right state
|
2017-04-19 23:00:11 +00:00
|
|
|
if sChk.Status != api.HealthCritical {
|
2015-04-12 00:53:48 +00:00
|
|
|
t.Fatalf("check not critical")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a TTL is setup
|
|
|
|
if _, ok := agent.checkMonitors["mem"]; !ok {
|
|
|
|
t.Fatalf("missing mem monitor")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_AddCheck_StartPassing(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",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2015-04-12 00:53:48 +00:00
|
|
|
}
|
|
|
|
chk := &CheckType{
|
|
|
|
Script: "exit 0",
|
|
|
|
Interval: 15 * time.Second,
|
|
|
|
}
|
2015-05-12 01:53:09 +00:00
|
|
|
err := agent.AddCheck(health, chk, false, "")
|
2015-04-12 00:53:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
|
|
|
sChk, ok := agent.state.Checks()["mem"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing mem check")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure our check is in the right state
|
2017-04-19 23:00:11 +00:00
|
|
|
if sChk.Status != api.HealthPassing {
|
2015-04-12 00:53:48 +00:00
|
|
|
t.Fatalf("check not passing")
|
|
|
|
}
|
|
|
|
|
2014-01-30 21:39:02 +00:00
|
|
|
// 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",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthCritical,
|
2014-04-21 21:42:42 +00:00
|
|
|
}
|
|
|
|
chk := &CheckType{
|
|
|
|
Script: "exit 0",
|
|
|
|
Interval: time.Microsecond,
|
|
|
|
}
|
2015-05-05 00:36:17 +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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 01:52:17 +00:00
|
|
|
func TestAgent_AddCheck_MissingService(t *testing.T) {
|
|
|
|
dir, agent := makeAgent(t, nextConfig())
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
health := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "baz",
|
|
|
|
Name: "baz check 1",
|
|
|
|
ServiceID: "baz",
|
|
|
|
}
|
|
|
|
chk := &CheckType{
|
|
|
|
Script: "exit 0",
|
|
|
|
Interval: time.Microsecond,
|
|
|
|
}
|
2015-05-05 00:36:17 +00:00
|
|
|
err := agent.AddCheck(health, chk, false, "")
|
2015-01-14 01:52:17 +00:00
|
|
|
if err == nil || err.Error() != `ServiceID "baz" does not exist` {
|
|
|
|
t.Fatalf("expected service id error, got: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-06 00:33:34 +00:00
|
|
|
func TestAgent_AddCheck_RestoreState(t *testing.T) {
|
|
|
|
dir, agent := makeAgent(t, nextConfig())
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
// Create some state and persist it
|
|
|
|
ttl := &CheckTTL{
|
|
|
|
CheckID: "baz",
|
|
|
|
TTL: time.Minute,
|
|
|
|
}
|
2017-04-19 23:00:11 +00:00
|
|
|
err := agent.persistCheckState(ttl, api.HealthPassing, "yup")
|
2015-06-06 00:33:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build and register the check definition and initial state
|
|
|
|
health := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "baz",
|
|
|
|
Name: "baz check 1",
|
|
|
|
}
|
|
|
|
chk := &CheckType{
|
|
|
|
TTL: time.Minute,
|
|
|
|
}
|
|
|
|
err = agent.AddCheck(health, chk, false, "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the check status was restored during registration
|
|
|
|
checks := agent.state.Checks()
|
|
|
|
check, ok := checks["baz"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing check")
|
|
|
|
}
|
2017-04-19 23:00:11 +00:00
|
|
|
if check.Status != api.HealthPassing {
|
2015-06-06 00:33:34 +00:00
|
|
|
t.Fatalf("bad: %#v", check)
|
|
|
|
}
|
|
|
|
if check.Output != "yup" {
|
|
|
|
t.Fatalf("bad: %#v", check)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-01-27 09:10:56 +00:00
|
|
|
// Remove without an ID
|
|
|
|
if err := agent.RemoveCheck("", false); err == nil {
|
|
|
|
t.Fatalf("should have errored")
|
|
|
|
}
|
|
|
|
|
2014-01-30 21:39:02 +00:00
|
|
|
health := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "mem",
|
|
|
|
Name: "memory util",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthCritical,
|
2014-01-30 21:39:02 +00:00
|
|
|
}
|
|
|
|
chk := &CheckType{
|
|
|
|
Script: "exit 0",
|
|
|
|
Interval: 15 * time.Second,
|
|
|
|
}
|
2015-05-05 00:36:17 +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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 07:05:55 +00:00
|
|
|
func TestAgent_updateTTLCheck(t *testing.T) {
|
2014-01-30 21:39:02 +00:00
|
|
|
dir, agent := makeAgent(t, nextConfig())
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
health := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "mem",
|
|
|
|
Name: "memory util",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthCritical,
|
2014-01-30 21:39:02 +00:00
|
|
|
}
|
|
|
|
chk := &CheckType{
|
|
|
|
TTL: 15 * time.Second,
|
|
|
|
}
|
2016-08-16 07:05:55 +00:00
|
|
|
|
|
|
|
// Add check and update it.
|
2015-05-05 00:36:17 +00:00
|
|
|
err := agent.AddCheck(health, chk, false, "")
|
2014-01-30 21:39:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-04-19 23:00:11 +00:00
|
|
|
if err := agent.updateTTLCheck("mem", api.HealthPassing, "foo"); err != nil {
|
2014-01-30 21:39:02 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-08-16 07:05:55 +00:00
|
|
|
// Ensure we have a check mapping.
|
2014-01-30 21:39:02 +00:00
|
|
|
status := agent.state.Checks()["mem"]
|
2017-04-19 23:00:11 +00:00
|
|
|
if status.Status != api.HealthPassing {
|
2014-01-30 21:39:02 +00:00
|
|
|
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()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, agent.RPC, "dc1")
|
2014-10-17 21:43:52 +00:00
|
|
|
|
|
|
|
// 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
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := agent.AddService(svc, nil, 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Persists to file if requested
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := agent.AddService(svc, nil, true, "mytoken"); 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)
|
|
|
|
}
|
2015-04-28 19:44:46 +00:00
|
|
|
expected, err := json.Marshal(persistedService{
|
2015-05-05 00:36:17 +00:00
|
|
|
Token: "mytoken",
|
2015-04-28 05:46:01 +00:00
|
|
|
Service: svc,
|
|
|
|
})
|
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))
|
|
|
|
}
|
2015-05-06 05:08:03 +00:00
|
|
|
|
|
|
|
// Updates service definition on disk
|
|
|
|
svc.Port = 8001
|
|
|
|
if err := agent.AddService(svc, nil, true, "mytoken"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
expected, err = json.Marshal(persistedService{
|
|
|
|
Token: "mytoken",
|
|
|
|
Service: 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))
|
|
|
|
}
|
2014-11-24 08:36:03 +00:00
|
|
|
agent.Shutdown()
|
|
|
|
|
|
|
|
// Should load it back during later start
|
2016-11-30 18:29:42 +00:00
|
|
|
agent2, err := Create(config, nil, nil, nil)
|
2014-11-24 08:36:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer agent2.Shutdown()
|
|
|
|
|
2015-05-06 05:08:03 +00:00
|
|
|
restored, ok := agent2.state.services[svc.ID]
|
|
|
|
if !ok {
|
2014-11-24 08:36:03 +00:00
|
|
|
t.Fatalf("bad: %#v", agent2.state.services)
|
|
|
|
}
|
2015-05-05 00:36:17 +00:00
|
|
|
if agent2.state.serviceTokens[svc.ID] != "mytoken" {
|
2015-04-28 05:46:01 +00:00
|
|
|
t.Fatalf("bad: %#v", agent2.state.services[svc.ID])
|
|
|
|
}
|
2015-05-06 05:08:03 +00:00
|
|
|
if restored.Port != 8001 {
|
|
|
|
t.Fatalf("bad: %#v", restored)
|
|
|
|
}
|
2014-11-26 07:58:02 +00:00
|
|
|
}
|
2014-11-24 08:36:03 +00:00
|
|
|
|
2015-04-28 19:44:46 +00:00
|
|
|
func TestAgent_persistedService_compat(t *testing.T) {
|
2015-04-28 19:18:41 +00:00
|
|
|
// Tests backwards compatibility of persisted services from pre-0.5.1
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode the NodeService directly. This is what previous versions
|
|
|
|
// would serialize to the file (without the wrapper)
|
|
|
|
encoded, err := json.Marshal(svc)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the content to the file
|
|
|
|
file := filepath.Join(agent.config.DataDir, servicesDir, stringHash(svc.ID))
|
|
|
|
if err := os.MkdirAll(filepath.Dir(file), 0700); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(file, encoded, 0600); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the services
|
|
|
|
if err := agent.loadServices(config); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the service was restored
|
|
|
|
services := agent.state.Services()
|
|
|
|
result, ok := services["redis"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing service")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(result, svc) {
|
|
|
|
t.Fatalf("bad: %#v", result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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))
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := agent.AddService(svc, nil, true, ""); err != nil {
|
2014-11-26 07:58:02 +00:00
|
|
|
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
|
|
|
|
2016-11-09 21:56:54 +00:00
|
|
|
// Re-add the service
|
|
|
|
if err := agent.AddService(svc, nil, true, ""); err != nil {
|
|
|
|
t.Fatalf("err: %v", 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
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := agent.AddService(svc1, nil, true, ""); err != nil {
|
2014-11-25 03:24:32 +00:00
|
|
|
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}
|
2016-11-30 18:29:42 +00:00
|
|
|
agent2, err := Create(config, nil, nil, nil)
|
2014-11-25 03:24:32 +00:00
|
|
|
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{
|
2015-01-14 01:52:17 +00:00
|
|
|
Node: config.NodeName,
|
|
|
|
CheckID: "mem",
|
|
|
|
Name: "memory check",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2014-11-24 08:36:03 +00:00
|
|
|
}
|
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
|
|
|
|
2016-06-06 08:53:30 +00:00
|
|
|
file := filepath.Join(agent.config.DataDir, checksDir, checkIDHash(check.CheckID))
|
2014-11-25 03:24:32 +00:00
|
|
|
|
|
|
|
// Not persisted if not requested
|
2015-05-05 00:36:17 +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
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := agent.AddCheck(check, chkType, true, "mytoken"); 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)
|
|
|
|
}
|
2015-04-28 19:44:46 +00:00
|
|
|
expected, err := json.Marshal(persistedCheck{
|
|
|
|
Check: check,
|
|
|
|
ChkType: chkType,
|
2015-05-05 00:36:17 +00:00
|
|
|
Token: "mytoken",
|
2015-04-28 19:44:46 +00:00
|
|
|
})
|
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))
|
|
|
|
}
|
2015-05-06 05:08:03 +00:00
|
|
|
|
|
|
|
// Updates the check definition on disk
|
|
|
|
check.Name = "mem1"
|
|
|
|
if err := agent.AddCheck(check, chkType, true, "mytoken"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
expected, err = json.Marshal(persistedCheck{
|
|
|
|
Check: check,
|
|
|
|
ChkType: chkType,
|
|
|
|
Token: "mytoken",
|
|
|
|
})
|
|
|
|
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))
|
|
|
|
}
|
2014-11-24 08:36:03 +00:00
|
|
|
agent.Shutdown()
|
|
|
|
|
|
|
|
// Should load it back during later start
|
2016-11-30 18:29:42 +00:00
|
|
|
agent2, err := Create(config, nil, nil, nil)
|
2014-11-24 08:36:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer agent2.Shutdown()
|
|
|
|
|
2015-04-28 19:44:46 +00:00
|
|
|
result, ok := agent2.state.checks[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)
|
|
|
|
}
|
2017-04-19 23:00:11 +00:00
|
|
|
if result.Status != api.HealthCritical {
|
2014-11-24 09:15:18 +00:00
|
|
|
t.Fatalf("bad: %#v", result)
|
|
|
|
}
|
2015-05-06 05:08:03 +00:00
|
|
|
if result.Name != "mem1" {
|
|
|
|
t.Fatalf("bad: %#v", result)
|
|
|
|
}
|
2014-11-29 20:25:01 +00:00
|
|
|
|
|
|
|
// Should have restored the monitor
|
2015-04-28 19:44:46 +00:00
|
|
|
if _, ok := agent2.checkMonitors[check.CheckID]; !ok {
|
2014-11-29 20:25:01 +00:00
|
|
|
t.Fatalf("bad: %#v", agent2.checkMonitors)
|
|
|
|
}
|
2015-05-05 00:36:17 +00:00
|
|
|
if agent2.state.checkTokens[check.CheckID] != "mytoken" {
|
2015-04-28 19:44:46 +00:00
|
|
|
t.Fatalf("bad: %s", agent2.state.checkTokens[check.CheckID])
|
2015-04-28 05:46:01 +00:00
|
|
|
}
|
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{
|
2015-01-14 01:52:17 +00:00
|
|
|
Node: config.NodeName,
|
|
|
|
CheckID: "mem",
|
|
|
|
Name: "memory check",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2014-11-26 07:58:02 +00:00
|
|
|
}
|
2014-11-24 08:36:03 +00:00
|
|
|
|
2016-06-06 08:53:30 +00:00
|
|
|
file := filepath.Join(agent.config.DataDir, checksDir, checkIDHash(check.CheckID))
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := agent.AddCheck(check, nil, true, ""); err != nil {
|
2014-11-26 07:58:02 +00:00
|
|
|
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{
|
2015-01-14 01:52:17 +00:00
|
|
|
Node: config.NodeName,
|
|
|
|
CheckID: "mem",
|
|
|
|
Name: "memory check",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2014-11-25 03:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// First persist the check
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := agent.AddCheck(check1, nil, true, ""); err != nil {
|
2014-11-25 03:24:32 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
agent.Shutdown()
|
|
|
|
|
|
|
|
// Start again with the check registered in config
|
|
|
|
check2 := &CheckDefinition{
|
2015-01-14 01:52:17 +00:00
|
|
|
ID: "mem",
|
|
|
|
Name: "memory check",
|
2014-11-25 03:24:32 +00:00
|
|
|
Notes: "my cool notes",
|
|
|
|
CheckType: CheckType{
|
|
|
|
Script: "/bin/check-redis.py",
|
|
|
|
Interval: 30 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Checks = []*CheckDefinition{check2}
|
2016-11-30 18:29:42 +00:00
|
|
|
agent2, err := Create(config, nil, nil, nil)
|
2014-11-25 03:24:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer agent2.Shutdown()
|
|
|
|
|
2016-06-06 08:53:30 +00:00
|
|
|
file := filepath.Join(agent.config.DataDir, checksDir, checkIDHash(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
|
|
|
|
2015-04-28 19:44:46 +00:00
|
|
|
func TestAgent_loadChecks_token(t *testing.T) {
|
|
|
|
config := nextConfig()
|
|
|
|
config.Checks = append(config.Checks, &CheckDefinition{
|
|
|
|
ID: "rabbitmq",
|
|
|
|
Name: "rabbitmq",
|
|
|
|
Token: "abc123",
|
|
|
|
CheckType: CheckType{
|
|
|
|
TTL: 10 * time.Second,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
checks := agent.state.Checks()
|
|
|
|
if _, ok := checks["rabbitmq"]; !ok {
|
|
|
|
t.Fatalf("missing check")
|
|
|
|
}
|
|
|
|
if token := agent.state.CheckToken("rabbitmq"); token != "abc123" {
|
|
|
|
t.Fatalf("bad: %s", token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2015-01-14 01:52:17 +00:00
|
|
|
// First register a service
|
|
|
|
svc := &structs.NodeService{
|
|
|
|
ID: "redis",
|
|
|
|
Service: "redis",
|
|
|
|
Tags: []string{"foo"},
|
|
|
|
Port: 8000,
|
|
|
|
}
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := agent.AddService(svc, nil, false, ""); err != nil {
|
2015-01-14 01:52:17 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a check
|
2015-01-08 06:26:40 +00:00
|
|
|
check1 := &structs.HealthCheck{
|
|
|
|
Node: config.NodeName,
|
2015-01-14 01:52:17 +00:00
|
|
|
CheckID: "service:redis",
|
2015-01-08 06:26:40 +00:00
|
|
|
Name: "redischeck",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2015-01-08 06:26:40 +00:00
|
|
|
ServiceID: "redis",
|
|
|
|
ServiceName: "redis",
|
|
|
|
}
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := agent.AddCheck(check1, nil, false, ""); err != nil {
|
2015-01-08 06:26:40 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
found := false
|
2017-04-20 18:42:22 +00:00
|
|
|
for check := range agent.state.Checks() {
|
2015-01-08 06:26:40 +00:00
|
|
|
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
|
2017-04-20 18:42:22 +00:00
|
|
|
for check := range agent.state.Checks() {
|
2015-01-08 06:26:40 +00:00
|
|
|
if check == check1.CheckID {
|
|
|
|
t.Fatalf("should have unloaded checks")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-28 19:44:46 +00:00
|
|
|
func TestAgent_loadServices_token(t *testing.T) {
|
|
|
|
config := nextConfig()
|
|
|
|
config.Services = append(config.Services, &ServiceDefinition{
|
|
|
|
ID: "rabbitmq",
|
|
|
|
Name: "rabbitmq",
|
|
|
|
Port: 5672,
|
|
|
|
Token: "abc123",
|
|
|
|
})
|
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
services := agent.state.Services()
|
|
|
|
if _, ok := services["rabbitmq"]; !ok {
|
|
|
|
t.Fatalf("missing service")
|
|
|
|
}
|
|
|
|
if token := agent.state.ServiceToken("rabbitmq"); token != "abc123" {
|
|
|
|
t.Fatalf("bad: %s", token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 06:26:40 +00:00
|
|
|
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
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := agent.AddService(svc, nil, false, ""); err != nil {
|
2015-01-08 06:26:40 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
found := false
|
2017-04-20 18:42:22 +00:00
|
|
|
for id := range agent.state.Services() {
|
2015-01-08 06:26:40 +00:00
|
|
|
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
|
2017-04-20 18:42:22 +00:00
|
|
|
for id := range agent.state.Services() {
|
2015-01-08 06:26:40 +00:00
|
|
|
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
|
|
|
|
2016-08-16 07:05:55 +00:00
|
|
|
func TestAgent_Service_MaintenanceMode(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
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := agent.AddService(svc, nil, false, ""); err != nil {
|
2015-01-15 08:25:36 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enter maintenance mode for the service
|
2015-09-10 19:08:08 +00:00
|
|
|
if err := agent.EnableServiceMaintenance("redis", "broken", "mytoken"); err != nil {
|
2015-01-15 08:25:36 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the critical health check was added
|
2015-01-15 20:20:57 +00:00
|
|
|
checkID := serviceMaintCheckID("redis")
|
2015-01-21 20:21:57 +00:00
|
|
|
check, ok := agent.state.Checks()[checkID]
|
|
|
|
if !ok {
|
2015-01-15 19:26:14 +00:00
|
|
|
t.Fatalf("should have registered critical maintenance check")
|
2015-01-15 08:25:36 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 19:08:08 +00:00
|
|
|
// Check that the token was used to register the check
|
|
|
|
if token := agent.state.CheckToken(checkID); token != "mytoken" {
|
|
|
|
t.Fatalf("expected 'mytoken', got: '%s'", token)
|
|
|
|
}
|
|
|
|
|
2015-01-21 20:21:57 +00:00
|
|
|
// Ensure the reason was set in notes
|
|
|
|
if check.Notes != "broken" {
|
|
|
|
t.Fatalf("bad: %#v", 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 20:20:57 +00:00
|
|
|
if _, ok := agent.state.Checks()[checkID]; ok {
|
2015-01-15 19:26:14 +00:00
|
|
|
t.Fatalf("should have deregistered maintenance check")
|
|
|
|
}
|
2015-01-21 20:21:57 +00:00
|
|
|
|
|
|
|
// Enter service maintenance mode without providing a reason
|
2015-09-10 19:08:08 +00:00
|
|
|
if err := agent.EnableServiceMaintenance("redis", "", ""); err != nil {
|
2015-01-21 20:21:57 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the check was registered with the default notes
|
|
|
|
check, ok = agent.state.Checks()[checkID]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should have registered critical check")
|
|
|
|
}
|
2015-01-21 22:45:09 +00:00
|
|
|
if check.Notes != defaultServiceMaintReason {
|
2015-01-21 20:21:57 +00:00
|
|
|
t.Fatalf("bad: %#v", check)
|
|
|
|
}
|
2015-01-15 19:26:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 07:05:55 +00:00
|
|
|
func TestAgent_Service_Reap(t *testing.T) {
|
|
|
|
config := nextConfig()
|
|
|
|
config.CheckReapInterval = time.Millisecond
|
|
|
|
config.CheckDeregisterIntervalMin = 0
|
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
svc := &structs.NodeService{
|
|
|
|
ID: "redis",
|
|
|
|
Service: "redis",
|
|
|
|
Tags: []string{"foo"},
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
chkTypes := CheckTypes{
|
|
|
|
&CheckType{
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2016-08-16 07:05:55 +00:00
|
|
|
TTL: 10 * time.Millisecond,
|
|
|
|
DeregisterCriticalServiceAfter: 100 * time.Millisecond,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the service.
|
|
|
|
if err := agent.AddService(svc, chkTypes, false, ""); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it's there and there's no critical check yet.
|
|
|
|
if _, ok := agent.state.Services()["redis"]; !ok {
|
|
|
|
t.Fatalf("should have redis service")
|
|
|
|
}
|
|
|
|
if checks := agent.state.CriticalChecks(); len(checks) > 0 {
|
|
|
|
t.Fatalf("should not have critical checks")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the check TTL to fail.
|
|
|
|
time.Sleep(30 * time.Millisecond)
|
|
|
|
if _, ok := agent.state.Services()["redis"]; !ok {
|
|
|
|
t.Fatalf("should have redis service")
|
|
|
|
}
|
|
|
|
if checks := agent.state.CriticalChecks(); len(checks) != 1 {
|
|
|
|
t.Fatalf("should have a critical check")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the TTL.
|
2017-04-19 23:00:11 +00:00
|
|
|
if err := agent.updateTTLCheck("service:redis", api.HealthPassing, "foo"); err != nil {
|
2016-08-16 07:05:55 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, ok := agent.state.Services()["redis"]; !ok {
|
|
|
|
t.Fatalf("should have redis service")
|
|
|
|
}
|
|
|
|
if checks := agent.state.CriticalChecks(); len(checks) > 0 {
|
|
|
|
t.Fatalf("should not have critical checks")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the check TTL to fail again.
|
|
|
|
time.Sleep(30 * time.Millisecond)
|
|
|
|
if _, ok := agent.state.Services()["redis"]; !ok {
|
|
|
|
t.Fatalf("should have redis service")
|
|
|
|
}
|
|
|
|
if checks := agent.state.CriticalChecks(); len(checks) != 1 {
|
|
|
|
t.Fatalf("should have a critical check")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the reap.
|
|
|
|
time.Sleep(300 * time.Millisecond)
|
|
|
|
if _, ok := agent.state.Services()["redis"]; ok {
|
|
|
|
t.Fatalf("redis service should have been reaped")
|
|
|
|
}
|
|
|
|
if checks := agent.state.CriticalChecks(); len(checks) > 0 {
|
|
|
|
t.Fatalf("should not have critical checks")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_Service_NoReap(t *testing.T) {
|
|
|
|
config := nextConfig()
|
|
|
|
config.CheckReapInterval = time.Millisecond
|
|
|
|
config.CheckDeregisterIntervalMin = 0
|
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
svc := &structs.NodeService{
|
|
|
|
ID: "redis",
|
|
|
|
Service: "redis",
|
|
|
|
Tags: []string{"foo"},
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
chkTypes := CheckTypes{
|
|
|
|
&CheckType{
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2016-08-16 07:05:55 +00:00
|
|
|
TTL: 10 * time.Millisecond,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the service.
|
|
|
|
if err := agent.AddService(svc, chkTypes, false, ""); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it's there and there's no critical check yet.
|
|
|
|
if _, ok := agent.state.Services()["redis"]; !ok {
|
|
|
|
t.Fatalf("should have redis service")
|
|
|
|
}
|
|
|
|
if checks := agent.state.CriticalChecks(); len(checks) > 0 {
|
|
|
|
t.Fatalf("should not have critical checks")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the check TTL to fail.
|
|
|
|
time.Sleep(30 * time.Millisecond)
|
|
|
|
if _, ok := agent.state.Services()["redis"]; !ok {
|
|
|
|
t.Fatalf("should have redis service")
|
|
|
|
}
|
|
|
|
if checks := agent.state.CriticalChecks(); len(checks) != 1 {
|
|
|
|
t.Fatalf("should have a critical check")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait a while and make sure it doesn't reap.
|
|
|
|
time.Sleep(300 * time.Millisecond)
|
|
|
|
if _, ok := agent.state.Services()["redis"]; !ok {
|
|
|
|
t.Fatalf("should have redis service")
|
|
|
|
}
|
|
|
|
if checks := agent.state.CriticalChecks(); len(checks) != 1 {
|
|
|
|
t.Fatalf("should have a critical check")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 19:28:42 +00:00
|
|
|
func TestAgent_addCheck_restoresSnapshot(t *testing.T) {
|
|
|
|
config := nextConfig()
|
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
// First register a service
|
|
|
|
svc := &structs.NodeService{
|
|
|
|
ID: "redis",
|
|
|
|
Service: "redis",
|
|
|
|
Tags: []string{"foo"},
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
if err := agent.AddService(svc, nil, false, ""); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a check
|
|
|
|
check1 := &structs.HealthCheck{
|
|
|
|
Node: config.NodeName,
|
|
|
|
CheckID: "service:redis",
|
|
|
|
Name: "redischeck",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2015-05-06 19:28:42 +00:00
|
|
|
ServiceID: "redis",
|
|
|
|
ServiceName: "redis",
|
|
|
|
}
|
|
|
|
if err := agent.AddCheck(check1, nil, false, ""); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-registering the service preserves the state of the check
|
|
|
|
chkTypes := CheckTypes{&CheckType{TTL: 30 * time.Second}}
|
|
|
|
if err := agent.AddService(svc, chkTypes, false, ""); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
check, ok := agent.state.Checks()["service:redis"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing check")
|
|
|
|
}
|
2017-04-19 23:00:11 +00:00
|
|
|
if check.Status != api.HealthPassing {
|
2015-05-06 19:28:42 +00:00
|
|
|
t.Fatalf("bad: %s", check.Status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:26:14 +00:00
|
|
|
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
|
2015-09-10 19:08:08 +00:00
|
|
|
agent.EnableNodeMaintenance("broken", "mytoken")
|
2015-01-15 19:26:14 +00:00
|
|
|
|
|
|
|
// Make sure the critical health check was added
|
2016-11-29 21:15:20 +00:00
|
|
|
check, ok := agent.state.Checks()[structs.NodeMaint]
|
2015-01-21 20:21:57 +00:00
|
|
|
if !ok {
|
2015-01-15 19:26:14 +00:00
|
|
|
t.Fatalf("should have registered critical node check")
|
|
|
|
}
|
|
|
|
|
2015-09-10 19:08:08 +00:00
|
|
|
// Check that the token was used to register the check
|
2016-11-29 21:15:20 +00:00
|
|
|
if token := agent.state.CheckToken(structs.NodeMaint); token != "mytoken" {
|
2015-09-10 19:08:08 +00:00
|
|
|
t.Fatalf("expected 'mytoken', got: '%s'", token)
|
|
|
|
}
|
|
|
|
|
2015-01-21 20:21:57 +00:00
|
|
|
// Ensure the reason was set in notes
|
|
|
|
if check.Notes != "broken" {
|
|
|
|
t.Fatalf("bad: %#v", check)
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:26:14 +00:00
|
|
|
// Leave maintenance mode
|
|
|
|
agent.DisableNodeMaintenance()
|
|
|
|
|
|
|
|
// Ensure the check was deregistered
|
2016-11-29 21:15:20 +00:00
|
|
|
if _, ok := agent.state.Checks()[structs.NodeMaint]; ok {
|
2015-01-15 19:26:14 +00:00
|
|
|
t.Fatalf("should have deregistered critical node check")
|
2015-01-15 08:25:36 +00:00
|
|
|
}
|
2015-01-21 20:21:57 +00:00
|
|
|
|
|
|
|
// Enter maintenance mode without passing a reason
|
2015-09-10 19:08:08 +00:00
|
|
|
agent.EnableNodeMaintenance("", "")
|
2015-01-21 20:21:57 +00:00
|
|
|
|
|
|
|
// Make sure the check was registered with the default note
|
2016-11-29 21:15:20 +00:00
|
|
|
check, ok = agent.state.Checks()[structs.NodeMaint]
|
2015-01-21 20:21:57 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should have registered critical node check")
|
|
|
|
}
|
2015-01-21 22:45:09 +00:00
|
|
|
if check.Notes != defaultNodeMaintReason {
|
2015-01-21 20:21:57 +00:00
|
|
|
t.Fatalf("bad: %#v", check)
|
|
|
|
}
|
2015-01-15 08:25:36 +00:00
|
|
|
}
|
2015-02-17 20:00:04 +00:00
|
|
|
|
|
|
|
func TestAgent_checkStateSnapshot(t *testing.T) {
|
|
|
|
config := nextConfig()
|
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
// First register a service
|
|
|
|
svc := &structs.NodeService{
|
|
|
|
ID: "redis",
|
|
|
|
Service: "redis",
|
|
|
|
Tags: []string{"foo"},
|
|
|
|
Port: 8000,
|
|
|
|
}
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := agent.AddService(svc, nil, false, ""); err != nil {
|
2015-02-17 20:00:04 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a check
|
|
|
|
check1 := &structs.HealthCheck{
|
|
|
|
Node: config.NodeName,
|
|
|
|
CheckID: "service:redis",
|
|
|
|
Name: "redischeck",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2015-02-17 20:00:04 +00:00
|
|
|
ServiceID: "redis",
|
|
|
|
ServiceName: "redis",
|
|
|
|
}
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := agent.AddCheck(check1, nil, true, ""); err != nil {
|
2015-02-17 20:00:04 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Snapshot the state
|
|
|
|
snap := agent.snapshotCheckState()
|
|
|
|
|
|
|
|
// Unload all of the checks
|
|
|
|
if err := agent.unloadChecks(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reload the checks
|
|
|
|
if err := agent.loadChecks(config); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore the state
|
|
|
|
agent.restoreCheckState(snap)
|
|
|
|
|
|
|
|
// Search for the check
|
|
|
|
out, ok := agent.state.Checks()[check1.CheckID]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("check should have been registered")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure state was restored
|
2017-04-19 23:00:11 +00:00
|
|
|
if out.Status != api.HealthPassing {
|
2015-02-17 20:00:04 +00:00
|
|
|
t.Fatalf("should have restored check state")
|
|
|
|
}
|
|
|
|
}
|
2015-03-11 23:13:19 +00:00
|
|
|
|
|
|
|
func TestAgent_loadChecks_checkFails(t *testing.T) {
|
|
|
|
config := nextConfig()
|
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
// Persist a health check with an invalid service ID
|
|
|
|
check := &structs.HealthCheck{
|
|
|
|
Node: config.NodeName,
|
|
|
|
CheckID: "service:redis",
|
|
|
|
Name: "redischeck",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2015-03-11 23:13:19 +00:00
|
|
|
ServiceID: "nope",
|
|
|
|
}
|
|
|
|
if err := agent.persistCheck(check, nil); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to make sure the check was persisted
|
2016-06-06 08:53:30 +00:00
|
|
|
checkHash := checkIDHash(check.CheckID)
|
2015-03-11 23:13:19 +00:00
|
|
|
checkPath := filepath.Join(config.DataDir, checksDir, checkHash)
|
|
|
|
if _, err := os.Stat(checkPath); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try loading the checks from the persisted files
|
|
|
|
if err := agent.loadChecks(config); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the erroneous check was purged
|
|
|
|
if _, err := os.Stat(checkPath); err == nil {
|
|
|
|
t.Fatalf("should have purged check")
|
|
|
|
}
|
|
|
|
}
|
2015-06-05 23:45:05 +00:00
|
|
|
|
2015-06-05 23:59:41 +00:00
|
|
|
func TestAgent_persistCheckState(t *testing.T) {
|
2015-06-05 23:45:05 +00:00
|
|
|
config := nextConfig()
|
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
// Create the TTL check to persist
|
|
|
|
check := &CheckTTL{
|
|
|
|
CheckID: "check1",
|
|
|
|
TTL: 10 * time.Minute,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Persist some check state for the check
|
2017-04-19 23:00:11 +00:00
|
|
|
err := agent.persistCheckState(check, api.HealthCritical, "nope")
|
2015-06-05 23:45:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the persisted file exists and has the content
|
|
|
|
file := filepath.Join(agent.config.DataDir, checkStateDir, stringHash("check1"))
|
|
|
|
buf, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the state
|
|
|
|
var p persistedCheckState
|
|
|
|
if err := json.Unmarshal(buf, &p); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the fields
|
|
|
|
if p.CheckID != "check1" {
|
|
|
|
t.Fatalf("bad: %#v", p)
|
|
|
|
}
|
|
|
|
if p.Output != "nope" {
|
|
|
|
t.Fatalf("bad: %#v", p)
|
|
|
|
}
|
2017-04-19 23:00:11 +00:00
|
|
|
if p.Status != api.HealthCritical {
|
2015-06-05 23:45:05 +00:00
|
|
|
t.Fatalf("bad: %#v", p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the expiration time was set
|
|
|
|
if p.Expires < time.Now().Unix() {
|
|
|
|
t.Fatalf("bad: %#v", p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 16:35:10 +00:00
|
|
|
func TestAgent_loadCheckState(t *testing.T) {
|
2015-06-05 23:45:05 +00:00
|
|
|
config := nextConfig()
|
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
// Create a check whose state will expire immediately
|
|
|
|
check := &CheckTTL{
|
|
|
|
CheckID: "check1",
|
|
|
|
TTL: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Persist the check state
|
2017-04-19 23:00:11 +00:00
|
|
|
err := agent.persistCheckState(check, api.HealthPassing, "yup")
|
2015-06-05 23:45:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-06-08 16:35:10 +00:00
|
|
|
// Try to load the state
|
2015-06-05 23:45:05 +00:00
|
|
|
health := &structs.HealthCheck{
|
|
|
|
CheckID: "check1",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthCritical,
|
2015-06-05 23:45:05 +00:00
|
|
|
}
|
2015-06-08 16:35:10 +00:00
|
|
|
if err := agent.loadCheckState(health); err != nil {
|
2015-06-05 23:45:05 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not have restored the status due to expiration
|
2017-04-19 23:00:11 +00:00
|
|
|
if health.Status != api.HealthCritical {
|
2015-06-05 23:45:05 +00:00
|
|
|
t.Fatalf("bad: %#v", health)
|
|
|
|
}
|
|
|
|
if health.Output != "" {
|
|
|
|
t.Fatalf("bad: %#v", health)
|
|
|
|
}
|
|
|
|
|
2015-06-05 23:59:41 +00:00
|
|
|
// Should have purged the state
|
|
|
|
file := filepath.Join(agent.config.DataDir, checksDir, stringHash("check1"))
|
|
|
|
if _, err := os.Stat(file); !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("should have purged state")
|
|
|
|
}
|
|
|
|
|
2015-06-05 23:45:05 +00:00
|
|
|
// Set a TTL which will not expire before we check it
|
|
|
|
check.TTL = time.Minute
|
2017-04-19 23:00:11 +00:00
|
|
|
err = agent.persistCheckState(check, api.HealthPassing, "yup")
|
2015-06-05 23:45:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-06-08 16:35:10 +00:00
|
|
|
// Try to load
|
|
|
|
if err := agent.loadCheckState(health); err != nil {
|
2015-06-05 23:45:05 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should have restored
|
2017-04-19 23:00:11 +00:00
|
|
|
if health.Status != api.HealthPassing {
|
2015-06-05 23:45:05 +00:00
|
|
|
t.Fatalf("bad: %#v", health)
|
|
|
|
}
|
|
|
|
if health.Output != "yup" {
|
|
|
|
t.Fatalf("bad: %#v", health)
|
|
|
|
}
|
|
|
|
}
|
2015-06-05 23:57:14 +00:00
|
|
|
|
|
|
|
func TestAgent_purgeCheckState(t *testing.T) {
|
|
|
|
config := nextConfig()
|
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
// No error if the state does not exist
|
|
|
|
if err := agent.purgeCheckState("check1"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Persist some state to the data dir
|
|
|
|
check := &CheckTTL{
|
|
|
|
CheckID: "check1",
|
|
|
|
TTL: time.Minute,
|
|
|
|
}
|
2017-04-19 23:00:11 +00:00
|
|
|
err := agent.persistCheckState(check, api.HealthPassing, "yup")
|
2015-06-05 23:57:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Purge the check state
|
|
|
|
if err := agent.purgeCheckState("check1"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removed the file
|
|
|
|
file := filepath.Join(agent.config.DataDir, checkStateDir, stringHash("check1"))
|
|
|
|
if _, err := os.Stat(file); !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("should have removed file")
|
|
|
|
}
|
|
|
|
}
|
2015-10-16 02:28:31 +00:00
|
|
|
|
|
|
|
func TestAgent_GetCoordinate(t *testing.T) {
|
|
|
|
check := func(server bool) {
|
|
|
|
config := nextConfig()
|
|
|
|
config.Server = server
|
|
|
|
dir, agent := makeAgent(t, config)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
// This doesn't verify the returned coordinate, but it makes
|
|
|
|
// sure that the agent chooses the correct Serf instance,
|
|
|
|
// depending on how it's configured as a client or a server.
|
|
|
|
// If it chooses the wrong one, this will crash.
|
|
|
|
if _, err := agent.GetCoordinate(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
check(true)
|
|
|
|
check(false)
|
|
|
|
}
|
2017-05-03 21:47:25 +00:00
|
|
|
|
|
|
|
func consulConfig(a *Agent) *consul.Config {
|
|
|
|
c, err := a.consulConfig()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|