2015-09-06 21:18:11 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2018-02-23 23:56:36 +00:00
|
|
|
"time"
|
2015-09-06 21:18:11 +00:00
|
|
|
|
2018-02-23 23:56:36 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2015-09-06 21:18:11 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2017-11-17 20:53:26 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-02-23 23:56:36 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-09-06 21:18:11 +00:00
|
|
|
)
|
|
|
|
|
2015-09-06 21:31:17 +00:00
|
|
|
func TestHTTP_NodesList(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2015-09-06 21:31:17 +00:00
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
// Create the node
|
|
|
|
node := mock.Node()
|
|
|
|
args := structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-09-06 21:31:17 +00:00
|
|
|
}
|
|
|
|
var resp structs.NodeUpdateResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := s.Agent.RPC("Node.Register", &args, &resp); err != nil {
|
2015-09-06 21:31:17 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/nodes", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.NodesRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the index
|
2021-05-07 17:58:40 +00:00
|
|
|
if respW.Header().Get("X-Nomad-Index") == "" {
|
2015-09-06 21:31:17 +00:00
|
|
|
t.Fatalf("missing index")
|
|
|
|
}
|
2021-05-07 17:58:40 +00:00
|
|
|
if respW.Header().Get("X-Nomad-KnownLeader") != "true" {
|
2015-09-06 21:31:17 +00:00
|
|
|
t.Fatalf("missing known leader")
|
|
|
|
}
|
2021-05-07 17:58:40 +00:00
|
|
|
if respW.Header().Get("X-Nomad-LastContact") == "" {
|
2015-09-06 21:31:17 +00:00
|
|
|
t.Fatalf("missing last contact")
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:46:57 +00:00
|
|
|
// Check the nodes
|
2015-09-06 21:31:17 +00:00
|
|
|
n := obj.([]*structs.NodeListStub)
|
|
|
|
if len(n) < 3 { // Maybe 4 including client
|
|
|
|
t.Fatalf("bad: %#v", n)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-22 22:44:33 +00:00
|
|
|
func TestHTTP_NodesPrefixList(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2016-01-14 20:57:43 +00:00
|
|
|
ids := []string{
|
|
|
|
"12345678-abcd-efab-cdef-123456789abc",
|
|
|
|
"12345678-aaaa-efab-cdef-123456789abc",
|
|
|
|
"1234aaaa-abcd-efab-cdef-123456789abc",
|
|
|
|
"1234bbbb-abcd-efab-cdef-123456789abc",
|
|
|
|
"1234cccc-abcd-efab-cdef-123456789abc",
|
|
|
|
"1234dddd-abcd-efab-cdef-123456789abc",
|
|
|
|
}
|
2015-12-22 22:44:33 +00:00
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
// Create the node
|
|
|
|
node := mock.Node()
|
|
|
|
node.ID = ids[i]
|
|
|
|
args := structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
var resp structs.NodeUpdateResponse
|
|
|
|
if err := s.Agent.RPC("Node.Register", &args, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the HTTP request
|
2016-01-14 20:57:43 +00:00
|
|
|
req, err := http.NewRequest("GET", "/v1/nodes?prefix=12345678", nil)
|
2015-12-22 22:44:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.NodesRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the index
|
2021-05-07 17:58:40 +00:00
|
|
|
if respW.Header().Get("X-Nomad-Index") == "" {
|
2015-12-22 22:44:33 +00:00
|
|
|
t.Fatalf("missing index")
|
|
|
|
}
|
2021-05-07 17:58:40 +00:00
|
|
|
if respW.Header().Get("X-Nomad-KnownLeader") != "true" {
|
2015-12-22 22:44:33 +00:00
|
|
|
t.Fatalf("missing known leader")
|
|
|
|
}
|
2021-05-07 17:58:40 +00:00
|
|
|
if respW.Header().Get("X-Nomad-LastContact") == "" {
|
2015-12-22 22:44:33 +00:00
|
|
|
t.Fatalf("missing last contact")
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:46:57 +00:00
|
|
|
// Check the nodes
|
2015-12-22 22:44:33 +00:00
|
|
|
n := obj.([]*structs.NodeListStub)
|
2016-01-14 20:57:43 +00:00
|
|
|
if len(n) != 2 {
|
2015-12-22 22:44:33 +00:00
|
|
|
t.Fatalf("bad: %#v", n)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-09-06 21:18:11 +00:00
|
|
|
func TestHTTP_NodeForceEval(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2015-09-06 21:31:17 +00:00
|
|
|
// Create the node
|
2015-09-06 21:18:11 +00:00
|
|
|
node := mock.Node()
|
|
|
|
args := structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-09-06 21:18:11 +00:00
|
|
|
}
|
|
|
|
var resp structs.NodeUpdateResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := s.Agent.RPC("Node.Register", &args, &resp); err != nil {
|
2015-09-06 21:18:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Directly manipulate the state
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
alloc1 := mock.Alloc()
|
|
|
|
alloc1.NodeID = node.ID
|
2016-07-21 21:43:21 +00:00
|
|
|
if err := state.UpsertJobSummary(999, mock.JobSummary(alloc1.JobID)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-10-19 13:30:15 +00:00
|
|
|
err := state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{alloc1})
|
2015-09-06 21:18:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("POST", "/v1/node/"+node.ID+"/evaluate", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.NodeSpecificRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the index
|
2021-05-07 17:58:40 +00:00
|
|
|
if respW.Header().Get("X-Nomad-Index") == "" {
|
2015-09-06 21:18:11 +00:00
|
|
|
t.Fatalf("missing index")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the response
|
|
|
|
upd := obj.(structs.NodeUpdateResponse)
|
|
|
|
if len(upd.EvalIDs) == 0 {
|
|
|
|
t.Fatalf("bad: %v", upd)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTP_NodeAllocations(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2015-09-06 21:18:11 +00:00
|
|
|
// Create the job
|
|
|
|
node := mock.Node()
|
|
|
|
args := structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-09-06 21:18:11 +00:00
|
|
|
}
|
|
|
|
var resp structs.NodeUpdateResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := s.Agent.RPC("Node.Register", &args, &resp); err != nil {
|
2015-09-06 21:18:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Directly manipulate the state
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
alloc1 := mock.Alloc()
|
|
|
|
alloc1.NodeID = node.ID
|
2016-07-21 21:43:21 +00:00
|
|
|
if err := state.UpsertJobSummary(999, mock.JobSummary(alloc1.JobID)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-17 20:53:26 +00:00
|
|
|
// Create a test event for the allocation
|
|
|
|
testEvent := structs.NewTaskEvent(structs.TaskStarted)
|
|
|
|
var events []*structs.TaskEvent
|
|
|
|
events = append(events, testEvent)
|
|
|
|
taskState := &structs.TaskState{Events: events}
|
|
|
|
alloc1.TaskStates = make(map[string]*structs.TaskState)
|
|
|
|
alloc1.TaskStates["test"] = taskState
|
|
|
|
|
2020-10-19 13:30:15 +00:00
|
|
|
err := state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{alloc1})
|
2015-09-06 21:18:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/node/"+node.ID+"/allocations", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.NodeSpecificRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the index
|
2021-05-07 17:58:40 +00:00
|
|
|
if respW.Header().Get("X-Nomad-Index") == "" {
|
2015-09-06 21:18:11 +00:00
|
|
|
t.Fatalf("missing index")
|
|
|
|
}
|
2021-05-07 17:58:40 +00:00
|
|
|
if respW.Header().Get("X-Nomad-KnownLeader") != "true" {
|
2015-09-06 21:18:11 +00:00
|
|
|
t.Fatalf("missing known leader")
|
|
|
|
}
|
2021-05-07 17:58:40 +00:00
|
|
|
if respW.Header().Get("X-Nomad-LastContact") == "" {
|
2015-09-06 21:18:11 +00:00
|
|
|
t.Fatalf("missing last contact")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the node
|
|
|
|
allocs := obj.([]*structs.Allocation)
|
|
|
|
if len(allocs) != 1 || allocs[0].ID != alloc1.ID {
|
|
|
|
t.Fatalf("bad: %#v", allocs)
|
|
|
|
}
|
2017-11-17 20:53:26 +00:00
|
|
|
expectedDisplayMsg := "Task started by client"
|
|
|
|
displayMsg := allocs[0].TaskStates["test"].Events[0].DisplayMessage
|
|
|
|
assert.Equal(t, expectedDisplayMsg, displayMsg)
|
2015-09-06 21:18:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-09-07 03:03:59 +00:00
|
|
|
func TestHTTP_NodeDrain(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-23 23:56:36 +00:00
|
|
|
require := require.New(t)
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2015-09-07 03:03:59 +00:00
|
|
|
// Create the node
|
|
|
|
node := mock.Node()
|
|
|
|
args := structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-09-07 03:03:59 +00:00
|
|
|
}
|
|
|
|
var resp structs.NodeUpdateResponse
|
2018-02-23 23:56:36 +00:00
|
|
|
require.Nil(s.Agent.RPC("Node.Register", &args, &resp))
|
2015-09-07 03:03:59 +00:00
|
|
|
|
2018-02-23 23:56:36 +00:00
|
|
|
drainReq := api.NodeUpdateDrainRequest{
|
|
|
|
NodeID: node.ID,
|
|
|
|
DrainSpec: &api.DrainSpec{
|
|
|
|
Deadline: 10 * time.Second,
|
|
|
|
},
|
2021-05-07 17:58:40 +00:00
|
|
|
Meta: map[string]string{
|
|
|
|
"reason": "drain",
|
|
|
|
},
|
2015-09-07 03:03:59 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 17:58:40 +00:00
|
|
|
beforeDrain := time.Unix(time.Now().Unix(), 0)
|
|
|
|
|
2015-09-07 03:03:59 +00:00
|
|
|
// Make the HTTP request
|
2018-02-23 23:56:36 +00:00
|
|
|
buf := encodeReq(drainReq)
|
|
|
|
req, err := http.NewRequest("POST", "/v1/node/"+node.ID+"/drain", buf)
|
|
|
|
require.Nil(err)
|
2015-09-07 03:03:59 +00:00
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.NodeSpecificRequest(respW, req)
|
2018-02-23 23:56:36 +00:00
|
|
|
require.Nil(err)
|
2015-09-07 03:03:59 +00:00
|
|
|
|
|
|
|
// Check for the index
|
2021-05-07 17:58:40 +00:00
|
|
|
require.NotEmpty(respW.Header().Get("X-Nomad-Index"))
|
2015-09-07 03:03:59 +00:00
|
|
|
|
|
|
|
// Check the response
|
2020-03-29 21:56:10 +00:00
|
|
|
dresp, ok := obj.(structs.NodeDrainUpdateResponse)
|
2018-02-23 23:56:36 +00:00
|
|
|
require.True(ok)
|
|
|
|
|
2021-05-07 17:58:40 +00:00
|
|
|
t.Logf("response index=%v node_update_index=0x%x", respW.Header().Get("X-Nomad-Index"),
|
2020-03-29 21:56:10 +00:00
|
|
|
dresp.NodeModifyIndex)
|
|
|
|
|
2018-02-23 23:56:36 +00:00
|
|
|
// Check that the node has been updated
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
out, err := state.NodeByID(nil, node.ID)
|
|
|
|
require.Nil(err)
|
2020-03-29 21:56:10 +00:00
|
|
|
|
2021-02-11 15:40:59 +00:00
|
|
|
// the node must either be in drain mode or ineligible
|
2020-03-29 21:56:10 +00:00
|
|
|
// once the node is recognize as not having any running allocs
|
2021-02-11 15:40:59 +00:00
|
|
|
if out.DrainStrategy != nil {
|
2020-03-29 21:56:10 +00:00
|
|
|
require.Equal(10*time.Second, out.DrainStrategy.Deadline)
|
|
|
|
} else {
|
|
|
|
require.Equal(structs.NodeSchedulingIneligible, out.SchedulingEligibility)
|
|
|
|
}
|
2018-02-23 23:56:36 +00:00
|
|
|
|
2021-05-07 17:58:40 +00:00
|
|
|
require.NotNil(out.LastDrain)
|
|
|
|
require.Equal(map[string]string{
|
|
|
|
"reason": "drain",
|
|
|
|
}, out.LastDrain.Meta)
|
|
|
|
|
2018-02-23 23:56:36 +00:00
|
|
|
// Make the HTTP request to unset drain
|
|
|
|
drainReq.DrainSpec = nil
|
2021-05-07 17:58:40 +00:00
|
|
|
drainReq.Meta = map[string]string{
|
|
|
|
"cancel_reason": "changed my mind",
|
|
|
|
}
|
2018-02-23 23:56:36 +00:00
|
|
|
buf = encodeReq(drainReq)
|
|
|
|
req, err = http.NewRequest("POST", "/v1/node/"+node.ID+"/drain", buf)
|
|
|
|
require.Nil(err)
|
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
2018-03-20 18:31:55 +00:00
|
|
|
_, err = s.Server.NodeSpecificRequest(respW, req)
|
2018-02-23 23:56:36 +00:00
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
out, err = state.NodeByID(nil, node.ID)
|
|
|
|
require.Nil(err)
|
|
|
|
require.Nil(out.DrainStrategy)
|
2021-05-07 17:58:40 +00:00
|
|
|
require.NotNil(out.LastDrain)
|
|
|
|
require.False(out.LastDrain.StartedAt.Before(beforeDrain))
|
|
|
|
require.False(out.LastDrain.UpdatedAt.Before(out.LastDrain.StartedAt))
|
|
|
|
require.Contains([]structs.DrainStatus{structs.DrainStatusCanceled, structs.DrainStatusComplete}, out.LastDrain.Status)
|
|
|
|
if out.LastDrain.Status == structs.DrainStatusComplete {
|
|
|
|
require.Equal(map[string]string{
|
|
|
|
"reason": "drain",
|
|
|
|
}, out.LastDrain.Meta)
|
|
|
|
} else if out.LastDrain.Status == structs.DrainStatusCanceled {
|
|
|
|
require.Equal(map[string]string{
|
|
|
|
"cancel_reason": "changed my mind",
|
|
|
|
}, out.LastDrain.Meta)
|
|
|
|
}
|
2015-09-07 03:03:59 +00:00
|
|
|
})
|
2018-02-27 20:59:27 +00:00
|
|
|
}
|
|
|
|
|
2018-02-27 22:46:40 +00:00
|
|
|
func TestHTTP_NodeEligible(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-27 20:59:27 +00:00
|
|
|
require := require.New(t)
|
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
|
|
|
// Create the node
|
|
|
|
node := mock.Node()
|
|
|
|
args := structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
var resp structs.NodeUpdateResponse
|
|
|
|
require.Nil(s.Agent.RPC("Node.Register", &args, &resp))
|
|
|
|
|
2019-07-15 15:34:53 +00:00
|
|
|
eligibilityReq := api.NodeUpdateEligibilityRequest{
|
2018-02-27 20:59:27 +00:00
|
|
|
Eligibility: structs.NodeSchedulingIneligible,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the HTTP request
|
2019-07-15 15:34:53 +00:00
|
|
|
buf := encodeReq(eligibilityReq)
|
2018-02-27 20:59:27 +00:00
|
|
|
req, err := http.NewRequest("POST", "/v1/node/"+node.ID+"/eligibility", buf)
|
|
|
|
require.Nil(err)
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
2018-03-27 22:53:24 +00:00
|
|
|
obj, err := s.Server.NodeSpecificRequest(respW, req)
|
2018-02-27 20:59:27 +00:00
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
// Check for the index
|
2021-05-07 17:58:40 +00:00
|
|
|
require.NotZero(respW.Header().Get("X-Nomad-Index"))
|
2018-02-27 20:59:27 +00:00
|
|
|
|
2018-03-27 22:53:24 +00:00
|
|
|
// Check the response
|
|
|
|
_, ok := obj.(structs.NodeEligibilityUpdateResponse)
|
|
|
|
require.True(ok)
|
|
|
|
|
2018-02-27 20:59:27 +00:00
|
|
|
// Check that the node has been updated
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
out, err := state.NodeByID(nil, node.ID)
|
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(structs.NodeSchedulingIneligible, out.SchedulingEligibility)
|
|
|
|
|
|
|
|
// Make the HTTP request to set something invalid
|
2019-07-15 15:34:53 +00:00
|
|
|
eligibilityReq.Eligibility = "foo"
|
|
|
|
buf = encodeReq(eligibilityReq)
|
2018-02-27 20:59:27 +00:00
|
|
|
req, err = http.NewRequest("POST", "/v1/node/"+node.ID+"/eligibility", buf)
|
|
|
|
require.Nil(err)
|
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
_, err = s.Server.NodeSpecificRequest(respW, req)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.Contains(err.Error(), "invalid")
|
|
|
|
})
|
2015-09-07 03:03:59 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 06:51:53 +00:00
|
|
|
func TestHTTP_NodePurge(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-10-26 06:51:53 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
|
|
|
// Create the node
|
|
|
|
node := mock.Node()
|
|
|
|
args := structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
var resp structs.NodeUpdateResponse
|
|
|
|
if err := s.Agent.RPC("Node.Register", &args, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add some allocations to the node
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
alloc1 := mock.Alloc()
|
|
|
|
alloc1.NodeID = node.ID
|
|
|
|
if err := state.UpsertJobSummary(999, mock.JobSummary(alloc1.JobID)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-10-19 13:30:15 +00:00
|
|
|
err := state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{alloc1})
|
2017-10-26 06:51:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the HTTP request to purge it
|
|
|
|
req, err := http.NewRequest("POST", "/v1/node/"+node.ID+"/purge", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.NodeSpecificRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the index
|
2021-05-07 17:58:40 +00:00
|
|
|
if respW.Header().Get("X-Nomad-Index") == "" {
|
2017-10-26 06:51:53 +00:00
|
|
|
t.Fatalf("missing index")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the response
|
|
|
|
upd := obj.(structs.NodeUpdateResponse)
|
|
|
|
if len(upd.EvalIDs) == 0 {
|
|
|
|
t.Fatalf("bad: %v", upd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the node is not present anymore
|
|
|
|
args1 := structs.NodeSpecificRequest{
|
|
|
|
NodeID: node.ID,
|
|
|
|
QueryOptions: structs.QueryOptions{Region: "global"},
|
|
|
|
}
|
|
|
|
var resp1 structs.SingleNodeResponse
|
|
|
|
if err := s.Agent.RPC("Node.GetNode", &args1, &resp1); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp1.Node != nil {
|
|
|
|
t.Fatalf("node still exists after purging: %#v", resp1.Node)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-09-06 21:18:11 +00:00
|
|
|
func TestHTTP_NodeQuery(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2015-09-06 21:18:11 +00:00
|
|
|
// Create the job
|
|
|
|
node := mock.Node()
|
|
|
|
args := structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-09-06 21:18:11 +00:00
|
|
|
}
|
|
|
|
var resp structs.NodeUpdateResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := s.Agent.RPC("Node.Register", &args, &resp); err != nil {
|
2015-09-06 21:18:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/node/"+node.ID, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.NodeSpecificRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the index
|
2021-05-07 17:58:40 +00:00
|
|
|
if respW.Header().Get("X-Nomad-Index") == "" {
|
2015-09-06 21:18:11 +00:00
|
|
|
t.Fatalf("missing index")
|
|
|
|
}
|
2021-05-07 17:58:40 +00:00
|
|
|
if respW.Header().Get("X-Nomad-KnownLeader") != "true" {
|
2015-09-06 21:18:11 +00:00
|
|
|
t.Fatalf("missing known leader")
|
|
|
|
}
|
2021-05-07 17:58:40 +00:00
|
|
|
if respW.Header().Get("X-Nomad-LastContact") == "" {
|
2015-09-06 21:18:11 +00:00
|
|
|
t.Fatalf("missing last contact")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the node
|
|
|
|
n := obj.(*structs.Node)
|
|
|
|
if n.ID != node.ID {
|
|
|
|
t.Fatalf("bad: %#v", n)
|
|
|
|
}
|
2018-03-14 00:59:37 +00:00
|
|
|
if len(n.Events) < 1 {
|
2018-03-08 14:34:08 +00:00
|
|
|
t.Fatalf("Expected node registration event to be populated: %#v", n)
|
|
|
|
}
|
2018-05-12 00:26:25 +00:00
|
|
|
if n.Events[0].Message != "Node registered" {
|
2018-03-08 14:34:08 +00:00
|
|
|
t.Fatalf("Expected node registration event to be first node event: %#v", n)
|
|
|
|
}
|
2015-09-06 21:18:11 +00:00
|
|
|
})
|
|
|
|
}
|