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