github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/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 res.Member.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.Members); n != 1 {
   107  		t.Fatalf("expected 1 member, got: %d", n)
   108  	}
   109  	if m := mem.Members[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 (a *AgentMember) String() string {
   128  	return "{Name: " + a.Name + " Region: " + a.Tags["region"] + " DC: " + a.Tags["dc"] + "}"
   129  }
   130  
   131  func TestAgents_Sort(t *testing.T) {
   132  	var sortTests = []struct {
   133  		in  []*AgentMember
   134  		out []*AgentMember
   135  	}{
   136  		{
   137  			[]*AgentMember{
   138  				&AgentMember{Name: "nomad-2.vac.us-east",
   139  					Tags: map[string]string{"region": "us-east", "dc": "us-east-1c"}},
   140  				&AgentMember{Name: "nomad-1.global",
   141  					Tags: map[string]string{"region": "global", "dc": "dc1"}},
   142  				&AgentMember{Name: "nomad-1.vac.us-east",
   143  					Tags: map[string]string{"region": "us-east", "dc": "us-east-1c"}},
   144  			},
   145  			[]*AgentMember{
   146  				&AgentMember{Name: "nomad-1.global",
   147  					Tags: map[string]string{"region": "global", "dc": "dc1"}},
   148  				&AgentMember{Name: "nomad-1.vac.us-east",
   149  					Tags: map[string]string{"region": "us-east", "dc": "us-east-1c"}},
   150  				&AgentMember{Name: "nomad-2.vac.us-east",
   151  					Tags: map[string]string{"region": "us-east", "dc": "us-east-1c"}},
   152  			},
   153  		},
   154  		{
   155  			[]*AgentMember{
   156  				&AgentMember{Name: "nomad-02.tam.us-east",
   157  					Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
   158  				&AgentMember{Name: "nomad-02.pal.us-west",
   159  					Tags: map[string]string{"region": "us-west", "dc": "palo_alto"}},
   160  				&AgentMember{Name: "nomad-01.pal.us-west",
   161  					Tags: map[string]string{"region": "us-west", "dc": "palo_alto"}},
   162  				&AgentMember{Name: "nomad-01.tam.us-east",
   163  					Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
   164  			},
   165  			[]*AgentMember{
   166  				&AgentMember{Name: "nomad-01.tam.us-east",
   167  					Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
   168  				&AgentMember{Name: "nomad-02.tam.us-east",
   169  					Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
   170  				&AgentMember{Name: "nomad-01.pal.us-west",
   171  					Tags: map[string]string{"region": "us-west", "dc": "palo_alto"}},
   172  				&AgentMember{Name: "nomad-02.pal.us-west",
   173  					Tags: map[string]string{"region": "us-west", "dc": "palo_alto"}},
   174  			},
   175  		},
   176  		{
   177  			[]*AgentMember{
   178  				&AgentMember{Name: "nomad-02.tam.us-east",
   179  					Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
   180  				&AgentMember{Name: "nomad-02.ams.europe",
   181  					Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
   182  				&AgentMember{Name: "nomad-01.tam.us-east",
   183  					Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
   184  				&AgentMember{Name: "nomad-01.ams.europe",
   185  					Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
   186  			},
   187  			[]*AgentMember{
   188  				&AgentMember{Name: "nomad-01.ams.europe",
   189  					Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
   190  				&AgentMember{Name: "nomad-02.ams.europe",
   191  					Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
   192  				&AgentMember{Name: "nomad-01.tam.us-east",
   193  					Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
   194  				&AgentMember{Name: "nomad-02.tam.us-east",
   195  					Tags: map[string]string{"region": "us-east", "dc": "tampa"}},
   196  			},
   197  		},
   198  		{
   199  			[]*AgentMember{
   200  				&AgentMember{Name: "nomad-02.ber.europe",
   201  					Tags: map[string]string{"region": "europe", "dc": "berlin"}},
   202  				&AgentMember{Name: "nomad-02.ams.europe",
   203  					Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
   204  				&AgentMember{Name: "nomad-01.ams.europe",
   205  					Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
   206  				&AgentMember{Name: "nomad-01.ber.europe",
   207  					Tags: map[string]string{"region": "europe", "dc": "berlin"}},
   208  			},
   209  			[]*AgentMember{
   210  				&AgentMember{Name: "nomad-01.ams.europe",
   211  					Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
   212  				&AgentMember{Name: "nomad-02.ams.europe",
   213  					Tags: map[string]string{"region": "europe", "dc": "amsterdam"}},
   214  				&AgentMember{Name: "nomad-01.ber.europe",
   215  					Tags: map[string]string{"region": "europe", "dc": "berlin"}},
   216  				&AgentMember{Name: "nomad-02.ber.europe",
   217  					Tags: map[string]string{"region": "europe", "dc": "berlin"}},
   218  			},
   219  		},
   220  		{
   221  			[]*AgentMember{
   222  				&AgentMember{Name: "nomad-1.global"},
   223  				&AgentMember{Name: "nomad-3.global"},
   224  				&AgentMember{Name: "nomad-2.global"},
   225  			},
   226  			[]*AgentMember{
   227  				&AgentMember{Name: "nomad-1.global"},
   228  				&AgentMember{Name: "nomad-2.global"},
   229  				&AgentMember{Name: "nomad-3.global"},
   230  			},
   231  		},
   232  	}
   233  	for _, tt := range sortTests {
   234  		sort.Sort(AgentMembersNameSort(tt.in))
   235  		if !reflect.DeepEqual(tt.in, tt.out) {
   236  			t.Errorf("\necpected: %s\nget     : %s", tt.in, tt.out)
   237  		}
   238  	}
   239  }