github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/command/agent/alloc_endpoint_test.go (about) 1 package agent 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "reflect" 7 "strings" 8 "testing" 9 10 "github.com/golang/snappy" 11 "github.com/hashicorp/nomad/nomad/mock" 12 "github.com/hashicorp/nomad/nomad/structs" 13 ) 14 15 func TestHTTP_AllocsList(t *testing.T) { 16 t.Parallel() 17 httpTest(t, nil, func(s *TestAgent) { 18 // Directly manipulate the state 19 state := s.Agent.server.State() 20 alloc1 := mock.Alloc() 21 alloc2 := mock.Alloc() 22 state.UpsertJobSummary(998, mock.JobSummary(alloc1.JobID)) 23 state.UpsertJobSummary(999, mock.JobSummary(alloc2.JobID)) 24 err := state.UpsertAllocs(1000, 25 []*structs.Allocation{alloc1, alloc2}) 26 if err != nil { 27 t.Fatalf("err: %v", err) 28 } 29 30 // Make the HTTP request 31 req, err := http.NewRequest("GET", "/v1/allocations", nil) 32 if err != nil { 33 t.Fatalf("err: %v", err) 34 } 35 respW := httptest.NewRecorder() 36 37 // Make the request 38 obj, err := s.Server.AllocsRequest(respW, req) 39 if err != nil { 40 t.Fatalf("err: %v", err) 41 } 42 43 // Check for the index 44 if respW.HeaderMap.Get("X-Nomad-Index") == "" { 45 t.Fatalf("missing index") 46 } 47 if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" { 48 t.Fatalf("missing known leader") 49 } 50 if respW.HeaderMap.Get("X-Nomad-LastContact") == "" { 51 t.Fatalf("missing last contact") 52 } 53 54 // Check the alloc 55 n := obj.([]*structs.AllocListStub) 56 if len(n) != 2 { 57 t.Fatalf("bad: %#v", n) 58 } 59 }) 60 } 61 62 func TestHTTP_AllocsPrefixList(t *testing.T) { 63 t.Parallel() 64 httpTest(t, nil, func(s *TestAgent) { 65 // Directly manipulate the state 66 state := s.Agent.server.State() 67 68 alloc1 := mock.Alloc() 69 alloc1.ID = "aaaaaaaa-e8f7-fd38-c855-ab94ceb89706" 70 alloc2 := mock.Alloc() 71 alloc2.ID = "aaabbbbb-e8f7-fd38-c855-ab94ceb89706" 72 summary1 := mock.JobSummary(alloc1.JobID) 73 summary2 := mock.JobSummary(alloc2.JobID) 74 if err := state.UpsertJobSummary(998, summary1); err != nil { 75 t.Fatal(err) 76 } 77 if err := state.UpsertJobSummary(999, summary2); err != nil { 78 t.Fatal(err) 79 } 80 if err := state.UpsertAllocs(1000, 81 []*structs.Allocation{alloc1, alloc2}); err != nil { 82 t.Fatalf("err: %v", err) 83 } 84 85 // Make the HTTP request 86 req, err := http.NewRequest("GET", "/v1/allocations?prefix=aaab", nil) 87 if err != nil { 88 t.Fatalf("err: %v", err) 89 } 90 respW := httptest.NewRecorder() 91 92 // Make the request 93 obj, err := s.Server.AllocsRequest(respW, req) 94 if err != nil { 95 t.Fatalf("err: %v", err) 96 } 97 98 // Check for the index 99 if respW.HeaderMap.Get("X-Nomad-Index") == "" { 100 t.Fatalf("missing index") 101 } 102 if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" { 103 t.Fatalf("missing known leader") 104 } 105 if respW.HeaderMap.Get("X-Nomad-LastContact") == "" { 106 t.Fatalf("missing last contact") 107 } 108 109 // Check the alloc 110 n := obj.([]*structs.AllocListStub) 111 if len(n) != 1 { 112 t.Fatalf("bad: %#v", n) 113 } 114 115 // Check the identifier 116 if n[0].ID != alloc2.ID { 117 t.Fatalf("expected alloc ID: %v, Actual: %v", alloc2.ID, n[0].ID) 118 } 119 }) 120 } 121 122 func TestHTTP_AllocQuery(t *testing.T) { 123 t.Parallel() 124 httpTest(t, nil, func(s *TestAgent) { 125 // Directly manipulate the state 126 state := s.Agent.server.State() 127 alloc := mock.Alloc() 128 if err := state.UpsertJobSummary(999, mock.JobSummary(alloc.JobID)); err != nil { 129 t.Fatal(err) 130 } 131 err := state.UpsertAllocs(1000, 132 []*structs.Allocation{alloc}) 133 if err != nil { 134 t.Fatalf("err: %v", err) 135 } 136 137 // Make the HTTP request 138 req, err := http.NewRequest("GET", "/v1/allocation/"+alloc.ID, nil) 139 if err != nil { 140 t.Fatalf("err: %v", err) 141 } 142 respW := httptest.NewRecorder() 143 144 // Make the request 145 obj, err := s.Server.AllocSpecificRequest(respW, req) 146 if err != nil { 147 t.Fatalf("err: %v", err) 148 } 149 150 // Check for the index 151 if respW.HeaderMap.Get("X-Nomad-Index") == "" { 152 t.Fatalf("missing index") 153 } 154 if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" { 155 t.Fatalf("missing known leader") 156 } 157 if respW.HeaderMap.Get("X-Nomad-LastContact") == "" { 158 t.Fatalf("missing last contact") 159 } 160 161 // Check the job 162 a := obj.(*structs.Allocation) 163 if a.ID != alloc.ID { 164 t.Fatalf("bad: %#v", a) 165 } 166 }) 167 } 168 169 func TestHTTP_AllocQuery_Payload(t *testing.T) { 170 t.Parallel() 171 httpTest(t, nil, func(s *TestAgent) { 172 // Directly manipulate the state 173 state := s.Agent.server.State() 174 alloc := mock.Alloc() 175 if err := state.UpsertJobSummary(999, mock.JobSummary(alloc.JobID)); err != nil { 176 t.Fatal(err) 177 } 178 179 // Insert Payload compressed 180 expected := []byte("hello world") 181 compressed := snappy.Encode(nil, expected) 182 alloc.Job.Payload = compressed 183 184 err := state.UpsertAllocs(1000, []*structs.Allocation{alloc}) 185 if err != nil { 186 t.Fatalf("err: %v", err) 187 } 188 189 // Make the HTTP request 190 req, err := http.NewRequest("GET", "/v1/allocation/"+alloc.ID, nil) 191 if err != nil { 192 t.Fatalf("err: %v", err) 193 } 194 respW := httptest.NewRecorder() 195 196 // Make the request 197 obj, err := s.Server.AllocSpecificRequest(respW, req) 198 if err != nil { 199 t.Fatalf("err: %v", err) 200 } 201 202 // Check for the index 203 if respW.HeaderMap.Get("X-Nomad-Index") == "" { 204 t.Fatalf("missing index") 205 } 206 if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" { 207 t.Fatalf("missing known leader") 208 } 209 if respW.HeaderMap.Get("X-Nomad-LastContact") == "" { 210 t.Fatalf("missing last contact") 211 } 212 213 // Check the job 214 a := obj.(*structs.Allocation) 215 if a.ID != alloc.ID { 216 t.Fatalf("bad: %#v", a) 217 } 218 219 // Check the payload is decompressed 220 if !reflect.DeepEqual(a.Job.Payload, expected) { 221 t.Fatalf("Payload not decompressed properly; got %#v; want %#v", a.Job.Payload, expected) 222 } 223 }) 224 } 225 226 func TestHTTP_AllocStats(t *testing.T) { 227 t.Parallel() 228 httpTest(t, nil, func(s *TestAgent) { 229 // Make the HTTP request 230 req, err := http.NewRequest("GET", "/v1/client/allocation/123/foo", nil) 231 if err != nil { 232 t.Fatalf("err: %v", err) 233 } 234 respW := httptest.NewRecorder() 235 236 // Make the request 237 _, err = s.Server.ClientAllocRequest(respW, req) 238 if !strings.Contains(err.Error(), resourceNotFoundErr) { 239 t.Fatalf("err: %v", err) 240 } 241 }) 242 } 243 244 func TestHTTP_AllocSnapshot(t *testing.T) { 245 t.Parallel() 246 httpTest(t, nil, func(s *TestAgent) { 247 // Make the HTTP request 248 req, err := http.NewRequest("GET", "/v1/client/allocation/123/snapshot", nil) 249 if err != nil { 250 t.Fatalf("err: %v", err) 251 } 252 respW := httptest.NewRecorder() 253 254 // Make the request 255 _, err = s.Server.ClientAllocRequest(respW, req) 256 if !strings.Contains(err.Error(), allocNotFoundErr) { 257 t.Fatalf("err: %v", err) 258 } 259 }) 260 } 261 262 func TestHTTP_AllocGC(t *testing.T) { 263 t.Parallel() 264 httpTest(t, nil, func(s *TestAgent) { 265 // Make the HTTP request 266 req, err := http.NewRequest("GET", "/v1/client/allocation/123/gc", nil) 267 if err != nil { 268 t.Fatalf("err: %v", err) 269 } 270 respW := httptest.NewRecorder() 271 272 // Make the request 273 _, err = s.Server.ClientAllocRequest(respW, req) 274 if !strings.Contains(err.Error(), "unable to collect allocation") { 275 t.Fatalf("err: %v", err) 276 } 277 }) 278 } 279 280 func TestHTTP_AllocAllGC(t *testing.T) { 281 t.Parallel() 282 httpTest(t, nil, func(s *TestAgent) { 283 // Make the HTTP request 284 req, err := http.NewRequest("GET", "/v1/client/gc", nil) 285 if err != nil { 286 t.Fatalf("err: %v", err) 287 } 288 respW := httptest.NewRecorder() 289 290 // Make the request 291 _, err = s.Server.ClientGCRequest(respW, req) 292 if err != nil { 293 t.Fatalf("err: %v", err) 294 } 295 }) 296 297 }