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"
|
2018-04-26 13:01:20 +00:00
|
|
|
"log"
|
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"
|
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
|
|
|
|
2017-10-21 19:15:01 +00:00
|
|
|
"github.com/hashicorp/consul/lib/freeport"
|
2017-05-05 11:48:34 +00:00
|
|
|
"github.com/hashicorp/consul/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 {
|
|
|
|
DNS int `json:"dns,omitempty"`
|
|
|
|
HTTP int `json:"http,omitempty"`
|
2017-04-14 20:37:29 +00:00
|
|
|
HTTPS int `json:"https,omitempty"`
|
2015-03-03 02:18:38 +00:00
|
|
|
SerfLan int `json:"serf_lan,omitempty"`
|
|
|
|
SerfWan int `json:"serf_wan,omitempty"`
|
|
|
|
Server int `json:"server,omitempty"`
|
|
|
|
}
|
|
|
|
|
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"`
|
|
|
|
ACLDefaultPolicy string `json:"acl_default_policy,omitempty"`
|
|
|
|
ACLEnforceVersion8 bool `json:"acl_enforce_version_8"`
|
|
|
|
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"`
|
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:"-"`
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-10-21 19:15:01 +00:00
|
|
|
ports := freeport.Get(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",
|
|
|
|
},
|
|
|
|
},
|
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
|
|
|
}
|
|
|
|
|
2015-03-11 04:53:51 +00:00
|
|
|
// NewTestServer is an easy helper method to create a new Consul
|
|
|
|
// test server with the most basic configuration.
|
2017-05-12 13:41:13 +00:00
|
|
|
func NewTestServer() (*TestServer, error) {
|
|
|
|
return NewTestServerConfigT(nil, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
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).
|
2017-05-12 13:41:13 +00:00
|
|
|
func NewTestServerConfigT(t *testing.T, 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.
|
|
|
|
func newTestServerConfigT(t *testing.T, 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
|
|
|
}
|
|
|
|
|
2017-05-12 13:41:13 +00:00
|
|
|
tmpdir := TempDir(t, "consul")
|
|
|
|
cfg := defaultServerConfig()
|
|
|
|
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 {
|
2017-03-23 20:26:05 +00:00
|
|
|
return nil, errors.Wrap(err, "failed marshaling json")
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 13:01:20 +00:00
|
|
|
log.Printf("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 {
|
|
|
|
defer 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
|
|
|
}
|
|
|
|
|
2015-06-08 14:16:11 +00:00
|
|
|
stdout := io.Writer(os.Stdout)
|
2017-05-12 13:41:13 +00:00
|
|
|
if cfg.Stdout != nil {
|
|
|
|
stdout = cfg.Stdout
|
2015-06-08 14:16:11 +00:00
|
|
|
}
|
|
|
|
stderr := io.Writer(os.Stderr)
|
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 {
|
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
|
2017-05-12 13:41:13 +00:00
|
|
|
if cfg.Bootstrap {
|
|
|
|
err = server.waitForLeader()
|
2015-05-09 01:16:35 +00:00
|
|
|
} else {
|
2017-05-12 13:41:13 +00:00
|
|
|
err = server.waitForAPI()
|
2017-03-23 20:26:05 +00:00
|
|
|
}
|
2017-05-12 13:41:13 +00:00
|
|
|
if err != nil {
|
2017-03-23 20:26:05 +00:00
|
|
|
defer server.Stop()
|
2017-05-12 13:41:13 +00:00
|
|
|
return nil, errors.Wrap(err, "failed waiting for server to start")
|
2015-05-09 01:11:17 +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 {
|
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 {
|
2017-05-19 08:49:46 +00:00
|
|
|
if err := s.cmd.Process.Signal(os.Interrupt); err != nil {
|
2017-05-12 13:41:13 +00:00
|
|
|
return errors.Wrap(err, "failed to kill consul server")
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2017-05-05 11:48:34 +00:00
|
|
|
type failer struct {
|
|
|
|
failed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *failer) Log(args ...interface{}) { fmt.Println(args) }
|
|
|
|
func (f *failer) FailNow() { f.failed = true }
|
|
|
|
|
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 {
|
2017-05-05 11:48:34 +00:00
|
|
|
f := &failer{}
|
|
|
|
retry.Run(f, func(r *retry.R) {
|
2017-04-21 00:02:42 +00:00
|
|
|
resp, err := s.HTTPClient.Get(s.url("/v1/agent/self"))
|
2015-05-09 01:11:17 +00:00
|
|
|
if err != nil {
|
2017-05-05 11:48:34 +00:00
|
|
|
r.Fatal(err)
|
2015-05-09 01:11:17 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if err := s.requireOK(resp); err != nil {
|
2018-03-19 16:56:00 +00:00
|
|
|
r.Fatal("failed OK response", err)
|
2015-05-09 01:11:17 +00:00
|
|
|
}
|
2017-05-05 11:48:34 +00:00
|
|
|
})
|
|
|
|
if f.failed {
|
|
|
|
return errors.New("failed waiting for API")
|
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
|
|
|
|
// 1 or more to be observed to confirm leader election is done.
|
2017-01-27 00:55:49 +00:00
|
|
|
// It then waits to ensure the anti-entropy sync has completed.
|
2017-03-23 20:26:05 +00:00
|
|
|
func (s *TestServer) waitForLeader() error {
|
2017-05-05 11:48:34 +00:00
|
|
|
f := &failer{}
|
2017-05-09 13:12:34 +00:00
|
|
|
timer := &retry.Timer{
|
|
|
|
Timeout: s.Config.ReadyTimeout,
|
|
|
|
Wait: 250 * time.Millisecond,
|
|
|
|
}
|
2017-01-27 00:55:49 +00:00
|
|
|
var index int64
|
2017-05-05 11:48:34 +00:00
|
|
|
retry.RunWith(timer, f, func(r *retry.R) {
|
2017-01-27 00:55:49 +00:00
|
|
|
// Query the API and check the status code.
|
2017-07-05 08:50:03 +00:00
|
|
|
url := s.url(fmt.Sprintf("/v1/catalog/nodes?index=%d", index))
|
2017-04-21 00:02:42 +00:00
|
|
|
resp, err := s.HTTPClient.Get(url)
|
2015-03-24 02:27:59 +00:00
|
|
|
if err != nil {
|
2017-05-05 11:48:34 +00:00
|
|
|
r.Fatal("failed http get", 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
|
|
|
}
|
2017-01-27 00:55:49 +00:00
|
|
|
index, err = strconv.ParseInt(resp.Header.Get("X-Consul-Index"), 10, 64)
|
|
|
|
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
|
|
|
}
|
|
|
|
if index == 0 {
|
2017-05-05 11:48:34 +00:00
|
|
|
r.Fatal("consul index is 0")
|
2015-03-03 02:18:38 +00:00
|
|
|
}
|
2017-01-26 05:13:03 +00:00
|
|
|
|
2017-01-27 00:55:49 +00:00
|
|
|
// Watch for the anti-entropy sync to finish.
|
2017-05-05 11:48:34 +00:00
|
|
|
var v []map[string]interface{}
|
2017-01-26 05:13:03 +00:00
|
|
|
dec := json.NewDecoder(resp.Body)
|
2017-05-05 11:48:34 +00:00
|
|
|
if err := dec.Decode(&v); err != nil {
|
|
|
|
r.Fatal(err)
|
2017-01-26 05:13:03 +00:00
|
|
|
}
|
2017-05-05 11:48:34 +00:00
|
|
|
if len(v) < 1 {
|
|
|
|
r.Fatal("No nodes")
|
2017-01-26 05:13:03 +00:00
|
|
|
}
|
2017-05-05 11:48:34 +00:00
|
|
|
taggedAddresses, ok := v[0]["TaggedAddresses"].(map[string]interface{})
|
2017-01-26 05:13:03 +00:00
|
|
|
if !ok {
|
2017-05-05 11:48:34 +00:00
|
|
|
r.Fatal("Missing tagged addresses")
|
2017-01-26 05:13:03 +00:00
|
|
|
}
|
|
|
|
if _, ok := taggedAddresses["lan"]; !ok {
|
2017-05-05 11:48:34 +00:00
|
|
|
r.Fatal("No lan tagged addresses")
|
2017-01-26 05:13:03 +00:00
|
|
|
}
|
2017-05-05 11:48:34 +00:00
|
|
|
})
|
|
|
|
if f.failed {
|
|
|
|
return errors.New("failed waiting for leader")
|
2015-03-11 01:08:14 +00:00
|
|
|
}
|
2015-03-24 02:27:59 +00:00
|
|
|
return nil
|
2015-03-11 01:08:14 +00:00
|
|
|
}
|