2015-09-06 23:02:53 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2021-12-10 18:43:03 +00:00
|
|
|
"fmt"
|
2015-09-06 23:02:53 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2021-12-10 18:43:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2015-09-06 23:02:53 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHTTP_EvalList(t *testing.T) {
|
2017-07-20 05:42:15 +00:00
|
|
|
t.Parallel()
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2015-09-06 23:02:53 +00:00
|
|
|
// Directly manipulate the state
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
eval1 := mock.Eval()
|
|
|
|
eval2 := mock.Eval()
|
2020-10-19 13:30:15 +00:00
|
|
|
err := state.UpsertEvals(structs.MsgTypeTestSetup, 1000, []*structs.Evaluation{eval1, eval2})
|
2021-12-10 18:43:03 +00:00
|
|
|
require.NoError(t, err)
|
2015-09-06 23:02:53 +00:00
|
|
|
|
2021-12-10 18:43:03 +00:00
|
|
|
// simple list request
|
2015-09-06 23:02:53 +00:00
|
|
|
req, err := http.NewRequest("GET", "/v1/evaluations", nil)
|
2021-12-10 18:43:03 +00:00
|
|
|
require.NoError(t, err)
|
2015-09-06 23:02:53 +00:00
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
obj, err := s.Server.EvalsRequest(respW, req)
|
2021-12-10 18:43:03 +00:00
|
|
|
require.NoError(t, err)
|
2015-09-06 23:02:53 +00:00
|
|
|
|
2021-12-10 18:43:03 +00:00
|
|
|
// check headers and response body
|
2022-01-12 15:10:25 +00:00
|
|
|
require.NotEqual(t, "", respW.Result().Header.Get("X-Nomad-Index"), "missing index")
|
|
|
|
require.Equal(t, "true", respW.Result().Header.Get("X-Nomad-KnownLeader"), "missing known leader")
|
|
|
|
require.NotEqual(t, "", respW.Result().Header.Get("X-Nomad-LastContact"), "missing last contact")
|
2021-12-10 18:43:03 +00:00
|
|
|
require.Len(t, obj.([]*structs.Evaluation), 2, "expected 2 evals")
|
|
|
|
|
|
|
|
// paginated list request
|
|
|
|
req, err = http.NewRequest("GET", "/v1/evaluations?per_page=1", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
obj, err = s.Server.EvalsRequest(respW, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// check response body
|
|
|
|
require.Len(t, obj.([]*structs.Evaluation), 1, "expected 1 eval")
|
|
|
|
|
|
|
|
// filtered list request
|
|
|
|
req, err = http.NewRequest("GET",
|
|
|
|
fmt.Sprintf("/v1/evaluations?per_page=10&job=%s", eval2.JobID), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
obj, err = s.Server.EvalsRequest(respW, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// check response body
|
|
|
|
require.Len(t, obj.([]*structs.Evaluation), 1, "expected 1 eval")
|
2015-09-06 23:02:53 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-24 10:46:59 +00:00
|
|
|
func TestHTTP_EvalPrefixList(t *testing.T) {
|
2017-07-20 05:42:15 +00:00
|
|
|
t.Parallel()
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2015-12-24 10:46:59 +00:00
|
|
|
// Directly manipulate the state
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
eval1 := mock.Eval()
|
|
|
|
eval1.ID = "aaabbbbb-e8f7-fd38-c855-ab94ceb89706"
|
|
|
|
eval2 := mock.Eval()
|
|
|
|
eval2.ID = "aaabbbbb-e8f7-fd38-c855-ab94ceb89706"
|
2020-10-19 13:30:15 +00:00
|
|
|
err := state.UpsertEvals(structs.MsgTypeTestSetup, 1000, []*structs.Evaluation{eval1, eval2})
|
2015-12-24 10:46:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/evaluations?prefix=aaab", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.EvalsRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the index
|
2022-01-12 15:10:25 +00:00
|
|
|
if respW.Result().Header.Get("X-Nomad-Index") == "" {
|
2015-12-24 10:46:59 +00:00
|
|
|
t.Fatalf("missing index")
|
|
|
|
}
|
2022-01-12 15:10:25 +00:00
|
|
|
if respW.Result().Header.Get("X-Nomad-KnownLeader") != "true" {
|
2015-12-24 10:46:59 +00:00
|
|
|
t.Fatalf("missing known leader")
|
|
|
|
}
|
2022-01-12 15:10:25 +00:00
|
|
|
if respW.Result().Header.Get("X-Nomad-LastContact") == "" {
|
2015-12-24 10:46:59 +00:00
|
|
|
t.Fatalf("missing last contact")
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:46:57 +00:00
|
|
|
// Check the eval
|
2015-12-24 10:46:59 +00:00
|
|
|
e := obj.([]*structs.Evaluation)
|
|
|
|
if len(e) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", e)
|
|
|
|
}
|
2016-01-06 21:46:57 +00:00
|
|
|
|
|
|
|
// Check the identifier
|
|
|
|
if e[0].ID != eval2.ID {
|
|
|
|
t.Fatalf("expected eval ID: %v, Actual: %v", eval2.ID, e[0].ID)
|
|
|
|
}
|
2015-12-24 10:46:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-09-06 23:18:25 +00:00
|
|
|
func TestHTTP_EvalAllocations(t *testing.T) {
|
2017-07-20 05:42:15 +00:00
|
|
|
t.Parallel()
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2015-09-06 23:18:25 +00:00
|
|
|
// Directly manipulate the state
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
alloc1 := mock.Alloc()
|
|
|
|
alloc2 := mock.Alloc()
|
|
|
|
alloc2.EvalID = alloc1.EvalID
|
2016-07-25 21:11:32 +00:00
|
|
|
state.UpsertJobSummary(998, mock.JobSummary(alloc1.JobID))
|
|
|
|
state.UpsertJobSummary(999, mock.JobSummary(alloc2.JobID))
|
2020-10-19 13:30:15 +00:00
|
|
|
err := state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{alloc1, alloc2})
|
2015-09-06 23:18:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET",
|
|
|
|
"/v1/evaluation/"+alloc1.EvalID+"/allocations", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.EvalSpecificRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the index
|
2022-01-12 15:10:25 +00:00
|
|
|
if respW.Result().Header.Get("X-Nomad-Index") == "" {
|
2015-09-06 23:18:25 +00:00
|
|
|
t.Fatalf("missing index")
|
|
|
|
}
|
2022-01-12 15:10:25 +00:00
|
|
|
if respW.Result().Header.Get("X-Nomad-KnownLeader") != "true" {
|
2015-09-06 23:18:25 +00:00
|
|
|
t.Fatalf("missing known leader")
|
|
|
|
}
|
2022-01-12 15:10:25 +00:00
|
|
|
if respW.Result().Header.Get("X-Nomad-LastContact") == "" {
|
2015-09-06 23:18:25 +00:00
|
|
|
t.Fatalf("missing last contact")
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:35:30 +00:00
|
|
|
// Check the output
|
2015-09-06 23:18:25 +00:00
|
|
|
allocs := obj.([]*structs.AllocListStub)
|
|
|
|
if len(allocs) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", allocs)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-09-06 23:02:53 +00:00
|
|
|
func TestHTTP_EvalQuery(t *testing.T) {
|
2017-07-20 05:42:15 +00:00
|
|
|
t.Parallel()
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2015-09-06 23:02:53 +00:00
|
|
|
// Directly manipulate the state
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
eval := mock.Eval()
|
2020-10-19 13:30:15 +00:00
|
|
|
err := state.UpsertEvals(structs.MsgTypeTestSetup, 1000, []*structs.Evaluation{eval})
|
2015-09-06 23:02:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/evaluation/"+eval.ID, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.EvalSpecificRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the index
|
2022-01-12 15:10:25 +00:00
|
|
|
if respW.Result().Header.Get("X-Nomad-Index") == "" {
|
2015-09-06 23:02:53 +00:00
|
|
|
t.Fatalf("missing index")
|
|
|
|
}
|
2022-01-12 15:10:25 +00:00
|
|
|
if respW.Result().Header.Get("X-Nomad-KnownLeader") != "true" {
|
2015-09-06 23:02:53 +00:00
|
|
|
t.Fatalf("missing known leader")
|
|
|
|
}
|
2022-01-12 15:10:25 +00:00
|
|
|
if respW.Result().Header.Get("X-Nomad-LastContact") == "" {
|
2015-09-06 23:02:53 +00:00
|
|
|
t.Fatalf("missing last contact")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the job
|
|
|
|
e := obj.(*structs.Evaluation)
|
|
|
|
if e.ID != eval.ID {
|
|
|
|
t.Fatalf("bad: %#v", e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|