2014-01-06 21:21:48 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2016-12-13 06:09:35 +00:00
|
|
|
"bytes"
|
2014-01-06 21:21:48 +00:00
|
|
|
"fmt"
|
2016-11-16 21:45:26 +00:00
|
|
|
"io"
|
2016-11-28 21:08:31 +00:00
|
|
|
"io/ioutil"
|
2014-01-06 21:21:48 +00:00
|
|
|
"net/http"
|
2015-01-15 09:17:35 +00:00
|
|
|
"net/http/httptest"
|
2014-01-06 21:21:48 +00:00
|
|
|
"os"
|
2015-07-30 19:02:37 +00:00
|
|
|
"reflect"
|
2016-03-03 01:08:06 +00:00
|
|
|
"strings"
|
2014-01-06 21:21:48 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2015-05-11 23:48:10 +00:00
|
|
|
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/structs"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2016-11-16 21:45:26 +00:00
|
|
|
"github.com/hashicorp/consul/logger"
|
2017-04-29 16:34:02 +00:00
|
|
|
"github.com/hashicorp/consul/testutil/retry"
|
2016-06-07 20:24:51 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2017-06-24 19:52:41 +00:00
|
|
|
"github.com/hashicorp/consul/watch"
|
2015-05-11 23:48:10 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2014-01-06 21:21:48 +00:00
|
|
|
)
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func makeReadOnlyAgentACL(t *testing.T, srv *HTTPServer) string {
|
2017-05-09 16:58:12 +00:00
|
|
|
args := map[string]interface{}{
|
2016-12-14 17:33:57 +00:00
|
|
|
"Name": "User Token",
|
|
|
|
"Type": "client",
|
2017-05-09 16:58:12 +00:00
|
|
|
"Rules": `agent "" { policy = "read" }`,
|
2016-12-14 17:33:57 +00:00
|
|
|
}
|
2017-05-09 16:58:12 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/acl/create?token=root", jsonReader(args))
|
2016-12-14 17:33:57 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.ACLCreate(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
aclResp := obj.(aclCreateResponse)
|
|
|
|
return aclResp.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_Services(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2014-01-21 01:06:44 +00:00
|
|
|
|
|
|
|
srv1 := &structs.NodeService{
|
|
|
|
ID: "mysql",
|
|
|
|
Service: "mysql",
|
2014-04-03 19:12:23 +00:00
|
|
|
Tags: []string{"master"},
|
2014-01-21 01:06:44 +00:00
|
|
|
Port: 5000,
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
a.state.AddService(srv1, "")
|
2014-01-21 01:06:44 +00:00
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/services", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentServices(nil, req)
|
2014-01-21 01:06:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
val := obj.(map[string]*structs.NodeService)
|
2017-07-14 05:33:47 +00:00
|
|
|
if len(val) != 1 {
|
2014-01-21 01:06:44 +00:00
|
|
|
t.Fatalf("bad services: %v", obj)
|
|
|
|
}
|
|
|
|
if val["mysql"].Port != 5000 {
|
|
|
|
t.Fatalf("bad service: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_Services_ACLFilter(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a.Shutdown()
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-07-14 05:33:47 +00:00
|
|
|
srv1 := &structs.NodeService{
|
|
|
|
ID: "mysql",
|
|
|
|
Service: "mysql",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 5000,
|
|
|
|
}
|
|
|
|
a.state.AddService(srv1, "")
|
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("no token", func(t *testing.T) {
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/services", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentServices(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
val := obj.(map[string]*structs.NodeService)
|
|
|
|
if len(val) != 0 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
2017-05-09 17:46:11 +00:00
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("root token", func(t *testing.T) {
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/services?token=root", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentServices(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
val := obj.(map[string]*structs.NodeService)
|
|
|
|
if len(val) != 1 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
2017-05-09 17:46:11 +00:00
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_Checks(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2014-01-21 01:06:44 +00:00
|
|
|
|
|
|
|
chk1 := &structs.HealthCheck{
|
2017-05-21 07:11:09 +00:00
|
|
|
Node: a.Config.NodeName,
|
2014-01-21 01:06:44 +00:00
|
|
|
CheckID: "mysql",
|
|
|
|
Name: "mysql",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2014-01-21 01:06:44 +00:00
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
a.state.AddCheck(chk1, "")
|
2014-01-21 01:06:44 +00:00
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/checks", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentChecks(nil, req)
|
2014-01-21 01:06:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
2016-06-07 20:24:51 +00:00
|
|
|
val := obj.(map[types.CheckID]*structs.HealthCheck)
|
2014-01-21 01:06:44 +00:00
|
|
|
if len(val) != 1 {
|
|
|
|
t.Fatalf("bad checks: %v", obj)
|
|
|
|
}
|
2017-04-19 23:00:11 +00:00
|
|
|
if val["mysql"].Status != api.HealthPassing {
|
2014-01-21 01:06:44 +00:00
|
|
|
t.Fatalf("bad check: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_Checks_ACLFilter(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a.Shutdown()
|
2016-12-14 22:16:46 +00:00
|
|
|
|
|
|
|
chk1 := &structs.HealthCheck{
|
2017-05-21 07:11:09 +00:00
|
|
|
Node: a.Config.NodeName,
|
2016-12-14 22:16:46 +00:00
|
|
|
CheckID: "mysql",
|
|
|
|
Name: "mysql",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
a.state.AddCheck(chk1, "")
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("no token", func(t *testing.T) {
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/checks", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentChecks(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
val := obj.(map[types.CheckID]*structs.HealthCheck)
|
|
|
|
if len(val) != 0 {
|
|
|
|
t.Fatalf("bad checks: %v", obj)
|
|
|
|
}
|
2017-05-09 17:46:11 +00:00
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("root token", func(t *testing.T) {
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/checks?token=root", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentChecks(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
val := obj.(map[types.CheckID]*structs.HealthCheck)
|
|
|
|
if len(val) != 1 {
|
|
|
|
t.Fatalf("bad checks: %v", obj)
|
|
|
|
}
|
2017-05-09 17:46:11 +00:00
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_Self(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-22 11:03:59 +00:00
|
|
|
cfg := TestConfig()
|
|
|
|
cfg.Meta = map[string]string{"somekey": "somevalue"}
|
|
|
|
a := NewTestAgent(t.Name(), cfg)
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2014-05-25 23:59:48 +00:00
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/self", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentSelf(nil, req)
|
2014-05-25 23:59:48 +00:00
|
|
|
if err != nil {
|
2015-07-30 19:02:37 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2014-05-25 23:59:48 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 00:46:29 +00:00
|
|
|
val := obj.(Self)
|
2017-05-21 07:11:09 +00:00
|
|
|
if int(val.Member.Port) != a.Config.Ports.SerfLan {
|
2014-05-27 22:09:28 +00:00
|
|
|
t.Fatalf("incorrect port: %v", obj)
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
if int(val.Config.Ports.SerfLan) != a.Config.Ports.SerfLan {
|
2014-05-25 23:59:48 +00:00
|
|
|
t.Fatalf("incorrect port: %v", obj)
|
|
|
|
}
|
2015-07-30 19:02:37 +00:00
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
c, err := a.GetLANCoordinate()
|
2015-07-30 19:02:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(c, val.Coord) {
|
|
|
|
t.Fatalf("coordinates are not equal: %v != %v", c, val.Coord)
|
|
|
|
}
|
2017-05-22 11:03:59 +00:00
|
|
|
if !reflect.DeepEqual(cfg.Meta, val.Meta) {
|
|
|
|
t.Fatalf("meta fields are not equal: %v != %v", cfg.Meta, val.Meta)
|
2017-01-06 01:21:56 +00:00
|
|
|
}
|
2015-07-30 19:02:37 +00:00
|
|
|
|
2016-12-13 06:09:35 +00:00
|
|
|
// Make sure there's nothing called "token" that's leaked.
|
2017-05-21 07:11:09 +00:00
|
|
|
raw, err := a.srv.marshalJSON(req, obj)
|
2016-12-13 06:09:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if bytes.Contains(bytes.ToLower(raw), []byte("token")) {
|
|
|
|
t.Fatalf("bad: %s", raw)
|
|
|
|
}
|
2014-05-25 23:59:48 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_Self_ACLDeny(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a.Shutdown()
|
2016-12-14 17:33:57 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("no token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/self", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentSelf(nil, req); !isPermissionDenied(err) {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 17:33:57 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("agent master token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/self?token=towel", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentSelf(nil, req); err != nil {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 17:33:57 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("read-only token", func(t *testing.T) {
|
2017-05-21 07:11:09 +00:00
|
|
|
ro := makeReadOnlyAgentACL(t, a.srv)
|
2017-05-09 17:46:11 +00:00
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/self?token=%s", ro), nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentSelf(nil, req); err != nil {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 17:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_Reload(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-22 11:03:59 +00:00
|
|
|
cfg := TestConfig()
|
2017-06-03 19:22:47 +00:00
|
|
|
cfg.ACLEnforceVersion8 = Bool(false)
|
2017-06-15 16:46:06 +00:00
|
|
|
cfg.Services = []*structs.ServiceDefinition{
|
|
|
|
&structs.ServiceDefinition{Name: "redis"},
|
2016-11-30 18:29:42 +00:00
|
|
|
}
|
2017-06-24 19:52:41 +00:00
|
|
|
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"datacenter": "dc1",
|
|
|
|
"type": "key",
|
|
|
|
"key": "test",
|
|
|
|
"handler": "true",
|
|
|
|
}
|
|
|
|
wp, err := watch.ParseExempt(params, []string{"handler"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Expected watch.Parse to succeed %v", err)
|
|
|
|
}
|
|
|
|
cfg.WatchPlans = append(cfg.WatchPlans, wp)
|
|
|
|
|
2017-06-03 19:22:47 +00:00
|
|
|
a := NewTestAgent(t.Name(), cfg)
|
|
|
|
defer a.Shutdown()
|
2016-11-30 18:29:42 +00:00
|
|
|
|
2017-06-03 19:22:47 +00:00
|
|
|
if _, ok := a.state.services["redis"]; !ok {
|
2016-11-30 18:29:42 +00:00
|
|
|
t.Fatalf("missing redis service")
|
|
|
|
}
|
|
|
|
|
2017-06-03 19:22:47 +00:00
|
|
|
cfg2 := TestConfig()
|
|
|
|
cfg2.ACLEnforceVersion8 = Bool(false)
|
2017-06-15 16:46:06 +00:00
|
|
|
cfg2.Services = []*structs.ServiceDefinition{
|
|
|
|
&structs.ServiceDefinition{Name: "redis-reloaded"},
|
2016-11-30 18:29:42 +00:00
|
|
|
}
|
|
|
|
|
2017-06-24 19:52:41 +00:00
|
|
|
if err := a.ReloadConfig(cfg2); err != nil {
|
2017-06-03 19:22:47 +00:00
|
|
|
t.Fatalf("got error %v want nil", err)
|
2016-11-30 18:29:42 +00:00
|
|
|
}
|
2017-06-03 19:22:47 +00:00
|
|
|
if _, ok := a.state.services["redis-reloaded"]; !ok {
|
2016-11-30 18:29:42 +00:00
|
|
|
t.Fatalf("missing redis-reloaded service")
|
|
|
|
}
|
2017-06-24 19:52:41 +00:00
|
|
|
|
|
|
|
for _, wp := range cfg.WatchPlans {
|
|
|
|
if !wp.IsStopped() {
|
|
|
|
t.Fatalf("Reloading configs should stop watch plans of the previous configuration")
|
|
|
|
}
|
|
|
|
}
|
2016-11-30 18:29:42 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_Reload_ACLDeny(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a.Shutdown()
|
2016-12-14 17:33:57 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("no token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/reload", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentReload(nil, req); !isPermissionDenied(err) {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 17:33:57 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("read-only token", func(t *testing.T) {
|
2017-05-21 07:11:09 +00:00
|
|
|
ro := makeReadOnlyAgentACL(t, a.srv)
|
2017-05-09 17:46:11 +00:00
|
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/agent/reload?token=%s", ro), nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentReload(nil, req); !isPermissionDenied(err) {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 17:33:57 +00:00
|
|
|
|
|
|
|
// This proves we call the ACL function, and we've got the other reload
|
|
|
|
// test to prove we do the reload, which should be sufficient.
|
|
|
|
// The reload logic is a little complex to set up so isn't worth
|
|
|
|
// repeating again here.
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_Members(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2014-01-06 21:21:48 +00:00
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/members", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentMembers(nil, req)
|
2014-01-06 21:21:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
val := obj.([]serf.Member)
|
|
|
|
if len(val) == 0 {
|
|
|
|
t.Fatalf("bad members: %v", obj)
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
if int(val[0].Port) != a.Config.Ports.SerfLan {
|
2014-01-06 21:21:48 +00:00
|
|
|
t.Fatalf("not lan: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_Members_WAN(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2014-01-06 21:21:48 +00:00
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/members?wan=true", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentMembers(nil, req)
|
2014-01-06 21:21:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
val := obj.([]serf.Member)
|
|
|
|
if len(val) == 0 {
|
|
|
|
t.Fatalf("bad members: %v", obj)
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
if int(val[0].Port) != a.Config.Ports.SerfWan {
|
2014-01-06 21:21:48 +00:00
|
|
|
t.Fatalf("not wan: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_Members_ACLFilter(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a.Shutdown()
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("no token", func(t *testing.T) {
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/members", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentMembers(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
val := obj.([]serf.Member)
|
|
|
|
if len(val) != 0 {
|
|
|
|
t.Fatalf("bad members: %v", obj)
|
|
|
|
}
|
2017-05-09 17:46:11 +00:00
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("root token", func(t *testing.T) {
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/members?token=root", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentMembers(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
val := obj.([]serf.Member)
|
|
|
|
if len(val) != 1 {
|
|
|
|
t.Fatalf("bad members: %v", obj)
|
|
|
|
}
|
2017-05-09 17:46:11 +00:00
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_Join(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a1 := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a1.Shutdown()
|
|
|
|
a2 := NewTestAgent(t.Name(), nil)
|
2014-01-06 21:21:48 +00:00
|
|
|
defer a2.Shutdown()
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", a2.Config.Ports.SerfLan)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/join/%s", addr), nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a1.srv.AgentJoin(nil, req)
|
2014-01-06 21:21:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("Err: %v", obj)
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
if len(a1.LANMembers()) != 2 {
|
2014-01-06 21:21:48 +00:00
|
|
|
t.Fatalf("should have 2 members")
|
|
|
|
}
|
2016-10-25 20:46:54 +00:00
|
|
|
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-04-29 16:34:02 +00:00
|
|
|
if got, want := len(a2.LANMembers()), 2; got != want {
|
|
|
|
r.Fatalf("got %d LAN members want %d", got, want)
|
|
|
|
}
|
|
|
|
})
|
2014-01-06 21:21:48 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_Join_WAN(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a1 := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a1.Shutdown()
|
|
|
|
a2 := NewTestAgent(t.Name(), nil)
|
2014-01-06 21:21:48 +00:00
|
|
|
defer a2.Shutdown()
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", a2.Config.Ports.SerfWan)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/join/%s?wan=true", addr), nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a1.srv.AgentJoin(nil, req)
|
2014-01-06 21:21:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("Err: %v", obj)
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
if len(a1.WANMembers()) != 2 {
|
2016-10-25 20:46:54 +00:00
|
|
|
t.Fatalf("should have 2 members")
|
|
|
|
}
|
|
|
|
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-04-29 16:34:02 +00:00
|
|
|
if got, want := len(a2.WANMembers()), 2; got != want {
|
|
|
|
r.Fatalf("got %d WAN members want %d", got, want)
|
|
|
|
}
|
|
|
|
})
|
2014-01-06 21:21:48 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_Join_ACLDeny(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a1 := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a1.Shutdown()
|
|
|
|
a2 := NewTestAgent(t.Name(), nil)
|
2016-12-14 17:33:57 +00:00
|
|
|
defer a2.Shutdown()
|
2017-05-21 07:11:09 +00:00
|
|
|
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", a2.Config.Ports.SerfLan)
|
2016-12-14 17:33:57 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("no token", func(t *testing.T) {
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/join/%s", addr), nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a1.srv.AgentJoin(nil, req); !isPermissionDenied(err) {
|
2016-12-14 17:33:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-05-09 17:46:11 +00:00
|
|
|
})
|
2016-12-14 17:33:57 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("agent master token", func(t *testing.T) {
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/join/%s?token=towel", addr), nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
_, err := a1.srv.AgentJoin(nil, req)
|
2016-12-14 17:33:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-05-09 17:46:11 +00:00
|
|
|
})
|
2016-12-14 17:33:57 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("read-only token", func(t *testing.T) {
|
2017-05-21 07:11:09 +00:00
|
|
|
ro := makeReadOnlyAgentACL(t, a1.srv)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/join/%s?token=%s", addr, ro), nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a1.srv.AgentJoin(nil, req); !isPermissionDenied(err) {
|
2016-12-14 17:33:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-05-09 17:46:11 +00:00
|
|
|
})
|
2016-12-14 17:33:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 04:43:55 +00:00
|
|
|
type mockNotifier struct{ s string }
|
|
|
|
|
|
|
|
func (n *mockNotifier) Notify(state string) error {
|
|
|
|
n.s = state
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_JoinLANNotify(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
a1 := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a1.Shutdown()
|
|
|
|
|
|
|
|
cfg2 := TestConfig()
|
|
|
|
cfg2.Server = false
|
|
|
|
cfg2.Bootstrap = false
|
|
|
|
a2 := NewTestAgent(t.Name(), cfg2)
|
|
|
|
defer a2.Shutdown()
|
|
|
|
|
|
|
|
notif := &mockNotifier{}
|
|
|
|
a1.joinLANNotifier = notif
|
|
|
|
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", a2.Config.Ports.SerfLan)
|
|
|
|
_, err := a1.JoinLAN([]string{addr})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := notif.s, "READY=1"; got != want {
|
|
|
|
t.Fatalf("got joinLAN notification %q want %q", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_Leave(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a1 := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a1.Shutdown()
|
2016-11-30 18:29:42 +00:00
|
|
|
|
2017-05-22 11:03:59 +00:00
|
|
|
cfg2 := TestConfig()
|
|
|
|
cfg2.Server = false
|
|
|
|
cfg2.Bootstrap = false
|
|
|
|
a2 := NewTestAgent(t.Name(), cfg2)
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a2.Shutdown()
|
2016-11-30 18:29:42 +00:00
|
|
|
|
|
|
|
// Join first
|
2017-05-21 07:11:09 +00:00
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", a2.Config.Ports.SerfLan)
|
|
|
|
_, err := a1.JoinLAN([]string{addr})
|
2016-11-30 18:29:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Graceful leave now
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/leave", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a2.srv.AgentLeave(nil, req)
|
2016-11-30 18:29:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("Err: %v", obj)
|
|
|
|
}
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-05-21 07:11:09 +00:00
|
|
|
m := a1.LANMembers()
|
2017-04-29 16:34:02 +00:00
|
|
|
if got, want := m[1].Status, serf.StatusLeft; got != want {
|
|
|
|
r.Fatalf("got status %q want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
2016-11-30 18:29:42 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_Leave_ACLDeny(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a.Shutdown()
|
2016-12-14 17:33:57 +00:00
|
|
|
|
2017-05-10 07:08:39 +00:00
|
|
|
t.Run("no token", func(t *testing.T) {
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/leave", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentLeave(nil, req); !isPermissionDenied(err) {
|
2016-12-14 17:33:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-05-09 17:46:11 +00:00
|
|
|
})
|
2016-12-14 17:33:57 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("read-only token", func(t *testing.T) {
|
2017-05-21 07:11:09 +00:00
|
|
|
ro := makeReadOnlyAgentACL(t, a.srv)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/agent/leave?token=%s", ro), nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentLeave(nil, req); !isPermissionDenied(err) {
|
2016-12-14 17:33:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-05-09 17:46:11 +00:00
|
|
|
})
|
2017-05-10 07:08:39 +00:00
|
|
|
|
|
|
|
// this sub-test will change the state so that there is no leader.
|
|
|
|
// it must therefore be the last one in this list.
|
|
|
|
t.Run("agent master token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/leave?token=towel", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentLeave(nil, req); err != nil {
|
2017-05-10 07:08:39 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 17:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_ForceLeave(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a1 := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a1.Shutdown()
|
|
|
|
a2 := NewTestAgent(t.Name(), nil)
|
2014-01-06 21:21:48 +00:00
|
|
|
|
|
|
|
// Join first
|
2017-05-21 07:11:09 +00:00
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", a2.Config.Ports.SerfLan)
|
|
|
|
_, err := a1.JoinLAN([]string{addr})
|
2014-01-06 21:21:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
// todo(fs): this test probably needs work
|
2014-01-06 21:21:48 +00:00
|
|
|
a2.Shutdown()
|
|
|
|
|
|
|
|
// Force leave now
|
2017-05-21 07:11:09 +00:00
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/force-leave/%s", a2.Config.NodeName), nil)
|
|
|
|
obj, err := a1.srv.AgentForceLeave(nil, req)
|
2014-01-06 21:21:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("Err: %v", obj)
|
|
|
|
}
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-05-21 07:11:09 +00:00
|
|
|
m := a1.LANMembers()
|
2017-04-29 16:34:02 +00:00
|
|
|
if got, want := m[1].Status, serf.StatusLeft; got != want {
|
|
|
|
r.Fatalf("got status %q want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2014-01-06 21:21:48 +00:00
|
|
|
}
|
2014-01-30 23:51:15 +00:00
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_ForceLeave_ACLDeny(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a.Shutdown()
|
2016-12-14 17:33:57 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("no token", func(t *testing.T) {
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/force-leave/nope", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentForceLeave(nil, req); !isPermissionDenied(err) {
|
2016-12-14 17:33:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-05-09 17:46:11 +00:00
|
|
|
})
|
2016-12-14 17:33:57 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("agent master token", func(t *testing.T) {
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/force-leave/nope?token=towel", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentForceLeave(nil, req); err != nil {
|
2016-12-14 17:33:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-05-09 17:46:11 +00:00
|
|
|
})
|
2016-12-14 17:33:57 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("read-only token", func(t *testing.T) {
|
2017-05-21 07:11:09 +00:00
|
|
|
ro := makeReadOnlyAgentACL(t, a.srv)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/force-leave/nope?token=%s", ro), nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentForceLeave(nil, req); !isPermissionDenied(err) {
|
2016-12-14 17:33:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-05-09 17:46:11 +00:00
|
|
|
})
|
2016-12-14 17:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_RegisterCheck(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2014-01-30 23:51:15 +00:00
|
|
|
|
|
|
|
// Register node
|
2017-06-15 16:46:06 +00:00
|
|
|
args := &structs.CheckDefinition{
|
2014-01-30 23:51:15 +00:00
|
|
|
Name: "test",
|
2017-05-15 19:49:13 +00:00
|
|
|
TTL: 15 * time.Second,
|
2014-01-30 23:51:15 +00:00
|
|
|
}
|
2017-05-09 16:58:12 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/register?token=abc123", jsonReader(args))
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentRegisterCheck(nil, req)
|
2014-01-30 23:51:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
2016-06-07 20:24:51 +00:00
|
|
|
checkID := types.CheckID("test")
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, ok := a.state.Checks()[checkID]; !ok {
|
2014-01-30 23:51:15 +00:00
|
|
|
t.Fatalf("missing test check")
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, ok := a.checkTTLs[checkID]; !ok {
|
2014-01-30 23:51:15 +00:00
|
|
|
t.Fatalf("missing test check ttl")
|
|
|
|
}
|
2015-04-28 20:06:02 +00:00
|
|
|
|
|
|
|
// Ensure the token was configured
|
2017-05-21 07:11:09 +00:00
|
|
|
if token := a.state.CheckToken(checkID); token == "" {
|
2015-04-28 20:06:02 +00:00
|
|
|
t.Fatalf("missing token")
|
|
|
|
}
|
2015-05-11 23:48:10 +00:00
|
|
|
|
2015-04-12 00:53:48 +00:00
|
|
|
// By default, checks start in critical state.
|
2017-05-21 07:11:09 +00:00
|
|
|
state := a.state.Checks()[checkID]
|
2017-04-19 23:00:11 +00:00
|
|
|
if state.Status != api.HealthCritical {
|
2015-04-12 00:53:48 +00:00
|
|
|
t.Fatalf("bad: %v", state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_RegisterCheck_Passing(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2015-04-12 00:53:48 +00:00
|
|
|
|
|
|
|
// Register node
|
2017-06-15 16:46:06 +00:00
|
|
|
args := &structs.CheckDefinition{
|
2017-05-15 19:49:13 +00:00
|
|
|
Name: "test",
|
|
|
|
TTL: 15 * time.Second,
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2015-04-12 00:53:48 +00:00
|
|
|
}
|
2017-05-09 16:58:12 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/register", jsonReader(args))
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentRegisterCheck(nil, req)
|
2015-04-12 00:53:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
2016-06-07 20:24:51 +00:00
|
|
|
checkID := types.CheckID("test")
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, ok := a.state.Checks()[checkID]; !ok {
|
2015-04-12 00:53:48 +00:00
|
|
|
t.Fatalf("missing test check")
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, ok := a.checkTTLs[checkID]; !ok {
|
2015-04-12 00:53:48 +00:00
|
|
|
t.Fatalf("missing test check ttl")
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
state := a.state.Checks()[checkID]
|
2017-04-19 23:00:11 +00:00
|
|
|
if state.Status != api.HealthPassing {
|
2015-04-12 00:53:48 +00:00
|
|
|
t.Fatalf("bad: %v", state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_RegisterCheck_BadStatus(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2015-04-12 00:53:48 +00:00
|
|
|
|
|
|
|
// Register node
|
2017-06-15 16:46:06 +00:00
|
|
|
args := &structs.CheckDefinition{
|
2017-05-15 19:49:13 +00:00
|
|
|
Name: "test",
|
|
|
|
TTL: 15 * time.Second,
|
2015-04-12 00:53:48 +00:00
|
|
|
Status: "fluffy",
|
|
|
|
}
|
2017-05-09 16:58:12 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/register", jsonReader(args))
|
2015-04-12 00:53:48 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentRegisterCheck(resp, req); err != nil {
|
2015-04-12 00:53:48 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 400 {
|
|
|
|
t.Fatalf("accepted bad status")
|
|
|
|
}
|
2014-01-30 23:51:15 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_RegisterCheck_ACLDeny(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a.Shutdown()
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-06-15 16:46:06 +00:00
|
|
|
args := &structs.CheckDefinition{
|
2016-12-14 22:16:46 +00:00
|
|
|
Name: "test",
|
2017-05-15 19:49:13 +00:00
|
|
|
TTL: 15 * time.Second,
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
2017-05-09 16:58:12 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("no token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/register", jsonReader(args))
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentRegisterCheck(nil, req); !isPermissionDenied(err) {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("root token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/register?token=root", jsonReader(args))
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentRegisterCheck(nil, req); err != nil {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_DeregisterCheck(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2014-01-30 23:51:15 +00:00
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.AddCheck(chk, nil, false, ""); err != nil {
|
2014-01-30 23:51:15 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register node
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/deregister/test", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentDeregisterCheck(nil, req)
|
2014-01-30 23:51:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, ok := a.state.Checks()["test"]; ok {
|
2014-01-30 23:51:15 +00:00
|
|
|
t.Fatalf("have test check")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_DeregisterCheckACLDeny(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a.Shutdown()
|
2016-12-14 22:16:46 +00:00
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.AddCheck(chk, nil, false, ""); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("no token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/deregister/test", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentDeregisterCheck(nil, req); !isPermissionDenied(err) {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("root token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/deregister/test?token=root", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentDeregisterCheck(nil, req); err != nil {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_PassCheck(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2014-01-30 23:51:15 +00:00
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
2017-06-15 16:46:06 +00:00
|
|
|
chkType := &structs.CheckType{TTL: 15 * time.Second}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.AddCheck(chk, chkType, false, ""); err != nil {
|
2014-01-30 23:51:15 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/pass/test", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentCheckPass(nil, req)
|
2014-01-30 23:51:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
2017-05-21 07:11:09 +00:00
|
|
|
state := a.state.Checks()["test"]
|
2017-04-19 23:00:11 +00:00
|
|
|
if state.Status != api.HealthPassing {
|
2014-01-30 23:51:15 +00:00
|
|
|
t.Fatalf("bad: %v", state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_PassCheck_ACLDeny(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a.Shutdown()
|
2016-12-14 22:16:46 +00:00
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
2017-06-15 16:46:06 +00:00
|
|
|
chkType := &structs.CheckType{TTL: 15 * time.Second}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.AddCheck(chk, chkType, false, ""); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("no token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/pass/test", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentCheckPass(nil, req); !isPermissionDenied(err) {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("root token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/pass/test?token=root", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentCheckPass(nil, req); err != nil {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_WarnCheck(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2014-01-30 23:51:15 +00:00
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
2017-06-15 16:46:06 +00:00
|
|
|
chkType := &structs.CheckType{TTL: 15 * time.Second}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.AddCheck(chk, chkType, false, ""); err != nil {
|
2014-01-30 23:51:15 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/warn/test", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentCheckWarn(nil, req)
|
2014-01-30 23:51:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
2017-05-21 07:11:09 +00:00
|
|
|
state := a.state.Checks()["test"]
|
2017-04-19 23:00:11 +00:00
|
|
|
if state.Status != api.HealthWarning {
|
2014-01-30 23:51:15 +00:00
|
|
|
t.Fatalf("bad: %v", state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_WarnCheck_ACLDeny(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a.Shutdown()
|
2016-12-14 22:16:46 +00:00
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
2017-06-15 16:46:06 +00:00
|
|
|
chkType := &structs.CheckType{TTL: 15 * time.Second}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.AddCheck(chk, chkType, false, ""); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("no token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/warn/test", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentCheckWarn(nil, req); !isPermissionDenied(err) {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("root token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/warn/test?token=root", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentCheckWarn(nil, req); err != nil {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_FailCheck(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2014-01-30 23:51:15 +00:00
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
2017-06-15 16:46:06 +00:00
|
|
|
chkType := &structs.CheckType{TTL: 15 * time.Second}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.AddCheck(chk, chkType, false, ""); err != nil {
|
2014-01-30 23:51:15 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/fail/test", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentCheckFail(nil, req)
|
2014-01-30 23:51:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
2017-05-21 07:11:09 +00:00
|
|
|
state := a.state.Checks()["test"]
|
2017-04-19 23:00:11 +00:00
|
|
|
if state.Status != api.HealthCritical {
|
2014-01-30 23:51:15 +00:00
|
|
|
t.Fatalf("bad: %v", state)
|
|
|
|
}
|
|
|
|
}
|
2014-01-30 23:56:03 +00:00
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_FailCheck_ACLDeny(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a.Shutdown()
|
2016-12-14 22:16:46 +00:00
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
2017-06-15 16:46:06 +00:00
|
|
|
chkType := &structs.CheckType{TTL: 15 * time.Second}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.AddCheck(chk, chkType, false, ""); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("no token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/fail/test", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentCheckFail(nil, req); !isPermissionDenied(err) {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("root token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/fail/test?token=root", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentCheckFail(nil, req); err != nil {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_UpdateCheck(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2016-03-03 01:08:06 +00:00
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
2017-06-15 16:46:06 +00:00
|
|
|
chkType := &structs.CheckType{TTL: 15 * time.Second}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.AddCheck(chk, chkType, false, ""); err != nil {
|
2016-03-03 01:08:06 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []checkUpdate{
|
2017-04-19 23:00:11 +00:00
|
|
|
checkUpdate{api.HealthPassing, "hello-passing"},
|
|
|
|
checkUpdate{api.HealthCritical, "hello-critical"},
|
|
|
|
checkUpdate{api.HealthWarning, "hello-warning"},
|
2016-03-03 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run(c.Status, func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/check/update/test", jsonReader(c))
|
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentCheckUpdate(resp, req)
|
2017-05-09 17:46:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf("expected 200, got %d", resp.Code)
|
|
|
|
}
|
2016-03-03 01:08:06 +00:00
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
state := a.state.Checks()["test"]
|
2017-05-09 17:46:11 +00:00
|
|
|
if state.Status != c.Status || state.Output != c.Output {
|
|
|
|
t.Fatalf("bad: %v", state)
|
|
|
|
}
|
|
|
|
})
|
2016-03-03 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("log output limit", func(t *testing.T) {
|
2017-05-09 16:58:12 +00:00
|
|
|
args := checkUpdate{
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2016-03-03 01:08:06 +00:00
|
|
|
Output: strings.Repeat("-= bad -=", 5*CheckBufSize),
|
|
|
|
}
|
2017-05-09 16:58:12 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/check/update/test", jsonReader(args))
|
2016-03-03 01:08:06 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentCheckUpdate(resp, req)
|
2016-03-03 01:08:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf("expected 200, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since we append some notes about truncating, we just do a
|
|
|
|
// rough check that the output buffer was cut down so this test
|
|
|
|
// isn't super brittle.
|
2017-05-21 07:11:09 +00:00
|
|
|
state := a.state.Checks()["test"]
|
2017-04-19 23:00:11 +00:00
|
|
|
if state.Status != api.HealthPassing || len(state.Output) > 2*CheckBufSize {
|
2016-03-03 01:08:06 +00:00
|
|
|
t.Fatalf("bad: %v", state)
|
|
|
|
}
|
2017-05-09 17:46:11 +00:00
|
|
|
})
|
2016-03-03 01:08:06 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("bogus status", func(t *testing.T) {
|
2017-05-09 16:58:12 +00:00
|
|
|
args := checkUpdate{Status: "itscomplicated"}
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/check/update/test", jsonReader(args))
|
2016-03-03 01:08:06 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentCheckUpdate(resp, req)
|
2016-03-03 01:08:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
if resp.Code != 400 {
|
|
|
|
t.Fatalf("expected 400, got %d", resp.Code)
|
|
|
|
}
|
2017-05-09 17:46:11 +00:00
|
|
|
})
|
2016-03-03 01:08:06 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("bogus verb", func(t *testing.T) {
|
2017-05-09 16:58:12 +00:00
|
|
|
args := checkUpdate{Status: api.HealthPassing}
|
|
|
|
req, _ := http.NewRequest("POST", "/v1/agent/check/update/test", jsonReader(args))
|
2016-03-03 01:08:06 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentCheckUpdate(resp, req)
|
2016-03-03 01:08:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
if resp.Code != 405 {
|
|
|
|
t.Fatalf("expected 405, got %d", resp.Code)
|
|
|
|
}
|
2017-05-09 17:46:11 +00:00
|
|
|
})
|
2016-03-03 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_UpdateCheck_ACLDeny(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a.Shutdown()
|
2016-12-14 22:16:46 +00:00
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
2017-06-15 16:46:06 +00:00
|
|
|
chkType := &structs.CheckType{TTL: 15 * time.Second}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.AddCheck(chk, chkType, false, ""); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("no token", func(t *testing.T) {
|
|
|
|
args := checkUpdate{api.HealthPassing, "hello-passing"}
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/check/update/test", jsonReader(args))
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentCheckUpdate(nil, req); !isPermissionDenied(err) {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("root token", func(t *testing.T) {
|
|
|
|
args := checkUpdate{api.HealthPassing, "hello-passing"}
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/check/update/test?token=root", jsonReader(args))
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentCheckUpdate(nil, req); err != nil {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_RegisterService(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2014-01-30 23:56:03 +00:00
|
|
|
|
2017-06-15 16:46:06 +00:00
|
|
|
args := &structs.ServiceDefinition{
|
2014-01-30 23:56:03 +00:00
|
|
|
Name: "test",
|
2014-04-03 19:12:23 +00:00
|
|
|
Tags: []string{"master"},
|
2014-01-30 23:56:03 +00:00
|
|
|
Port: 8000,
|
2017-06-15 16:46:06 +00:00
|
|
|
Check: structs.CheckType{
|
2014-01-30 23:56:03 +00:00
|
|
|
TTL: 15 * time.Second,
|
|
|
|
},
|
2017-06-15 16:46:06 +00:00
|
|
|
Checks: []*structs.CheckType{
|
|
|
|
&structs.CheckType{
|
2015-01-14 03:08:30 +00:00
|
|
|
TTL: 20 * time.Second,
|
|
|
|
},
|
2017-06-15 16:46:06 +00:00
|
|
|
&structs.CheckType{
|
2015-01-14 03:08:30 +00:00
|
|
|
TTL: 30 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
2014-01-30 23:56:03 +00:00
|
|
|
}
|
2017-05-09 16:58:12 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/service/register?token=abc123", jsonReader(args))
|
2014-01-30 23:56:03 +00:00
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentRegisterService(nil, req)
|
2014-01-30 23:56:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the servie
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, ok := a.state.Services()["test"]; !ok {
|
2014-01-30 23:56:03 +00:00
|
|
|
t.Fatalf("missing test service")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
2017-05-21 07:11:09 +00:00
|
|
|
checks := a.state.Checks()
|
2015-01-14 03:08:30 +00:00
|
|
|
if len(checks) != 3 {
|
|
|
|
t.Fatalf("bad: %v", checks)
|
2014-01-30 23:56:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
if len(a.checkTTLs) != 3 {
|
|
|
|
t.Fatalf("missing test check ttls: %v", a.checkTTLs)
|
2014-01-30 23:56:03 +00:00
|
|
|
}
|
2015-04-28 20:06:02 +00:00
|
|
|
|
|
|
|
// Ensure the token was configured
|
2017-05-21 07:11:09 +00:00
|
|
|
if token := a.state.ServiceToken("test"); token == "" {
|
2015-04-28 20:06:02 +00:00
|
|
|
t.Fatalf("missing token")
|
|
|
|
}
|
2014-01-30 23:56:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_RegisterService_ACLDeny(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a.Shutdown()
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-06-15 16:46:06 +00:00
|
|
|
args := &structs.ServiceDefinition{
|
2016-12-14 22:16:46 +00:00
|
|
|
Name: "test",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 8000,
|
2017-06-15 16:46:06 +00:00
|
|
|
Check: structs.CheckType{
|
2016-12-14 22:16:46 +00:00
|
|
|
TTL: 15 * time.Second,
|
|
|
|
},
|
2017-06-15 16:46:06 +00:00
|
|
|
Checks: []*structs.CheckType{
|
|
|
|
&structs.CheckType{
|
2016-12-14 22:16:46 +00:00
|
|
|
TTL: 20 * time.Second,
|
|
|
|
},
|
2017-06-15 16:46:06 +00:00
|
|
|
&structs.CheckType{
|
2016-12-14 22:16:46 +00:00
|
|
|
TTL: 30 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("no token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/service/register", jsonReader(args))
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentRegisterService(nil, req); !isPermissionDenied(err) {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("root token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/service/register?token=root", jsonReader(args))
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentRegisterService(nil, req); err != nil {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 16:34:45 +00:00
|
|
|
func TestAgent_RegisterService_InvalidAddress(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2017-05-08 16:34:45 +00:00
|
|
|
|
2017-05-09 07:29:13 +00:00
|
|
|
for _, addr := range []string{"0.0.0.0", "::", "[::]"} {
|
|
|
|
t.Run("addr "+addr, func(t *testing.T) {
|
2017-06-15 16:46:06 +00:00
|
|
|
args := &structs.ServiceDefinition{
|
2017-05-09 07:29:13 +00:00
|
|
|
Name: "test",
|
|
|
|
Address: addr,
|
|
|
|
Port: 8000,
|
|
|
|
}
|
2017-05-09 16:58:12 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/service/register?token=abc123", jsonReader(args))
|
2017-05-09 07:29:13 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
_, err := a.srv.AgentRegisterService(resp, req)
|
2017-05-09 16:58:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("got error %v want nil", err)
|
|
|
|
}
|
2017-05-09 07:29:13 +00:00
|
|
|
if got, want := resp.Code, 400; got != want {
|
|
|
|
t.Fatalf("got code %d want %d", got, want)
|
|
|
|
}
|
|
|
|
if got, want := resp.Body.String(), "Invalid service address"; got != want {
|
|
|
|
t.Fatalf("got body %q want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
2017-05-08 16:34:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_DeregisterService(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2014-01-30 23:56:03 +00:00
|
|
|
|
|
|
|
service := &structs.NodeService{
|
|
|
|
ID: "test",
|
|
|
|
Service: "test",
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.AddService(service, nil, false, ""); err != nil {
|
2014-01-30 23:56:03 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/service/deregister/test", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.AgentDeregisterService(nil, req)
|
2014-01-30 23:56:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, ok := a.state.Services()["test"]; ok {
|
2014-01-30 23:56:03 +00:00
|
|
|
t.Fatalf("have test service")
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, ok := a.state.Checks()["test"]; ok {
|
2014-01-30 23:56:03 +00:00
|
|
|
t.Fatalf("have test check")
|
|
|
|
}
|
|
|
|
}
|
2015-01-15 09:17:35 +00:00
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_DeregisterService_ACLDeny(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a.Shutdown()
|
2016-12-14 22:16:46 +00:00
|
|
|
|
|
|
|
service := &structs.NodeService{
|
|
|
|
ID: "test",
|
|
|
|
Service: "test",
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.AddService(service, nil, false, ""); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("no token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/service/deregister/test", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentDeregisterService(nil, req); !isPermissionDenied(err) {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("root token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/service/deregister/test?token=root", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentDeregisterService(nil, req); err != nil {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_ServiceMaintenance_BadRequest(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2015-01-15 09:17:35 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("not PUT", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/service/maintenance/test?enable=true", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentServiceMaintenance(resp, req); err != nil {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 405 {
|
|
|
|
t.Fatalf("expected 405, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
})
|
2015-01-15 09:17:35 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("not enabled", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/service/maintenance/test", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentServiceMaintenance(resp, req); err != nil {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 400 {
|
|
|
|
t.Fatalf("expected 400, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
})
|
2015-01-15 09:17:35 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("no service id", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/service/maintenance/?enable=true", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentServiceMaintenance(resp, req); err != nil {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 400 {
|
|
|
|
t.Fatalf("expected 400, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
})
|
2015-01-15 18:51:00 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("bad service id", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/service/maintenance/_nope_?enable=true", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentServiceMaintenance(resp, req); err != nil {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 404 {
|
|
|
|
t.Fatalf("expected 404, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
})
|
2015-01-15 09:17:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_ServiceMaintenance_Enable(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2015-01-15 09:17:35 +00:00
|
|
|
|
|
|
|
// Register the service
|
|
|
|
service := &structs.NodeService{
|
|
|
|
ID: "test",
|
|
|
|
Service: "test",
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.AddService(service, nil, false, ""); err != nil {
|
2015-01-15 09:17:35 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-01-15 18:51:00 +00:00
|
|
|
// Force the service into maintenance mode
|
2015-09-10 19:08:08 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/service/maintenance/test?enable=true&reason=broken&token=mytoken", nil)
|
2015-01-15 09:17:35 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentServiceMaintenance(resp, req); err != nil {
|
2015-01-15 09:17:35 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf("expected 200, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the maintenance check was registered
|
2015-01-15 20:20:57 +00:00
|
|
|
checkID := serviceMaintCheckID("test")
|
2017-05-21 07:11:09 +00:00
|
|
|
check, ok := a.state.Checks()[checkID]
|
2015-01-21 20:21:57 +00:00
|
|
|
if !ok {
|
2015-01-15 09:17:35 +00:00
|
|
|
t.Fatalf("should have registered maintenance check")
|
|
|
|
}
|
2015-01-21 20:21:57 +00:00
|
|
|
|
2015-09-10 19:08:08 +00:00
|
|
|
// Ensure the token was added
|
2017-05-21 07:11:09 +00:00
|
|
|
if token := a.state.CheckToken(checkID); token != "mytoken" {
|
2015-09-10 19:08:08 +00:00
|
|
|
t.Fatalf("expected 'mytoken', got '%s'", token)
|
|
|
|
}
|
|
|
|
|
2015-01-21 20:21:57 +00:00
|
|
|
// Ensure the reason was set in notes
|
|
|
|
if check.Notes != "broken" {
|
|
|
|
t.Fatalf("bad: %#v", check)
|
|
|
|
}
|
2015-01-15 09:17:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_ServiceMaintenance_Disable(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2015-01-15 09:17:35 +00:00
|
|
|
|
|
|
|
// Register the service
|
|
|
|
service := &structs.NodeService{
|
|
|
|
ID: "test",
|
|
|
|
Service: "test",
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.AddService(service, nil, false, ""); err != nil {
|
2015-01-15 09:17:35 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force the service into maintenance mode
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.EnableServiceMaintenance("test", "", ""); err != nil {
|
2015-01-15 09:17:35 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Leave maintenance mode
|
2015-01-15 18:51:00 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/service/maintenance/test?enable=false", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentServiceMaintenance(resp, req); err != nil {
|
2015-01-15 09:17:35 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf("expected 200, got %d", resp.Code)
|
|
|
|
}
|
2015-01-15 18:51:00 +00:00
|
|
|
|
|
|
|
// Ensure the maintenance check was removed
|
2015-01-15 20:20:57 +00:00
|
|
|
checkID := serviceMaintCheckID("test")
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, ok := a.state.Checks()[checkID]; ok {
|
2015-01-15 18:51:00 +00:00
|
|
|
t.Fatalf("should have removed maintenance check")
|
|
|
|
}
|
2015-01-15 09:17:35 +00:00
|
|
|
}
|
2015-01-15 19:26:14 +00:00
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_ServiceMaintenance_ACLDeny(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a.Shutdown()
|
2016-12-14 22:16:46 +00:00
|
|
|
|
|
|
|
// Register the service.
|
|
|
|
service := &structs.NodeService{
|
|
|
|
ID: "test",
|
|
|
|
Service: "test",
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.AddService(service, nil, false, ""); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("no token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/service/maintenance/test?enable=true&reason=broken", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentServiceMaintenance(nil, req); !isPermissionDenied(err) {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("root token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/service/maintenance/test?enable=true&reason=broken&token=root", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentServiceMaintenance(nil, req); err != nil {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_NodeMaintenance_BadRequest(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2015-01-15 19:26:14 +00:00
|
|
|
|
|
|
|
// Fails on non-PUT
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/self/maintenance?enable=true", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentNodeMaintenance(resp, req); err != nil {
|
2015-01-15 19:26:14 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 405 {
|
|
|
|
t.Fatalf("expected 405, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fails when no enable flag provided
|
|
|
|
req, _ = http.NewRequest("PUT", "/v1/agent/self/maintenance", nil)
|
|
|
|
resp = httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentNodeMaintenance(resp, req); err != nil {
|
2015-01-15 19:26:14 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 400 {
|
|
|
|
t.Fatalf("expected 400, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_NodeMaintenance_Enable(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2015-01-15 19:26:14 +00:00
|
|
|
|
|
|
|
// Force the node into maintenance mode
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/self/maintenance?enable=true&reason=broken&token=mytoken", nil)
|
2015-01-15 19:26:14 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentNodeMaintenance(resp, req); err != nil {
|
2015-01-15 19:26:14 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf("expected 200, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the maintenance check was registered
|
2017-05-21 07:11:09 +00:00
|
|
|
check, ok := a.state.Checks()[structs.NodeMaint]
|
2015-01-21 20:21:57 +00:00
|
|
|
if !ok {
|
2015-01-15 19:26:14 +00:00
|
|
|
t.Fatalf("should have registered maintenance check")
|
|
|
|
}
|
2015-01-21 20:21:57 +00:00
|
|
|
|
2015-09-10 19:08:08 +00:00
|
|
|
// Check that the token was used
|
2017-05-21 07:11:09 +00:00
|
|
|
if token := a.state.CheckToken(structs.NodeMaint); token != "mytoken" {
|
2015-09-10 19:08:08 +00:00
|
|
|
t.Fatalf("expected 'mytoken', got '%s'", token)
|
|
|
|
}
|
|
|
|
|
2015-01-21 20:21:57 +00:00
|
|
|
// Ensure the reason was set in notes
|
|
|
|
if check.Notes != "broken" {
|
|
|
|
t.Fatalf("bad: %#v", check)
|
|
|
|
}
|
2015-01-15 19:26:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_NodeMaintenance_Disable(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2015-01-15 19:26:14 +00:00
|
|
|
|
|
|
|
// Force the node into maintenance mode
|
2017-05-21 07:11:09 +00:00
|
|
|
a.EnableNodeMaintenance("", "")
|
2015-01-15 19:26:14 +00:00
|
|
|
|
|
|
|
// Leave maintenance mode
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/self/maintenance?enable=false", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentNodeMaintenance(resp, req); err != nil {
|
2015-01-15 19:26:14 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf("expected 200, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the maintenance check was removed
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, ok := a.state.Checks()[structs.NodeMaint]; ok {
|
2015-01-15 19:26:14 +00:00
|
|
|
t.Fatalf("should have removed maintenance check")
|
|
|
|
}
|
|
|
|
}
|
2015-01-14 01:52:17 +00:00
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_NodeMaintenance_ACLDeny(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a.Shutdown()
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("no token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/self/maintenance?enable=true&reason=broken", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentNodeMaintenance(nil, req); !isPermissionDenied(err) {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Run("root token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/self/maintenance?enable=true&reason=broken&token=root", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentNodeMaintenance(nil, req); err != nil {
|
2017-05-09 17:46:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_RegisterCheck_Service(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2015-01-14 01:52:17 +00:00
|
|
|
|
2017-06-15 16:46:06 +00:00
|
|
|
args := &structs.ServiceDefinition{
|
2015-01-14 01:52:17 +00:00
|
|
|
Name: "memcache",
|
|
|
|
Port: 8000,
|
2017-06-15 16:46:06 +00:00
|
|
|
Check: structs.CheckType{
|
2015-01-14 01:52:17 +00:00
|
|
|
TTL: 15 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-09 16:58:12 +00:00
|
|
|
// First register the service
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/service/register", jsonReader(args))
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentRegisterService(nil, req); err != nil {
|
2015-01-14 01:52:17 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now register an additional check
|
2017-06-15 16:46:06 +00:00
|
|
|
checkArgs := &structs.CheckDefinition{
|
2015-01-14 01:52:17 +00:00
|
|
|
Name: "memcache_check2",
|
|
|
|
ServiceID: "memcache",
|
2017-05-15 19:49:13 +00:00
|
|
|
TTL: 15 * time.Second,
|
2015-01-14 01:52:17 +00:00
|
|
|
}
|
2017-05-09 16:58:12 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/agent/check/register", jsonReader(checkArgs))
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentRegisterCheck(nil, req); err != nil {
|
2015-01-14 01:52:17 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
2017-05-21 07:11:09 +00:00
|
|
|
result := a.state.Checks()
|
2015-01-14 01:52:17 +00:00
|
|
|
if _, ok := result["service:memcache"]; !ok {
|
|
|
|
t.Fatalf("missing memcached check")
|
|
|
|
}
|
|
|
|
if _, ok := result["memcache_check2"]; !ok {
|
|
|
|
t.Fatalf("missing memcache_check2 check")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the new check is associated with the service
|
|
|
|
if result["memcache_check2"].ServiceID != "memcache" {
|
|
|
|
t.Fatalf("bad: %#v", result["memcached_check2"])
|
|
|
|
}
|
|
|
|
}
|
2016-11-16 21:45:26 +00:00
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_Monitor(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2016-11-16 21:45:26 +00:00
|
|
|
logWriter := logger.NewLogWriter(512)
|
2017-05-21 07:11:09 +00:00
|
|
|
a := &TestAgent{
|
|
|
|
Name: t.Name(),
|
|
|
|
LogWriter: logWriter,
|
2017-05-22 23:43:34 +00:00
|
|
|
LogOutput: io.MultiWriter(os.Stderr, logWriter),
|
2017-05-21 07:11:09 +00:00
|
|
|
}
|
|
|
|
a.Start()
|
|
|
|
defer a.Shutdown()
|
2016-11-16 21:45:26 +00:00
|
|
|
|
2016-11-28 21:08:31 +00:00
|
|
|
// Try passing an invalid log level
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/monitor?loglevel=invalid", nil)
|
2016-11-16 21:45:26 +00:00
|
|
|
resp := newClosableRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentMonitor(resp, req); err != nil {
|
2016-11-28 21:08:31 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 400 {
|
|
|
|
t.Fatalf("bad: %v", resp.Code)
|
|
|
|
}
|
|
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
if !strings.Contains(string(body), "Unknown log level") {
|
|
|
|
t.Fatalf("bad: %s", body)
|
|
|
|
}
|
|
|
|
|
2016-12-15 02:13:30 +00:00
|
|
|
// Try to stream logs until we see the expected log line
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2016-12-15 02:13:30 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/agent/monitor?loglevel=debug", nil)
|
|
|
|
resp = newClosableRecorder()
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentMonitor(resp, req); err != nil {
|
2016-12-15 02:13:30 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
2016-11-16 21:45:26 +00:00
|
|
|
}
|
2016-12-15 02:13:30 +00:00
|
|
|
close(done)
|
|
|
|
}()
|
2016-11-16 21:45:26 +00:00
|
|
|
|
2016-12-15 02:13:30 +00:00
|
|
|
resp.Close()
|
|
|
|
<-done
|
|
|
|
|
2017-04-29 16:34:02 +00:00
|
|
|
got := resp.Body.Bytes()
|
|
|
|
want := []byte("raft: Initial configuration (index=1)")
|
|
|
|
if !bytes.Contains(got, want) {
|
|
|
|
r.Fatalf("got %q and did not find %q", got, want)
|
2016-11-16 21:45:26 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2016-11-16 21:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type closableRecorder struct {
|
|
|
|
*httptest.ResponseRecorder
|
|
|
|
closer chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newClosableRecorder() *closableRecorder {
|
|
|
|
r := httptest.NewRecorder()
|
|
|
|
closer := make(chan bool)
|
|
|
|
return &closableRecorder{r, closer}
|
|
|
|
}
|
|
|
|
|
2016-12-15 02:13:30 +00:00
|
|
|
func (r *closableRecorder) Close() {
|
|
|
|
close(r.closer)
|
|
|
|
}
|
|
|
|
|
2016-11-16 21:45:26 +00:00
|
|
|
func (r *closableRecorder) CloseNotify() <-chan bool {
|
|
|
|
return r.closer
|
|
|
|
}
|
2016-12-14 17:33:57 +00:00
|
|
|
|
|
|
|
func TestAgent_Monitor_ACLDeny(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
|
|
defer a.Shutdown()
|
2016-12-14 17:33:57 +00:00
|
|
|
|
|
|
|
// Try without a token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/monitor", nil)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.AgentMonitor(nil, req); !isPermissionDenied(err) {
|
2016-12-14 17:33:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This proves we call the ACL function, and we've got the other monitor
|
|
|
|
// test to prove monitor works, which should be sufficient. The monitor
|
|
|
|
// logic is a little complex to set up so isn't worth repeating again
|
|
|
|
// here.
|
|
|
|
}
|
2017-07-26 18:03:43 +00:00
|
|
|
|
|
|
|
func TestAgent_Token(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
cfg := TestACLConfig()
|
|
|
|
cfg.ACLToken = ""
|
|
|
|
cfg.ACLAgentToken = ""
|
|
|
|
cfg.ACLAgentMasterToken = ""
|
|
|
|
a := NewTestAgent(t.Name(), cfg)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
b := func(token string) io.Reader {
|
|
|
|
return jsonReader(&api.AgentToken{Token: token})
|
|
|
|
}
|
|
|
|
|
|
|
|
badJSON := func() io.Reader {
|
|
|
|
return jsonReader(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
method, url string
|
|
|
|
body io.Reader
|
|
|
|
code int
|
|
|
|
userToken string
|
|
|
|
agentToken string
|
|
|
|
masterToken string
|
|
|
|
}{
|
|
|
|
{"bad method", "GET", "acl_token", b("X"), http.StatusMethodNotAllowed, "", "", ""},
|
|
|
|
{"bad token name", "PUT", "nope?token=root", b("X"), http.StatusNotFound, "", "", ""},
|
|
|
|
{"bad JSON", "PUT", "acl_token?token=root", badJSON(), http.StatusBadRequest, "", "", ""},
|
|
|
|
{"set user", "PUT", "acl_token?token=root", b("U"), http.StatusOK, "U", "U", ""},
|
|
|
|
{"set agent", "PUT", "acl_agent_token?token=root", b("A"), http.StatusOK, "U", "A", ""},
|
|
|
|
{"set master", "PUT", "acl_agent_master_token?token=root", b("M"), http.StatusOK, "U", "A", "M"},
|
|
|
|
{"clear user", "PUT", "acl_token?token=root", b(""), http.StatusOK, "", "A", "M"},
|
|
|
|
{"clear agent", "PUT", "acl_agent_token?token=root", b(""), http.StatusOK, "", "", "M"},
|
|
|
|
{"clear master", "PUT", "acl_agent_master_token?token=root", b(""), http.StatusOK, "", "", ""},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
url := fmt.Sprintf("/v1/agent/token/%s", tt.url)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
req, _ := http.NewRequest(tt.method, url, tt.body)
|
|
|
|
if _, err := a.srv.AgentToken(resp, req); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if got, want := resp.Code, tt.code; got != want {
|
|
|
|
t.Fatalf("got %d want %d", got, want)
|
|
|
|
}
|
|
|
|
if got, want := a.tokens.UserToken(), tt.userToken; got != want {
|
|
|
|
t.Fatalf("got %q want %q", got, want)
|
|
|
|
}
|
|
|
|
if got, want := a.tokens.AgentToken(), tt.agentToken; got != want {
|
|
|
|
t.Fatalf("got %q want %q", got, want)
|
|
|
|
}
|
|
|
|
if tt.masterToken != "" && !a.tokens.IsAgentMasterToken(tt.masterToken) {
|
|
|
|
t.Fatalf("%q should be the master token", tt.masterToken)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// This one returns an error that is interpreted by the HTTP wrapper, so
|
|
|
|
// doesn't fit into our table above.
|
|
|
|
t.Run("permission denied", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/token/acl_token", b("X"))
|
|
|
|
if _, err := a.srv.AgentToken(nil, req); !isPermissionDenied(err) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if got, want := a.tokens.UserToken(), ""; got != want {
|
|
|
|
t.Fatalf("got %q want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|