github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/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: helper.StringToPtr("job1"), 32 //Name: helper.StringToPtr("Job #1"), 33 //Type: helper.StringToPtr(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: helper.StringToPtr("job1"), 78 //Name: helper.StringToPtr("Job #1"), 79 //Type: helper.StringToPtr(JobTypeService), 80 //} 81 82 //eval, _, err := c.Jobs().Register(job, nil) 83 //if err != nil { 84 //t.Fatalf("err: %s", err) 85 //} 86 87 //// List the allocations by prefix 88 //allocs, qm, err = a.PrefixList("foobar") 89 //if err != nil { 90 //t.Fatalf("err: %s", err) 91 //} 92 //if qm.LastIndex == 0 { 93 //t.Fatalf("bad index: %d", qm.LastIndex) 94 //} 95 96 //// Check that we got the allocation back 97 //if len(allocs) == 0 || allocs[0].EvalID != eval { 98 //t.Fatalf("bad: %#v", allocs) 99 //} 100 } 101 102 func TestAllocations_CreateIndexSort(t *testing.T) { 103 allocs := []*AllocationListStub{ 104 &AllocationListStub{CreateIndex: 2}, 105 &AllocationListStub{CreateIndex: 1}, 106 &AllocationListStub{CreateIndex: 5}, 107 } 108 sort.Sort(AllocIndexSort(allocs)) 109 110 expect := []*AllocationListStub{ 111 &AllocationListStub{CreateIndex: 5}, 112 &AllocationListStub{CreateIndex: 2}, 113 &AllocationListStub{CreateIndex: 1}, 114 } 115 if !reflect.DeepEqual(allocs, expect) { 116 t.Fatalf("\n\n%#v\n\n%#v", allocs, expect) 117 } 118 }