2015-09-06 22:37:21 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2017-10-07 01:45:55 +00:00
|
|
|
"fmt"
|
2015-09-06 22:37:21 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2016-12-02 00:27:22 +00:00
|
|
|
"reflect"
|
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-10-13 22:45:35 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad"
|
2015-09-06 22:37:21 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2017-10-04 22:08:58 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
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()
|
|
|
|
alloc2 := mock.Alloc()
|
2016-07-25 21:11:32 +00:00
|
|
|
state.UpsertJobSummary(998, mock.JobSummary(alloc1.JobID))
|
|
|
|
state.UpsertJobSummary(999, mock.JobSummary(alloc2.JobID))
|
2015-09-07 03:47:42 +00:00
|
|
|
err := state.UpsertAllocs(1000,
|
2015-09-06 22:37:21 +00:00
|
|
|
[]*structs.Allocation{alloc1, alloc2})
|
|
|
|
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
|
2015-09-06 22:37:21 +00:00
|
|
|
n := obj.([]*structs.AllocListStub)
|
|
|
|
if len(n) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", n)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
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"
|
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)
|
|
|
|
}
|
|
|
|
if err := state.UpsertAllocs(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)
|
|
|
|
}
|
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)
|
|
|
|
}
|
2015-09-07 03:47:42 +00:00
|
|
|
err := state.UpsertAllocs(1000,
|
2015-09-06 22:49:44 +00:00
|
|
|
[]*structs.Allocation{alloc})
|
|
|
|
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
|
|
|
|
|
|
|
err := state.UpsertAllocs(1000, []*structs.Allocation{alloc})
|
|
|
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-28 00:28:17 +00:00
|
|
|
func TestHTTP_AllocStats(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-05-28 00:28:17 +00:00
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/client/allocation/123/foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
2016-09-22 04:28:12 +00:00
|
|
|
// Make the request
|
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
|
|
|
if !strings.Contains(err.Error(), resourceNotFoundErr) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-06 00:33:05 +00:00
|
|
|
func TestHTTP_AllocStats_ACL(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
httpACLTest(t, nil, func(s *TestAgent) {
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/client/allocation/123/stats", nil)
|
|
|
|
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)
|
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Contains(err.Error(), "unknown allocation ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Contains(err.Error(), "unknown allocation ID")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
assert := assert.New(t)
|
|
|
|
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)
|
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
// Make the unauthorized request
|
|
|
|
respW := httptest.NewRecorder()
|
2017-10-07 00:20:30 +00:00
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
|
|
|
assert.NotNil(err)
|
2017-10-12 00:04:09 +00:00
|
|
|
assert.EqualError(err, structs.ErrPermissionDenied.Error())
|
2017-10-07 00:20:30 +00:00
|
|
|
|
|
|
|
// Create an allocation
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
|
2017-10-13 22:45:35 +00:00
|
|
|
validMigrateToken, err := nomad.GenerateMigrateToken(alloc.ID, s.Agent.Client().Node().SecretID)
|
2017-10-07 00:20:30 +00:00
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
// 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)
|
2017-10-07 00:20:30 +00:00
|
|
|
assert.Nil(err)
|
|
|
|
|
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)
|
2017-10-12 00:04:09 +00:00
|
|
|
assert.NotContains(err.Error(), structs.ErrPermissionDenied.Error())
|
2017-10-04 18:46:47 +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()
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2016-12-20 22:27:38 +00:00
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/client/allocation/123/gc", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
_, err = s.Server.ClientAllocRequest(respW, req)
|
|
|
|
if !strings.Contains(err.Error(), "unable to collect allocation") {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-06 00:33:05 +00:00
|
|
|
func TestHTTP_AllocGC_ACL(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
httpACLTest(t, nil, func(s *TestAgent) {
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/client/allocation/123/gc", nil)
|
|
|
|
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)
|
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Contains(err.Error(), "not present")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Contains(err.Error(), "not present")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-20 22:27:38 +00:00
|
|
|
func TestHTTP_AllocAllGC(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-20 22:27:38 +00:00
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/client/gc", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
_, err = s.Server.ClientGCRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2017-10-04 22:08:58 +00:00
|
|
|
|
|
|
|
func TestHTTP_AllocAllGC_ACL(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
httpACLTest(t, nil, func(s *TestAgent) {
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/client/gc", nil)
|
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
// Try request without a token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err := s.Server.ClientGCRequest(respW, req)
|
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.Equal(http.StatusOK, respW.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a management token
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
setToken(req, s.RootToken)
|
|
|
|
_, err := s.Server.ClientGCRequest(respW, req)
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.Equal(http.StatusOK, respW.Code)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|