2015-01-06 18:40:00 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
crand "crypto/rand"
|
2016-03-24 18:24:18 +00:00
|
|
|
"crypto/tls"
|
2015-01-06 18:40:00 +00:00
|
|
|
"fmt"
|
2015-01-06 23:26:50 +00:00
|
|
|
"io/ioutil"
|
2015-01-06 18:40:00 +00:00
|
|
|
"net/http"
|
2015-01-06 23:26:50 +00:00
|
|
|
"os"
|
2015-01-16 18:56:47 +00:00
|
|
|
"path/filepath"
|
2016-03-24 18:24:18 +00:00
|
|
|
"reflect"
|
2015-01-16 18:56:47 +00:00
|
|
|
"runtime"
|
2017-04-18 23:39:23 +00:00
|
|
|
"strings"
|
2015-01-06 18:40:00 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2015-01-07 00:48:54 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/testutil"
|
2015-01-06 18:40:00 +00:00
|
|
|
)
|
|
|
|
|
2015-01-16 01:24:15 +00:00
|
|
|
type configCallback func(c *Config)
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-03-03 02:18:38 +00:00
|
|
|
func makeClient(t *testing.T) (*Client, *testutil.TestServer) {
|
2015-03-03 03:20:13 +00:00
|
|
|
return makeClientWithConfig(t, nil, nil)
|
2015-01-14 19:30:43 +00:00
|
|
|
}
|
|
|
|
|
2015-06-13 21:51:30 +00:00
|
|
|
func makeACLClient(t *testing.T) (*Client, *testutil.TestServer) {
|
|
|
|
return makeClientWithConfig(t, func(clientConfig *Config) {
|
|
|
|
clientConfig.Token = "root"
|
|
|
|
}, func(serverConfig *testutil.TestServerConfig) {
|
|
|
|
serverConfig.ACLMasterToken = "root"
|
|
|
|
serverConfig.ACLDatacenter = "dc1"
|
|
|
|
serverConfig.ACLDefaultPolicy = "deny"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-03 02:18:38 +00:00
|
|
|
func makeClientWithConfig(
|
|
|
|
t *testing.T,
|
|
|
|
cb1 configCallback,
|
|
|
|
cb2 testutil.ServerConfigCallback) (*Client, *testutil.TestServer) {
|
|
|
|
|
2015-01-16 01:24:15 +00:00
|
|
|
// Make client config
|
2015-01-14 19:30:43 +00:00
|
|
|
conf := DefaultConfig()
|
2015-03-03 02:18:38 +00:00
|
|
|
if cb1 != nil {
|
|
|
|
cb1(conf)
|
|
|
|
}
|
2015-03-03 03:20:13 +00:00
|
|
|
// Create server
|
2017-03-23 22:27:16 +00:00
|
|
|
server, err := testutil.NewTestServerConfig(cb2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-11 16:47:47 +00:00
|
|
|
conf.Address = server.HTTPAddr
|
2015-03-03 03:20:13 +00:00
|
|
|
|
2015-01-16 01:24:15 +00:00
|
|
|
// Create client
|
2015-01-14 19:30:43 +00:00
|
|
|
client, err := NewClient(conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-01-06 23:26:50 +00:00
|
|
|
return client, server
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testKey() string {
|
|
|
|
buf := make([]byte, 16)
|
|
|
|
if _, err := crand.Read(buf); err != nil {
|
|
|
|
panic(fmt.Errorf("Failed to read random bytes: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
|
|
|
|
buf[0:4],
|
|
|
|
buf[4:6],
|
|
|
|
buf[6:8],
|
|
|
|
buf[8:10],
|
|
|
|
buf[10:16])
|
|
|
|
}
|
|
|
|
|
2015-03-18 14:41:00 +00:00
|
|
|
func TestDefaultConfig_env(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-03-18 14:41:00 +00:00
|
|
|
addr := "1.2.3.4:5678"
|
|
|
|
token := "abcd1234"
|
|
|
|
auth := "username:password"
|
|
|
|
|
2016-08-03 06:10:11 +00:00
|
|
|
os.Setenv(HTTPAddrEnvName, addr)
|
|
|
|
defer os.Setenv(HTTPAddrEnvName, "")
|
2016-08-03 06:11:04 +00:00
|
|
|
os.Setenv(HTTPTokenEnvName, token)
|
|
|
|
defer os.Setenv(HTTPTokenEnvName, "")
|
|
|
|
os.Setenv(HTTPAuthEnvName, auth)
|
|
|
|
defer os.Setenv(HTTPAuthEnvName, "")
|
|
|
|
os.Setenv(HTTPSSLEnvName, "1")
|
|
|
|
defer os.Setenv(HTTPSSLEnvName, "")
|
2017-04-14 20:37:29 +00:00
|
|
|
os.Setenv(HTTPCAFile, "ca.pem")
|
|
|
|
defer os.Setenv(HTTPCAFile, "")
|
|
|
|
os.Setenv(HTTPCAPath, "certs/")
|
|
|
|
defer os.Setenv(HTTPCAPath, "")
|
|
|
|
os.Setenv(HTTPClientCert, "client.crt")
|
|
|
|
defer os.Setenv(HTTPClientCert, "")
|
|
|
|
os.Setenv(HTTPClientKey, "client.key")
|
|
|
|
defer os.Setenv(HTTPClientKey, "")
|
|
|
|
os.Setenv(HTTPTLSServerName, "consul.test")
|
|
|
|
defer os.Setenv(HTTPTLSServerName, "")
|
2016-08-03 06:11:04 +00:00
|
|
|
os.Setenv(HTTPSSLVerifyEnvName, "0")
|
|
|
|
defer os.Setenv(HTTPSSLVerifyEnvName, "")
|
2015-03-18 14:41:00 +00:00
|
|
|
|
2016-03-10 20:29:50 +00:00
|
|
|
for i, config := range []*Config{DefaultConfig(), DefaultNonPooledConfig()} {
|
|
|
|
if config.Address != addr {
|
|
|
|
t.Errorf("expected %q to be %q", config.Address, addr)
|
|
|
|
}
|
|
|
|
if config.Token != token {
|
|
|
|
t.Errorf("expected %q to be %q", config.Token, token)
|
|
|
|
}
|
|
|
|
if config.HttpAuth == nil {
|
|
|
|
t.Fatalf("expected HttpAuth to be enabled")
|
|
|
|
}
|
|
|
|
if config.HttpAuth.Username != "username" {
|
|
|
|
t.Errorf("expected %q to be %q", config.HttpAuth.Username, "username")
|
|
|
|
}
|
|
|
|
if config.HttpAuth.Password != "password" {
|
|
|
|
t.Errorf("expected %q to be %q", config.HttpAuth.Password, "password")
|
|
|
|
}
|
|
|
|
if config.Scheme != "https" {
|
|
|
|
t.Errorf("expected %q to be %q", config.Scheme, "https")
|
|
|
|
}
|
2017-04-14 20:37:29 +00:00
|
|
|
if config.TLSConfig.CAFile != "ca.pem" {
|
|
|
|
t.Errorf("expected %q to be %q", config.TLSConfig.CAFile, "ca.pem")
|
|
|
|
}
|
|
|
|
if config.TLSConfig.CAPath != "certs/" {
|
|
|
|
t.Errorf("expected %q to be %q", config.TLSConfig.CAPath, "certs/")
|
|
|
|
}
|
|
|
|
if config.TLSConfig.CertFile != "client.crt" {
|
|
|
|
t.Errorf("expected %q to be %q", config.TLSConfig.CertFile, "client.crt")
|
|
|
|
}
|
|
|
|
if config.TLSConfig.KeyFile != "client.key" {
|
|
|
|
t.Errorf("expected %q to be %q", config.TLSConfig.KeyFile, "client.key")
|
|
|
|
}
|
|
|
|
if config.TLSConfig.Address != "consul.test" {
|
|
|
|
t.Errorf("expected %q to be %q", config.TLSConfig.Address, "consul.test")
|
|
|
|
}
|
|
|
|
if !config.TLSConfig.InsecureSkipVerify {
|
2016-03-10 20:29:50 +00:00
|
|
|
t.Errorf("expected SSL verification to be off")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use keep alives as a check for whether pooling is on or off.
|
|
|
|
if pooled := i == 0; pooled {
|
2017-04-18 23:39:23 +00:00
|
|
|
if config.Transport.DisableKeepAlives != false {
|
2016-03-10 20:29:50 +00:00
|
|
|
t.Errorf("expected keep alives to be enabled")
|
|
|
|
}
|
|
|
|
} else {
|
2017-04-18 23:39:23 +00:00
|
|
|
if config.Transport.DisableKeepAlives != true {
|
2016-03-10 20:29:50 +00:00
|
|
|
t.Errorf("expected keep alives to be disabled")
|
|
|
|
}
|
|
|
|
}
|
2015-03-18 14:41:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 18:24:18 +00:00
|
|
|
func TestSetupTLSConfig(t *testing.T) {
|
|
|
|
// A default config should result in a clean default client config.
|
|
|
|
tlsConfig := &TLSConfig{}
|
|
|
|
cc, err := SetupTLSConfig(tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-04-14 20:37:29 +00:00
|
|
|
expected := &tls.Config{RootCAs: cc.RootCAs}
|
2016-03-24 18:24:18 +00:00
|
|
|
if !reflect.DeepEqual(cc, expected) {
|
2017-04-14 20:37:29 +00:00
|
|
|
t.Fatalf("bad: \n%v, \n%v", cc, expected)
|
2016-03-24 18:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try some address variations with and without ports.
|
|
|
|
tlsConfig.Address = "127.0.0.1"
|
|
|
|
cc, err = SetupTLSConfig(tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
expected.ServerName = "127.0.0.1"
|
|
|
|
if !reflect.DeepEqual(cc, expected) {
|
|
|
|
t.Fatalf("bad: %v", cc)
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig.Address = "127.0.0.1:80"
|
|
|
|
cc, err = SetupTLSConfig(tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
expected.ServerName = "127.0.0.1"
|
|
|
|
if !reflect.DeepEqual(cc, expected) {
|
|
|
|
t.Fatalf("bad: %v", cc)
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig.Address = "demo.consul.io:80"
|
|
|
|
cc, err = SetupTLSConfig(tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
expected.ServerName = "demo.consul.io"
|
|
|
|
if !reflect.DeepEqual(cc, expected) {
|
|
|
|
t.Fatalf("bad: %v", cc)
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig.Address = "[2001:db8:a0b:12f0::1]"
|
|
|
|
cc, err = SetupTLSConfig(tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
expected.ServerName = "[2001:db8:a0b:12f0::1]"
|
|
|
|
if !reflect.DeepEqual(cc, expected) {
|
|
|
|
t.Fatalf("bad: %v", cc)
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig.Address = "[2001:db8:a0b:12f0::1]:80"
|
|
|
|
cc, err = SetupTLSConfig(tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
expected.ServerName = "2001:db8:a0b:12f0::1"
|
|
|
|
if !reflect.DeepEqual(cc, expected) {
|
|
|
|
t.Fatalf("bad: %v", cc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip verification.
|
|
|
|
tlsConfig.InsecureSkipVerify = true
|
|
|
|
cc, err = SetupTLSConfig(tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
expected.InsecureSkipVerify = true
|
|
|
|
if !reflect.DeepEqual(cc, expected) {
|
|
|
|
t.Fatalf("bad: %v", cc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a new config that hits all the file parsers.
|
|
|
|
tlsConfig = &TLSConfig{
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
}
|
|
|
|
cc, err = SetupTLSConfig(tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(cc.Certificates) != 1 {
|
|
|
|
t.Fatalf("missing certificate: %v", cc.Certificates)
|
|
|
|
}
|
|
|
|
if cc.RootCAs == nil {
|
|
|
|
t.Fatalf("didn't load root CAs")
|
|
|
|
}
|
2017-04-14 20:37:29 +00:00
|
|
|
|
|
|
|
// Use a directory to load the certs instead
|
|
|
|
cc, err = SetupTLSConfig(&TLSConfig{
|
|
|
|
CAPath: "../test/ca_path",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(cc.RootCAs.Subjects()) != 2 {
|
|
|
|
t.Fatalf("didn't load root CAs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientTLSOptions(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
_, s := makeClientWithConfig(t, nil, func(conf *testutil.TestServerConfig) {
|
|
|
|
conf.CAFile = "../test/client_certs/rootca.crt"
|
|
|
|
conf.CertFile = "../test/client_certs/server.crt"
|
|
|
|
conf.KeyFile = "../test/client_certs/server.key"
|
|
|
|
conf.VerifyIncoming = true
|
|
|
|
})
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
// Set up a client without certs
|
|
|
|
clientWithoutCerts, err := NewClient(&Config{
|
|
|
|
Address: s.HTTPSAddr,
|
|
|
|
Scheme: "https",
|
|
|
|
TLSConfig: TLSConfig{
|
|
|
|
Address: "consul.test",
|
|
|
|
CAFile: "../test/client_certs/rootca.crt",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should fail
|
|
|
|
_, err = clientWithoutCerts.Agent().Self()
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "bad certificate") {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up a client with valid certs
|
|
|
|
clientWithCerts, err := NewClient(&Config{
|
|
|
|
Address: s.HTTPSAddr,
|
|
|
|
Scheme: "https",
|
|
|
|
TLSConfig: TLSConfig{
|
|
|
|
Address: "consul.test",
|
|
|
|
CAFile: "../test/client_certs/rootca.crt",
|
|
|
|
CertFile: "../test/client_certs/client.crt",
|
|
|
|
KeyFile: "../test/client_certs/client.key",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should succeed
|
|
|
|
_, err = clientWithCerts.Agent().Self()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-03-24 18:24:18 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
func TestSetQueryOptions(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
|
|
|
r := c.newRequest("GET", "/v1/kv/foo")
|
|
|
|
q := &QueryOptions{
|
|
|
|
Datacenter: "foo",
|
|
|
|
AllowStale: true,
|
|
|
|
RequireConsistent: true,
|
|
|
|
WaitIndex: 1000,
|
|
|
|
WaitTime: 100 * time.Second,
|
|
|
|
Token: "12345",
|
2015-10-16 04:42:09 +00:00
|
|
|
Near: "nodex",
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
r.setQueryOptions(q)
|
|
|
|
|
|
|
|
if r.params.Get("dc") != "foo" {
|
|
|
|
t.Fatalf("bad: %v", r.params)
|
|
|
|
}
|
|
|
|
if _, ok := r.params["stale"]; !ok {
|
|
|
|
t.Fatalf("bad: %v", r.params)
|
|
|
|
}
|
|
|
|
if _, ok := r.params["consistent"]; !ok {
|
|
|
|
t.Fatalf("bad: %v", r.params)
|
|
|
|
}
|
|
|
|
if r.params.Get("index") != "1000" {
|
|
|
|
t.Fatalf("bad: %v", r.params)
|
|
|
|
}
|
|
|
|
if r.params.Get("wait") != "100000ms" {
|
|
|
|
t.Fatalf("bad: %v", r.params)
|
|
|
|
}
|
2016-08-02 20:54:59 +00:00
|
|
|
if r.header.Get("X-Consul-Token") != "12345" {
|
|
|
|
t.Fatalf("bad: %v", r.header)
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
2015-10-16 04:42:09 +00:00
|
|
|
if r.params.Get("near") != "nodex" {
|
|
|
|
t.Fatalf("bad: %v", r.params)
|
|
|
|
}
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetWriteOptions(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
|
|
|
r := c.newRequest("GET", "/v1/kv/foo")
|
|
|
|
q := &WriteOptions{
|
|
|
|
Datacenter: "foo",
|
|
|
|
Token: "23456",
|
|
|
|
}
|
|
|
|
r.setWriteOptions(q)
|
|
|
|
|
|
|
|
if r.params.Get("dc") != "foo" {
|
|
|
|
t.Fatalf("bad: %v", r.params)
|
|
|
|
}
|
2016-08-02 20:54:59 +00:00
|
|
|
if r.header.Get("X-Consul-Token") != "23456" {
|
|
|
|
t.Fatalf("bad: %v", r.header)
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestToHTTP(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
|
|
|
r := c.newRequest("DELETE", "/v1/kv/foo")
|
|
|
|
q := &QueryOptions{
|
|
|
|
Datacenter: "foo",
|
|
|
|
}
|
|
|
|
r.setQueryOptions(q)
|
|
|
|
req, err := r.toHTTP()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Method != "DELETE" {
|
|
|
|
t.Fatalf("bad: %v", req)
|
|
|
|
}
|
2015-01-14 19:30:43 +00:00
|
|
|
if req.URL.RequestURI() != "/v1/kv/foo?dc=foo" {
|
2015-01-06 18:40:00 +00:00
|
|
|
t.Fatalf("bad: %v", req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseQueryMeta(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 18:40:00 +00:00
|
|
|
resp := &http.Response{
|
|
|
|
Header: make(map[string][]string),
|
|
|
|
}
|
|
|
|
resp.Header.Set("X-Consul-Index", "12345")
|
|
|
|
resp.Header.Set("X-Consul-LastContact", "80")
|
|
|
|
resp.Header.Set("X-Consul-KnownLeader", "true")
|
2016-08-16 18:31:41 +00:00
|
|
|
resp.Header.Set("X-Consul-Translate-Addresses", "true")
|
2015-01-06 18:40:00 +00:00
|
|
|
|
|
|
|
qm := &QueryMeta{}
|
|
|
|
if err := parseQueryMeta(resp, qm); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if qm.LastIndex != 12345 {
|
|
|
|
t.Fatalf("Bad: %v", qm)
|
|
|
|
}
|
|
|
|
if qm.LastContact != 80*time.Millisecond {
|
|
|
|
t.Fatalf("Bad: %v", qm)
|
|
|
|
}
|
|
|
|
if !qm.KnownLeader {
|
|
|
|
t.Fatalf("Bad: %v", qm)
|
|
|
|
}
|
2016-08-16 18:31:41 +00:00
|
|
|
if !qm.AddressTranslationEnabled {
|
|
|
|
t.Fatalf("Bad: %v", qm)
|
|
|
|
}
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
2015-01-16 18:56:47 +00:00
|
|
|
|
|
|
|
func TestAPI_UnixSocket(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-16 18:56:47 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
tempDir, err := ioutil.TempDir("", "consul")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tempDir)
|
|
|
|
socket := filepath.Join(tempDir, "test.sock")
|
|
|
|
|
|
|
|
c, s := makeClientWithConfig(t, func(c *Config) {
|
|
|
|
c.Address = "unix://" + socket
|
2015-03-03 02:18:38 +00:00
|
|
|
}, func(c *testutil.TestServerConfig) {
|
|
|
|
c.Addresses = &testutil.TestAddressConfig{
|
2015-01-16 18:56:47 +00:00
|
|
|
HTTP: "unix://" + socket,
|
|
|
|
}
|
|
|
|
})
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-16 18:56:47 +00:00
|
|
|
|
2015-12-17 15:56:50 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
info, err := agent.Self()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if info["Config"]["NodeName"] == "" {
|
|
|
|
t.Fatalf("bad: %v", info)
|
2015-01-16 18:56:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-06 19:05:11 +00:00
|
|
|
|
|
|
|
func TestAPI_durToMsec(t *testing.T) {
|
|
|
|
if ms := durToMsec(0); ms != "0ms" {
|
|
|
|
t.Fatalf("bad: %s", ms)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ms := durToMsec(time.Millisecond); ms != "1ms" {
|
|
|
|
t.Fatalf("bad: %s", ms)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ms := durToMsec(time.Microsecond); ms != "1ms" {
|
|
|
|
t.Fatalf("bad: %s", ms)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ms := durToMsec(5 * time.Millisecond); ms != "5ms" {
|
|
|
|
t.Fatalf("bad: %s", ms)
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 19:35:16 +00:00
|
|
|
|
|
|
|
func TestAPI_IsServerError(t *testing.T) {
|
|
|
|
if IsServerError(nil) {
|
|
|
|
t.Fatalf("should not be a server error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if IsServerError(fmt.Errorf("not the error you are looking for")) {
|
|
|
|
t.Fatalf("should not be a server error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !IsServerError(fmt.Errorf(serverError)) {
|
|
|
|
t.Fatalf("should be a server error")
|
|
|
|
}
|
|
|
|
}
|