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"
|
2020-08-11 16:12:55 +00:00
|
|
|
"syscall"
|
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
|
|
|
|
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"
|
2021-11-27 20:53:51 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/sdk/freeport"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
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"`
|
2022-07-08 17:01:13 +00:00
|
|
|
GRPC int `json:"grpc,omitempty"`
|
2018-06-11 20:25:13 +00:00
|
|
|
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"`
|
2022-01-20 21:49:36 +00:00
|
|
|
Partition string `json:"partition,omitempty"`
|
|
|
|
RetryJoin []string `json:"retry_join,omitempty"`
|
2017-04-28 23:15:55 +00:00
|
|
|
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"`
|
|
|
|
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"`
|
2021-09-08 15:12:54 +00:00
|
|
|
SkipLeaveOnInt bool `json:"skip_leave_on_interrupt"`
|
2022-08-01 19:22:36 +00:00
|
|
|
Peering *TestPeeringConfig `json:"peering,omitempty"`
|
2017-05-09 13:12:34 +00:00
|
|
|
ReadyTimeout time.Duration `json:"-"`
|
2021-09-08 15:12:54 +00:00
|
|
|
StopTimeout time.Duration `json:"-"`
|
2020-05-06 20:40:16 +00:00
|
|
|
Stdout io.Writer `json:"-"`
|
|
|
|
Stderr io.Writer `json:"-"`
|
2017-04-28 23:15:55 +00:00
|
|
|
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 {
|
|
|
|
Replication string `json:"replication,omitempty"`
|
|
|
|
Default string `json:"default,omitempty"`
|
|
|
|
Agent string `json:"agent,omitempty"`
|
2022-01-20 12:47:50 +00:00
|
|
|
|
|
|
|
// Note: this field is marshaled as master for compatibility with
|
|
|
|
// versions of Consul prior to 1.11.
|
|
|
|
InitialManagement string `json:"master,omitempty"`
|
|
|
|
|
|
|
|
// Note: this field is marshaled as agent_master for compatibility with
|
|
|
|
// versions of Consul prior to 1.11.
|
|
|
|
AgentRecovery string `json:"agent_master,omitempty"`
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 19:22:36 +00:00
|
|
|
type TestPeeringConfig struct {
|
|
|
|
Enabled bool `json:"enabled,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.
|
2020-09-10 13:04:56 +00:00
|
|
|
func defaultServerConfig(t TestingTB) *TestServerConfig {
|
2017-03-27 08:28:54 +00:00
|
|
|
nodeID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
ports, err := freeport.Take(7)
|
2021-11-27 20:53:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to take ports: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-05-06 20:40:16 +00:00
|
|
|
logBuffer := NewLogBuffer(t)
|
2019-08-27 21:16:41 +00:00
|
|
|
|
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],
|
2022-07-08 17:01:13 +00:00
|
|
|
GRPC: ports[6],
|
2015-03-03 02:18:38 +00:00
|
|
|
},
|
2021-09-08 15:12:54 +00:00
|
|
|
ReadyTimeout: 10 * time.Second,
|
|
|
|
StopTimeout: 10 * time.Second,
|
|
|
|
SkipLeaveOnInt: true,
|
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)
|
|
|
|
},
|
2022-08-01 19:22:36 +00:00
|
|
|
Stdout: logBuffer,
|
|
|
|
Stderr: logBuffer,
|
|
|
|
Peering: &TestPeeringConfig{Enabled: true},
|
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
|
2022-07-08 17:01:13 +00:00
|
|
|
GRPCAddr 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
|
|
|
}
|
|
|
|
|
2020-09-10 13:04:56 +00:00
|
|
|
// NewTestServerConfigT creates a new TestServer, and makes a call to an optional
|
2017-03-23 20:26:05 +00:00
|
|
|
// 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).
|
2021-11-02 18:02:10 +00:00
|
|
|
// This function will call the `consul` binary in GOPATH.
|
2020-09-10 13:04:56 +00:00
|
|
|
func NewTestServerConfigT(t TestingTB, 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")
|
|
|
|
}
|
|
|
|
|
2020-05-06 20:40:16 +00:00
|
|
|
cfg := defaultServerConfig(t)
|
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
|
|
|
|
2020-05-06 20:40:16 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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...)
|
2020-05-06 20:40:16 +00:00
|
|
|
cmd.Stdout = cfg.Stdout
|
|
|
|
cmd.Stderr = cfg.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),
|
2022-07-08 17:01:13 +00:00
|
|
|
GRPCAddr: fmt.Sprintf("127.0.0.1:%d", cfg.Ports.GRPC),
|
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 {
|
2020-05-06 20:40:16 +00:00
|
|
|
if err := server.Stop(); err != nil {
|
|
|
|
t.Logf("server stop failed with: %v", err)
|
|
|
|
}
|
2019-07-12 15:37:29 +00:00
|
|
|
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
|
|
|
|
2020-08-06 21:00:20 +00:00
|
|
|
waitDone := make(chan error)
|
|
|
|
go func() {
|
|
|
|
waitDone <- s.cmd.Wait()
|
|
|
|
close(waitDone)
|
|
|
|
}()
|
|
|
|
|
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.
|
2020-08-06 21:00:20 +00:00
|
|
|
select {
|
|
|
|
case err := <-waitDone:
|
|
|
|
return err
|
2021-09-08 15:12:54 +00:00
|
|
|
case <-time.After(s.Config.StopTimeout):
|
2020-08-11 16:12:55 +00:00
|
|
|
s.cmd.Process.Signal(syscall.SIGABRT)
|
2021-06-09 15:30:26 +00:00
|
|
|
<-waitDone
|
2020-08-06 21:00:20 +00:00
|
|
|
return fmt.Errorf("timeout waiting for server to stop gracefully")
|
|
|
|
}
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 21:25:17 +00:00
|
|
|
// waitForAPI waits for the /status/leader HTTP endpoint to start
|
2015-05-09 01:11:17 +00:00
|
|
|
// responding. This is an indication that the agent has started,
|
|
|
|
// but will likely return before a leader is elected.
|
2020-07-07 21:25:17 +00:00
|
|
|
// Note: We do not check for a successful response status because
|
|
|
|
// we want this function to return without error even when
|
|
|
|
// there's no leader 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-07-07 21:25:17 +00:00
|
|
|
url := s.url("/v1/status/leader")
|
2022-01-20 12:47:50 +00:00
|
|
|
resp, err := s.privilegedGet(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
|
|
|
}
|
2020-08-06 21:00:20 +00:00
|
|
|
resp.Body.Close()
|
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.
|
2021-03-16 22:05:39 +00:00
|
|
|
func (s *TestServer) WaitForLeader(t testing.TB) {
|
2019-07-12 15:37:29 +00:00
|
|
|
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")
|
2022-01-20 12:47:50 +00:00
|
|
|
resp, err := s.privilegedGet(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 {
|
2020-10-06 18:24:05 +00:00
|
|
|
r.Fatalf("failed OK response: %v", 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 {
|
2020-10-06 18:24:05 +00:00
|
|
|
r.Fatalf("bad consul index: %v", 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.
|
2021-03-16 22:05:39 +00:00
|
|
|
func (s *TestServer) WaitForActiveCARoot(t testing.TB) {
|
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")
|
2022-01-20 12:47:50 +00:00
|
|
|
resp, err := s.privilegedGet(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 {
|
2020-10-06 18:24:05 +00:00
|
|
|
r.Fatalf("failed OK response: %v", err)
|
2020-01-20 14:56:56 +00:00
|
|
|
}
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// WaitForServiceIntentions waits until the server can accept config entry
|
|
|
|
// kinds of service-intentions meaning any migration bootstrapping from pre-1.9
|
|
|
|
// intentions has completed.
|
2021-03-16 22:05:39 +00:00
|
|
|
func (s *TestServer) WaitForServiceIntentions(t testing.TB) {
|
2020-10-06 18:24:05 +00:00
|
|
|
const fakeConfigName = "Sa4ohw5raith4si0Ohwuqu3lowiethoh"
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
// Try to delete a non-existent service-intentions config entry. The
|
|
|
|
// preflightCheck call in agent/consul/config_endpoint.go will fail if
|
|
|
|
// we aren't ready yet, vs just doing no work instead.
|
|
|
|
url := s.url("/v1/config/service-intentions/" + fakeConfigName)
|
2022-01-20 12:47:50 +00:00
|
|
|
resp, err := s.privilegedDelete(url)
|
2020-10-06 18:24:05 +00:00
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("failed http get '%s': %v", url, err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if err := s.requireOK(resp); err != nil {
|
|
|
|
r.Fatalf("failed OK response: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
2021-03-16 22:05:39 +00:00
|
|
|
func (s *TestServer) WaitForSerfCheck(t testing.TB) {
|
2018-09-18 16:47:01 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
// Query the API and check the status code.
|
|
|
|
url := s.url("/v1/catalog/nodes?index=0")
|
2022-01-20 12:47:50 +00:00
|
|
|
resp, err := s.privilegedGet(url)
|
2018-09-18 16:47:01 +00:00
|
|
|
if err != nil {
|
2020-10-06 18:24:05 +00:00
|
|
|
r.Fatalf("failed http get: %v", err)
|
2018-09-18 16:47:01 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if err := s.requireOK(resp); err != nil {
|
2020-10-06 18:24:05 +00:00
|
|
|
r.Fatalf("failed OK response: %v", err)
|
2018-09-18 16:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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"]))
|
2022-01-20 12:47:50 +00:00
|
|
|
resp, err = s.privilegedGet(url)
|
2018-09-18 16:47:01 +00:00
|
|
|
if err != nil {
|
2020-10-06 18:24:05 +00:00
|
|
|
r.Fatalf("failed http get: %v", err)
|
2018-09-18 16:47:01 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if err := s.requireOK(resp); err != nil {
|
2020-10-06 18:24:05 +00:00
|
|
|
r.Fatalf("failed OK response: %v", err)
|
2018-09-18 16:47:01 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2022-01-20 12:47:50 +00:00
|
|
|
func (s *TestServer) privilegedGet(url string) (*http.Response, error) {
|
2020-05-29 21:16:03 +00:00
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-20 12:47:50 +00:00
|
|
|
if s.Config.ACL.Tokens.InitialManagement != "" {
|
|
|
|
req.Header.Set("x-consul-token", s.Config.ACL.Tokens.InitialManagement)
|
2020-05-29 21:16:03 +00:00
|
|
|
}
|
|
|
|
return s.HTTPClient.Do(req)
|
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
|
2022-01-20 12:47:50 +00:00
|
|
|
func (s *TestServer) privilegedDelete(url string) (*http.Response, error) {
|
2020-10-06 18:24:05 +00:00
|
|
|
req, err := http.NewRequest("DELETE", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-20 12:47:50 +00:00
|
|
|
if s.Config.ACL.Tokens.InitialManagement != "" {
|
|
|
|
req.Header.Set("x-consul-token", s.Config.ACL.Tokens.InitialManagement)
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
|
|
|
return s.HTTPClient.Do(req)
|
|
|
|
}
|