github.com/djenriquez/nomad-1@v0.8.1/client/util_test.go (about)

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