2014-01-06 21:21:48 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2016-12-13 06:09:35 +00:00
|
|
|
"bytes"
|
2016-12-14 17:33:57 +00:00
|
|
|
"encoding/json"
|
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
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2017-02-25 02:11:05 +00:00
|
|
|
"github.com/hashicorp/consul/command/base"
|
2015-05-11 23:48:10 +00:00
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
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"
|
2015-05-11 23:48:10 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2016-11-30 18:29:42 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2014-01-06 21:21:48 +00:00
|
|
|
)
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func makeReadOnlyAgentACL(t *testing.T, srv *HTTPServer) string {
|
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(body)
|
|
|
|
raw := map[string]interface{}{
|
|
|
|
"Name": "User Token",
|
|
|
|
"Type": "client",
|
|
|
|
"Rules": fmt.Sprintf(`agent "" { policy = "read" }`),
|
|
|
|
}
|
|
|
|
enc.Encode(raw)
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/acl/create?token=root", body)
|
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) {
|
2014-01-21 01:06:44 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
2015-05-05 00:36:17 +00:00
|
|
|
srv.agent.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)
|
2016-12-14 22:16:46 +00:00
|
|
|
obj, err := 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)
|
2014-10-15 22:03:43 +00:00
|
|
|
if len(val) != 2 {
|
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) {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// Try no token.
|
|
|
|
{
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/services", nil)
|
2016-12-14 22:16:46 +00:00
|
|
|
obj, err := srv.AgentServices(nil, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
val := obj.(map[string]*structs.NodeService)
|
|
|
|
if len(val) != 0 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try the root token (we will get the implicit "consul" service).
|
|
|
|
{
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/services?token=root", nil)
|
2016-12-14 22:16:46 +00:00
|
|
|
obj, err := srv.AgentServices(nil, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
val := obj.(map[string]*structs.NodeService)
|
|
|
|
if len(val) != 1 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_Checks(t *testing.T) {
|
2014-01-21 01:06:44 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
chk1 := &structs.HealthCheck{
|
|
|
|
Node: srv.agent.config.NodeName,
|
|
|
|
CheckID: "mysql",
|
|
|
|
Name: "mysql",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2014-01-21 01:06:44 +00:00
|
|
|
}
|
2015-05-05 00:36:17 +00:00
|
|
|
srv.agent.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)
|
2016-12-14 22:16:46 +00:00
|
|
|
obj, err := 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) {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
chk1 := &structs.HealthCheck{
|
|
|
|
Node: srv.agent.config.NodeName,
|
|
|
|
CheckID: "mysql",
|
|
|
|
Name: "mysql",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
srv.agent.state.AddCheck(chk1, "")
|
|
|
|
|
|
|
|
// Try no token.
|
|
|
|
{
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/checks", nil)
|
2016-12-14 22:16:46 +00:00
|
|
|
obj, err := srv.AgentChecks(nil, req)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try the root token.
|
|
|
|
{
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/checks?token=root", nil)
|
2016-12-14 22:16:46 +00:00
|
|
|
obj, err := srv.AgentChecks(nil, req)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_Self(t *testing.T) {
|
2017-01-06 01:21:56 +00:00
|
|
|
meta := map[string]string{
|
|
|
|
"somekey": "somevalue",
|
|
|
|
}
|
|
|
|
dir, srv := makeHTTPServerWithConfig(t, func(conf *Config) {
|
|
|
|
conf.Meta = meta
|
|
|
|
})
|
2014-05-25 23:59:48 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/self", nil)
|
2014-05-25 23:59:48 +00:00
|
|
|
obj, err := srv.AgentSelf(nil, req)
|
|
|
|
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)
|
2014-05-27 22:09:28 +00:00
|
|
|
if int(val.Member.Port) != srv.agent.config.Ports.SerfLan {
|
|
|
|
t.Fatalf("incorrect port: %v", obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
if int(val.Config.Ports.SerfLan) != srv.agent.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
|
|
|
|
|
|
|
c, err := srv.agent.server.GetLANCoordinate()
|
|
|
|
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-01-06 01:21:56 +00:00
|
|
|
if !reflect.DeepEqual(meta, val.Meta) {
|
|
|
|
t.Fatalf("meta fields are not equal: %v != %v", meta, val.Meta)
|
|
|
|
}
|
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.
|
|
|
|
raw, err := srv.marshalJSON(req, obj)
|
|
|
|
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) {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// Try without a token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/self", nil)
|
|
|
|
_, err := srv.AgentSelf(nil, req)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
2016-12-14 17:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try the agent master token (resolved on the agent).
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/agent/self?token=towel", nil)
|
|
|
|
if _, err := srv.AgentSelf(nil, req); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2016-12-14 17:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try a read only token (resolved on the servers).
|
|
|
|
ro := makeReadOnlyAgentACL(t, srv)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", fmt.Sprintf("/v1/agent/self?token=%s", ro), nil)
|
|
|
|
if _, err := srv.AgentSelf(nil, req); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2016-12-14 17:33:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_Reload(t *testing.T) {
|
2016-11-30 18:29:42 +00:00
|
|
|
conf := nextConfig()
|
|
|
|
tmpDir, err := ioutil.TempDir("", "consul")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
// Write initial config, to be reloaded later
|
|
|
|
tmpFile, err := ioutil.TempFile(tmpDir, "config")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-03-24 16:23:25 +00:00
|
|
|
_, err = tmpFile.WriteString(`{"acl_enforce_version_8": false, "service":{"name":"redis"}}`)
|
2016-11-30 18:29:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
tmpFile.Close()
|
|
|
|
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
close(shutdownCh)
|
|
|
|
<-doneCh
|
|
|
|
}()
|
|
|
|
|
|
|
|
cmd := &Command{
|
|
|
|
ShutdownCh: shutdownCh,
|
2017-02-25 02:11:05 +00:00
|
|
|
Command: base.Command{
|
|
|
|
Flags: base.FlagSetNone,
|
2017-04-21 00:02:42 +00:00
|
|
|
UI: new(cli.MockUi),
|
2017-02-25 02:11:05 +00:00
|
|
|
},
|
2016-11-30 18:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-server",
|
2017-05-03 21:47:25 +00:00
|
|
|
"-advertise", "127.0.0.1",
|
2016-11-30 18:29:42 +00:00
|
|
|
"-data-dir", tmpDir,
|
|
|
|
"-http-port", fmt.Sprintf("%d", conf.Ports.HTTP),
|
|
|
|
"-config-file", tmpFile.Name(),
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
cmd.Run(args)
|
|
|
|
close(doneCh)
|
|
|
|
}()
|
|
|
|
|
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(cmd.httpServers), 1; got != want {
|
|
|
|
r.Fatalf("got %d servers want %d", got, want)
|
|
|
|
}
|
|
|
|
})
|
2016-11-30 18:29:42 +00:00
|
|
|
|
|
|
|
if _, ok := cmd.agent.state.services["redis"]; !ok {
|
|
|
|
t.Fatalf("missing redis service")
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
data := []byte(`{"acl_enforce_version_8": false, "service":{"name":"redis-reloaded"}}`)
|
|
|
|
if err := ioutil.WriteFile(tmpFile.Name(), data, 0644); err != nil {
|
2016-11-30 18:29:42 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
srv := cmd.httpServers[0]
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/reload", nil)
|
|
|
|
if _, err := srv.AgentReload(nil, req); err != nil {
|
2016-11-30 18:29:42 +00:00
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := cmd.agent.state.services["redis-reloaded"]; !ok {
|
|
|
|
t.Fatalf("missing redis-reloaded service")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_Reload_ACLDeny(t *testing.T) {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// Try without a token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/reload", nil)
|
|
|
|
_, err := srv.AgentReload(nil, req)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
2016-12-14 17:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try with a read only token (resolved on the servers).
|
|
|
|
ro := makeReadOnlyAgentACL(t, srv)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("PUT", fmt.Sprintf("/v1/agent/reload?token=%s", ro), nil)
|
|
|
|
_, err = srv.AgentReload(nil, req)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
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) {
|
2014-01-06 21:21:48 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/members", nil)
|
2014-01-06 21:21:48 +00:00
|
|
|
obj, err := srv.AgentMembers(nil, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
val := obj.([]serf.Member)
|
|
|
|
if len(val) == 0 {
|
|
|
|
t.Fatalf("bad members: %v", obj)
|
|
|
|
}
|
|
|
|
|
2014-04-11 22:46:55 +00:00
|
|
|
if int(val[0].Port) != srv.agent.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) {
|
2014-01-06 21:21:48 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/members?wan=true", nil)
|
2014-01-06 21:21:48 +00:00
|
|
|
obj, err := srv.AgentMembers(nil, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
val := obj.([]serf.Member)
|
|
|
|
if len(val) == 0 {
|
|
|
|
t.Fatalf("bad members: %v", obj)
|
|
|
|
}
|
|
|
|
|
2014-04-11 22:46:55 +00:00
|
|
|
if int(val[0].Port) != srv.agent.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) {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// Try no token.
|
|
|
|
{
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/members", nil)
|
2016-12-14 22:16:46 +00:00
|
|
|
obj, err := srv.AgentMembers(nil, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
val := obj.([]serf.Member)
|
|
|
|
if len(val) != 0 {
|
|
|
|
t.Fatalf("bad members: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try the root token.
|
|
|
|
{
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/members?token=root", nil)
|
2016-12-14 22:16:46 +00:00
|
|
|
obj, err := srv.AgentMembers(nil, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
val := obj.([]serf.Member)
|
|
|
|
if len(val) != 1 {
|
|
|
|
t.Fatalf("bad members: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_Join(t *testing.T) {
|
2014-01-06 21:21:48 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
dir2, a2 := makeAgent(t, nextConfig())
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer a2.Shutdown()
|
|
|
|
|
2014-04-11 22:46:55 +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)
|
2014-01-06 21:21:48 +00:00
|
|
|
obj, err := srv.AgentJoin(nil, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("Err: %v", obj)
|
|
|
|
}
|
|
|
|
|
2016-10-25 20:46:54 +00:00
|
|
|
if len(srv.agent.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) {
|
2014-01-06 21:21:48 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
dir2, a2 := makeAgent(t, nextConfig())
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer a2.Shutdown()
|
|
|
|
|
2014-04-11 22:46:55 +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)
|
2014-01-06 21:21:48 +00:00
|
|
|
obj, err := srv.AgentJoin(nil, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("Err: %v", obj)
|
|
|
|
}
|
|
|
|
|
2016-10-25 20:46:54 +00:00
|
|
|
if len(srv.agent.WANMembers()) != 2 {
|
|
|
|
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) {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
dir2, a2 := makeAgent(t, nextConfig())
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer a2.Shutdown()
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", a2.config.Ports.SerfLan)
|
|
|
|
|
|
|
|
// Try without a token.
|
|
|
|
{
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/join/%s", addr), nil)
|
|
|
|
_, err := srv.AgentJoin(nil, req)
|
2016-12-14 17:33:57 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try the agent master token (resolved on the agent).
|
|
|
|
{
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/join/%s?token=towel", addr), nil)
|
|
|
|
_, err := srv.AgentJoin(nil, req)
|
2016-12-14 17:33:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with a read only token (resolved on the servers).
|
|
|
|
ro := makeReadOnlyAgentACL(t, srv)
|
|
|
|
{
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/join/%s?token=%s", addr, ro), nil)
|
|
|
|
_, err := srv.AgentJoin(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
2016-12-14 17:33:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_Leave(t *testing.T) {
|
2016-11-30 18:29:42 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
dir2, srv2 := makeHTTPServerWithConfig(t, func(c *Config) {
|
|
|
|
c.Server = false
|
|
|
|
c.Bootstrap = false
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer srv2.Shutdown()
|
|
|
|
|
|
|
|
// Join first
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", srv2.agent.config.Ports.SerfLan)
|
|
|
|
_, err := srv.agent.JoinLAN([]string{addr})
|
|
|
|
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)
|
2016-11-30 18:29:42 +00:00
|
|
|
obj, err := srv2.AgentLeave(nil, req)
|
|
|
|
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) {
|
2016-11-30 18:29:42 +00:00
|
|
|
m := srv.agent.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) {
|
|
|
|
// Try without a token.
|
|
|
|
func() {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/leave", nil)
|
|
|
|
_, err := srv.AgentLeave(nil, req)
|
2016-12-14 17:33:57 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Try the agent master token (resolved on the agent).
|
|
|
|
func() {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/leave?token=towel", nil)
|
|
|
|
_, err := srv.AgentLeave(nil, req)
|
2016-12-14 17:33:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Try with a read only token (resolved on the servers).
|
|
|
|
func() {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
ro := makeReadOnlyAgentACL(t, srv)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/agent/leave?token=%s", ro), nil)
|
|
|
|
_, err := srv.AgentLeave(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
2016-12-14 17:33:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_ForceLeave(t *testing.T) {
|
2014-01-06 21:21:48 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
dir2, a2 := makeAgent(t, nextConfig())
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer a2.Shutdown()
|
|
|
|
|
|
|
|
// Join first
|
2014-04-11 22:46:55 +00:00
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", a2.config.Ports.SerfLan)
|
2014-01-06 21:21:48 +00:00
|
|
|
_, err := srv.agent.JoinLAN([]string{addr})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
a2.Shutdown()
|
|
|
|
|
|
|
|
// Force leave now
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/force-leave/%s", a2.config.NodeName), nil)
|
2014-01-06 21:21:48 +00:00
|
|
|
obj, err := srv.AgentForceLeave(nil, req)
|
|
|
|
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) {
|
2014-05-07 20:50:18 +00:00
|
|
|
m := srv.agent.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) {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// Try without a token.
|
|
|
|
{
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/force-leave/nope", nil)
|
|
|
|
_, err := srv.AgentForceLeave(nil, req)
|
2016-12-14 17:33:57 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try the agent master token (resolved on the agent).
|
|
|
|
{
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/force-leave/nope?token=towel", nil)
|
|
|
|
_, err := srv.AgentForceLeave(nil, req)
|
2016-12-14 17:33:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try a read only token (resolved on the servers).
|
|
|
|
ro := makeReadOnlyAgentACL(t, srv)
|
|
|
|
{
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/force-leave/nope?token=%s", ro), nil)
|
|
|
|
_, err := srv.AgentForceLeave(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
2016-12-14 17:33:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_RegisterCheck(t *testing.T) {
|
2014-01-30 23:51:15 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// Register node
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/register?token=abc123", nil)
|
2014-01-30 23:51:15 +00:00
|
|
|
args := &CheckDefinition{
|
|
|
|
Name: "test",
|
|
|
|
CheckType: CheckType{
|
|
|
|
TTL: 15 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
req.Body = encodeReq(args)
|
|
|
|
|
|
|
|
obj, err := srv.AgentRegisterCheck(nil, req)
|
|
|
|
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")
|
|
|
|
if _, ok := srv.agent.state.Checks()[checkID]; !ok {
|
2014-01-30 23:51:15 +00:00
|
|
|
t.Fatalf("missing test check")
|
|
|
|
}
|
|
|
|
|
2016-06-07 20:24:51 +00:00
|
|
|
if _, ok := srv.agent.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
|
2016-06-07 20:24:51 +00:00
|
|
|
if token := srv.agent.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.
|
2016-06-07 20:24:51 +00:00
|
|
|
state := srv.agent.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) {
|
2015-04-12 00:53:48 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// Register node
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/register", nil)
|
2015-04-12 00:53:48 +00:00
|
|
|
args := &CheckDefinition{
|
|
|
|
Name: "test",
|
|
|
|
CheckType: CheckType{
|
|
|
|
TTL: 15 * time.Second,
|
|
|
|
},
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2015-04-12 00:53:48 +00:00
|
|
|
}
|
|
|
|
req.Body = encodeReq(args)
|
|
|
|
|
|
|
|
obj, err := srv.AgentRegisterCheck(nil, req)
|
|
|
|
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")
|
|
|
|
if _, ok := srv.agent.state.Checks()[checkID]; !ok {
|
2015-04-12 00:53:48 +00:00
|
|
|
t.Fatalf("missing test check")
|
|
|
|
}
|
|
|
|
|
2016-06-07 20:24:51 +00:00
|
|
|
if _, ok := srv.agent.checkTTLs[checkID]; !ok {
|
2015-04-12 00:53:48 +00:00
|
|
|
t.Fatalf("missing test check ttl")
|
|
|
|
}
|
|
|
|
|
2016-06-07 20:24:51 +00:00
|
|
|
state := srv.agent.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) {
|
2015-04-12 00:53:48 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// Register node
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/register", nil)
|
2015-04-12 00:53:48 +00:00
|
|
|
args := &CheckDefinition{
|
|
|
|
Name: "test",
|
|
|
|
CheckType: CheckType{
|
|
|
|
TTL: 15 * time.Second,
|
|
|
|
},
|
|
|
|
Status: "fluffy",
|
|
|
|
}
|
|
|
|
req.Body = encodeReq(args)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
if _, err := srv.AgentRegisterCheck(resp, req); err != nil {
|
|
|
|
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) {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// Try with no token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/register", nil)
|
2016-12-14 22:16:46 +00:00
|
|
|
args := &CheckDefinition{
|
|
|
|
Name: "test",
|
|
|
|
CheckType: CheckType{
|
|
|
|
TTL: 15 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
req.Body = encodeReq(args)
|
2017-05-09 11:38:05 +00:00
|
|
|
_, err := srv.AgentRegisterCheck(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try the root token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/agent/check/register?token=root", nil)
|
2016-12-14 22:16:46 +00:00
|
|
|
req.Body = encodeReq(args)
|
2017-05-09 11:38:05 +00:00
|
|
|
if _, err := srv.AgentRegisterCheck(nil, req); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_DeregisterCheck(t *testing.T) {
|
2014-01-30 23:51:15 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := srv.agent.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)
|
2014-01-30 23:51:15 +00:00
|
|
|
obj, err := srv.AgentDeregisterCheck(nil, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
|
|
|
if _, ok := srv.agent.state.Checks()["test"]; ok {
|
|
|
|
t.Fatalf("have test check")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_DeregisterCheckACLDeny(t *testing.T) {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
|
|
|
if err := srv.agent.AddCheck(chk, nil, false, ""); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with no token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/deregister/test", nil)
|
|
|
|
_, err := srv.AgentDeregisterCheck(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with the root token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/agent/check/deregister/test?token=root", nil)
|
|
|
|
if _, err := srv.AgentDeregisterCheck(nil, req); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_PassCheck(t *testing.T) {
|
2014-01-30 23:51:15 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
|
|
|
chkType := &CheckType{TTL: 15 * time.Second}
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := srv.agent.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)
|
2014-01-30 23:51:15 +00:00
|
|
|
obj, err := srv.AgentCheckPass(nil, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
|
|
|
state := srv.agent.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) {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
|
|
|
chkType := &CheckType{TTL: 15 * time.Second}
|
|
|
|
if err := srv.agent.AddCheck(chk, chkType, false, ""); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with no token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/pass/test", nil)
|
|
|
|
_, err := srv.AgentCheckPass(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with the root token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/agent/check/pass/test?token=root", nil)
|
|
|
|
if _, err := srv.AgentCheckPass(nil, req); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_WarnCheck(t *testing.T) {
|
2014-01-30 23:51:15 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
|
|
|
chkType := &CheckType{TTL: 15 * time.Second}
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := srv.agent.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)
|
2014-01-30 23:51:15 +00:00
|
|
|
obj, err := srv.AgentCheckWarn(nil, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
|
|
|
state := srv.agent.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) {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
|
|
|
chkType := &CheckType{TTL: 15 * time.Second}
|
|
|
|
if err := srv.agent.AddCheck(chk, chkType, false, ""); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with no token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/warn/test", nil)
|
|
|
|
_, err := srv.AgentCheckWarn(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with the root token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/agent/check/warn/test?token=root", nil)
|
|
|
|
if _, err := srv.AgentCheckWarn(nil, req); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_FailCheck(t *testing.T) {
|
2014-01-30 23:51:15 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
|
|
|
chkType := &CheckType{TTL: 15 * time.Second}
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := srv.agent.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)
|
2014-01-30 23:51:15 +00:00
|
|
|
obj, err := srv.AgentCheckFail(nil, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
|
|
|
state := srv.agent.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) {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
|
|
|
chkType := &CheckType{TTL: 15 * time.Second}
|
|
|
|
if err := srv.agent.AddCheck(chk, chkType, false, ""); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with no token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/check/fail/test", nil)
|
|
|
|
_, err := srv.AgentCheckFail(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with the root token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/agent/check/fail/test?token=root", nil)
|
|
|
|
if _, err := srv.AgentCheckFail(nil, req); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_UpdateCheck(t *testing.T) {
|
2016-03-03 01:08:06 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
|
|
|
chkType := &CheckType{TTL: 15 * time.Second}
|
|
|
|
if err := srv.agent.AddCheck(chk, chkType, false, ""); err != nil {
|
|
|
|
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 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/check/update/test", nil)
|
2016-03-03 01:08:06 +00:00
|
|
|
req.Body = encodeReq(c)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.AgentCheckUpdate(resp, req)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
state := srv.agent.state.Checks()["test"]
|
|
|
|
if state.Status != c.Status || state.Output != c.Output {
|
|
|
|
t.Fatalf("bad: %v", state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure abusive levels of output are capped.
|
|
|
|
{
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/check/update/test", nil)
|
2016-03-03 01:08:06 +00:00
|
|
|
update := 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),
|
|
|
|
}
|
|
|
|
req.Body = encodeReq(update)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.AgentCheckUpdate(resp, req)
|
|
|
|
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.
|
|
|
|
state := srv.agent.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check a bogus status.
|
|
|
|
{
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/check/update/test", nil)
|
2016-03-03 01:08:06 +00:00
|
|
|
update := checkUpdate{
|
|
|
|
Status: "itscomplicated",
|
|
|
|
}
|
|
|
|
req.Body = encodeReq(update)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.AgentCheckUpdate(resp, req)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check a bogus verb.
|
|
|
|
{
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("POST", "/v1/agent/check/update/test", nil)
|
2016-03-03 01:08:06 +00:00
|
|
|
update := checkUpdate{
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2016-03-03 01:08:06 +00:00
|
|
|
}
|
|
|
|
req.Body = encodeReq(update)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.AgentCheckUpdate(resp, req)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
func TestAgent_UpdateCheck_ACLDeny(t *testing.T) {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
|
|
|
|
chkType := &CheckType{TTL: 15 * time.Second}
|
|
|
|
if err := srv.agent.AddCheck(chk, chkType, false, ""); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with no token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/check/update/test", nil)
|
2017-04-19 23:00:11 +00:00
|
|
|
req.Body = encodeReq(checkUpdate{api.HealthPassing, "hello-passing"})
|
2017-05-09 11:38:05 +00:00
|
|
|
_, err := srv.AgentCheckUpdate(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with the root token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("PUT", "/v1/agent/check/update/test?token=root", nil)
|
2017-04-19 23:00:11 +00:00
|
|
|
req.Body = encodeReq(checkUpdate{api.HealthPassing, "hello-passing"})
|
2017-05-09 11:38:05 +00:00
|
|
|
if _, err := srv.AgentCheckUpdate(nil, req); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
func TestAgent_RegisterService(t *testing.T) {
|
2014-01-30 23:56:03 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/service/register?token=abc123", nil)
|
2014-01-30 23:56:03 +00:00
|
|
|
args := &ServiceDefinition{
|
|
|
|
Name: "test",
|
2014-04-03 19:12:23 +00:00
|
|
|
Tags: []string{"master"},
|
2014-01-30 23:56:03 +00:00
|
|
|
Port: 8000,
|
2014-02-05 02:32:17 +00:00
|
|
|
Check: CheckType{
|
2014-01-30 23:56:03 +00:00
|
|
|
TTL: 15 * time.Second,
|
|
|
|
},
|
2015-01-14 03:08:30 +00:00
|
|
|
Checks: CheckTypes{
|
|
|
|
&CheckType{
|
|
|
|
TTL: 20 * time.Second,
|
|
|
|
},
|
|
|
|
&CheckType{
|
|
|
|
TTL: 30 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
2014-01-30 23:56:03 +00:00
|
|
|
}
|
|
|
|
req.Body = encodeReq(args)
|
|
|
|
|
|
|
|
obj, err := srv.AgentRegisterService(nil, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the servie
|
|
|
|
if _, ok := srv.agent.state.Services()["test"]; !ok {
|
|
|
|
t.Fatalf("missing test service")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
2015-01-14 03:08:30 +00:00
|
|
|
checks := srv.agent.state.Checks()
|
|
|
|
if len(checks) != 3 {
|
|
|
|
t.Fatalf("bad: %v", checks)
|
2014-01-30 23:56:03 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 03:08:30 +00:00
|
|
|
if len(srv.agent.checkTTLs) != 3 {
|
|
|
|
t.Fatalf("missing test check ttls: %v", srv.agent.checkTTLs)
|
2014-01-30 23:56:03 +00:00
|
|
|
}
|
2015-04-28 20:06:02 +00:00
|
|
|
|
|
|
|
// Ensure the token was configured
|
|
|
|
if token := srv.agent.state.ServiceToken("test"); token == "" {
|
|
|
|
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) {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
args := &ServiceDefinition{
|
|
|
|
Name: "test",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 8000,
|
|
|
|
Check: CheckType{
|
|
|
|
TTL: 15 * time.Second,
|
|
|
|
},
|
|
|
|
Checks: CheckTypes{
|
|
|
|
&CheckType{
|
|
|
|
TTL: 20 * time.Second,
|
|
|
|
},
|
|
|
|
&CheckType{
|
|
|
|
TTL: 30 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with no token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/service/register", nil)
|
2016-12-14 22:16:46 +00:00
|
|
|
req.Body = encodeReq(args)
|
2017-05-09 11:38:05 +00:00
|
|
|
_, err := srv.AgentRegisterService(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with the root token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/agent/service/register?token=root", nil)
|
2016-12-14 22:16:46 +00:00
|
|
|
req.Body = encodeReq(args)
|
2017-05-09 11:38:05 +00:00
|
|
|
if _, err := srv.AgentRegisterService(nil, req); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-08 16:34:45 +00:00
|
|
|
func TestAgent_RegisterService_InvalidAddress(t *testing.T) {
|
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-05-09 07:29:13 +00:00
|
|
|
for _, addr := range []string{"0.0.0.0", "::", "[::]"} {
|
|
|
|
t.Run("addr "+addr, func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/service/register?token=abc123", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
args := &ServiceDefinition{
|
|
|
|
Name: "test",
|
|
|
|
Address: addr,
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
req.Body = encodeReq(args)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err = srv.AgentRegisterService(resp, req)
|
|
|
|
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) {
|
2014-01-30 23:56:03 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
service := &structs.NodeService{
|
|
|
|
ID: "test",
|
|
|
|
Service: "test",
|
|
|
|
}
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := srv.agent.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)
|
2014-01-30 23:56:03 +00:00
|
|
|
obj, err := srv.AgentDeregisterService(nil, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
|
|
|
if _, ok := srv.agent.state.Services()["test"]; ok {
|
|
|
|
t.Fatalf("have test service")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := srv.agent.state.Checks()["test"]; ok {
|
|
|
|
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) {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
service := &structs.NodeService{
|
|
|
|
ID: "test",
|
|
|
|
Service: "test",
|
|
|
|
}
|
|
|
|
if err := srv.agent.AddService(service, nil, false, ""); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try without a token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/service/deregister/test", nil)
|
|
|
|
_, err := srv.AgentDeregisterService(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with the root.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/agent/service/deregister/test?token=root", nil)
|
|
|
|
if _, err := srv.AgentDeregisterService(nil, req); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_ServiceMaintenance_BadRequest(t *testing.T) {
|
2015-01-15 09:17:35 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// Fails on non-PUT
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/service/maintenance/test?enable=true", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
if _, err := srv.AgentServiceMaintenance(resp, req); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 405 {
|
2015-01-15 18:51:00 +00:00
|
|
|
t.Fatalf("expected 405, got %d", resp.Code)
|
2015-01-15 09:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fails when no enable flag provided
|
|
|
|
req, _ = http.NewRequest("PUT", "/v1/agent/service/maintenance/test", nil)
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
if _, err := srv.AgentServiceMaintenance(resp, req); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 400 {
|
2015-01-15 18:51:00 +00:00
|
|
|
t.Fatalf("expected 400, got %d", resp.Code)
|
2015-01-15 09:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fails when no service ID provided
|
|
|
|
req, _ = http.NewRequest("PUT", "/v1/agent/service/maintenance/?enable=true", nil)
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
if _, err := srv.AgentServiceMaintenance(resp, req); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 400 {
|
2015-01-15 18:51:00 +00:00
|
|
|
t.Fatalf("expected 400, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fails when bad service ID provided
|
|
|
|
req, _ = http.NewRequest("PUT", "/v1/agent/service/maintenance/_nope_?enable=true", nil)
|
|
|
|
resp = httptest.NewRecorder()
|
2015-01-21 22:12:18 +00:00
|
|
|
if _, err := srv.AgentServiceMaintenance(resp, req); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-01-15 18:51:00 +00:00
|
|
|
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) {
|
2015-01-15 09:17:35 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// Register the service
|
|
|
|
service := &structs.NodeService{
|
|
|
|
ID: "test",
|
|
|
|
Service: "test",
|
|
|
|
}
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := srv.agent.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()
|
|
|
|
if _, err := srv.AgentServiceMaintenance(resp, req); err != nil {
|
|
|
|
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")
|
2015-01-21 20:21:57 +00:00
|
|
|
check, ok := srv.agent.state.Checks()[checkID]
|
|
|
|
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
|
|
|
|
if token := srv.agent.state.CheckToken(checkID); token != "mytoken" {
|
|
|
|
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) {
|
2015-01-15 09:17:35 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// Register the service
|
|
|
|
service := &structs.NodeService{
|
|
|
|
ID: "test",
|
|
|
|
Service: "test",
|
|
|
|
}
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := srv.agent.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
|
2015-09-10 19:08:08 +00:00
|
|
|
if err := srv.agent.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()
|
2015-01-15 09:17:35 +00:00
|
|
|
if _, err := srv.AgentServiceMaintenance(resp, req); err != nil {
|
|
|
|
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")
|
|
|
|
if _, ok := srv.agent.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) {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// Register the service.
|
|
|
|
service := &structs.NodeService{
|
|
|
|
ID: "test",
|
|
|
|
Service: "test",
|
|
|
|
}
|
|
|
|
if err := srv.agent.AddService(service, nil, false, ""); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with no token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/service/maintenance/test?enable=true&reason=broken", nil)
|
|
|
|
_, err := srv.AgentServiceMaintenance(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with the root token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("PUT", "/v1/agent/service/maintenance/test?enable=true&reason=broken&token=root", nil)
|
|
|
|
if _, err := srv.AgentServiceMaintenance(nil, req); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_NodeMaintenance_BadRequest(t *testing.T) {
|
2015-01-15 19:26:14 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// Fails on non-PUT
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/self/maintenance?enable=true", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
if _, err := srv.AgentNodeMaintenance(resp, req); err != nil {
|
|
|
|
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()
|
|
|
|
if _, err := srv.AgentNodeMaintenance(resp, req); err != nil {
|
|
|
|
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) {
|
2015-01-15 19:26:14 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
if _, err := srv.AgentNodeMaintenance(resp, req); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf("expected 200, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the maintenance check was registered
|
2016-11-29 21:15:20 +00:00
|
|
|
check, ok := srv.agent.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
|
2016-11-29 21:15:20 +00:00
|
|
|
if token := srv.agent.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) {
|
2015-01-15 19:26:14 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// Force the node into maintenance mode
|
2015-09-10 19:08:08 +00:00
|
|
|
srv.agent.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()
|
|
|
|
if _, err := srv.AgentNodeMaintenance(resp, req); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf("expected 200, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the maintenance check was removed
|
2016-11-29 21:15:20 +00:00
|
|
|
if _, ok := srv.agent.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) {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// Try with no token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/agent/self/maintenance?enable=true&reason=broken", nil)
|
|
|
|
_, err := srv.AgentNodeMaintenance(nil, req)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with the root token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("PUT", "/v1/agent/self/maintenance?enable=true&reason=broken&token=root", nil)
|
|
|
|
if _, err := srv.AgentNodeMaintenance(nil, req); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_RegisterCheck_Service(t *testing.T) {
|
2015-01-14 01:52:17 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// First register the service
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/service/register", nil)
|
2015-01-14 01:52:17 +00:00
|
|
|
args := &ServiceDefinition{
|
|
|
|
Name: "memcache",
|
|
|
|
Port: 8000,
|
|
|
|
Check: CheckType{
|
|
|
|
TTL: 15 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
req.Body = encodeReq(args)
|
|
|
|
|
|
|
|
if _, err := srv.AgentRegisterService(nil, req); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now register an additional check
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/agent/check/register", nil)
|
2015-01-14 01:52:17 +00:00
|
|
|
checkArgs := &CheckDefinition{
|
|
|
|
Name: "memcache_check2",
|
|
|
|
ServiceID: "memcache",
|
|
|
|
CheckType: CheckType{
|
|
|
|
TTL: 15 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
req.Body = encodeReq(checkArgs)
|
|
|
|
|
|
|
|
if _, err := srv.AgentRegisterCheck(nil, req); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a check mapping
|
|
|
|
result := srv.agent.state.Checks()
|
|
|
|
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) {
|
2016-11-16 21:45:26 +00:00
|
|
|
logWriter := logger.NewLogWriter(512)
|
2016-12-01 17:34:21 +00:00
|
|
|
logger := io.MultiWriter(os.Stdout, logWriter)
|
2016-11-16 21:45:26 +00:00
|
|
|
|
2016-11-28 21:08:31 +00:00
|
|
|
dir, srv := makeHTTPServerWithConfigLog(t, nil, logger, logWriter)
|
2016-11-16 21:45:26 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
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()
|
2016-11-28 21:08:31 +00:00
|
|
|
if _, err := srv.AgentMonitor(resp, req); err != nil {
|
|
|
|
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() {
|
|
|
|
if _, err := srv.AgentMonitor(resp, req); err != nil {
|
|
|
|
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) {
|
|
|
|
dir, srv := makeHTTPServerWithACLs(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// Try without a token.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/monitor", nil)
|
|
|
|
if _, err := srv.AgentMonitor(nil, req); err != errPermissionDenied {
|
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.
|
|
|
|
}
|