open-nomad/api/agent_test.go

273 lines
7.5 KiB
Go
Raw Normal View History

2015-09-08 18:41:03 +00:00
package api
import (
"reflect"
"sort"
2015-09-08 18:41:03 +00:00
"testing"
2015-09-14 20:16:42 +00:00
"github.com/hashicorp/nomad/testutil"
2015-09-08 18:41:03 +00:00
)
func TestAgent_Self(t *testing.T) {
c, s := makeClient(t, nil, nil)
defer s.Stop()
// Get a handle on the Agent endpoints
a := c.Agent()
// Query the endpoint
res, err := a.Self()
if err != nil {
t.Fatalf("err: %s", err)
}
// Check that we got a valid response
if name, ok := res["member"]["Name"]; !ok || name == "" {
t.Fatalf("bad member name in response: %#v", res)
}
// Local cache was populated
if a.nodeName == "" || a.datacenter == "" || a.region == "" {
t.Fatalf("cache should be populated, got: %#v", a)
}
2015-09-08 18:41:03 +00:00
}
func TestAgent_NodeName(t *testing.T) {
c, s := makeClient(t, nil, nil)
defer s.Stop()
a := c.Agent()
// Query the agent for the node name
res, err := a.NodeName()
if err != nil {
t.Fatalf("err: %s", err)
}
if res == "" {
t.Fatalf("expected node name, got nothing")
}
}
func TestAgent_Datacenter(t *testing.T) {
c, s := makeClient(t, nil, nil)
defer s.Stop()
a := c.Agent()
// Query the agent for the datacenter
dc, err := a.Datacenter()
if err != nil {
t.Fatalf("err: %s", err)
}
if dc != "dc1" {
t.Fatalf("expected dc1, got: %q", dc)
}
}
2015-09-08 21:26:53 +00:00
func TestAgent_Join(t *testing.T) {
2015-09-11 22:40:54 +00:00
c1, s1 := makeClient(t, nil, nil)
defer s1.Stop()
2015-09-14 20:16:42 +00:00
a1 := c1.Agent()
2015-09-11 22:40:54 +00:00
2015-09-14 20:16:42 +00:00
_, s2 := makeClient(t, nil, func(c *testutil.TestServerConfig) {
2015-09-25 04:17:33 +00:00
c.Server.BootstrapExpect = 0
2015-09-11 22:40:54 +00:00
})
defer s2.Stop()
2015-09-08 21:26:53 +00:00
// Attempting to join a non-existent host returns error
2015-09-11 22:40:54 +00:00
n, err := a1.Join("nope")
if err == nil {
2015-09-08 21:26:53 +00:00
t.Fatalf("expected error, got nothing")
}
2015-09-11 22:40:54 +00:00
if n != 0 {
t.Fatalf("expected 0 nodes, got: %d", n)
}
2015-09-08 21:26:53 +00:00
2015-09-11 22:40:54 +00:00
// Returns correctly if join succeeds
n, err = a1.Join(s2.SerfAddr)
if err != nil {
2015-09-08 21:26:53 +00:00
t.Fatalf("err: %s", err)
}
2015-09-11 22:40:54 +00:00
if n != 1 {
t.Fatalf("expected 1 node, got: %d", n)
}
2015-09-08 21:26:53 +00:00
}
2015-09-08 21:47:29 +00:00
func TestAgent_Members(t *testing.T) {
c, s := makeClient(t, nil, nil)
defer s.Stop()
a := c.Agent()
// Query nomad for all the known members
mem, err := a.Members()
if err != nil {
t.Fatalf("err: %s", err)
}
// Check that we got the expected result
if n := len(mem); n != 1 {
t.Fatalf("expected 1 member, got: %d", n)
}
if m := mem[0]; m.Name == "" || m.Addr == "" || m.Port == 0 {
t.Fatalf("bad member: %#v", m)
}
}
func TestAgent_ForceLeave(t *testing.T) {
c, s := makeClient(t, nil, nil)
defer s.Stop()
a := c.Agent()
// Force-leave on a non-existent node does not error
if err := a.ForceLeave("nope"); err != nil {
t.Fatalf("err: %s", err)
}
// TODO: test force-leave on an existing node
}
2015-09-25 04:17:33 +00:00
func TestAgent_SetServers(t *testing.T) {
c, s := makeClient(t, nil, func(c *testutil.TestServerConfig) {
c.Client.Enabled = true
c.Server.BootstrapExpect = 0
})
defer s.Stop()
a := c.Agent()
// Attempting to set an empty list errors
err := a.SetServers([]string{})
if err == nil {
t.Fatalf("expected error, got nothing")
}
// Setting a valid list works
err = a.SetServers([]string{"foo", "bar"})
if err != nil {
t.Fatalf("err: %s", err)
}
// Returns the proper list of servers
out, err := a.Servers()
if err != nil {
t.Fatalf("err: %s", err)
}
if n := len(out); n != 2 {
t.Fatalf("expected 2 servers, got: %d", n)
}
if out[0] != "foo" || out[1] != "bar" {
t.Fatalf("bad server list: %v", out)
}
}
func (a *AgentMember) String() string {
return "{Name: " + a.Name + " Region: " + a.Tags["region"] + " DC: " + a.Tags["dc"] + "}"
}
func TestAgents_Sort(t *testing.T) {
var sortTests = []struct {
in []*AgentMember
out []*AgentMember
}{
{
[]*AgentMember{
&AgentMember{Name: "nomad-2.vac.us-east",
Tags: map[string]string{"region": "us-east", "dc": "us-east-1c"}},
&AgentMember{Name: "nomad-1.global",
Tags: map[string]string{"region": "global", "dc": "dc1"}},
&AgentMember{Name: "nomad-1.vac.us-east",
Tags: map[string]string{"region": "us-east", "dc": "us-east-1c"}},
},
[]*AgentMember{
&AgentMember{Name: "nomad-1.global",
Tags: map[string]string{"region": "global", "dc": "dc1"}},
&AgentMember{Name: "nomad-1.vac.us-east",
Tags: map[string]string{"region": "us-east", "dc": "us-east-1c"}},
&AgentMember{Name: "nomad-2.vac.us-east",
Tags: map[string]string{"region": "us-east", "dc": "us-east-1c"}},
},
},
{
[]*AgentMember{
&AgentMember{Name: "nomad-02.tam.us-east",
Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
&AgentMember{Name: "nomad-02.pal.us-west",
Tags: map[string]string{"region": "us-west", "dc": "palo_alto"}},
&AgentMember{Name: "nomad-01.pal.us-west",
Tags: map[string]string{"region": "us-west", "dc": "palo_alto"}},
&AgentMember{Name: "nomad-01.tam.us-east",
Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
},
[]*AgentMember{
&AgentMember{Name: "nomad-01.tam.us-east",
Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
&AgentMember{Name: "nomad-02.tam.us-east",
Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
&AgentMember{Name: "nomad-01.pal.us-west",
Tags: map[string]string{"region": "us-west", "dc": "palo_alto"}},
&AgentMember{Name: "nomad-02.pal.us-west",
Tags: map[string]string{"region": "us-west", "dc": "palo_alto"}},
},
},
{
[]*AgentMember{
&AgentMember{Name: "nomad-02.tam.us-east",
Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
&AgentMember{Name: "nomad-02.ams.europe",
Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
&AgentMember{Name: "nomad-01.tam.us-east",
Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
&AgentMember{Name: "nomad-01.ams.europe",
Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
},
[]*AgentMember{
&AgentMember{Name: "nomad-01.ams.europe",
Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
&AgentMember{Name: "nomad-02.ams.europe",
Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
&AgentMember{Name: "nomad-01.tam.us-east",
Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
&AgentMember{Name: "nomad-02.tam.us-east",
Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
},
},
{
[]*AgentMember{
&AgentMember{Name: "nomad-02.ber.europe",
Tags: map[string]string{"region": "europe", "dc": "berlin"}},
&AgentMember{Name: "nomad-02.ams.europe",
Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
&AgentMember{Name: "nomad-01.ams.europe",
Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
&AgentMember{Name: "nomad-01.ber.europe",
Tags: map[string]string{"region": "europe", "dc": "berlin"}},
},
[]*AgentMember{
&AgentMember{Name: "nomad-01.ams.europe",
Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
&AgentMember{Name: "nomad-02.ams.europe",
Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
&AgentMember{Name: "nomad-01.ber.europe",
Tags: map[string]string{"region": "europe", "dc": "berlin"}},
&AgentMember{Name: "nomad-02.ber.europe",
Tags: map[string]string{"region": "europe", "dc": "berlin"}},
},
},
{
[]*AgentMember{
&AgentMember{Name: "nomad-1.global"},
&AgentMember{Name: "nomad-3.global"},
&AgentMember{Name: "nomad-2.global"},
},
[]*AgentMember{
&AgentMember{Name: "nomad-1.global"},
&AgentMember{Name: "nomad-2.global"},
&AgentMember{Name: "nomad-3.global"},
},
},
}
for _, tt := range sortTests {
sort.Sort(AgentMembersNameSort(tt.in))
if !reflect.DeepEqual(tt.in, tt.out) {
t.Errorf("\necpected: %s\nget : %s", tt.in, tt.out)
}
}
}