open-consul/command/agent/agent_test.go

282 lines
6.4 KiB
Go
Raw Normal View History

2013-12-23 21:52:10 +00:00
package agent
import (
"fmt"
"github.com/hashicorp/consul/consul"
"github.com/hashicorp/consul/consul/structs"
2013-12-30 22:42:41 +00:00
"io"
2013-12-23 21:52:10 +00:00
"io/ioutil"
"os"
"sync/atomic"
"testing"
"time"
)
var offset uint64
func nextConfig() *Config {
idx := atomic.AddUint64(&offset, 1)
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-01-02 21:12:05 +00:00
conf.DNSAddr = fmt.Sprintf("127.0.0.1:%d", 18600+idx)
conf.HTTPAddr = fmt.Sprintf("127.0.0.1:%d", 18500+idx)
conf.RPCAddr = fmt.Sprintf("127.0.0.1:%d", 18400+idx)
2013-12-23 21:52:10 +00:00
conf.SerfBindAddr = "127.0.0.1"
2014-01-02 21:12:05 +00:00
conf.SerfLanPort = int(18200 + idx)
conf.SerfWanPort = int(18300 + idx)
2013-12-23 21:52:10 +00:00
conf.Server = true
2014-01-02 21:12:05 +00:00
conf.ServerAddr = fmt.Sprintf("127.0.0.1:%d", 18100+idx)
2013-12-23 21:52:10 +00:00
cons := consul.DefaultConfig()
conf.ConsulConfig = cons
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
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
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
}
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")
}
}
func TestAgent_RPCPing(t *testing.T) {
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)
}
}
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",
Tag: "foo",
Port: 8000,
}
chk := &CheckType{TTL: time.Minute}
err := agent.AddService(srv, chk)
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")
}
}
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
if err := agent.RemoveService("redis"); err != nil {
t.Fatalf("err: %v", err)
}
srv := &structs.NodeService{
ID: "redis",
Service: "redis",
Port: 8000,
}
chk := &CheckType{TTL: time.Minute}
if err := agent.AddService(srv, chk); err != nil {
t.Fatalf("err: %v", err)
}
// Remove the service
if err := agent.RemoveService("redis"); 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 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",
Status: structs.HealthUnknown,
}
chk := &CheckType{
Script: "exit 0",
Interval: 15 * time.Second,
}
err := agent.AddCheck(health, chk)
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")
}
}
func TestAgent_RemoveCheck(t *testing.T) {
dir, agent := makeAgent(t, nextConfig())
defer os.RemoveAll(dir)
defer agent.Shutdown()
// Remove check that doesn't exist
if err := agent.RemoveCheck("mem"); err != nil {
t.Fatalf("err: %v", err)
}
health := &structs.HealthCheck{
Node: "foo",
CheckID: "mem",
Name: "memory util",
Status: structs.HealthUnknown,
}
chk := &CheckType{
Script: "exit 0",
Interval: 15 * time.Second,
}
err := agent.AddCheck(health, chk)
if err != nil {
t.Fatalf("err: %v", err)
}
// Remove check
if err := agent.RemoveCheck("mem"); err != nil {
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",
Status: structs.HealthUnknown,
}
chk := &CheckType{
TTL: 15 * time.Second,
}
err := agent.AddCheck(health, chk)
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)
}
if status.Notes != "foo" {
t.Fatalf("bad: %v", status)
}
}