github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/nomad/util_test.go (about)

     1  package nomad
     2  
     3  import (
     4  	"net"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/nomad/nomad/structs"
     9  	"github.com/hashicorp/serf/serf"
    10  )
    11  
    12  func TestIsNomadServer(t *testing.T) {
    13  	m := serf.Member{
    14  		Name: "foo",
    15  		Addr: net.IP([]byte{127, 0, 0, 1}),
    16  		Tags: map[string]string{
    17  			"role":   "nomad",
    18  			"region": "aws",
    19  			"dc":     "east-aws",
    20  			"port":   "10000",
    21  			"vsn":    "1",
    22  		},
    23  	}
    24  	valid, parts := isNomadServer(m)
    25  	if !valid || parts.Region != "aws" ||
    26  		parts.Datacenter != "east-aws" || parts.Port != 10000 {
    27  		t.Fatalf("bad: %v %v", valid, parts)
    28  	}
    29  	if parts.Name != "foo" {
    30  		t.Fatalf("bad: %v", parts)
    31  	}
    32  	if parts.Bootstrap {
    33  		t.Fatalf("unexpected bootstrap")
    34  	}
    35  	if parts.Expect != 0 {
    36  		t.Fatalf("bad: %v", parts.Expect)
    37  	}
    38  
    39  	m.Tags["bootstrap"] = "1"
    40  	valid, parts = isNomadServer(m)
    41  	if !valid || !parts.Bootstrap {
    42  		t.Fatalf("expected bootstrap")
    43  	}
    44  	if parts.Addr.String() != "127.0.0.1:10000" {
    45  		t.Fatalf("bad addr: %v", parts.Addr)
    46  	}
    47  	if parts.MajorVersion != 1 {
    48  		t.Fatalf("bad: %v", parts)
    49  	}
    50  
    51  	m.Tags["expect"] = "3"
    52  	delete(m.Tags, "bootstrap")
    53  	valid, parts = isNomadServer(m)
    54  	if !valid || parts.Expect != 3 {
    55  		t.Fatalf("bad: %v", parts.Expect)
    56  	}
    57  }
    58  
    59  func TestShuffleStrings(t *testing.T) {
    60  	// Generate input
    61  	inp := make([]string, 10)
    62  	for idx := range inp {
    63  		inp[idx] = structs.GenerateUUID()
    64  	}
    65  
    66  	// Copy the input
    67  	orig := make([]string, len(inp))
    68  	copy(orig, inp)
    69  
    70  	// Shuffle
    71  	shuffleStrings(inp)
    72  
    73  	// Ensure order is not the same
    74  	if reflect.DeepEqual(inp, orig) {
    75  		t.Fatalf("shuffle failed")
    76  	}
    77  }
    78  
    79  func TestMaxUint64(t *testing.T) {
    80  	if maxUint64(1, 2) != 2 {
    81  		t.Fatalf("bad")
    82  	}
    83  	if maxUint64(2, 2) != 2 {
    84  		t.Fatalf("bad")
    85  	}
    86  	if maxUint64(2, 1) != 2 {
    87  		t.Fatalf("bad")
    88  	}
    89  }