2015-09-06 22:37:21 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2017-11-16 01:44:57 +00:00
|
|
|
"archive/tar"
|
2017-10-07 01:45:55 +00:00
|
|
|
"fmt"
|
2017-11-16 01:44:57 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2015-09-06 22:37:21 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-11-16 01:44:57 +00:00
|
|
|
"os"
|
2016-12-02 00:27:22 +00:00
|
|
|
"reflect"
|
2019-06-21 16:20:06 +00:00
|
|
|
"strconv"
|
2016-05-28 00:28:17 +00:00
|
|
|
"strings"
|
2015-09-06 22:37:21 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-12-02 00:27:22 +00:00
|
|
|
"github.com/golang/snappy"
|
2017-10-04 22:08:58 +00:00
|
|
|
"github.com/hashicorp/nomad/acl"
|
2017-11-16 01:44:57 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2018-05-31 20:11:11 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2018-02-06 18:53:00 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2015-09-06 22:37:21 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2017-11-16 01:44:57 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2018-02-13 22:54:27 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-09-06 22:37:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHTTP_AllocsList(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 22:37:21 +00:00
|
|
|
// Directly manipulate the state
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
alloc1 := mock.Alloc()
|
2017-11-17 20:53:26 +00:00
|
|
|
testEvent := structs.NewTaskEvent(structs.TaskSiblingFailed)
|
|
|
|
var events1 []*structs.TaskEvent
|
|
|
|
events1 = append(events1, testEvent)
|
|
|
|
taskState := &structs.TaskState{Events: events1}
|
|
|
|
alloc1.TaskStates = make(map[string]*structs.TaskState)
|
|
|
|
alloc1.TaskStates["test"] = taskState
|
|
|
|
|
2015-09-06 22:37:21 +00:00
|
|
|
alloc2 := mock.Alloc()
|
2017-11-17 20:53:26 +00:00
|
|
|
alloc2.TaskStates = make(map[string]*structs.TaskState)
|
|
|
|
alloc2.TaskStates["test"] = taskState
|
|
|
|
|
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 22:37:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/allocations", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.AllocsRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the index
|
|
|
|
if respW.HeaderMap.Get("X-Nomad-Index") == "" {
|
|
|
|
t.Fatalf("missing index")
|
|
|
|
}
|
|
|
|
if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" {
|
|
|
|
t.Fatalf("missing known leader")
|
|
|
|
}
|
|
|
|
if respW.HeaderMap.Get("X-Nomad-LastContact") == "" {
|
|
|
|
t.Fatalf("missing last contact")
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:46:57 +00:00
|
|
|
// Check the alloc
|
2017-11-17 20:53:26 +00:00
|
|
|
allocs := obj.([]*structs.AllocListStub)
|
|
|
|
if len(allocs) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", allocs)
|
|
|
|
}
|
|
|
|
expectedMsg := "Task's sibling failed"
|
|
|
|
displayMsg1 := allocs[0].TaskStates["test"].Events[0].DisplayMessage
|
2018-02-13 22:54:27 +00:00
|
|
|
require.Equal(t, expectedMsg, displayMsg1, "DisplayMessage should be set")
|
2017-11-17 20:53:26 +00:00
|
|
|
displayMsg2 := allocs[0].TaskStates["test"].Events[0].DisplayMessage
|
2018-02-13 22:54:27 +00:00
|
|
|
require.Equal(t, expectedMsg, displayMsg2, "DisplayMessage should be set")
|
2015-09-06 22:37:21 +00:00
|
|
|
})
|
|
|
|
}
|
2015-12-24 10:46:59 +00:00
|
|
|
|
|
|
|
func TestHTTP_AllocsPrefixList(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()
|
2016-07-21 21:43:21 +00:00
|
|
|
|
2015-12-24 10:46:59 +00:00
|
|
|
alloc1 := mock.Alloc()
|
|
|
|
alloc1.ID = "aaaaaaaa-e8f7-fd38-c855-ab94ceb89706"
|
|
|
|
alloc2 := mock.Alloc()
|
|
|
|
alloc2.ID = "aaabbbbb-e8f7-fd38-c855-ab94ceb89706"
|
2017-11-17 20:53:26 +00:00
|
|
|
|
|
|
|
testEvent := structs.NewTaskEvent(structs.TaskSiblingFailed)
|
|
|
|
var events1 []*structs.TaskEvent
|
|
|
|
events1 = append(events1, testEvent)
|
|
|
|
taskState := &structs.TaskState{Events: events1}
|
|
|
|
alloc2.TaskStates = make(map[string]*structs.TaskState)
|
|
|
|
alloc2.TaskStates["test"] = taskState
|
|
|
|
|
2016-07-21 21:43:21 +00:00
|
|
|
summary1 := mock.JobSummary(alloc1.JobID)
|
|
|
|
summary2 := mock.JobSummary(alloc2.JobID)
|
|
|
|
if err := state.UpsertJobSummary(998, summary1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := state.UpsertJobSummary(999, summary2); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-10-19 13:30:15 +00:00
|
|
|
if err := state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{alloc1, alloc2}); err != nil {
|
2015-12-24 10:46:59 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/allocations?prefix=aaab", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.AllocsRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the index
|
|
|
|
if respW.HeaderMap.Get("X-Nomad-Index") == "" {
|
|
|
|
t.Fatalf("missing index")
|
|
|
|
}
|
|
|
|
if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" {
|
|
|
|
t.Fatalf("missing known leader")
|
|
|
|
}
|
|
|
|
if respW.HeaderMap.Get("X-Nomad-LastContact") == "" {
|
|
|
|
t.Fatalf("missing last contact")
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:46:57 +00:00
|
|
|
// Check the alloc
|
2015-12-24 10:46:59 +00:00
|
|
|
n := obj.([]*structs.AllocListStub)
|
|
|
|
if len(n) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", n)
|
|
|
|
}
|
2016-01-06 21:46:57 +00:00
|
|
|
|
|
|
|
// Check the identifier
|
|
|
|
if n[0].ID != alloc2.ID {
|
|
|
|
t.Fatalf("expected alloc ID: %v, Actual: %v", alloc2.ID, n[0].ID)
|
|
|
|
}
|
2017-11-17 20:53:26 +00:00
|
|
|
expectedMsg := "Task's sibling failed"
|
|
|
|
displayMsg1 := n[0].TaskStates["test"].Events[0].DisplayMessage
|
2018-02-13 22:54:27 +00:00
|
|
|
require.Equal(t, expectedMsg, displayMsg1, "DisplayMessage should be set")
|
2017-11-17 20:53:26 +00:00
|
|
|
|
2015-12-24 10:46:59 +00:00
|
|
|
})
|
|
|
|
}
|
2015-09-06 22:49:44 +00:00
|
|
|
|
|
|
|
func TestHTTP_AllocQuery(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 22:49:44 +00:00
|
|
|
// Directly manipulate the state
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
alloc := mock.Alloc()
|
2016-07-21 21:43:21 +00:00
|
|
|
if err := state.UpsertJobSummary(999, mock.JobSummary(alloc.JobID)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-10-19 13:30:15 +00:00
|
|
|
err := state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{alloc})
|
2015-09-06 22:49:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/allocation/"+alloc.ID, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.AllocSpecificRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the index
|
|
|
|
if respW.HeaderMap.Get("X-Nomad-Index") == "" {
|
|
|
|
t.Fatalf("missing index")
|
|
|
|
}
|
|
|
|
if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" {
|
|
|
|
t.Fatalf("missing known leader")
|
|
|
|
}
|
|
|
|
if respW.HeaderMap.Get("X-Nomad-LastContact") == "" {
|
|
|
|
t.Fatalf("missing last contact")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the job
|
|
|
|
a := obj.(*structs.Allocation)
|
|
|
|
if a.ID != alloc.ID {
|
|
|
|
t.Fatalf("bad: %#v", a)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-05-28 00:28:17 +00:00
|
|
|
|
2016-12-14 20:50:08 +00:00
|
|
|
func TestHTTP_AllocQuery_Payload(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) {
|
2016-12-02 00:27:22 +00:00
|
|
|
// Directly manipulate the state
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
if err := state.UpsertJobSummary(999, mock.JobSummary(alloc.JobID)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-12-14 20:50:08 +00:00
|
|
|
// Insert Payload compressed
|
2016-12-02 00:27:22 +00:00
|
|
|
expected := []byte("hello world")
|
|
|
|
compressed := snappy.Encode(nil, expected)
|
2016-12-14 20:50:08 +00:00
|
|
|
alloc.Job.Payload = compressed
|
2016-12-02 00:27:22 +00:00
|
|
|
|
2020-10-19 13:30:15 +00:00
|
|
|
err := state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{alloc})
|
2016-12-02 00:27:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/allocation/"+alloc.ID, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.AllocSpecificRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the index
|
|
|
|
if respW.HeaderMap.Get("X-Nomad-Index") == "" {
|
|
|
|
t.Fatalf("missing index")
|
|
|
|
}
|
|
|
|
if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" {
|
|
|
|
t.Fatalf("missing known leader")
|
|
|
|
}
|
|
|
|
if respW.HeaderMap.Get("X-Nomad-LastContact") == "" {
|
|
|
|
t.Fatalf("missing last contact")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the job
|
|
|
|
a := obj.(*structs.Allocation)
|
|
|
|
if a.ID != alloc.ID {
|
|
|
|
t.Fatalf("bad: %#v", a)
|
|
|
|
}
|
|
|
|
|
2016-12-14 20:50:08 +00:00
|
|
|
// Check the payload is decompressed
|
|
|
|
if !reflect.DeepEqual(a.Job.Payload, expected) {
|
|
|
|
t.Fatalf("Payload not decompressed properly; got %#v; want %#v", a.Job.Payload, expected)
|
2016-12-02 00:27:22 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-01 12:56:02 +00:00
|
|
|
func TestHTTP_AllocRestart(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Validates that all methods of forwarding the request are processed correctly
|
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
|
|
|
// Local node, local resp
|
|
|
|
{
|
|
|
|
// Make the HTTP request
|
|
|
|
buf := encodeReq(map[string]string{})
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/allocation/%s/restart", uuid.Generate()), buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.True(structs.IsErrUnknownAllocation(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local node, server resp
|
|
|
|
{
|
|
|
|
srv := s.server
|
|
|
|
s.server = nil
|
|
|
|
|
|
|
|
buf := encodeReq(map[string]string{})
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/allocation/%s/restart", uuid.Generate()), buf)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.True(structs.IsErrUnknownAllocation(err))
|
|
|
|
|
|
|
|
s.server = srv
|
|
|
|
}
|
|
|
|
|
|
|
|
// no client, server resp
|
|
|
|
{
|
|
|
|
c := s.client
|
|
|
|
s.client = nil
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
n, err := s.server.State().NodeByID(nil, c.NodeID())
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return n != nil, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have client: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
buf := encodeReq(map[string]string{})
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/allocation/%s/restart", uuid.Generate()), buf)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.True(structs.IsErrUnknownAllocation(err))
|
|
|
|
|
|
|
|
s.client = c
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTP_AllocRestart_ACL(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
httpACLTest(t, nil, func(s *TestAgent) {
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
|
|
|
|
// If there's no token, we expect the request to fail.
|
|
|
|
{
|
|
|
|
buf := encodeReq(map[string]string{})
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/allocation/%s/restart", uuid.Generate()), buf)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
|
|
|
require.NotNil(err)
|
2019-10-01 20:06:24 +00:00
|
|
|
require.True(structs.IsErrUnknownAllocation(err), "(%T) %v", err, err)
|
2019-04-01 12:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with an invalid token and expect it to fail
|
|
|
|
{
|
|
|
|
buf := encodeReq(map[string]string{})
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/allocation/%s/restart", uuid.Generate()), buf)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1005, "invalid", mock.NodePolicy(acl.PolicyWrite))
|
|
|
|
setToken(req, token)
|
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
|
|
|
require.NotNil(err)
|
2019-10-01 20:06:24 +00:00
|
|
|
require.True(structs.IsErrUnknownAllocation(err), "(%T) %v", err, err)
|
2019-04-01 12:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a valid token
|
|
|
|
// Still returns an error because the alloc does not exist
|
|
|
|
{
|
|
|
|
buf := encodeReq(map[string]string{})
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/allocation/%s/restart", uuid.Generate()), buf)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
policy := mock.NamespacePolicy(structs.DefaultNamespace, "", []string{acl.NamespaceCapabilityAllocLifecycle})
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1007, "valid", policy)
|
|
|
|
setToken(req, token)
|
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
|
|
|
require.NotNil(err)
|
2019-10-01 20:06:24 +00:00
|
|
|
require.True(structs.IsErrUnknownAllocation(err), "(%T) %v", err, err)
|
2019-04-01 12:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a management token
|
|
|
|
// Still returns an error because the alloc does not exist
|
|
|
|
{
|
|
|
|
buf := encodeReq(map[string]string{})
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/allocation/%s/restart", uuid.Generate()), buf)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
setToken(req, s.RootToken)
|
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.True(structs.IsErrUnknownAllocation(err))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-01 14:21:03 +00:00
|
|
|
func TestHTTP_AllocStop(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
|
|
|
// Directly manipulate the state
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
require := require.New(t)
|
|
|
|
require.NoError(state.UpsertJobSummary(999, mock.JobSummary(alloc.JobID)))
|
|
|
|
|
2020-10-19 13:30:15 +00:00
|
|
|
require.NoError(state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{alloc}))
|
2019-04-01 14:21:03 +00:00
|
|
|
|
2019-04-03 10:46:15 +00:00
|
|
|
// Test that the happy path works
|
2019-04-01 14:21:03 +00:00
|
|
|
{
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("POST", "/v1/allocation/"+alloc.ID+"/stop", nil)
|
|
|
|
require.NoError(err)
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.AllocSpecificRequest(respW, req)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
a := obj.(*structs.AllocStopResponse)
|
|
|
|
require.NotEmpty(a.EvalID, "missing eval")
|
|
|
|
require.NotEmpty(a.Index, "missing index")
|
2019-06-21 16:20:06 +00:00
|
|
|
headerIndex, _ := strconv.ParseUint(respW.Header().Get("X-Nomad-Index"), 10, 64)
|
|
|
|
require.Equal(a.Index, headerIndex)
|
2019-04-01 14:21:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 10:46:15 +00:00
|
|
|
// Test that we 404 when the allocid is invalid
|
2019-04-01 14:21:03 +00:00
|
|
|
{
|
|
|
|
// Make the HTTP request
|
2019-06-20 20:52:40 +00:00
|
|
|
req, err := http.NewRequest("POST", "/v1/allocation/"+uuid.Generate()+"/stop", nil)
|
2019-04-01 14:21:03 +00:00
|
|
|
require.NoError(err)
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
2019-06-20 20:52:40 +00:00
|
|
|
_, err = s.Server.AllocSpecificRequest(respW, req)
|
|
|
|
require.NotNil(err)
|
|
|
|
if !strings.Contains(err.Error(), allocNotFoundErr) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2019-04-01 14:21:03 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-28 00:28:17 +00:00
|
|
|
func TestHTTP_AllocStats(t *testing.T) {
|
2017-07-20 05:42:15 +00:00
|
|
|
t.Parallel()
|
2018-02-06 18:53:00 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2018-02-06 18:53:00 +00:00
|
|
|
// Local node, local resp
|
|
|
|
{
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/allocation/%s/stats", uuid.Generate()), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
|
|
|
require.NotNil(err)
|
2018-02-13 23:50:51 +00:00
|
|
|
require.True(structs.IsErrUnknownAllocation(err))
|
2016-05-28 00:28:17 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 18:53:00 +00:00
|
|
|
// Local node, server resp
|
|
|
|
{
|
|
|
|
srv := s.server
|
|
|
|
s.server = nil
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/allocation/%s/stats", uuid.Generate()), nil)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
|
|
|
require.NotNil(err)
|
2018-02-13 23:50:51 +00:00
|
|
|
require.True(structs.IsErrUnknownAllocation(err))
|
2018-02-06 18:53:00 +00:00
|
|
|
|
|
|
|
s.server = srv
|
|
|
|
}
|
|
|
|
|
|
|
|
// no client, server resp
|
|
|
|
{
|
|
|
|
c := s.client
|
|
|
|
s.client = nil
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
n, err := s.server.State().NodeByID(nil, c.NodeID())
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return n != nil, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have client: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/allocation/%s/stats", uuid.Generate()), nil)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
|
|
|
require.NotNil(err)
|
2018-02-13 23:50:51 +00:00
|
|
|
require.True(structs.IsErrUnknownAllocation(err))
|
2018-02-06 18:53:00 +00:00
|
|
|
|
|
|
|
s.client = c
|
2016-09-22 04:28:12 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-06 00:33:05 +00:00
|
|
|
func TestHTTP_AllocStats_ACL(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-02-13 22:54:27 +00:00
|
|
|
require := require.New(t)
|
2017-10-06 00:33:05 +00:00
|
|
|
|
|
|
|
httpACLTest(t, nil, func(s *TestAgent) {
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
|
|
|
|
// Make the HTTP request
|
2018-02-06 18:53:00 +00:00
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/allocation/%s/stats", uuid.Generate()), nil)
|
2017-10-06 00:33:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request without a token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err := s.Server.ClientAllocRequest(respW, req)
|
2018-02-13 22:54:27 +00:00
|
|
|
require.NotNil(err)
|
2019-10-01 20:06:24 +00:00
|
|
|
require.True(structs.IsErrUnknownAllocation(err), "(%T) %v", err, err)
|
2017-10-06 00:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with an invalid token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1005, "invalid", mock.NodePolicy(acl.PolicyWrite))
|
|
|
|
setToken(req, token)
|
|
|
|
_, err := s.Server.ClientAllocRequest(respW, req)
|
2018-02-13 22:54:27 +00:00
|
|
|
require.NotNil(err)
|
2019-10-01 20:06:24 +00:00
|
|
|
require.True(structs.IsErrUnknownAllocation(err), "(%T) %v", err, err)
|
2017-10-06 00:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a valid token
|
|
|
|
// Still returns an error because the alloc does not exist
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
policy := mock.NamespacePolicy(structs.DefaultNamespace, "", []string{acl.NamespaceCapabilityReadJob})
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1007, "valid", policy)
|
|
|
|
setToken(req, token)
|
|
|
|
_, err := s.Server.ClientAllocRequest(respW, req)
|
2018-02-13 22:54:27 +00:00
|
|
|
require.NotNil(err)
|
2019-10-01 20:06:24 +00:00
|
|
|
require.True(structs.IsErrUnknownAllocation(err), "(%T) %v", err, err)
|
2017-10-06 00:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a management token
|
|
|
|
// Still returns an error because the alloc does not exist
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
setToken(req, s.RootToken)
|
|
|
|
_, err := s.Server.ClientAllocRequest(respW, req)
|
2018-02-13 22:54:27 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.True(structs.IsErrUnknownAllocation(err))
|
2017-10-06 00:33:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-22 04:28:12 +00:00
|
|
|
func TestHTTP_AllocSnapshot(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) {
|
2016-09-22 04:28:12 +00:00
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/client/allocation/123/snapshot", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
2016-05-28 00:28:17 +00:00
|
|
|
// Make the request
|
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
2016-05-30 23:58:32 +00:00
|
|
|
if !strings.Contains(err.Error(), allocNotFoundErr) {
|
2016-05-28 00:28:17 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-12-20 22:27:38 +00:00
|
|
|
|
2017-10-04 18:46:47 +00:00
|
|
|
func TestHTTP_AllocSnapshot_WithMigrateToken(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-02-13 22:54:27 +00:00
|
|
|
require := require.New(t)
|
2017-10-04 18:46:47 +00:00
|
|
|
httpACLTest(t, nil, func(s *TestAgent) {
|
2017-10-10 00:23:26 +00:00
|
|
|
// Request without a token fails
|
2017-10-04 18:46:47 +00:00
|
|
|
req, err := http.NewRequest("GET", "/v1/client/allocation/123/snapshot", nil)
|
2018-02-13 22:54:27 +00:00
|
|
|
require.Nil(err)
|
2017-10-04 18:46:47 +00:00
|
|
|
|
|
|
|
// Make the unauthorized request
|
|
|
|
respW := httptest.NewRecorder()
|
2017-10-07 00:20:30 +00:00
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
2018-02-13 22:54:27 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.EqualError(err, structs.ErrPermissionDenied.Error())
|
2017-10-07 00:20:30 +00:00
|
|
|
|
|
|
|
// Create an allocation
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
|
2018-01-12 21:58:44 +00:00
|
|
|
validMigrateToken, err := structs.GenerateMigrateToken(alloc.ID, s.Agent.Client().Node().SecretID)
|
2018-02-13 22:54:27 +00:00
|
|
|
require.Nil(err)
|
2017-10-07 00:20:30 +00:00
|
|
|
|
|
|
|
// Request with a token succeeds
|
2017-10-07 01:45:55 +00:00
|
|
|
url := fmt.Sprintf("/v1/client/allocation/%s/snapshot", alloc.ID)
|
|
|
|
req, err = http.NewRequest("GET", url, nil)
|
2018-02-13 22:54:27 +00:00
|
|
|
require.Nil(err)
|
2017-10-07 00:20:30 +00:00
|
|
|
|
2017-10-07 01:45:55 +00:00
|
|
|
req.Header.Set("X-Nomad-Token", validMigrateToken)
|
|
|
|
|
2017-10-07 00:20:30 +00:00
|
|
|
// Make the unauthorized request
|
|
|
|
respW = httptest.NewRecorder()
|
2017-10-04 18:46:47 +00:00
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
2018-02-13 22:54:27 +00:00
|
|
|
require.NotContains(err.Error(), structs.ErrPermissionDenied.Error())
|
2017-10-04 18:46:47 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-16 01:44:57 +00:00
|
|
|
// TestHTTP_AllocSnapshot_Atomic ensures that when a client encounters an error
|
|
|
|
// snapshotting a valid tar is not returned.
|
|
|
|
func TestHTTP_AllocSnapshot_Atomic(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-05-31 19:05:44 +00:00
|
|
|
httpTest(t, func(c *Config) {
|
|
|
|
// Disable the schedulers
|
2018-05-31 20:11:11 +00:00
|
|
|
c.Server.NumSchedulers = helper.IntToPtr(0)
|
2018-05-31 19:05:44 +00:00
|
|
|
}, func(s *TestAgent) {
|
2017-11-16 01:44:57 +00:00
|
|
|
// Create an alloc
|
|
|
|
state := s.server.State()
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.Job.TaskGroups[0].Tasks[0].Driver = "mock_driver"
|
2018-11-06 17:04:07 +00:00
|
|
|
alloc.Job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
|
|
|
|
"run_for": "30s",
|
|
|
|
}
|
2017-11-16 01:44:57 +00:00
|
|
|
alloc.NodeID = s.client.NodeID()
|
|
|
|
state.UpsertJobSummary(998, mock.JobSummary(alloc.JobID))
|
2020-10-19 13:30:15 +00:00
|
|
|
if err := state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{alloc.Copy()}); err != nil {
|
2017-11-16 01:44:57 +00:00
|
|
|
t.Fatalf("error upserting alloc: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the client to run it
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2018-09-27 00:33:32 +00:00
|
|
|
if _, err := s.client.GetAllocState(alloc.ID); err != nil {
|
2017-11-16 01:44:57 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
serverAlloc, err := state.AllocByID(nil, alloc.ID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return serverAlloc.ClientStatus == structs.AllocClientStatusRunning, fmt.Errorf(serverAlloc.ClientStatus)
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("client not running alloc: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Now write to its shared dir
|
|
|
|
allocDirI, err := s.client.GetAllocFS(alloc.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find alloc dir: %v", err)
|
|
|
|
}
|
|
|
|
allocDir := allocDirI.(*allocdir.AllocDir)
|
|
|
|
|
|
|
|
// Remove the task dir to break Snapshot
|
|
|
|
os.RemoveAll(allocDir.TaskDirs["web"].LocalDir)
|
|
|
|
|
2018-02-13 22:54:27 +00:00
|
|
|
// require Snapshot fails
|
2017-11-30 01:24:58 +00:00
|
|
|
if err := allocDir.Snapshot(ioutil.Discard); err != nil {
|
2018-09-13 17:43:40 +00:00
|
|
|
t.Logf("[DEBUG] agent.test: snapshot returned error: %v", err)
|
2017-11-30 01:24:58 +00:00
|
|
|
} else {
|
|
|
|
t.Errorf("expected Snapshot() to fail but it did not")
|
2017-11-16 01:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make the HTTP request to ensure the Snapshot error is
|
2017-11-17 01:48:06 +00:00
|
|
|
// propagated through to the HTTP layer. Since the tar is
|
|
|
|
// streamed over a 200 HTTP response the only way to signal an
|
|
|
|
// error is by writing a marker file.
|
2017-11-16 01:44:57 +00:00
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/allocation/%s/snapshot", alloc.ID), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the request via the mux to make sure the error returned
|
2018-03-11 18:39:26 +00:00
|
|
|
// by Snapshot is properly propagated via HTTP
|
2017-11-16 01:44:57 +00:00
|
|
|
s.Server.mux.ServeHTTP(respW, req)
|
|
|
|
resp := respW.Result()
|
|
|
|
r := tar.NewReader(resp.Body)
|
2017-11-17 01:48:06 +00:00
|
|
|
errorFilename := allocdir.SnapshotErrorFilename(alloc.ID)
|
|
|
|
markerFound := false
|
|
|
|
markerContents := ""
|
2017-11-16 01:44:57 +00:00
|
|
|
for {
|
|
|
|
header, err := r.Next()
|
|
|
|
if err != nil {
|
2017-11-17 01:48:06 +00:00
|
|
|
if err != io.EOF {
|
|
|
|
// Huh, I wonder how a non-EOF error can happen?
|
|
|
|
t.Errorf("Unexpected error while streaming: %v", err)
|
2017-11-16 01:44:57 +00:00
|
|
|
}
|
2017-11-17 01:48:06 +00:00
|
|
|
break
|
2017-11-16 01:44:57 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 01:48:06 +00:00
|
|
|
if markerFound {
|
|
|
|
// No more files should be found after the failure marker
|
|
|
|
t.Errorf("Next file found after error marker: %s", header.Name)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if header.Name == errorFilename {
|
|
|
|
// Found it!
|
|
|
|
markerFound = true
|
|
|
|
buf := make([]byte, int(header.Size))
|
2018-03-27 18:14:53 +00:00
|
|
|
if _, err := r.Read(buf); err != nil && err != io.EOF {
|
2017-11-17 01:48:06 +00:00
|
|
|
t.Errorf("Unexpected error reading error marker %s: %v", errorFilename, err)
|
|
|
|
} else {
|
|
|
|
markerContents = string(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !markerFound {
|
|
|
|
t.Fatalf("marker file %s not written; bad tar will be treated as good!", errorFilename)
|
|
|
|
}
|
|
|
|
if markerContents == "" {
|
|
|
|
t.Fatalf("marker file %s empty", markerContents)
|
|
|
|
} else {
|
|
|
|
t.Logf("EXPECTED snapshot error: %s", markerContents)
|
2017-11-16 01:44:57 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-20 22:27:38 +00:00
|
|
|
func TestHTTP_AllocGC(t *testing.T) {
|
2017-07-20 05:42:15 +00:00
|
|
|
t.Parallel()
|
2018-02-06 18:53:00 +00:00
|
|
|
require := require.New(t)
|
|
|
|
path := fmt.Sprintf("/v1/client/allocation/%s/gc", uuid.Generate())
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2018-02-06 18:53:00 +00:00
|
|
|
// Local node, local resp
|
|
|
|
{
|
|
|
|
req, err := http.NewRequest("GET", path, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
2018-02-13 23:50:51 +00:00
|
|
|
if !structs.IsErrUnknownAllocation(err) {
|
2018-02-06 18:53:00 +00:00
|
|
|
t.Fatalf("unexpected err: %v", err)
|
|
|
|
}
|
2016-12-20 22:27:38 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 18:53:00 +00:00
|
|
|
// Local node, server resp
|
|
|
|
{
|
|
|
|
srv := s.server
|
|
|
|
s.server = nil
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", path, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
2018-02-13 23:50:51 +00:00
|
|
|
if !structs.IsErrUnknownAllocation(err) {
|
2018-02-06 18:53:00 +00:00
|
|
|
t.Fatalf("unexpected err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.server = srv
|
|
|
|
}
|
|
|
|
|
|
|
|
// no client, server resp
|
|
|
|
{
|
|
|
|
c := s.client
|
|
|
|
s.client = nil
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
n, err := s.server.State().NodeByID(nil, c.NodeID())
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return n != nil, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have client: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", path, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
|
|
|
require.NotNil(err)
|
2018-02-13 23:50:51 +00:00
|
|
|
if !structs.IsErrUnknownAllocation(err) {
|
2018-02-06 18:53:00 +00:00
|
|
|
t.Fatalf("unexpected err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.client = c
|
2016-12-20 22:27:38 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-06 00:33:05 +00:00
|
|
|
func TestHTTP_AllocGC_ACL(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-02-13 22:54:27 +00:00
|
|
|
require := require.New(t)
|
2018-02-06 18:53:00 +00:00
|
|
|
path := fmt.Sprintf("/v1/client/allocation/%s/gc", uuid.Generate())
|
2017-10-06 00:33:05 +00:00
|
|
|
|
|
|
|
httpACLTest(t, nil, func(s *TestAgent) {
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
|
|
|
|
// Make the HTTP request
|
2018-02-06 18:53:00 +00:00
|
|
|
req, err := http.NewRequest("GET", path, nil)
|
2017-10-06 00:33:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request without a token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err := s.Server.ClientAllocRequest(respW, req)
|
2018-02-13 22:54:27 +00:00
|
|
|
require.NotNil(err)
|
2019-10-01 20:06:24 +00:00
|
|
|
require.True(structs.IsErrUnknownAllocation(err), "(%T) %v", err, err)
|
2017-10-06 00:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with an invalid token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1005, "invalid", mock.NodePolicy(acl.PolicyWrite))
|
|
|
|
setToken(req, token)
|
|
|
|
_, err := s.Server.ClientAllocRequest(respW, req)
|
2018-02-13 22:54:27 +00:00
|
|
|
require.NotNil(err)
|
2019-10-01 20:06:24 +00:00
|
|
|
require.True(structs.IsErrUnknownAllocation(err), "(%T) %v", err, err)
|
2017-10-06 00:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a valid token
|
|
|
|
// Still returns an error because the alloc does not exist
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
policy := mock.NamespacePolicy(structs.DefaultNamespace, "", []string{acl.NamespaceCapabilitySubmitJob})
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1007, "valid", policy)
|
|
|
|
setToken(req, token)
|
|
|
|
_, err := s.Server.ClientAllocRequest(respW, req)
|
2018-02-13 22:54:27 +00:00
|
|
|
require.NotNil(err)
|
2019-10-01 20:06:24 +00:00
|
|
|
require.True(structs.IsErrUnknownAllocation(err), "(%T) %v", err, err)
|
2017-10-06 00:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a management token
|
|
|
|
// Still returns an error because the alloc does not exist
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
setToken(req, s.RootToken)
|
|
|
|
_, err := s.Server.ClientAllocRequest(respW, req)
|
2018-02-13 22:54:27 +00:00
|
|
|
require.NotNil(err)
|
2018-02-06 18:53:00 +00:00
|
|
|
require.True(structs.IsErrUnknownAllocation(err))
|
2017-10-06 00:33:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-20 22:27:38 +00:00
|
|
|
func TestHTTP_AllocAllGC(t *testing.T) {
|
2017-07-20 05:42:15 +00:00
|
|
|
t.Parallel()
|
2018-02-06 18:53:00 +00:00
|
|
|
require := require.New(t)
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2018-02-06 18:53:00 +00:00
|
|
|
// Local node, local resp
|
|
|
|
{
|
|
|
|
req, err := http.NewRequest("GET", "/v1/client/gc", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.ClientGCRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected err: %v", err)
|
|
|
|
}
|
2016-12-20 22:27:38 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 18:53:00 +00:00
|
|
|
// Local node, server resp
|
|
|
|
{
|
|
|
|
srv := s.server
|
|
|
|
s.server = nil
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/gc?node_id=%s", uuid.Generate()), nil)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.ClientGCRequest(respW, req)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.Contains(err.Error(), "Unknown node")
|
|
|
|
|
|
|
|
s.server = srv
|
|
|
|
}
|
|
|
|
|
2018-05-30 19:45:10 +00:00
|
|
|
// client stats from server, should not error
|
2018-02-06 18:53:00 +00:00
|
|
|
{
|
|
|
|
c := s.client
|
|
|
|
s.client = nil
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
n, err := s.server.State().NodeByID(nil, c.NodeID())
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return n != nil, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have client: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/gc?node_id=%s", c.NodeID()), nil)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.ClientGCRequest(respW, req)
|
2018-05-30 19:40:09 +00:00
|
|
|
require.Nil(err)
|
2018-02-06 18:53:00 +00:00
|
|
|
|
|
|
|
s.client = c
|
2016-12-20 22:27:38 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2017-10-04 22:08:58 +00:00
|
|
|
|
|
|
|
func TestHTTP_AllocAllGC_ACL(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-02-13 22:54:27 +00:00
|
|
|
require := require.New(t)
|
2017-10-04 22:08:58 +00:00
|
|
|
httpACLTest(t, nil, func(s *TestAgent) {
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/client/gc", nil)
|
2018-02-13 22:54:27 +00:00
|
|
|
require.Nil(err)
|
2017-10-04 22:08:58 +00:00
|
|
|
|
|
|
|
// Try request without a token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err := s.Server.ClientGCRequest(respW, req)
|
2018-02-13 22:54:27 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
2017-10-04 22:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with an invalid token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1005, "invalid", mock.NodePolicy(acl.PolicyRead))
|
|
|
|
setToken(req, token)
|
|
|
|
_, err := s.Server.ClientGCRequest(respW, req)
|
2018-02-13 22:54:27 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
2017-10-04 22:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a valid token
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1007, "valid", mock.NodePolicy(acl.PolicyWrite))
|
|
|
|
setToken(req, token)
|
|
|
|
_, err := s.Server.ClientGCRequest(respW, req)
|
2018-02-13 22:54:27 +00:00
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(http.StatusOK, respW.Code)
|
2017-10-04 22:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a management token
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
setToken(req, s.RootToken)
|
|
|
|
_, err := s.Server.ClientGCRequest(respW, req)
|
2018-02-13 22:54:27 +00:00
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(http.StatusOK, respW.Code)
|
2017-10-04 22:08:58 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-04-03 15:18:54 +00:00
|
|
|
|
|
|
|
func TestHTTP_ReadWsHandshake(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
token string
|
|
|
|
handshake bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "plain compatible mode",
|
|
|
|
token: "",
|
|
|
|
handshake: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "handshake unauthenticated",
|
|
|
|
token: "",
|
|
|
|
handshake: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "handshake authenticated",
|
|
|
|
token: "mysupersecret",
|
|
|
|
handshake: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
|
|
|
|
called := false
|
|
|
|
readFn := func(h interface{}) error {
|
|
|
|
called = true
|
|
|
|
if !c.handshake {
|
|
|
|
return fmt.Errorf("should not be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
hm := h.(*wsHandshakeMessage)
|
|
|
|
hm.Version = 1
|
|
|
|
hm.AuthToken = c.token
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
req := httptest.NewRequest("PUT", "/target", nil)
|
|
|
|
if c.handshake {
|
|
|
|
req.URL.RawQuery = "ws_handshake=true"
|
|
|
|
}
|
|
|
|
|
|
|
|
var q structs.QueryOptions
|
|
|
|
|
|
|
|
err := readWsHandshake(readFn, req, &q)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, c.token, q.AuthToken)
|
|
|
|
require.Equal(t, c.handshake, called)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|