github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/client/util_test.go (about)

     1  package client
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/nomad/nomad/mock"
     8  	"github.com/hashicorp/nomad/nomad/structs"
     9  )
    10  
    11  func TestDiffAllocs(t *testing.T) {
    12  	t.Parallel()
    13  	alloc1 := mock.Alloc() // Ignore
    14  	alloc2 := mock.Alloc() // Update
    15  	alloc2u := new(structs.Allocation)
    16  	*alloc2u = *alloc2
    17  	alloc2u.AllocModifyIndex += 1
    18  	alloc3 := mock.Alloc() // Remove
    19  	alloc4 := mock.Alloc() // Add
    20  
    21  	exist := []*structs.Allocation{
    22  		alloc1,
    23  		alloc2,
    24  		alloc3,
    25  	}
    26  	update := &allocUpdates{
    27  		pulled: map[string]*structs.Allocation{
    28  			alloc2u.ID: alloc2u,
    29  			alloc4.ID:  alloc4,
    30  		},
    31  		filtered: map[string]struct{}{
    32  			alloc1.ID: struct{}{},
    33  		},
    34  	}
    35  
    36  	result := diffAllocs(exist, update)
    37  
    38  	if len(result.ignore) != 1 || result.ignore[0] != alloc1 {
    39  		t.Fatalf("Bad: %#v", result.ignore)
    40  	}
    41  	if len(result.added) != 1 || result.added[0] != alloc4 {
    42  		t.Fatalf("Bad: %#v", result.added)
    43  	}
    44  	if len(result.removed) != 1 || result.removed[0] != alloc3 {
    45  		t.Fatalf("Bad: %#v", result.removed)
    46  	}
    47  	if len(result.updated) != 1 {
    48  		t.Fatalf("Bad: %#v", result.updated)
    49  	}
    50  	if result.updated[0].exist != alloc2 || result.updated[0].updated != alloc2u {
    51  		t.Fatalf("Bad: %#v", result.updated)
    52  	}
    53  }
    54  
    55  func TestShuffleStrings(t *testing.T) {
    56  	t.Parallel()
    57  	// Generate input
    58  	inp := make([]string, 10)
    59  	for idx := range inp {
    60  		inp[idx] = structs.GenerateUUID()
    61  	}
    62  
    63  	// Copy the input
    64  	orig := make([]string, len(inp))
    65  	copy(orig, inp)
    66  
    67  	// Shuffle
    68  	shuffleStrings(inp)
    69  
    70  	// Ensure order is not the same
    71  	if reflect.DeepEqual(inp, orig) {
    72  		t.Fatalf("shuffle failed")
    73  	}
    74  }