github.com/mattyr/nomad@v0.3.3-0.20160919021406-3485a065154a/api/allocations_test.go (about)

     1  package api
     2  
     3  import (
     4  	"reflect"
     5  	"sort"
     6  	"testing"
     7  )
     8  
     9  func TestAllocations_List(t *testing.T) {
    10  	c, s := makeClient(t, nil, nil)
    11  	defer s.Stop()
    12  	a := c.Allocations()
    13  
    14  	// Querying when no allocs exist returns nothing
    15  	allocs, qm, err := a.List(nil)
    16  	if err != nil {
    17  		t.Fatalf("err: %s", err)
    18  	}
    19  	if qm.LastIndex != 0 {
    20  		t.Fatalf("bad index: %d", qm.LastIndex)
    21  	}
    22  	if n := len(allocs); n != 0 {
    23  		t.Fatalf("expected 0 allocs, got: %d", n)
    24  	}
    25  
    26  	// TODO: do something that causes an allocation to actually happen
    27  	// so we can query for them.
    28  	return
    29  
    30  	job := &Job{
    31  		ID:   "job1",
    32  		Name: "Job #1",
    33  		Type: JobTypeService,
    34  	}
    35  	eval, _, err := c.Jobs().Register(job, nil)
    36  	if err != nil {
    37  		t.Fatalf("err: %s", err)
    38  	}
    39  
    40  	// List the allocations again
    41  	allocs, qm, err = a.List(nil)
    42  	if err != nil {
    43  		t.Fatalf("err: %s", err)
    44  	}
    45  	if qm.LastIndex == 0 {
    46  		t.Fatalf("bad index: %d", qm.LastIndex)
    47  	}
    48  
    49  	// Check that we got the allocation back
    50  	if len(allocs) == 0 || allocs[0].EvalID != eval {
    51  		t.Fatalf("bad: %#v", allocs)
    52  	}
    53  }
    54  
    55  func TestAllocations_PrefixList(t *testing.T) {
    56  	c, s := makeClient(t, nil, nil)
    57  	defer s.Stop()
    58  	a := c.Allocations()
    59  
    60  	// Querying when no allocs exist returns nothing
    61  	allocs, qm, err := a.PrefixList("")
    62  	if err != nil {
    63  		t.Fatalf("err: %s", err)
    64  	}
    65  	if qm.LastIndex != 0 {
    66  		t.Fatalf("bad index: %d", qm.LastIndex)
    67  	}
    68  	if n := len(allocs); n != 0 {
    69  		t.Fatalf("expected 0 allocs, got: %d", n)
    70  	}
    71  
    72  	// TODO: do something that causes an allocation to actually happen
    73  	// so we can query for them.
    74  	return
    75  
    76  	job := &Job{
    77  		ID:   "job1",
    78  		Name: "Job #1",
    79  		Type: JobTypeService,
    80  	}
    81  	eval, _, err := c.Jobs().Register(job, nil)
    82  	if err != nil {
    83  		t.Fatalf("err: %s", err)
    84  	}
    85  
    86  	// List the allocations by prefix
    87  	allocs, qm, err = a.PrefixList("foobar")
    88  	if err != nil {
    89  		t.Fatalf("err: %s", err)
    90  	}
    91  	if qm.LastIndex == 0 {
    92  		t.Fatalf("bad index: %d", qm.LastIndex)
    93  	}
    94  
    95  	// Check that we got the allocation back
    96  	if len(allocs) == 0 || allocs[0].EvalID != eval {
    97  		t.Fatalf("bad: %#v", allocs)
    98  	}
    99  }
   100  
   101  func TestAllocations_CreateIndexSort(t *testing.T) {
   102  	allocs := []*AllocationListStub{
   103  		&AllocationListStub{CreateIndex: 2},
   104  		&AllocationListStub{CreateIndex: 1},
   105  		&AllocationListStub{CreateIndex: 5},
   106  	}
   107  	sort.Sort(AllocIndexSort(allocs))
   108  
   109  	expect := []*AllocationListStub{
   110  		&AllocationListStub{CreateIndex: 5},
   111  		&AllocationListStub{CreateIndex: 2},
   112  		&AllocationListStub{CreateIndex: 1},
   113  	}
   114  	if !reflect.DeepEqual(allocs, expect) {
   115  		t.Fatalf("\n\n%#v\n\n%#v", allocs, expect)
   116  	}
   117  }