github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/nomad/util_test.go (about)

     1  package nomad
     2  
     3  import (
     4  	"net"
     5  	"reflect"
     6  	"testing"
     7  
     8  	version "github.com/hashicorp/go-version"
     9  	"github.com/hashicorp/nomad/helper/uuid"
    10  	"github.com/hashicorp/serf/serf"
    11  )
    12  
    13  func TestIsNomadServer(t *testing.T) {
    14  	t.Parallel()
    15  	m := serf.Member{
    16  		Name:   "foo",
    17  		Addr:   net.IP([]byte{127, 0, 0, 1}),
    18  		Status: serf.StatusAlive,
    19  		Tags: map[string]string{
    20  			"role":     "nomad",
    21  			"region":   "aws",
    22  			"dc":       "east-aws",
    23  			"rpc_addr": "1.1.1.1",
    24  			"port":     "10000",
    25  			"vsn":      "1",
    26  			"raft_vsn": "2",
    27  			"build":    "0.7.0+ent",
    28  		},
    29  	}
    30  	valid, parts := isNomadServer(m)
    31  	if !valid || parts.Region != "aws" ||
    32  		parts.Datacenter != "east-aws" || parts.Port != 10000 {
    33  		t.Fatalf("bad: %v %v", valid, parts)
    34  	}
    35  	if parts.Name != "foo" {
    36  		t.Fatalf("bad: %v", parts)
    37  	}
    38  	if parts.Bootstrap {
    39  		t.Fatalf("unexpected bootstrap")
    40  	}
    41  	if parts.Expect != 0 {
    42  		t.Fatalf("bad: %v", parts.Expect)
    43  	}
    44  	if parts.Status != serf.StatusAlive {
    45  		t.Fatalf("bad: %v", parts.Status)
    46  	}
    47  	if parts.RaftVersion != 2 {
    48  		t.Fatalf("bad: %v", parts.RaftVersion)
    49  	}
    50  	if parts.RPCAddr.String() != "1.1.1.1:10000" {
    51  		t.Fatalf("bad: %v", parts.RPCAddr.String())
    52  	}
    53  	if seg := parts.Build.Segments(); len(seg) != 3 {
    54  		t.Fatalf("bad: %v", parts.Build)
    55  	} else if seg[0] != 0 && seg[1] != 7 && seg[2] != 0 {
    56  		t.Fatalf("bad: %v", parts.Build)
    57  	}
    58  
    59  	m.Tags["bootstrap"] = "1"
    60  	valid, parts = isNomadServer(m)
    61  	if !valid || !parts.Bootstrap {
    62  		t.Fatalf("expected bootstrap")
    63  	}
    64  	if parts.Addr.String() != "127.0.0.1:10000" {
    65  		t.Fatalf("bad addr: %v", parts.Addr)
    66  	}
    67  	if parts.MajorVersion != 1 {
    68  		t.Fatalf("bad: %v", parts)
    69  	}
    70  
    71  	m.Tags["expect"] = "3"
    72  	delete(m.Tags, "bootstrap")
    73  	valid, parts = isNomadServer(m)
    74  	if !valid || parts.Expect != 3 {
    75  		t.Fatalf("bad: %v", parts.Expect)
    76  	}
    77  }
    78  
    79  func TestServersMeetMinimumVersion(t *testing.T) {
    80  	t.Parallel()
    81  	makeMember := func(version string) serf.Member {
    82  		return serf.Member{
    83  			Name: "foo",
    84  			Addr: net.IP([]byte{127, 0, 0, 1}),
    85  			Tags: map[string]string{
    86  				"role":   "nomad",
    87  				"region": "aws",
    88  				"dc":     "east-aws",
    89  				"port":   "10000",
    90  				"build":  version,
    91  				"vsn":    "1",
    92  			},
    93  			Status: serf.StatusAlive,
    94  		}
    95  	}
    96  
    97  	cases := []struct {
    98  		members  []serf.Member
    99  		ver      *version.Version
   100  		expected bool
   101  	}{
   102  		// One server, meets reqs
   103  		{
   104  			members: []serf.Member{
   105  				makeMember("0.7.5"),
   106  			},
   107  			ver:      version.Must(version.NewVersion("0.7.5")),
   108  			expected: true,
   109  		},
   110  		// One server in dev, meets reqs
   111  		{
   112  			members: []serf.Member{
   113  				makeMember("0.8.5-dev"),
   114  			},
   115  			ver:      version.Must(version.NewVersion("0.7.5")),
   116  			expected: true,
   117  		},
   118  		// One server with meta, meets reqs
   119  		{
   120  			members: []serf.Member{
   121  				makeMember("0.7.5+ent"),
   122  			},
   123  			ver:      version.Must(version.NewVersion("0.7.5")),
   124  			expected: true,
   125  		},
   126  		// One server, doesn't meet reqs
   127  		{
   128  			members: []serf.Member{
   129  				makeMember("0.7.5"),
   130  			},
   131  			ver:      version.Must(version.NewVersion("0.8.0")),
   132  			expected: false,
   133  		},
   134  		// Multiple servers, meets req version
   135  		{
   136  			members: []serf.Member{
   137  				makeMember("0.7.5"),
   138  				makeMember("0.8.0"),
   139  			},
   140  			ver:      version.Must(version.NewVersion("0.7.5")),
   141  			expected: true,
   142  		},
   143  		// Multiple servers, doesn't meet req version
   144  		{
   145  			members: []serf.Member{
   146  				makeMember("0.7.5"),
   147  				makeMember("0.8.0"),
   148  			},
   149  			ver:      version.Must(version.NewVersion("0.8.0")),
   150  			expected: false,
   151  		},
   152  	}
   153  
   154  	for _, tc := range cases {
   155  		result := ServersMeetMinimumVersion(tc.members, tc.ver)
   156  		if result != tc.expected {
   157  			t.Fatalf("bad: %v, %v, %v", result, tc.ver.String(), tc)
   158  		}
   159  	}
   160  }
   161  
   162  func TestShuffleStrings(t *testing.T) {
   163  	t.Parallel()
   164  	// Generate input
   165  	inp := make([]string, 10)
   166  	for idx := range inp {
   167  		inp[idx] = uuid.Generate()
   168  	}
   169  
   170  	// Copy the input
   171  	orig := make([]string, len(inp))
   172  	copy(orig, inp)
   173  
   174  	// Shuffle
   175  	shuffleStrings(inp)
   176  
   177  	// Ensure order is not the same
   178  	if reflect.DeepEqual(inp, orig) {
   179  		t.Fatalf("shuffle failed")
   180  	}
   181  }
   182  
   183  func TestMaxUint64(t *testing.T) {
   184  	t.Parallel()
   185  	if maxUint64(1, 2) != 2 {
   186  		t.Fatalf("bad")
   187  	}
   188  	if maxUint64(2, 2) != 2 {
   189  		t.Fatalf("bad")
   190  	}
   191  	if maxUint64(2, 1) != 2 {
   192  		t.Fatalf("bad")
   193  	}
   194  }