From 06e11e4b085ca68ce23acbcd19cea3028c05f92f Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Mon, 23 Dec 2013 13:52:10 -0800 Subject: [PATCH] Adding more agent tests --- command/agent/agent_test.go | 87 +++++++++++++++++++++++++++ command/agent/config_test.go | 3 + command/agent/http_test.go | 15 +++++ command/agent/status_endpoint_test.go | 43 +++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 command/agent/agent_test.go create mode 100644 command/agent/config_test.go create mode 100644 command/agent/http_test.go create mode 100644 command/agent/status_endpoint_test.go diff --git a/command/agent/agent_test.go b/command/agent/agent_test.go new file mode 100644 index 000000000..09fc23434 --- /dev/null +++ b/command/agent/agent_test.go @@ -0,0 +1,87 @@ +package agent + +import ( + "fmt" + "github.com/hashicorp/consul/consul" + "io/ioutil" + "os" + "sync/atomic" + "testing" + "time" +) + +var offset uint64 + +func nextConfig() *Config { + idx := atomic.AddUint64(&offset, 1) + conf := DefaultConfig() + + conf.HTTPAddr = fmt.Sprintf("127.0.0.1:%d", 8500+10*idx) + conf.RPCAddr = fmt.Sprintf("127.0.0.1:%d", 8400+10*idx) + conf.SerfBindAddr = "127.0.0.1" + conf.SerfLanPort = int(8301 + 10*idx) + conf.SerfWanPort = int(8302 + 10*idx) + conf.Server = true + + cons := consul.DefaultConfig() + conf.ConsulConfig = cons + + cons.SerfLANConfig.MemberlistConfig.ProbeTimeout = 200 * time.Millisecond + cons.SerfLANConfig.MemberlistConfig.ProbeInterval = time.Second + cons.SerfLANConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond + + cons.SerfWANConfig.MemberlistConfig.ProbeTimeout = 200 * time.Millisecond + cons.SerfWANConfig.MemberlistConfig.ProbeInterval = time.Second + cons.SerfWANConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond + + cons.RaftConfig.HeartbeatTimeout = 40 * time.Millisecond + cons.RaftConfig.ElectionTimeout = 40 * time.Millisecond + + return conf +} + +func makeAgent(t *testing.T, conf *Config) (string, *Agent) { + dir, err := ioutil.TempDir("", "agent") + if err != nil { + t.Fatalf(fmt.Sprintf("err: %v", err)) + } + + conf.DataDir = dir + agent, err := Create(conf, nil) + if err != nil { + os.RemoveAll(dir) + t.Fatalf(fmt.Sprintf("err: %v", err)) + } + + return dir, agent +} + +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) + } +} diff --git a/command/agent/config_test.go b/command/agent/config_test.go new file mode 100644 index 000000000..0fdcf0d8a --- /dev/null +++ b/command/agent/config_test.go @@ -0,0 +1,3 @@ +package agent + +// TODO: diff --git a/command/agent/http_test.go b/command/agent/http_test.go new file mode 100644 index 000000000..0cf13f92c --- /dev/null +++ b/command/agent/http_test.go @@ -0,0 +1,15 @@ +package agent + +import ( + "testing" +) + +func makeHTTPServer(t *testing.T) (string, *HTTPServer) { + conf := nextConfig() + dir, agent := makeAgent(t, conf) + server, err := NewServer(agent, agent.logOutput, conf.HTTPAddr) + if err != nil { + t.Fatalf("err: %v", err) + } + return dir, server +} diff --git a/command/agent/status_endpoint_test.go b/command/agent/status_endpoint_test.go new file mode 100644 index 000000000..aaf0e4559 --- /dev/null +++ b/command/agent/status_endpoint_test.go @@ -0,0 +1,43 @@ +package agent + +import ( + "os" + "testing" + "time" +) + +func TestStatusLeader(t *testing.T) { + dir, srv := makeHTTPServer(t) + defer os.RemoveAll(dir) + defer srv.Shutdown() + defer srv.agent.Shutdown() + + // Wait for a leader + time.Sleep(100 * time.Millisecond) + + obj, err := srv.StatusLeader(nil) + if err != nil { + t.Fatalf("Err: %v", err) + } + val := obj.(string) + if val == "" { + t.Fatalf("bad addr: %v", obj) + } +} + +func TestStatusPeers(t *testing.T) { + dir, srv := makeHTTPServer(t) + defer os.RemoveAll(dir) + defer srv.Shutdown() + defer srv.agent.Shutdown() + + obj, err := srv.StatusPeers(nil) + if err != nil { + t.Fatalf("Err: %v", err) + } + + peers := obj.([]string) + if len(peers) != 1 { + t.Fatalf("bad peers: %v", peers) + } +}