2015-03-03 02:18:38 +00:00
|
|
|
package testutil
|
|
|
|
|
2015-03-11 04:38:31 +00:00
|
|
|
// TestServer is a test helper. It uses a fork/exec model to create
|
|
|
|
// a test Consul server instance in the background and initialize it
|
|
|
|
// with some data and/or services. The test server can then be used
|
|
|
|
// to run a unit test, and offers an easy API to tear itself down
|
|
|
|
// when the test has completed. The only prerequisite is to have a consul
|
|
|
|
// binary available on the $PATH.
|
|
|
|
//
|
|
|
|
// This package does not use Consul's official API client. This is
|
|
|
|
// because we use TestServer to test the API client, which would
|
|
|
|
// otherwise cause an import cycle.
|
|
|
|
|
2015-03-03 02:18:38 +00:00
|
|
|
import (
|
2017-02-11 02:11:21 +00:00
|
|
|
"context"
|
2015-03-03 02:18:38 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-03-11 01:08:14 +00:00
|
|
|
"io"
|
2015-03-03 02:18:38 +00:00
|
|
|
"io/ioutil"
|
2015-03-20 00:44:04 +00:00
|
|
|
"net"
|
2015-03-03 02:18:38 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2017-05-12 13:41:13 +00:00
|
|
|
"path/filepath"
|
2020-01-22 18:45:34 +00:00
|
|
|
"runtime"
|
2017-01-27 00:55:49 +00:00
|
|
|
"strconv"
|
2015-03-20 00:44:04 +00:00
|
|
|
"strings"
|
2017-05-12 13:41:13 +00:00
|
|
|
"testing"
|
2017-05-05 11:48:34 +00:00
|
|
|
"time"
|
2015-10-22 14:47:50 +00:00
|
|
|
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/freeport"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2015-10-22 18:14:22 +00:00
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
2017-03-27 08:28:54 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
2017-03-23 20:26:05 +00:00
|
|
|
"github.com/pkg/errors"
|
2015-03-03 02:18:38 +00:00
|
|
|
)
|
|
|
|
|
2016-08-25 00:33:53 +00:00
|
|
|
// TestPerformanceConfig configures the performance parameters.
|
|
|
|
type TestPerformanceConfig struct {
|
|
|
|
RaftMultiplier uint `json:"raft_multiplier,omitempty"`
|
|
|
|
}
|
|
|
|
|
2015-03-11 04:53:51 +00:00
|
|
|
// TestPortConfig configures the various ports used for services
|
|
|
|
// provided by the Consul server.
|
2015-03-03 02:18:38 +00:00
|
|
|
type TestPortConfig struct {
|
2018-06-11 20:25:13 +00:00
|
|
|
DNS int `json:"dns,omitempty"`
|
|
|
|
HTTP int `json:"http,omitempty"`
|
|
|
|
HTTPS int `json:"https,omitempty"`
|
|
|
|
SerfLan int `json:"serf_lan,omitempty"`
|
|
|
|
SerfWan int `json:"serf_wan,omitempty"`
|
|
|
|
Server int `json:"server,omitempty"`
|
|
|
|
ProxyMinPort int `json:"proxy_min_port,omitempty"`
|
|
|
|
ProxyMaxPort int `json:"proxy_max_port,omitempty"`
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 04:53:51 +00:00
|
|
|
// TestAddressConfig contains the bind addresses for various
|
|
|
|
// components of the Consul server.
|
2015-03-03 02:18:38 +00:00
|
|
|
type TestAddressConfig struct {
|
|
|
|
HTTP string `json:"http,omitempty"`
|
|
|
|
}
|
|
|
|
|
2017-08-14 14:36:07 +00:00
|
|
|
// TestNetworkSegment contains the configuration for a network segment.
|
|
|
|
type TestNetworkSegment struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Bind string `json:"bind"`
|
|
|
|
Port int `json:"port"`
|
|
|
|
Advertise string `json:"advertise"`
|
|
|
|
}
|
|
|
|
|
2015-03-11 04:53:51 +00:00
|
|
|
// TestServerConfig is the main server configuration struct.
|
2015-03-03 02:18:38 +00:00
|
|
|
type TestServerConfig struct {
|
2017-04-28 23:15:55 +00:00
|
|
|
NodeName string `json:"node_name"`
|
|
|
|
NodeID string `json:"node_id"`
|
|
|
|
NodeMeta map[string]string `json:"node_meta,omitempty"`
|
|
|
|
Performance *TestPerformanceConfig `json:"performance,omitempty"`
|
|
|
|
Bootstrap bool `json:"bootstrap,omitempty"`
|
|
|
|
Server bool `json:"server,omitempty"`
|
|
|
|
DataDir string `json:"data_dir,omitempty"`
|
|
|
|
Datacenter string `json:"datacenter,omitempty"`
|
2017-08-14 14:36:07 +00:00
|
|
|
Segments []TestNetworkSegment `json:"segments"`
|
2017-04-28 23:15:55 +00:00
|
|
|
DisableCheckpoint bool `json:"disable_update_check"`
|
|
|
|
LogLevel string `json:"log_level,omitempty"`
|
|
|
|
Bind string `json:"bind_addr,omitempty"`
|
|
|
|
Addresses *TestAddressConfig `json:"addresses,omitempty"`
|
|
|
|
Ports *TestPortConfig `json:"ports,omitempty"`
|
|
|
|
RaftProtocol int `json:"raft_protocol,omitempty"`
|
|
|
|
ACLMasterToken string `json:"acl_master_token,omitempty"`
|
|
|
|
ACLDatacenter string `json:"acl_datacenter,omitempty"`
|
2018-10-19 16:04:07 +00:00
|
|
|
PrimaryDatacenter string `json:"primary_datacenter,omitempty"`
|
2017-04-28 23:15:55 +00:00
|
|
|
ACLDefaultPolicy string `json:"acl_default_policy,omitempty"`
|
2018-10-19 16:28:36 +00:00
|
|
|
ACL TestACLs `json:"acl,omitempty"`
|
2017-04-28 23:15:55 +00:00
|
|
|
Encrypt string `json:"encrypt,omitempty"`
|
|
|
|
CAFile string `json:"ca_file,omitempty"`
|
|
|
|
CertFile string `json:"cert_file,omitempty"`
|
|
|
|
KeyFile string `json:"key_file,omitempty"`
|
|
|
|
VerifyIncoming bool `json:"verify_incoming,omitempty"`
|
|
|
|
VerifyIncomingRPC bool `json:"verify_incoming_rpc,omitempty"`
|
|
|
|
VerifyIncomingHTTPS bool `json:"verify_incoming_https,omitempty"`
|
|
|
|
VerifyOutgoing bool `json:"verify_outgoing,omitempty"`
|
2017-07-17 18:20:35 +00:00
|
|
|
EnableScriptChecks bool `json:"enable_script_checks,omitempty"`
|
2018-04-26 13:01:20 +00:00
|
|
|
Connect map[string]interface{} `json:"connect,omitempty"`
|
2018-10-17 20:20:35 +00:00
|
|
|
EnableDebug bool `json:"enable_debug,omitempty"`
|
2017-05-09 13:12:34 +00:00
|
|
|
ReadyTimeout time.Duration `json:"-"`
|
2017-04-28 23:15:55 +00:00
|
|
|
Stdout, Stderr io.Writer `json:"-"`
|
|
|
|
Args []string `json:"-"`
|
2019-08-27 21:16:41 +00:00
|
|
|
ReturnPorts func() `json:"-"`
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
type TestACLs struct {
|
|
|
|
Enabled bool `json:"enabled,omitempty"`
|
|
|
|
TokenReplication bool `json:"enable_token_replication,omitempty"`
|
|
|
|
PolicyTTL string `json:"policy_ttl,omitempty"`
|
|
|
|
TokenTTL string `json:"token_ttl,omitempty"`
|
|
|
|
DownPolicy string `json:"down_policy,omitempty"`
|
|
|
|
DefaultPolicy string `json:"default_policy,omitempty"`
|
|
|
|
EnableKeyListPolicy bool `json:"enable_key_list_policy,omitempty"`
|
|
|
|
Tokens TestTokens `json:"tokens,omitempty"`
|
|
|
|
DisabledTTL string `json:"disabled_ttl,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestTokens struct {
|
|
|
|
Master string `json:"master,omitempty"`
|
|
|
|
Replication string `json:"replication,omitempty"`
|
|
|
|
AgentMaster string `json:"agent_master,omitempty"`
|
|
|
|
Default string `json:"default,omitempty"`
|
|
|
|
Agent string `json:"agent,omitempty"`
|
|
|
|
}
|
|
|
|
|
2015-03-11 04:53:51 +00:00
|
|
|
// ServerConfigCallback is a function interface which can be
|
|
|
|
// passed to NewTestServerConfig to modify the server config.
|
2015-03-03 02:18:38 +00:00
|
|
|
type ServerConfigCallback func(c *TestServerConfig)
|
|
|
|
|
2015-03-11 04:53:51 +00:00
|
|
|
// defaultServerConfig returns a new TestServerConfig struct
|
|
|
|
// with all of the listen ports incremented by one.
|
2015-03-03 02:18:38 +00:00
|
|
|
func defaultServerConfig() *TestServerConfig {
|
2017-03-27 08:28:54 +00:00
|
|
|
nodeID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:16:41 +00:00
|
|
|
ports := freeport.MustTake(6)
|
|
|
|
|
2015-03-03 02:18:38 +00:00
|
|
|
return &TestServerConfig{
|
2017-09-25 18:40:42 +00:00
|
|
|
NodeName: "node-" + nodeID,
|
2017-03-27 08:28:54 +00:00
|
|
|
NodeID: nodeID,
|
2015-03-24 02:27:59 +00:00
|
|
|
DisableCheckpoint: true,
|
2016-08-25 00:33:53 +00:00
|
|
|
Performance: &TestPerformanceConfig{
|
|
|
|
RaftMultiplier: 1,
|
|
|
|
},
|
|
|
|
Bootstrap: true,
|
|
|
|
Server: true,
|
|
|
|
LogLevel: "debug",
|
|
|
|
Bind: "127.0.0.1",
|
|
|
|
Addresses: &TestAddressConfig{},
|
2015-03-03 02:18:38 +00:00
|
|
|
Ports: &TestPortConfig{
|
2017-09-25 18:40:42 +00:00
|
|
|
DNS: ports[0],
|
|
|
|
HTTP: ports[1],
|
|
|
|
HTTPS: ports[2],
|
|
|
|
SerfLan: ports[3],
|
|
|
|
SerfWan: ports[4],
|
|
|
|
Server: ports[5],
|
2015-03-03 02:18:38 +00:00
|
|
|
},
|
2017-05-12 19:04:34 +00:00
|
|
|
ReadyTimeout: 10 * time.Second,
|
2018-05-10 16:04:33 +00:00
|
|
|
Connect: map[string]interface{}{
|
|
|
|
"enabled": true,
|
|
|
|
"ca_config": map[string]interface{}{
|
|
|
|
// const TestClusterID causes import cycle so hard code it here.
|
|
|
|
"cluster_id": "11111111-2222-3333-4444-555555555555",
|
|
|
|
},
|
|
|
|
},
|
2019-08-27 21:16:41 +00:00
|
|
|
ReturnPorts: func() {
|
|
|
|
freeport.Return(ports)
|
|
|
|
},
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 04:53:51 +00:00
|
|
|
// TestService is used to serialize a service definition.
|
2015-03-11 01:47:45 +00:00
|
|
|
type TestService struct {
|
|
|
|
ID string `json:",omitempty"`
|
|
|
|
Name string `json:",omitempty"`
|
|
|
|
Tags []string `json:",omitempty"`
|
|
|
|
Address string `json:",omitempty"`
|
|
|
|
Port int `json:",omitempty"`
|
|
|
|
}
|
|
|
|
|
2015-03-11 04:53:51 +00:00
|
|
|
// TestCheck is used to serialize a check definition.
|
2015-03-11 01:47:45 +00:00
|
|
|
type TestCheck struct {
|
|
|
|
ID string `json:",omitempty"`
|
|
|
|
Name string `json:",omitempty"`
|
|
|
|
ServiceID string `json:",omitempty"`
|
|
|
|
TTL string `json:",omitempty"`
|
|
|
|
}
|
|
|
|
|
2015-03-11 23:10:07 +00:00
|
|
|
// TestKVResponse is what we use to decode KV data.
|
|
|
|
type TestKVResponse struct {
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2015-03-11 04:53:51 +00:00
|
|
|
// TestServer is the main server wrapper struct.
|
2015-03-03 02:18:38 +00:00
|
|
|
type TestServer struct {
|
2016-04-15 12:35:45 +00:00
|
|
|
cmd *exec.Cmd
|
2015-03-11 18:08:08 +00:00
|
|
|
Config *TestServerConfig
|
|
|
|
|
2017-04-14 20:37:29 +00:00
|
|
|
HTTPAddr string
|
|
|
|
HTTPSAddr string
|
|
|
|
LANAddr string
|
|
|
|
WANAddr string
|
2015-03-20 00:44:04 +00:00
|
|
|
|
2017-04-21 00:02:42 +00:00
|
|
|
HTTPClient *http.Client
|
2017-05-12 13:41:13 +00:00
|
|
|
|
|
|
|
tmpdir string
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 22:51:49 +00:00
|
|
|
// Deprecated: Use NewTestServerT instead.
|
2017-05-12 13:41:13 +00:00
|
|
|
func NewTestServer() (*TestServer, error) {
|
|
|
|
return NewTestServerConfigT(nil, nil)
|
|
|
|
}
|
|
|
|
|
2019-11-08 22:51:49 +00:00
|
|
|
// NewTestServerT is an easy helper method to create a new Consul
|
|
|
|
// test server with the most basic configuration.
|
|
|
|
func NewTestServerT(t *testing.T) (*TestServer, error) {
|
|
|
|
if t == nil {
|
|
|
|
return nil, errors.New("testutil: a non-nil *testing.T is required")
|
|
|
|
}
|
|
|
|
return NewTestServerConfigT(t, nil)
|
|
|
|
}
|
|
|
|
|
2017-05-12 13:41:13 +00:00
|
|
|
func NewTestServerConfig(cb ServerConfigCallback) (*TestServer, error) {
|
|
|
|
return NewTestServerConfigT(nil, cb)
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 20:26:05 +00:00
|
|
|
// NewTestServerConfig creates a new TestServer, and makes a call to an optional
|
|
|
|
// callback function to modify the configuration. If there is an error
|
|
|
|
// configuring or starting the server, the server will NOT be running when the
|
|
|
|
// function returns (thus you do not need to stop it).
|
2019-09-20 21:01:08 +00:00
|
|
|
func NewTestServerConfigT(t testing.TB, cb ServerConfigCallback) (*TestServer, error) {
|
2017-09-25 18:40:42 +00:00
|
|
|
return newTestServerConfigT(t, cb)
|
2017-07-05 05:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newTestServerConfigT is the internal helper for NewTestServerConfigT.
|
2019-09-20 21:01:08 +00:00
|
|
|
func newTestServerConfigT(t testing.TB, cb ServerConfigCallback) (*TestServer, error) {
|
2017-05-12 13:41:13 +00:00
|
|
|
path, err := exec.LookPath("consul")
|
|
|
|
if err != nil || path == "" {
|
2017-03-23 20:26:05 +00:00
|
|
|
return nil, fmt.Errorf("consul not found on $PATH - download and install " +
|
2017-01-18 00:20:29 +00:00
|
|
|
"consul or skip this test")
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 15:37:29 +00:00
|
|
|
prefix := "consul"
|
|
|
|
if t != nil {
|
|
|
|
// Use test name for tmpdir if available
|
|
|
|
prefix = strings.Replace(t.Name(), "/", "_", -1)
|
|
|
|
}
|
|
|
|
tmpdir, err := ioutil.TempDir("", prefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create tempdir")
|
|
|
|
}
|
|
|
|
|
2017-05-12 13:41:13 +00:00
|
|
|
cfg := defaultServerConfig()
|
2019-09-18 17:07:11 +00:00
|
|
|
testWriter := TestWriter(t)
|
|
|
|
cfg.Stdout = testWriter
|
|
|
|
cfg.Stderr = testWriter
|
2019-08-27 21:16:41 +00:00
|
|
|
|
2017-05-12 13:41:13 +00:00
|
|
|
cfg.DataDir = filepath.Join(tmpdir, "data")
|
2015-03-03 02:18:38 +00:00
|
|
|
if cb != nil {
|
2017-05-12 13:41:13 +00:00
|
|
|
cb(cfg)
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 13:41:13 +00:00
|
|
|
b, err := json.Marshal(cfg)
|
2015-03-03 02:18:38 +00:00
|
|
|
if err != nil {
|
2019-08-27 21:16:41 +00:00
|
|
|
cfg.ReturnPorts()
|
2019-07-12 15:37:29 +00:00
|
|
|
os.RemoveAll(tmpdir)
|
2017-03-23 20:26:05 +00:00
|
|
|
return nil, errors.Wrap(err, "failed marshaling json")
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
2019-09-20 21:01:08 +00:00
|
|
|
|
2019-09-20 19:52:02 +00:00
|
|
|
if t != nil {
|
|
|
|
// if you really want this output ensure to pass a valid t
|
|
|
|
t.Logf("CONFIG JSON: %s", string(b))
|
|
|
|
}
|
2017-05-12 13:41:13 +00:00
|
|
|
configFile := filepath.Join(tmpdir, "config.json")
|
|
|
|
if err := ioutil.WriteFile(configFile, b, 0644); err != nil {
|
2019-08-27 21:16:41 +00:00
|
|
|
cfg.ReturnPorts()
|
2019-07-12 15:37:29 +00:00
|
|
|
os.RemoveAll(tmpdir)
|
2017-03-23 20:26:05 +00:00
|
|
|
return nil, errors.Wrap(err, "failed writing config content")
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
stdout := testWriter
|
2017-05-12 13:41:13 +00:00
|
|
|
if cfg.Stdout != nil {
|
|
|
|
stdout = cfg.Stdout
|
2015-06-08 14:16:11 +00:00
|
|
|
}
|
2019-09-18 17:07:11 +00:00
|
|
|
stderr := testWriter
|
2017-05-12 13:41:13 +00:00
|
|
|
if cfg.Stderr != nil {
|
|
|
|
stderr = cfg.Stderr
|
2015-06-08 14:16:11 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 02:18:38 +00:00
|
|
|
// Start the server
|
2017-05-12 13:41:13 +00:00
|
|
|
args := []string{"agent", "-config-file", configFile}
|
|
|
|
args = append(args, cfg.Args...)
|
2016-11-30 18:29:42 +00:00
|
|
|
cmd := exec.Command("consul", args...)
|
2015-06-08 14:16:11 +00:00
|
|
|
cmd.Stdout = stdout
|
|
|
|
cmd.Stderr = stderr
|
2015-03-03 02:18:38 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
2019-08-27 21:16:41 +00:00
|
|
|
cfg.ReturnPorts()
|
2019-07-12 15:37:29 +00:00
|
|
|
os.RemoveAll(tmpdir)
|
2017-03-23 20:26:05 +00:00
|
|
|
return nil, errors.Wrap(err, "failed starting command")
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 13:41:13 +00:00
|
|
|
httpAddr := fmt.Sprintf("127.0.0.1:%d", cfg.Ports.HTTP)
|
|
|
|
client := cleanhttp.DefaultClient()
|
|
|
|
if strings.HasPrefix(cfg.Addresses.HTTP, "unix://") {
|
|
|
|
httpAddr = cfg.Addresses.HTTP
|
|
|
|
tr := cleanhttp.DefaultTransport()
|
|
|
|
tr.DialContext = func(_ context.Context, _, _ string) (net.Conn, error) {
|
|
|
|
return net.Dial("unix", httpAddr[len("unix://"):])
|
2015-10-22 14:47:50 +00:00
|
|
|
}
|
2017-05-12 13:41:13 +00:00
|
|
|
client = &http.Client{Transport: tr}
|
2015-03-20 00:44:04 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 02:18:38 +00:00
|
|
|
server := &TestServer{
|
2017-05-12 13:41:13 +00:00
|
|
|
Config: cfg,
|
2016-04-15 12:35:45 +00:00
|
|
|
cmd: cmd,
|
2015-03-11 18:08:08 +00:00
|
|
|
|
2017-04-14 20:37:29 +00:00
|
|
|
HTTPAddr: httpAddr,
|
2017-05-12 13:41:13 +00:00
|
|
|
HTTPSAddr: fmt.Sprintf("127.0.0.1:%d", cfg.Ports.HTTPS),
|
|
|
|
LANAddr: fmt.Sprintf("127.0.0.1:%d", cfg.Ports.SerfLan),
|
|
|
|
WANAddr: fmt.Sprintf("127.0.0.1:%d", cfg.Ports.SerfWan),
|
2015-03-20 00:44:04 +00:00
|
|
|
|
2017-04-21 00:02:42 +00:00
|
|
|
HTTPClient: client,
|
2017-05-12 13:41:13 +00:00
|
|
|
|
|
|
|
tmpdir: tmpdir,
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 03:20:13 +00:00
|
|
|
// Wait for the server to be ready
|
2019-07-12 15:37:29 +00:00
|
|
|
if err := server.waitForAPI(); err != nil {
|
|
|
|
server.Stop()
|
|
|
|
return nil, err
|
2015-05-09 01:11:17 +00:00
|
|
|
}
|
2019-07-12 15:37:29 +00:00
|
|
|
|
2017-03-23 20:26:05 +00:00
|
|
|
return server, nil
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 04:53:51 +00:00
|
|
|
// Stop stops the test Consul server, and removes the Consul data
|
|
|
|
// directory once we are done.
|
2017-03-23 20:26:05 +00:00
|
|
|
func (s *TestServer) Stop() error {
|
2019-08-27 21:16:41 +00:00
|
|
|
defer s.Config.ReturnPorts()
|
2017-05-12 13:41:13 +00:00
|
|
|
defer os.RemoveAll(s.tmpdir)
|
2015-03-03 02:18:38 +00:00
|
|
|
|
2017-05-12 13:41:13 +00:00
|
|
|
// There was no process
|
|
|
|
if s.cmd == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-23 20:26:05 +00:00
|
|
|
|
2017-05-12 13:41:13 +00:00
|
|
|
if s.cmd.Process != nil {
|
2020-01-22 16:34:35 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
2020-01-22 18:45:34 +00:00
|
|
|
if err := s.cmd.Process.Kill(); err != nil {
|
2020-01-22 16:34:35 +00:00
|
|
|
return errors.Wrap(err, "failed to kill consul server")
|
|
|
|
}
|
|
|
|
} else { // interrupt is not supported in windows
|
2020-01-22 18:45:34 +00:00
|
|
|
if err := s.cmd.Process.Signal(os.Interrupt); err != nil {
|
2020-01-22 16:34:35 +00:00
|
|
|
return errors.Wrap(err, "failed to kill consul server")
|
|
|
|
}
|
2017-05-12 13:41:13 +00:00
|
|
|
}
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
2016-04-15 12:35:45 +00:00
|
|
|
|
2017-05-12 13:41:13 +00:00
|
|
|
// wait for the process to exit to be sure that the data dir can be
|
|
|
|
// deleted on all platforms.
|
|
|
|
return s.cmd.Wait()
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 01:11:17 +00:00
|
|
|
// waitForAPI waits for only the agent HTTP endpoint to start
|
|
|
|
// responding. This is an indication that the agent has started,
|
|
|
|
// but will likely return before a leader is elected.
|
2017-03-23 20:26:05 +00:00
|
|
|
func (s *TestServer) waitForAPI() error {
|
2019-07-12 15:37:29 +00:00
|
|
|
var failed bool
|
|
|
|
|
|
|
|
// This retry replicates the logic of retry.Run to allow for nested retries.
|
|
|
|
// By returning an error we can wrap TestServer creation with retry.Run
|
|
|
|
// in makeClientWithConfig.
|
|
|
|
timer := retry.TwoSeconds()
|
|
|
|
deadline := time.Now().Add(timer.Timeout)
|
|
|
|
for !time.Now().After(deadline) {
|
|
|
|
time.Sleep(timer.Wait)
|
|
|
|
|
2020-05-29 21:16:03 +00:00
|
|
|
url := s.url("/v1/agent/self")
|
|
|
|
resp, err := s.masterGet(url)
|
2015-05-09 01:11:17 +00:00
|
|
|
if err != nil {
|
2019-07-12 15:37:29 +00:00
|
|
|
failed = true
|
|
|
|
continue
|
2015-05-09 01:11:17 +00:00
|
|
|
}
|
2019-07-12 15:37:29 +00:00
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
if err = s.requireOK(resp); err != nil {
|
|
|
|
failed = true
|
|
|
|
continue
|
2015-05-09 01:11:17 +00:00
|
|
|
}
|
2019-07-12 15:37:29 +00:00
|
|
|
failed = false
|
|
|
|
}
|
|
|
|
if failed {
|
|
|
|
return fmt.Errorf("api unavailable")
|
2017-03-23 20:26:05 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-05-09 01:11:17 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 04:53:51 +00:00
|
|
|
// waitForLeader waits for the Consul server's HTTP API to become
|
|
|
|
// available, and then waits for a known leader and an index of
|
2019-07-12 15:37:29 +00:00
|
|
|
// 2 or more to be observed to confirm leader election is done.
|
|
|
|
func (s *TestServer) WaitForLeader(t *testing.T) {
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-01-27 00:55:49 +00:00
|
|
|
// Query the API and check the status code.
|
2019-07-12 15:37:29 +00:00
|
|
|
url := s.url("/v1/catalog/nodes")
|
2020-05-29 21:16:03 +00:00
|
|
|
resp, err := s.masterGet(url)
|
2015-03-24 02:27:59 +00:00
|
|
|
if err != nil {
|
2019-07-12 15:37:29 +00:00
|
|
|
r.Fatalf("failed http get '%s': %v", url, err)
|
2015-03-24 02:27:59 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if err := s.requireOK(resp); err != nil {
|
2017-05-05 11:48:34 +00:00
|
|
|
r.Fatal("failed OK response", err)
|
2015-03-24 02:27:59 +00:00
|
|
|
}
|
2015-03-03 02:18:38 +00:00
|
|
|
|
2017-01-27 00:55:49 +00:00
|
|
|
// Ensure we have a leader and a node registration.
|
2015-03-03 02:18:38 +00:00
|
|
|
if leader := resp.Header.Get("X-Consul-KnownLeader"); leader != "true" {
|
2017-05-05 11:48:34 +00:00
|
|
|
r.Fatalf("Consul leader status: %#v", leader)
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
2019-07-12 15:37:29 +00:00
|
|
|
index, err := strconv.ParseInt(resp.Header.Get("X-Consul-Index"), 10, 64)
|
2017-01-27 00:55:49 +00:00
|
|
|
if err != nil {
|
2017-05-05 11:48:34 +00:00
|
|
|
r.Fatal("bad consul index", err)
|
2017-01-27 00:55:49 +00:00
|
|
|
}
|
2019-07-12 15:37:29 +00:00
|
|
|
if index < 2 {
|
|
|
|
r.Fatal("consul index should be at least 2")
|
2017-01-26 05:13:03 +00:00
|
|
|
}
|
2017-05-05 11:48:34 +00:00
|
|
|
})
|
2015-03-11 01:08:14 +00:00
|
|
|
}
|
2018-09-18 16:47:01 +00:00
|
|
|
|
2020-01-20 14:56:56 +00:00
|
|
|
// WaitForActiveCARoot waits until the server can return a Connect CA meaning
|
|
|
|
// connect has completed bootstrapping and is ready to use.
|
|
|
|
func (s *TestServer) WaitForActiveCARoot(t *testing.T) {
|
2020-01-27 19:34:04 +00:00
|
|
|
// don't need to fully decode the response
|
|
|
|
type rootsResponse struct {
|
|
|
|
ActiveRootID string
|
|
|
|
TrustDomain string
|
|
|
|
Roots []interface{}
|
|
|
|
}
|
|
|
|
|
2020-01-20 14:56:56 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
// Query the API and check the status code.
|
|
|
|
url := s.url("/v1/agent/connect/ca/roots")
|
2020-05-29 21:16:03 +00:00
|
|
|
resp, err := s.masterGet(url)
|
2020-01-20 14:56:56 +00:00
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("failed http get '%s': %v", url, err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
// Roots will return an error status until it's been bootstrapped. We could
|
|
|
|
// parse the body and sanity check but that causes either import cycles
|
|
|
|
// since this is used in both `api` and consul test or duplication. The 200
|
|
|
|
// is all we really need to wait for.
|
|
|
|
if err := s.requireOK(resp); err != nil {
|
|
|
|
r.Fatal("failed OK response", err)
|
|
|
|
}
|
2020-01-27 19:34:04 +00:00
|
|
|
|
|
|
|
var roots rootsResponse
|
|
|
|
|
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
if err := dec.Decode(&roots); err != nil {
|
|
|
|
r.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if roots.ActiveRootID == "" || len(roots.Roots) < 1 {
|
|
|
|
r.Fatalf("/v1/agent/connect/ca/roots returned 200 but without roots: %+v", roots)
|
|
|
|
}
|
2020-01-20 14:56:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-18 16:47:01 +00:00
|
|
|
// WaitForSerfCheck ensures we have a node with serfHealth check registered
|
|
|
|
// Behavior mirrors testrpc.WaitForTestAgent but avoids the dependency cycle in api pkg
|
|
|
|
func (s *TestServer) WaitForSerfCheck(t *testing.T) {
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
// Query the API and check the status code.
|
|
|
|
url := s.url("/v1/catalog/nodes?index=0")
|
2020-05-29 21:16:03 +00:00
|
|
|
resp, err := s.masterGet(url)
|
2018-09-18 16:47:01 +00:00
|
|
|
if err != nil {
|
|
|
|
r.Fatal("failed http get", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if err := s.requireOK(resp); err != nil {
|
|
|
|
r.Fatal("failed OK response", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch for the anti-entropy sync to finish.
|
|
|
|
var payload []map[string]interface{}
|
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
if err := dec.Decode(&payload); err != nil {
|
|
|
|
r.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(payload) < 1 {
|
|
|
|
r.Fatal("No nodes")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the serfHealth check is registered
|
|
|
|
url = s.url(fmt.Sprintf("/v1/health/node/%s", payload[0]["Node"]))
|
2020-05-29 21:16:03 +00:00
|
|
|
resp, err = s.masterGet(url)
|
2018-09-18 16:47:01 +00:00
|
|
|
if err != nil {
|
|
|
|
r.Fatal("failed http get", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if err := s.requireOK(resp); err != nil {
|
|
|
|
r.Fatal("failed OK response", err)
|
|
|
|
}
|
|
|
|
dec = json.NewDecoder(resp.Body)
|
|
|
|
if err = dec.Decode(&payload); err != nil {
|
|
|
|
r.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var found bool
|
|
|
|
for _, check := range payload {
|
|
|
|
if check["CheckID"].(string) == "serfHealth" {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
r.Fatal("missing serfHealth registration")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-05-29 21:16:03 +00:00
|
|
|
|
|
|
|
func (s *TestServer) masterGet(url string) (*http.Response, error) {
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if s.Config.ACL.Tokens.Master != "" {
|
|
|
|
req.Header.Set("x-consul-token", s.Config.ACL.Tokens.Master)
|
|
|
|
}
|
|
|
|
return s.HTTPClient.Do(req)
|
|
|
|
}
|