2015-01-06 18:40:00 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-11-30 18:29:42 +00:00
|
|
|
"io/ioutil"
|
2017-09-25 23:06:49 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-01-21 19:08:57 +00:00
|
|
|
"strings"
|
2015-01-06 18:40:00 +00:00
|
|
|
"testing"
|
2016-11-16 21:45:26 +00:00
|
|
|
"time"
|
2016-11-30 18:29:42 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/testutil"
|
2017-10-25 05:09:06 +00:00
|
|
|
"github.com/hashicorp/consul/testutil/retry"
|
2016-11-30 18:29:42 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2018-03-27 23:50:17 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-01-06 18:40:00 +00:00
|
|
|
)
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_AgentSelf(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
info, err := agent.Self()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-09-27 18:47:40 +00:00
|
|
|
name := info["Config"]["NodeName"].(string)
|
2015-01-06 18:40:00 +00:00
|
|
|
if name == "" {
|
|
|
|
t.Fatalf("bad: %v", info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-08 08:31:38 +00:00
|
|
|
func TestAPI_AgentMetrics(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
2017-11-10 21:03:31 +00:00
|
|
|
timer := &retry.Timer{Timeout: 10 * time.Second, Wait: 500 * time.Millisecond}
|
|
|
|
retry.RunWith(timer, t, func(r *retry.R) {
|
2017-11-07 05:43:39 +00:00
|
|
|
metrics, err := agent.Metrics()
|
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-10-25 05:09:06 +00:00
|
|
|
for _, g := range metrics.Gauges {
|
|
|
|
if g.Name == "consul.runtime.alloc_bytes" {
|
|
|
|
return
|
|
|
|
}
|
2017-09-25 22:27:04 +00:00
|
|
|
}
|
2017-10-25 05:09:06 +00:00
|
|
|
r.Fatalf("missing runtime metrics")
|
|
|
|
})
|
2017-08-08 08:31:38 +00:00
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_AgentReload(t *testing.T) {
|
2016-11-30 18:29:42 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create our initial empty config file, to be overwritten later
|
2017-09-25 22:26:49 +00:00
|
|
|
cfgDir := testutil.TempDir(t, "consul-config")
|
|
|
|
defer os.RemoveAll(cfgDir)
|
|
|
|
|
|
|
|
cfgFilePath := filepath.Join(cfgDir, "reload.json")
|
|
|
|
configFile, err := os.Create(cfgFilePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create file %v, got error:%v", cfgFilePath, err)
|
2016-11-30 18:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c, s := makeClientWithConfig(t, nil, func(conf *testutil.TestServerConfig) {
|
|
|
|
conf.Args = []string{"-config-file", configFile.Name()}
|
|
|
|
})
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
// Update the config file with a service definition
|
2018-04-21 15:34:29 +00:00
|
|
|
config := `{"service":{"name":"redis", "port":1234, "Meta": {"some": "meta"}}}`
|
2017-09-25 22:26:49 +00:00
|
|
|
err = ioutil.WriteFile(configFile.Name(), []byte(config), 0644)
|
2016-11-30 18:29:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = agent.Reload(); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
services, err := agent.Services()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
service, ok := services["redis"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("bad: %v", ok)
|
|
|
|
}
|
|
|
|
if service.Port != 1234 {
|
|
|
|
t.Fatalf("bad: %v", service.Port)
|
|
|
|
}
|
2018-04-21 15:34:29 +00:00
|
|
|
if service.Meta["some"] != "meta" {
|
|
|
|
t.Fatalf("Missing metadata some:=meta in %v", service)
|
|
|
|
}
|
2016-11-30 18:29:42 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 00:39:46 +00:00
|
|
|
func TestAPI_AgentMembersOpts(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s1 := makeClient(t)
|
|
|
|
_, s2 := makeClientWithConfig(t, nil, func(c *testutil.TestServerConfig) {
|
|
|
|
c.Datacenter = "dc2"
|
|
|
|
})
|
|
|
|
defer s1.Stop()
|
|
|
|
defer s2.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
s2.JoinWAN(t, s1.WANAddr)
|
|
|
|
|
|
|
|
members, err := agent.MembersOpts(MembersOpts{WAN: true})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(members) != 2 {
|
|
|
|
t.Fatalf("bad: %v", members)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_AgentMembers(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
members, err := agent.Members(false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(members) != 1 {
|
|
|
|
t.Fatalf("bad: %v", members)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_AgentServices(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
reg := &AgentServiceRegistration{
|
2015-01-22 21:42:22 +00:00
|
|
|
Name: "foo",
|
|
|
|
Tags: []string{"bar", "baz"},
|
|
|
|
Port: 8000,
|
2015-01-06 18:40:00 +00:00
|
|
|
Check: &AgentServiceCheck{
|
|
|
|
TTL: "15s",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-12 00:53:48 +00:00
|
|
|
services, err := agent.Services()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, ok := services["foo"]; !ok {
|
|
|
|
t.Fatalf("missing service: %v", services)
|
|
|
|
}
|
|
|
|
checks, err := agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
chk, ok := checks["service:foo"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing check: %v", checks)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks should default to critical
|
2016-04-23 23:01:59 +00:00
|
|
|
if chk.Status != HealthCritical {
|
2015-04-12 00:53:48 +00:00
|
|
|
t.Fatalf("Bad: %#v", chk)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := agent.ServiceDeregister("foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 13:24:24 +00:00
|
|
|
func TestAPI_AgentServices_ManagedConnectProxy(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
Tags: []string{"bar", "baz"},
|
|
|
|
Port: 8000,
|
|
|
|
Check: &AgentServiceCheck{
|
|
|
|
TTL: "15s",
|
|
|
|
},
|
|
|
|
Connect: &AgentServiceConnect{
|
|
|
|
Proxy: &AgentServiceConnectProxy{
|
|
|
|
ExecMode: ProxyExecModeScript,
|
2018-05-10 16:04:33 +00:00
|
|
|
Command: []string{"foo.rb"},
|
2018-04-20 13:24:24 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
services, err := agent.Services()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, ok := services["foo"]; !ok {
|
|
|
|
t.Fatalf("missing service: %v", services)
|
|
|
|
}
|
|
|
|
checks, err := agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
chk, ok := checks["service:foo"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing check: %v", checks)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks should default to critical
|
|
|
|
if chk.Status != HealthCritical {
|
|
|
|
t.Fatalf("Bad: %#v", chk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proxy config should be present in response
|
|
|
|
require.Equal(t, reg.Connect, services["foo"].Connect)
|
|
|
|
|
|
|
|
if err := agent.ServiceDeregister("foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPI_AgentServices_ExternalConnectProxy(t *testing.T) {
|
2018-03-26 15:51:43 +00:00
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
// Register service
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
// Register proxy
|
|
|
|
reg = &AgentServiceRegistration{
|
|
|
|
Kind: ServiceKindConnectProxy,
|
|
|
|
Name: "foo-proxy",
|
|
|
|
Port: 8001,
|
|
|
|
ProxyDestination: "foo",
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
services, err := agent.Services()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, ok := services["foo"]; !ok {
|
|
|
|
t.Fatalf("missing service: %v", services)
|
|
|
|
}
|
|
|
|
if _, ok := services["foo-proxy"]; !ok {
|
|
|
|
t.Fatalf("missing proxy service: %v", services)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := agent.ServiceDeregister("foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if err := agent.ServiceDeregister("foo-proxy"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_AgentServices_CheckPassing(t *testing.T) {
|
2015-05-12 01:53:09 +00:00
|
|
|
t.Parallel()
|
2015-04-12 00:53:48 +00:00
|
|
|
c, s := makeClient(t)
|
2015-05-12 01:53:09 +00:00
|
|
|
defer s.Stop()
|
2015-04-12 00:53:48 +00:00
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
Tags: []string{"bar", "baz"},
|
|
|
|
Port: 8000,
|
|
|
|
Check: &AgentServiceCheck{
|
|
|
|
TTL: "15s",
|
2016-04-23 23:01:59 +00:00
|
|
|
Status: HealthPassing,
|
2015-04-12 00:53:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
services, err := agent.Services()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, ok := services["foo"]; !ok {
|
|
|
|
t.Fatalf("missing service: %v", services)
|
|
|
|
}
|
|
|
|
|
|
|
|
checks, err := agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-04-12 00:53:48 +00:00
|
|
|
chk, ok := checks["service:foo"]
|
|
|
|
if !ok {
|
2015-01-06 18:40:00 +00:00
|
|
|
t.Fatalf("missing check: %v", checks)
|
|
|
|
}
|
|
|
|
|
2016-04-23 23:01:59 +00:00
|
|
|
if chk.Status != HealthPassing {
|
2015-04-12 00:53:48 +00:00
|
|
|
t.Fatalf("Bad: %#v", chk)
|
|
|
|
}
|
2015-01-06 18:40:00 +00:00
|
|
|
if err := agent.ServiceDeregister("foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_AgentServices_CheckBadStatus(t *testing.T) {
|
2015-05-12 01:53:09 +00:00
|
|
|
t.Parallel()
|
2015-04-12 00:53:48 +00:00
|
|
|
c, s := makeClient(t)
|
2015-05-12 01:53:09 +00:00
|
|
|
defer s.Stop()
|
2015-04-12 00:53:48 +00:00
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
Tags: []string{"bar", "baz"},
|
|
|
|
Port: 8000,
|
|
|
|
Check: &AgentServiceCheck{
|
|
|
|
TTL: "15s",
|
|
|
|
Status: "fluffy",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err == nil {
|
|
|
|
t.Fatalf("bad status accepted")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 20:59:55 +00:00
|
|
|
func TestAPI_AgentServices_CheckID(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
Tags: []string{"bar", "baz"},
|
|
|
|
Port: 8000,
|
|
|
|
Check: &AgentServiceCheck{
|
|
|
|
CheckID: "foo-ttl",
|
|
|
|
TTL: "15s",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
checks, err := agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, ok := checks["foo-ttl"]; !ok {
|
|
|
|
t.Fatalf("missing check: %v", checks)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_AgentServiceAddress(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-22 10:50:20 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-22 10:50:20 +00:00
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
reg1 := &AgentServiceRegistration{
|
|
|
|
Name: "foo1",
|
|
|
|
Port: 8000,
|
|
|
|
Address: "192.168.0.42",
|
|
|
|
}
|
|
|
|
reg2 := &AgentServiceRegistration{
|
2015-01-22 21:42:22 +00:00
|
|
|
Name: "foo2",
|
|
|
|
Port: 8000,
|
2015-01-22 10:50:20 +00:00
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg1); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg2); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
services, err := agent.Services()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-01-22 21:42:22 +00:00
|
|
|
|
2015-01-22 10:50:20 +00:00
|
|
|
if _, ok := services["foo1"]; !ok {
|
|
|
|
t.Fatalf("missing service: %v", services)
|
|
|
|
}
|
|
|
|
if _, ok := services["foo2"]; !ok {
|
|
|
|
t.Fatalf("missing service: %v", services)
|
|
|
|
}
|
|
|
|
|
|
|
|
if services["foo1"].Address != "192.168.0.42" {
|
|
|
|
t.Fatalf("missing Address field in service foo1: %v", services)
|
|
|
|
}
|
|
|
|
if services["foo2"].Address != "" {
|
|
|
|
t.Fatalf("missing Address field in service foo2: %v", services)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := agent.ServiceDeregister("foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_AgentEnableTagOverride(t *testing.T) {
|
2016-02-16 19:45:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
reg1 := &AgentServiceRegistration{
|
|
|
|
Name: "foo1",
|
|
|
|
Port: 8000,
|
|
|
|
Address: "192.168.0.42",
|
|
|
|
EnableTagOverride: true,
|
|
|
|
}
|
|
|
|
reg2 := &AgentServiceRegistration{
|
|
|
|
Name: "foo2",
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg1); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg2); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
services, err := agent.Services()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := services["foo1"]; !ok {
|
|
|
|
t.Fatalf("missing service: %v", services)
|
|
|
|
}
|
|
|
|
if services["foo1"].EnableTagOverride != true {
|
|
|
|
t.Fatalf("tag override not set on service foo1: %v", services)
|
|
|
|
}
|
|
|
|
if _, ok := services["foo2"]; !ok {
|
|
|
|
t.Fatalf("missing service: %v", services)
|
|
|
|
}
|
|
|
|
if services["foo2"].EnableTagOverride != false {
|
|
|
|
t.Fatalf("tag override set on service foo2: %v", services)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_AgentServices_MultipleChecks(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-14 03:18:46 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-14 03:18:46 +00:00
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
Tags: []string{"bar", "baz"},
|
|
|
|
Port: 8000,
|
|
|
|
Checks: AgentServiceChecks{
|
|
|
|
&AgentServiceCheck{
|
|
|
|
TTL: "15s",
|
|
|
|
},
|
|
|
|
&AgentServiceCheck{
|
|
|
|
TTL: "30s",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
services, err := agent.Services()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, ok := services["foo"]; !ok {
|
|
|
|
t.Fatalf("missing service: %v", services)
|
|
|
|
}
|
|
|
|
|
|
|
|
checks, err := agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, ok := checks["service:foo:1"]; !ok {
|
|
|
|
t.Fatalf("missing check: %v", checks)
|
|
|
|
}
|
|
|
|
if _, ok := checks["service:foo:2"]; !ok {
|
|
|
|
t.Fatalf("missing check: %v", checks)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_AgentSetTTLStatus(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
Check: &AgentServiceCheck{
|
|
|
|
TTL: "15s",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-03-04 23:18:25 +00:00
|
|
|
verify := func(status, output string) {
|
|
|
|
checks, err := agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
chk, ok := checks["service:foo"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing check: %v", checks)
|
|
|
|
}
|
|
|
|
if chk.Status != status {
|
|
|
|
t.Fatalf("Bad: %#v", chk)
|
|
|
|
}
|
|
|
|
if chk.Output != output {
|
|
|
|
t.Fatalf("Bad: %#v", chk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := agent.WarnTTL("service:foo", "foo"); err != nil {
|
2015-01-06 18:40:00 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-04-23 23:01:59 +00:00
|
|
|
verify(HealthWarning, "foo")
|
2015-01-06 18:40:00 +00:00
|
|
|
|
2016-03-04 23:18:25 +00:00
|
|
|
if err := agent.PassTTL("service:foo", "bar"); err != nil {
|
2015-01-06 18:40:00 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-04-23 23:01:59 +00:00
|
|
|
verify(HealthPassing, "bar")
|
2016-03-04 23:18:25 +00:00
|
|
|
|
|
|
|
if err := agent.FailTTL("service:foo", "baz"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
2016-04-23 23:01:59 +00:00
|
|
|
verify(HealthCritical, "baz")
|
2016-03-04 23:18:25 +00:00
|
|
|
|
|
|
|
if err := agent.UpdateTTL("service:foo", "foo", "warn"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
2016-04-23 23:01:59 +00:00
|
|
|
verify(HealthWarning, "foo")
|
2016-03-04 23:18:25 +00:00
|
|
|
|
|
|
|
if err := agent.UpdateTTL("service:foo", "bar", "pass"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-04-23 23:01:59 +00:00
|
|
|
verify(HealthPassing, "bar")
|
2016-03-04 23:18:25 +00:00
|
|
|
|
|
|
|
if err := agent.UpdateTTL("service:foo", "baz", "fail"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-04-23 23:01:59 +00:00
|
|
|
verify(HealthCritical, "baz")
|
2016-03-04 23:18:25 +00:00
|
|
|
|
2016-04-23 23:01:59 +00:00
|
|
|
if err := agent.UpdateTTL("service:foo", "foo", HealthWarning); err != nil {
|
2016-03-04 23:18:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-04-23 23:01:59 +00:00
|
|
|
verify(HealthWarning, "foo")
|
2016-03-04 23:18:25 +00:00
|
|
|
|
2016-04-23 23:01:59 +00:00
|
|
|
if err := agent.UpdateTTL("service:foo", "bar", HealthPassing); err != nil {
|
2016-03-04 23:18:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-04-23 23:01:59 +00:00
|
|
|
verify(HealthPassing, "bar")
|
2016-03-04 23:18:25 +00:00
|
|
|
|
2016-04-23 23:01:59 +00:00
|
|
|
if err := agent.UpdateTTL("service:foo", "baz", HealthCritical); err != nil {
|
2016-03-04 23:18:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
2016-04-23 23:01:59 +00:00
|
|
|
verify(HealthCritical, "baz")
|
2015-01-06 18:40:00 +00:00
|
|
|
|
|
|
|
if err := agent.ServiceDeregister("foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_AgentChecks(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
reg := &AgentCheckRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
}
|
|
|
|
reg.TTL = "15s"
|
|
|
|
if err := agent.CheckRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
checks, err := agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-04-12 00:53:48 +00:00
|
|
|
chk, ok := checks["foo"]
|
|
|
|
if !ok {
|
2015-01-06 18:40:00 +00:00
|
|
|
t.Fatalf("missing check: %v", checks)
|
|
|
|
}
|
2016-04-23 23:01:59 +00:00
|
|
|
if chk.Status != HealthCritical {
|
2015-04-12 00:53:48 +00:00
|
|
|
t.Fatalf("check not critical: %v", chk)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := agent.CheckDeregister("foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 18:28:39 +00:00
|
|
|
func TestAPI_AgentScriptCheck(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClientWithConfig(t, nil, func(c *testutil.TestServerConfig) {
|
|
|
|
c.EnableScriptChecks = true
|
|
|
|
})
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
t.Run("node script check", func(t *testing.T) {
|
|
|
|
reg := &AgentCheckRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
AgentServiceCheck: AgentServiceCheck{
|
|
|
|
Interval: "10s",
|
|
|
|
Args: []string{"sh", "-c", "false"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := agent.CheckRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
checks, err := agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, ok := checks["foo"]; !ok {
|
|
|
|
t.Fatalf("missing check: %v", checks)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("service script check", func(t *testing.T) {
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "bar",
|
|
|
|
Port: 1234,
|
|
|
|
Checks: AgentServiceChecks{
|
|
|
|
&AgentServiceCheck{
|
|
|
|
Interval: "10s",
|
|
|
|
Args: []string{"sh", "-c", "false"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
services, err := agent.Services()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, ok := services["bar"]; !ok {
|
|
|
|
t.Fatalf("missing service: %v", services)
|
|
|
|
}
|
|
|
|
|
|
|
|
checks, err := agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, ok := checks["service:bar"]; !ok {
|
|
|
|
t.Fatalf("missing check: %v", checks)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_AgentCheckStartPassing(t *testing.T) {
|
2015-05-12 01:53:09 +00:00
|
|
|
t.Parallel()
|
2015-04-12 00:53:48 +00:00
|
|
|
c, s := makeClient(t)
|
2015-05-12 01:53:09 +00:00
|
|
|
defer s.Stop()
|
2015-04-12 00:53:48 +00:00
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
reg := &AgentCheckRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
AgentServiceCheck: AgentServiceCheck{
|
2016-04-23 23:01:59 +00:00
|
|
|
Status: HealthPassing,
|
2015-04-12 00:53:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
reg.TTL = "15s"
|
|
|
|
if err := agent.CheckRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
checks, err := agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
chk, ok := checks["foo"]
|
|
|
|
if !ok {
|
2015-01-06 18:40:00 +00:00
|
|
|
t.Fatalf("missing check: %v", checks)
|
|
|
|
}
|
2016-04-23 23:01:59 +00:00
|
|
|
if chk.Status != HealthPassing {
|
2015-04-12 00:53:48 +00:00
|
|
|
t.Fatalf("check not passing: %v", chk)
|
|
|
|
}
|
2015-01-06 18:40:00 +00:00
|
|
|
|
|
|
|
if err := agent.CheckDeregister("foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_AgentChecks_serviceBound(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-14 03:18:46 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-14 03:18:46 +00:00
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
// First register a service
|
|
|
|
serviceReg := &AgentServiceRegistration{
|
|
|
|
Name: "redis",
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(serviceReg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a check bound to the service
|
|
|
|
reg := &AgentCheckRegistration{
|
|
|
|
Name: "redischeck",
|
|
|
|
ServiceID: "redis",
|
|
|
|
}
|
|
|
|
reg.TTL = "15s"
|
2016-08-16 07:05:55 +00:00
|
|
|
reg.DeregisterCriticalServiceAfter = "nope"
|
|
|
|
err := agent.CheckRegister(reg)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "invalid duration") {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
reg.DeregisterCriticalServiceAfter = "90m"
|
2015-01-14 03:18:46 +00:00
|
|
|
if err := agent.CheckRegister(reg); err != nil {
|
2015-11-18 15:40:02 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
checks, err := agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
check, ok := checks["redischeck"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing check: %v", checks)
|
|
|
|
}
|
|
|
|
if check.ServiceID != "redis" {
|
|
|
|
t.Fatalf("missing service association for check: %v", check)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_AgentChecks_Docker(t *testing.T) {
|
2015-11-18 15:40:02 +00:00
|
|
|
t.Parallel()
|
2017-07-17 18:20:35 +00:00
|
|
|
c, s := makeClientWithConfig(t, nil, func(c *testutil.TestServerConfig) {
|
|
|
|
c.EnableScriptChecks = true
|
|
|
|
})
|
2015-11-18 15:40:02 +00:00
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
// First register a service
|
|
|
|
serviceReg := &AgentServiceRegistration{
|
|
|
|
Name: "redis",
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(serviceReg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a check bound to the service
|
|
|
|
reg := &AgentCheckRegistration{
|
|
|
|
Name: "redischeck",
|
|
|
|
ServiceID: "redis",
|
|
|
|
AgentServiceCheck: AgentServiceCheck{
|
|
|
|
DockerContainerID: "f972c95ebf0e",
|
2018-05-08 22:31:53 +00:00
|
|
|
Args: []string{"/bin/true"},
|
2015-11-18 15:40:02 +00:00
|
|
|
Shell: "/bin/bash",
|
|
|
|
Interval: "10s",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := agent.CheckRegister(reg); err != nil {
|
2015-01-14 03:18:46 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
checks, err := agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
check, ok := checks["redischeck"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing check: %v", checks)
|
|
|
|
}
|
|
|
|
if check.ServiceID != "redis" {
|
|
|
|
t.Fatalf("missing service association for check: %v", check)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_AgentJoin(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
info, err := agent.Self()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join ourself
|
2017-10-04 17:43:17 +00:00
|
|
|
addr := info["DebugConfig"]["SerfAdvertiseAddrLAN"].(string)
|
2017-09-27 18:47:40 +00:00
|
|
|
// strip off 'tcp://'
|
|
|
|
addr = addr[len("tcp://"):]
|
2015-01-06 18:40:00 +00:00
|
|
|
err = agent.Join(addr, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_AgentLeave(t *testing.T) {
|
2016-11-30 18:29:42 +00:00
|
|
|
t.Parallel()
|
|
|
|
c1, s1 := makeClient(t)
|
|
|
|
defer s1.Stop()
|
|
|
|
|
|
|
|
c2, s2 := makeClientWithConfig(t, nil, func(conf *testutil.TestServerConfig) {
|
|
|
|
conf.Server = false
|
|
|
|
conf.Bootstrap = false
|
|
|
|
})
|
|
|
|
defer s2.Stop()
|
|
|
|
|
|
|
|
if err := c2.Agent().Join(s1.LANAddr, false); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-01-27 06:22:18 +00:00
|
|
|
// We sometimes see an EOF response to this one, depending on timing.
|
|
|
|
err := c2.Agent().Leave()
|
2017-03-22 16:56:53 +00:00
|
|
|
if err != nil && !strings.Contains(err.Error(), "EOF") {
|
2016-11-30 18:29:42 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the second agent's status is 'Left'
|
|
|
|
members, err := c1.Agent().Members(false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
member := members[0]
|
|
|
|
if member.Name == s1.Config.NodeName {
|
|
|
|
member = members[1]
|
|
|
|
}
|
|
|
|
if member.Status != int(serf.StatusLeft) {
|
|
|
|
t.Fatalf("bad: %v", *member)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_AgentForceLeave(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
// Eject somebody
|
|
|
|
err := agent.ForceLeave("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2015-01-21 18:51:26 +00:00
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_AgentMonitor(t *testing.T) {
|
2016-11-16 21:45:26 +00:00
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
2016-11-28 21:13:49 +00:00
|
|
|
logCh, err := agent.Monitor("info", nil, nil)
|
2016-11-16 21:45:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the first log message and validate it
|
|
|
|
select {
|
|
|
|
case log := <-logCh:
|
2017-01-18 06:20:11 +00:00
|
|
|
if !strings.Contains(log, "[INFO]") {
|
2016-11-16 21:45:26 +00:00
|
|
|
t.Fatalf("bad: %q", log)
|
|
|
|
}
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatalf("failed to get a log message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 20:58:55 +00:00
|
|
|
func TestAPI_ServiceMaintenance(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-21 18:51:26 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-21 18:51:26 +00:00
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
// First register a service
|
|
|
|
serviceReg := &AgentServiceRegistration{
|
|
|
|
Name: "redis",
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(serviceReg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable maintenance mode
|
2015-01-21 21:02:47 +00:00
|
|
|
if err := agent.EnableServiceMaintenance("redis", "broken"); err != nil {
|
2015-01-21 18:51:26 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a critical check was added
|
|
|
|
checks, err := agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
for _, check := range checks {
|
2015-01-21 19:08:57 +00:00
|
|
|
if strings.Contains(check.CheckID, "maintenance") {
|
2015-01-21 18:51:26 +00:00
|
|
|
found = true
|
2016-04-23 23:01:59 +00:00
|
|
|
if check.Status != HealthCritical || check.Notes != "broken" {
|
2015-01-21 18:51:26 +00:00
|
|
|
t.Fatalf("bad: %#v", checks)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("bad: %#v", checks)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable maintenance mode
|
|
|
|
if err := agent.DisableServiceMaintenance("redis"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the critical health check was removed
|
|
|
|
checks, err = agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
for _, check := range checks {
|
2015-01-21 19:08:57 +00:00
|
|
|
if strings.Contains(check.CheckID, "maintenance") {
|
|
|
|
t.Fatalf("should have removed health check")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 20:58:55 +00:00
|
|
|
func TestAPI_NodeMaintenance(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-21 19:08:57 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-21 19:08:57 +00:00
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
// Enable maintenance mode
|
2015-01-21 21:02:47 +00:00
|
|
|
if err := agent.EnableNodeMaintenance("broken"); err != nil {
|
2015-01-21 19:08:57 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that a critical check was added
|
|
|
|
checks, err := agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
for _, check := range checks {
|
|
|
|
if strings.Contains(check.CheckID, "maintenance") {
|
|
|
|
found = true
|
2016-04-23 23:01:59 +00:00
|
|
|
if check.Status != HealthCritical || check.Notes != "broken" {
|
2015-01-21 19:08:57 +00:00
|
|
|
t.Fatalf("bad: %#v", checks)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("bad: %#v", checks)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable maintenance mode
|
|
|
|
if err := agent.DisableNodeMaintenance(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the check was removed
|
|
|
|
checks, err = agent.Checks()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
for _, check := range checks {
|
|
|
|
if strings.Contains(check.CheckID, "maintenance") {
|
2015-01-21 18:51:26 +00:00
|
|
|
t.Fatalf("should have removed health check")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-26 18:03:43 +00:00
|
|
|
|
|
|
|
func TestAPI_AgentUpdateToken(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeACLClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
if _, err := agent.UpdateACLToken("root", nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := agent.UpdateACLAgentToken("root", nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := agent.UpdateACLAgentMasterToken("root", nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-08-03 22:39:31 +00:00
|
|
|
|
|
|
|
if _, err := agent.UpdateACLReplicationToken("root", nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|
2018-03-27 23:50:17 +00:00
|
|
|
|
|
|
|
func TestAPI_AgentConnectCARoots_empty(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require := require.New(t)
|
2018-05-10 16:14:16 +00:00
|
|
|
c, s := makeClientWithConfig(t, nil, func(c *testutil.TestServerConfig) {
|
|
|
|
c.Connect = nil // disable connect to prevent CA beening bootstrapped
|
|
|
|
})
|
2018-03-27 23:50:17 +00:00
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
list, meta, err := agent.ConnectCARoots(nil)
|
2018-04-26 13:01:20 +00:00
|
|
|
require.NoError(err)
|
2018-03-27 23:50:17 +00:00
|
|
|
require.Equal(uint64(0), meta.LastIndex)
|
|
|
|
require.Len(list.Roots, 0)
|
|
|
|
}
|
2018-04-05 11:53:42 +00:00
|
|
|
|
2018-04-26 13:01:20 +00:00
|
|
|
func TestAPI_AgentConnectCARoots_list(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require := require.New(t)
|
2018-05-10 16:14:16 +00:00
|
|
|
c, s := makeClient(t)
|
2018-04-26 13:01:20 +00:00
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
list, meta, err := agent.ConnectCARoots(nil)
|
|
|
|
require.NoError(err)
|
|
|
|
require.True(meta.LastIndex > 0)
|
|
|
|
require.Len(list.Roots, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPI_AgentConnectCALeaf(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require := require.New(t)
|
2018-05-10 16:14:16 +00:00
|
|
|
c, s := makeClient(t)
|
2018-04-26 13:01:20 +00:00
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
// Setup service
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
Tags: []string{"bar", "baz"},
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
require.NoError(agent.ServiceRegister(reg))
|
|
|
|
|
|
|
|
leaf, meta, err := agent.ConnectCALeaf("foo", nil)
|
|
|
|
require.NoError(err)
|
|
|
|
require.True(meta.LastIndex > 0)
|
|
|
|
// Sanity checks here as we have actual certificate validation checks at many
|
|
|
|
// other levels.
|
|
|
|
require.NotEmpty(leaf.SerialNumber)
|
|
|
|
require.NotEmpty(leaf.CertPEM)
|
|
|
|
require.NotEmpty(leaf.PrivateKeyPEM)
|
|
|
|
require.Equal("foo", leaf.Service)
|
|
|
|
require.True(strings.HasSuffix(leaf.ServiceURI, "/svc/foo"))
|
|
|
|
require.True(leaf.ModifyIndex > 0)
|
|
|
|
require.True(leaf.ValidAfter.Before(time.Now()))
|
|
|
|
require.True(leaf.ValidBefore.After(time.Now()))
|
|
|
|
}
|
|
|
|
|
2018-04-05 11:53:42 +00:00
|
|
|
func TestAPI_AgentConnectAuthorize(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
params := &AgentAuthorizeParams{
|
|
|
|
Target: "foo",
|
|
|
|
ClientCertSerial: "fake",
|
|
|
|
// Importing connect.TestSpiffeIDService creates an import cycle
|
2018-05-10 16:04:33 +00:00
|
|
|
ClientCertURI: "spiffe://11111111-2222-3333-4444-555555555555.consul/ns/default/dc/ny1/svc/web",
|
2018-04-05 11:53:42 +00:00
|
|
|
}
|
|
|
|
auth, err := agent.ConnectAuthorize(params)
|
|
|
|
require.Nil(err)
|
|
|
|
require.True(auth.Authorized)
|
|
|
|
require.Equal(auth.Reason, "ACLs disabled, access is allowed by default")
|
|
|
|
}
|
2018-04-20 13:24:24 +00:00
|
|
|
|
|
|
|
func TestAPI_AgentConnectProxyConfig(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-04-26 13:01:20 +00:00
|
|
|
c, s := makeClientWithConfig(t, nil, func(c *testutil.TestServerConfig) {
|
|
|
|
// Force auto port range to 1 port so we have deterministic response.
|
|
|
|
c.Connect = map[string]interface{}{
|
|
|
|
"proxy_defaults": map[string]interface{}{
|
|
|
|
"bind_min_port": 20000,
|
|
|
|
"bind_max_port": 20000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
2018-04-20 13:24:24 +00:00
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
Tags: []string{"bar", "baz"},
|
|
|
|
Port: 8000,
|
2018-04-05 16:15:43 +00:00
|
|
|
Connect: &AgentServiceConnect{
|
|
|
|
Proxy: &AgentServiceConnectProxy{
|
2018-05-10 16:14:16 +00:00
|
|
|
Command: []string{"consul connect proxy"},
|
2018-04-05 16:15:43 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
2018-04-20 13:24:24 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-04-05 16:15:43 +00:00
|
|
|
config, qm, err := agent.ConnectProxyConfig("foo-proxy", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
expectConfig := &ConnectProxyConfig{
|
|
|
|
ProxyServiceID: "foo-proxy",
|
|
|
|
TargetServiceID: "foo",
|
|
|
|
TargetServiceName: "foo",
|
2018-05-10 16:14:16 +00:00
|
|
|
ContentHash: "93baee1d838888ae",
|
2018-04-05 16:15:43 +00:00
|
|
|
ExecMode: "daemon",
|
2018-05-12 00:19:54 +00:00
|
|
|
Command: []string{"consul", "connect", "proxy"},
|
2018-04-05 16:15:43 +00:00
|
|
|
Config: map[string]interface{}{
|
2018-04-26 13:01:20 +00:00
|
|
|
"bind_address": "127.0.0.1",
|
|
|
|
"bind_port": float64(20000),
|
|
|
|
"foo": "bar",
|
|
|
|
"local_service_address": "127.0.0.1:8000",
|
2018-04-05 16:15:43 +00:00
|
|
|
},
|
2018-04-20 13:24:24 +00:00
|
|
|
}
|
2018-04-05 16:15:43 +00:00
|
|
|
require.Equal(t, expectConfig, config)
|
2018-05-10 16:14:16 +00:00
|
|
|
require.Equal(t, expectConfig.ContentHash, qm.LastContentHash)
|
2018-04-20 13:24:24 +00:00
|
|
|
}
|