github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/client/util_test.go (about)

     1  package client
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"reflect"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/hashicorp/nomad/nomad/mock"
    12  	"github.com/hashicorp/nomad/nomad/structs"
    13  )
    14  
    15  func TestDiffAllocs(t *testing.T) {
    16  	alloc1 := mock.Alloc() // Ignore
    17  	alloc2 := mock.Alloc() // Update
    18  	alloc2u := new(structs.Allocation)
    19  	*alloc2u = *alloc2
    20  	alloc2u.ModifyIndex += 1
    21  	alloc3 := mock.Alloc() // Remove
    22  	alloc4 := mock.Alloc() // Add
    23  
    24  	exist := []*structs.Allocation{
    25  		alloc1,
    26  		alloc2,
    27  		alloc3,
    28  	}
    29  	updated := []*structs.Allocation{
    30  		alloc1,
    31  		alloc2u,
    32  		alloc4,
    33  	}
    34  
    35  	result := diffAllocs(exist, updated)
    36  
    37  	if len(result.ignore) != 1 || result.ignore[0] != alloc1 {
    38  		t.Fatalf("Bad: %#v", result.ignore)
    39  	}
    40  	if len(result.added) != 1 || result.added[0] != alloc4 {
    41  		t.Fatalf("Bad: %#v", result.added)
    42  	}
    43  	if len(result.removed) != 1 || result.removed[0] != alloc3 {
    44  		t.Fatalf("Bad: %#v", result.removed)
    45  	}
    46  	if len(result.updated) != 1 {
    47  		t.Fatalf("Bad: %#v", result.updated)
    48  	}
    49  	if result.updated[0].exist != alloc2 || result.updated[0].updated != alloc2u {
    50  		t.Fatalf("Bad: %#v", result.updated)
    51  	}
    52  }
    53  
    54  func TestRandomStagger(t *testing.T) {
    55  	intv := time.Minute
    56  	for i := 0; i < 10; i++ {
    57  		stagger := randomStagger(intv)
    58  		if stagger < 0 || stagger >= intv {
    59  			t.Fatalf("Bad: %v", stagger)
    60  		}
    61  	}
    62  }
    63  
    64  func TestShuffleStrings(t *testing.T) {
    65  	// Generate input
    66  	inp := make([]string, 10)
    67  	for idx := range inp {
    68  		inp[idx] = structs.GenerateUUID()
    69  	}
    70  
    71  	// Copy the input
    72  	orig := make([]string, len(inp))
    73  	copy(orig, inp)
    74  
    75  	// Shuffle
    76  	shuffleStrings(inp)
    77  
    78  	// Ensure order is not the same
    79  	if reflect.DeepEqual(inp, orig) {
    80  		t.Fatalf("shuffle failed")
    81  	}
    82  }
    83  
    84  func TestPersistRestoreState(t *testing.T) {
    85  	dir, err := ioutil.TempDir("", "nomad")
    86  	if err != nil {
    87  		t.Fatalf("err: %s", err)
    88  	}
    89  	defer os.RemoveAll(dir)
    90  
    91  	// Use a state path inside a non-existent directory. This
    92  	// verifies that the directory is created properly.
    93  	statePath := filepath.Join(dir, "subdir", "test-persist")
    94  
    95  	type stateTest struct {
    96  		Foo int
    97  		Bar string
    98  		Baz bool
    99  	}
   100  	state := stateTest{
   101  		Foo: 42,
   102  		Bar: "the quick brown fox",
   103  		Baz: true,
   104  	}
   105  
   106  	err = persistState(statePath, &state)
   107  	if err != nil {
   108  		t.Fatalf("err: %v", err)
   109  	}
   110  
   111  	var out stateTest
   112  	err = restoreState(statePath, &out)
   113  	if err != nil {
   114  		t.Fatalf("err: %v", err)
   115  	}
   116  
   117  	if !reflect.DeepEqual(state, out) {
   118  		t.Fatalf("bad: %#v %#v", state, out)
   119  	}
   120  }