github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/command/agent/alloc_endpoint_test.go (about) 1 package agent 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "testing" 7 8 "github.com/hashicorp/nomad/nomad/mock" 9 "github.com/hashicorp/nomad/nomad/structs" 10 ) 11 12 func TestHTTP_AllocsList(t *testing.T) { 13 httpTest(t, nil, func(s *TestServer) { 14 // Directly manipulate the state 15 state := s.Agent.server.State() 16 alloc1 := mock.Alloc() 17 alloc2 := mock.Alloc() 18 err := state.UpsertAllocs(1000, 19 []*structs.Allocation{alloc1, alloc2}) 20 if err != nil { 21 t.Fatalf("err: %v", err) 22 } 23 24 // Make the HTTP request 25 req, err := http.NewRequest("GET", "/v1/allocations", nil) 26 if err != nil { 27 t.Fatalf("err: %v", err) 28 } 29 respW := httptest.NewRecorder() 30 31 // Make the request 32 obj, err := s.Server.AllocsRequest(respW, req) 33 if err != nil { 34 t.Fatalf("err: %v", err) 35 } 36 37 // Check for the index 38 if respW.HeaderMap.Get("X-Nomad-Index") == "" { 39 t.Fatalf("missing index") 40 } 41 if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" { 42 t.Fatalf("missing known leader") 43 } 44 if respW.HeaderMap.Get("X-Nomad-LastContact") == "" { 45 t.Fatalf("missing last contact") 46 } 47 48 // Check the job 49 n := obj.([]*structs.AllocListStub) 50 if len(n) != 2 { 51 t.Fatalf("bad: %#v", n) 52 } 53 }) 54 } 55 56 func TestHTTP_AllocQuery(t *testing.T) { 57 httpTest(t, nil, func(s *TestServer) { 58 // Directly manipulate the state 59 state := s.Agent.server.State() 60 alloc := mock.Alloc() 61 err := state.UpsertAllocs(1000, 62 []*structs.Allocation{alloc}) 63 if err != nil { 64 t.Fatalf("err: %v", err) 65 } 66 67 // Make the HTTP request 68 req, err := http.NewRequest("GET", "/v1/allocation/"+alloc.ID, nil) 69 if err != nil { 70 t.Fatalf("err: %v", err) 71 } 72 respW := httptest.NewRecorder() 73 74 // Make the request 75 obj, err := s.Server.AllocSpecificRequest(respW, req) 76 if err != nil { 77 t.Fatalf("err: %v", err) 78 } 79 80 // Check for the index 81 if respW.HeaderMap.Get("X-Nomad-Index") == "" { 82 t.Fatalf("missing index") 83 } 84 if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" { 85 t.Fatalf("missing known leader") 86 } 87 if respW.HeaderMap.Get("X-Nomad-LastContact") == "" { 88 t.Fatalf("missing last contact") 89 } 90 91 // Check the job 92 a := obj.(*structs.Allocation) 93 if a.ID != alloc.ID { 94 t.Fatalf("bad: %#v", a) 95 } 96 }) 97 }