github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/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_CreateIndexSort(t *testing.T) { 56 allocs := []*AllocationListStub{ 57 &AllocationListStub{CreateIndex: 2}, 58 &AllocationListStub{CreateIndex: 1}, 59 &AllocationListStub{CreateIndex: 5}, 60 } 61 sort.Sort(AllocIndexSort(allocs)) 62 63 expect := []*AllocationListStub{ 64 &AllocationListStub{CreateIndex: 5}, 65 &AllocationListStub{CreateIndex: 2}, 66 &AllocationListStub{CreateIndex: 1}, 67 } 68 if !reflect.DeepEqual(allocs, expect) { 69 t.Fatalf("\n\n%#v\n\n%#v", allocs, expect) 70 } 71 }