github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/command/agent/eval_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_EvalList(t *testing.T) { 13 httpTest(t, nil, func(s *TestServer) { 14 // Directly manipulate the state 15 state := s.Agent.server.State() 16 eval1 := mock.Eval() 17 eval2 := mock.Eval() 18 err := state.UpsertEvals(1000, 19 []*structs.Evaluation{eval1, eval2}) 20 if err != nil { 21 t.Fatalf("err: %v", err) 22 } 23 24 // Make the HTTP request 25 req, err := http.NewRequest("GET", "/v1/evaluations", 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.EvalsRequest(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 eval 49 e := obj.([]*structs.Evaluation) 50 if len(e) != 2 { 51 t.Fatalf("bad: %#v", e) 52 } 53 }) 54 } 55 56 func TestHTTP_EvalPrefixList(t *testing.T) { 57 httpTest(t, nil, func(s *TestServer) { 58 // Directly manipulate the state 59 state := s.Agent.server.State() 60 eval1 := mock.Eval() 61 eval1.ID = "aaabbbbb-e8f7-fd38-c855-ab94ceb89706" 62 eval2 := mock.Eval() 63 eval2.ID = "aaabbbbb-e8f7-fd38-c855-ab94ceb89706" 64 err := state.UpsertEvals(1000, 65 []*structs.Evaluation{eval1, eval2}) 66 if err != nil { 67 t.Fatalf("err: %v", err) 68 } 69 70 // Make the HTTP request 71 req, err := http.NewRequest("GET", "/v1/evaluations?prefix=aaab", nil) 72 if err != nil { 73 t.Fatalf("err: %v", err) 74 } 75 respW := httptest.NewRecorder() 76 77 // Make the request 78 obj, err := s.Server.EvalsRequest(respW, req) 79 if err != nil { 80 t.Fatalf("err: %v", err) 81 } 82 83 // Check for the index 84 if respW.HeaderMap.Get("X-Nomad-Index") == "" { 85 t.Fatalf("missing index") 86 } 87 if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" { 88 t.Fatalf("missing known leader") 89 } 90 if respW.HeaderMap.Get("X-Nomad-LastContact") == "" { 91 t.Fatalf("missing last contact") 92 } 93 94 // Check the eval 95 e := obj.([]*structs.Evaluation) 96 if len(e) != 1 { 97 t.Fatalf("bad: %#v", e) 98 } 99 100 // Check the identifier 101 if e[0].ID != eval2.ID { 102 t.Fatalf("expected eval ID: %v, Actual: %v", eval2.ID, e[0].ID) 103 } 104 }) 105 } 106 107 func TestHTTP_EvalAllocations(t *testing.T) { 108 httpTest(t, nil, func(s *TestServer) { 109 // Directly manipulate the state 110 state := s.Agent.server.State() 111 alloc1 := mock.Alloc() 112 alloc2 := mock.Alloc() 113 alloc2.EvalID = alloc1.EvalID 114 state.UpsertJobSummary(998, mock.JobSummary(alloc1.JobID)) 115 state.UpsertJobSummary(999, mock.JobSummary(alloc2.JobID)) 116 err := state.UpsertAllocs(1000, 117 []*structs.Allocation{alloc1, alloc2}) 118 if err != nil { 119 t.Fatalf("err: %v", err) 120 } 121 122 // Make the HTTP request 123 req, err := http.NewRequest("GET", 124 "/v1/evaluation/"+alloc1.EvalID+"/allocations", nil) 125 if err != nil { 126 t.Fatalf("err: %v", err) 127 } 128 respW := httptest.NewRecorder() 129 130 // Make the request 131 obj, err := s.Server.EvalSpecificRequest(respW, req) 132 if err != nil { 133 t.Fatalf("err: %v", err) 134 } 135 136 // Check for the index 137 if respW.HeaderMap.Get("X-Nomad-Index") == "" { 138 t.Fatalf("missing index") 139 } 140 if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" { 141 t.Fatalf("missing known leader") 142 } 143 if respW.HeaderMap.Get("X-Nomad-LastContact") == "" { 144 t.Fatalf("missing last contact") 145 } 146 147 // Check the ouptput 148 allocs := obj.([]*structs.AllocListStub) 149 if len(allocs) != 2 { 150 t.Fatalf("bad: %#v", allocs) 151 } 152 }) 153 } 154 155 func TestHTTP_EvalQuery(t *testing.T) { 156 httpTest(t, nil, func(s *TestServer) { 157 // Directly manipulate the state 158 state := s.Agent.server.State() 159 eval := mock.Eval() 160 err := state.UpsertEvals(1000, []*structs.Evaluation{eval}) 161 if err != nil { 162 t.Fatalf("err: %v", err) 163 } 164 165 // Make the HTTP request 166 req, err := http.NewRequest("GET", "/v1/evaluation/"+eval.ID, nil) 167 if err != nil { 168 t.Fatalf("err: %v", err) 169 } 170 respW := httptest.NewRecorder() 171 172 // Make the request 173 obj, err := s.Server.EvalSpecificRequest(respW, req) 174 if err != nil { 175 t.Fatalf("err: %v", err) 176 } 177 178 // Check for the index 179 if respW.HeaderMap.Get("X-Nomad-Index") == "" { 180 t.Fatalf("missing index") 181 } 182 if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" { 183 t.Fatalf("missing known leader") 184 } 185 if respW.HeaderMap.Get("X-Nomad-LastContact") == "" { 186 t.Fatalf("missing last contact") 187 } 188 189 // Check the job 190 e := obj.(*structs.Evaluation) 191 if e.ID != eval.ID { 192 t.Fatalf("bad: %#v", e) 193 } 194 }) 195 }