github.com/huiliang/nomad@v0.2.1-0.20151124023127-7a8b664699ff/api/agent_test.go (about)

     1  package api
     2  
     3  import (
     4  	"reflect"
     5  	"sort"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/nomad/testutil"
     9  )
    10  
    11  func TestAgent_Self(t *testing.T) {
    12  	c, s := makeClient(t, nil, nil)
    13  	defer s.Stop()
    14  
    15  	// Get a handle on the Agent endpoints
    16  	a := c.Agent()
    17  
    18  	// Query the endpoint
    19  	res, err := a.Self()
    20  	if err != nil {
    21  		t.Fatalf("err: %s", err)
    22  	}
    23  
    24  	// Check that we got a valid response
    25  	if name, ok := res["member"]["Name"]; !ok || name == "" {
    26  		t.Fatalf("bad member name in response: %#v", res)
    27  	}
    28  
    29  	// Local cache was populated
    30  	if a.nodeName == "" || a.datacenter == "" || a.region == "" {
    31  		t.Fatalf("cache should be populated, got: %#v", a)
    32  	}
    33  }
    34  
    35  func TestAgent_NodeName(t *testing.T) {
    36  	c, s := makeClient(t, nil, nil)
    37  	defer s.Stop()
    38  	a := c.Agent()
    39  
    40  	// Query the agent for the node name
    41  	res, err := a.NodeName()
    42  	if err != nil {
    43  		t.Fatalf("err: %s", err)
    44  	}
    45  	if res == "" {
    46  		t.Fatalf("expected node name, got nothing")
    47  	}
    48  }
    49  
    50  func TestAgent_Datacenter(t *testing.T) {
    51  	c, s := makeClient(t, nil, nil)
    52  	defer s.Stop()
    53  	a := c.Agent()
    54  
    55  	// Query the agent for the datacenter
    56  	dc, err := a.Datacenter()
    57  	if err != nil {
    58  		t.Fatalf("err: %s", err)
    59  	}
    60  	if dc != "dc1" {
    61  		t.Fatalf("expected dc1, got: %q", dc)
    62  	}
    63  }
    64  
    65  func TestAgent_Join(t *testing.T) {
    66  	c1, s1 := makeClient(t, nil, nil)
    67  	defer s1.Stop()
    68  	a1 := c1.Agent()
    69  
    70  	_, s2 := makeClient(t, nil, func(c *testutil.TestServerConfig) {
    71  		c.Server.BootstrapExpect = 0
    72  	})
    73  	defer s2.Stop()
    74  
    75  	// Attempting to join a non-existent host returns error
    76  	n, err := a1.Join("nope")
    77  	if err == nil {
    78  		t.Fatalf("expected error, got nothing")
    79  	}
    80  	if n != 0 {
    81  		t.Fatalf("expected 0 nodes, got: %d", n)
    82  	}
    83  
    84  	// Returns correctly if join succeeds
    85  	n, err = a1.Join(s2.SerfAddr)
    86  	if err != nil {
    87  		t.Fatalf("err: %s", err)
    88  	}
    89  	if n != 1 {
    90  		t.Fatalf("expected 1 node, got: %d", n)
    91  	}
    92  }
    93  
    94  func TestAgent_Members(t *testing.T) {
    95  	c, s := makeClient(t, nil, nil)
    96  	defer s.Stop()
    97  	a := c.Agent()
    98  
    99  	// Query nomad for all the known members
   100  	mem, err := a.Members()
   101  	if err != nil {
   102  		t.Fatalf("err: %s", err)
   103  	}
   104  
   105  	// Check that we got the expected result
   106  	if n := len(mem); n != 1 {
   107  		t.Fatalf("expected 1 member, got: %d", n)
   108  	}
   109  	if m := mem[0]; m.Name == "" || m.Addr == "" || m.Port == 0 {
   110  		t.Fatalf("bad member: %#v", m)
   111  	}
   112  }
   113  
   114  func TestAgent_ForceLeave(t *testing.T) {
   115  	c, s := makeClient(t, nil, nil)
   116  	defer s.Stop()
   117  	a := c.Agent()
   118  
   119  	// Force-leave on a non-existent node does not error
   120  	if err := a.ForceLeave("nope"); err != nil {
   121  		t.Fatalf("err: %s", err)
   122  	}
   123  
   124  	// TODO: test force-leave on an existing node
   125  }
   126  
   127  func TestAgent_SetServers(t *testing.T) {
   128  	c, s := makeClient(t, nil, func(c *testutil.TestServerConfig) {
   129  		c.Client.Enabled = true
   130  		c.Server.BootstrapExpect = 0
   131  	})
   132  	defer s.Stop()
   133  	a := c.Agent()
   134  
   135  	// Attempting to set an empty list errors
   136  	err := a.SetServers([]string{})
   137  	if err == nil {
   138  		t.Fatalf("expected error, got nothing")
   139  	}
   140  
   141  	// Setting a valid list works
   142  	err = a.SetServers([]string{"foo", "bar"})
   143  	if err != nil {
   144  		t.Fatalf("err: %s", err)
   145  	}
   146  
   147  	// Returns the proper list of servers
   148  	out, err := a.Servers()
   149  	if err != nil {
   150  		t.Fatalf("err: %s", err)
   151  	}
   152  	if n := len(out); n != 2 {
   153  		t.Fatalf("expected 2 servers, got: %d", n)
   154  	}
   155  	if out[0] != "foo" || out[1] != "bar" {
   156  		t.Fatalf("bad server list: %v", out)
   157  	}
   158  }
   159  
   160  func (a *AgentMember) String() string {
   161  	return "{Name: " + a.Name + " Region: " + a.Tags["region"] + " DC: " + a.Tags["dc"] + "}"
   162  }
   163  
   164  func TestAgents_Sort(t *testing.T) {
   165  	var sortTests = []struct {
   166  		in  []*AgentMember
   167  		out []*AgentMember
   168  	}{
   169  		{
   170  			[]*AgentMember{
   171  				&AgentMember{Name: "nomad-2.vac.us-east",
   172  					Tags: map[string]string{"region": "us-east", "dc": "us-east-1c"}},
   173  				&AgentMember{Name: "nomad-1.global",
   174  					Tags: map[string]string{"region": "global", "dc": "dc1"}},
   175  				&AgentMember{Name: "nomad-1.vac.us-east",
   176  					Tags: map[string]string{"region": "us-east", "dc": "us-east-1c"}},
   177  			},
   178  			[]*AgentMember{
   179  				&AgentMember{Name: "nomad-1.global",
   180  					Tags: map[string]string{"region": "global", "dc": "dc1"}},
   181  				&AgentMember{Name: "nomad-1.vac.us-east",
   182  					Tags: map[string]string{"region": "us-east", "dc": "us-east-1c"}},
   183  				&AgentMember{Name: "nomad-2.vac.us-east",
   184  					Tags: map[string]string{"region": "us-east", "dc": "us-east-1c"}},
   185  			},
   186  		},
   187  		{
   188  			[]*AgentMember{
   189  				&AgentMember{Name: "nomad-02.tam.us-east",
   190  					Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
   191  				&AgentMember{Name: "nomad-02.pal.us-west",
   192  					Tags: map[string]string{"region": "us-west", "dc": "palo_alto"}},
   193  				&AgentMember{Name: "nomad-01.pal.us-west",
   194  					Tags: map[string]string{"region": "us-west", "dc": "palo_alto"}},
   195  				&AgentMember{Name: "nomad-01.tam.us-east",
   196  					Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
   197  			},
   198  			[]*AgentMember{
   199  				&AgentMember{Name: "nomad-01.tam.us-east",
   200  					Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
   201  				&AgentMember{Name: "nomad-02.tam.us-east",
   202  					Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
   203  				&AgentMember{Name: "nomad-01.pal.us-west",
   204  					Tags: map[string]string{"region": "us-west", "dc": "palo_alto"}},
   205  				&AgentMember{Name: "nomad-02.pal.us-west",
   206  					Tags: map[string]string{"region": "us-west", "dc": "palo_alto"}},
   207  			},
   208  		},
   209  		{
   210  			[]*AgentMember{
   211  				&AgentMember{Name: "nomad-02.tam.us-east",
   212  					Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
   213  				&AgentMember{Name: "nomad-02.ams.europe",
   214  					Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
   215  				&AgentMember{Name: "nomad-01.tam.us-east",
   216  					Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
   217  				&AgentMember{Name: "nomad-01.ams.europe",
   218  					Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
   219  			},
   220  			[]*AgentMember{
   221  				&AgentMember{Name: "nomad-01.ams.europe",
   222  					Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
   223  				&AgentMember{Name: "nomad-02.ams.europe",
   224  					Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
   225  				&AgentMember{Name: "nomad-01.tam.us-east",
   226  					Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
   227  				&AgentMember{Name: "nomad-02.tam.us-east",
   228  					Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
   229  			},
   230  		},
   231  		{
   232  			[]*AgentMember{
   233  				&AgentMember{Name: "nomad-02.ber.europe",
   234  					Tags: map[string]string{"region": "europe", "dc": "berlin"}},
   235  				&AgentMember{Name: "nomad-02.ams.europe",
   236  					Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
   237  				&AgentMember{Name: "nomad-01.ams.europe",
   238  					Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
   239  				&AgentMember{Name: "nomad-01.ber.europe",
   240  					Tags: map[string]string{"region": "europe", "dc": "berlin"}},
   241  			},
   242  			[]*AgentMember{
   243  				&AgentMember{Name: "nomad-01.ams.europe",
   244  					Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
   245  				&AgentMember{Name: "nomad-02.ams.europe",
   246  					Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
   247  				&AgentMember{Name: "nomad-01.ber.europe",
   248  					Tags: map[string]string{"region": "europe", "dc": "berlin"}},
   249  				&AgentMember{Name: "nomad-02.ber.europe",
   250  					Tags: map[string]string{"region": "europe", "dc": "berlin"}},
   251  			},
   252  		},
   253  		{
   254  			[]*AgentMember{
   255  				&AgentMember{Name: "nomad-1.global"},
   256  				&AgentMember{Name: "nomad-3.global"},
   257  				&AgentMember{Name: "nomad-2.global"},
   258  			},
   259  			[]*AgentMember{
   260  				&AgentMember{Name: "nomad-1.global"},
   261  				&AgentMember{Name: "nomad-2.global"},
   262  				&AgentMember{Name: "nomad-3.global"},
   263  			},
   264  		},
   265  	}
   266  	for _, tt := range sortTests {
   267  		sort.Sort(AgentMembersNameSort(tt.in))
   268  		if !reflect.DeepEqual(tt.in, tt.out) {
   269  			t.Errorf("\necpected: %s\nget     : %s", tt.in, tt.out)
   270  		}
   271  	}
   272  }