test: refactor command tests to use TestAgent

This commit is contained in:
Frank Schroeder 2017-05-22 13:59:36 +02:00
parent 98ac791577
commit 26474ce9c2
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
27 changed files with 329 additions and 455 deletions

View File

@ -12,6 +12,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/consul" "github.com/hashicorp/consul/consul"
"github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/consul/structs"
"github.com/hashicorp/consul/logger" "github.com/hashicorp/consul/logger"
@ -189,6 +190,23 @@ func (a *TestAgent) Shutdown() error {
return a.Agent.Shutdown() return a.Agent.Shutdown()
} }
func (a *TestAgent) HTTPAddr() string {
if a.srv == nil {
return ""
}
return a.srv.Addr
}
func (a *TestAgent) Client() *api.Client {
conf := api.DefaultConfig()
conf.Address = a.HTTPAddr()
c, err := api.NewClient(conf)
if err != nil {
panic(fmt.Sprintf("Error creating consul API client: %s", err))
}
return c
}
func (a *TestAgent) consulConfig() *consul.Config { func (a *TestAgent) consulConfig() *consul.Config {
c, err := a.Agent.consulConfig() c, err := a.Agent.consulConfig()
if err != nil { if err != nil {
@ -209,9 +227,10 @@ func pickRandomPorts(c *Config) {
port := 1030 + int(rand.Int31n(64400)) port := 1030 + int(rand.Int31n(64400))
c.Ports.DNS = port + 1 c.Ports.DNS = port + 1
c.Ports.HTTP = port + 2 c.Ports.HTTP = port + 2
c.Ports.SerfLan = port + 3 c.Ports.HTTPS = port + 3
c.Ports.SerfWan = port + 4 c.Ports.SerfLan = port + 4
c.Ports.Server = port + 5 c.Ports.SerfWan = port + 5
c.Ports.Server = port + 6
} }
// BoolTrue and BoolFalse exist to create a *bool value. // BoolTrue and BoolFalse exist to create a *bool value.

View File

@ -4,6 +4,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
@ -13,7 +14,7 @@ func TestEventCommand_implements(t *testing.T) {
} }
func TestEventCommandRun(t *testing.T) { func TestEventCommandRun(t *testing.T) {
a1 := testAgent(t) a1 := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a1.Shutdown()
ui := new(cli.MockUi) ui := new(cli.MockUi)
@ -23,7 +24,7 @@ func TestEventCommandRun(t *testing.T) {
Flags: base.FlagSetClientHTTP, Flags: base.FlagSetClientHTTP,
}, },
} }
args := []string{"-http-addr=" + a1.httpAddr, "-name=cmd"} args := []string{"-http-addr=" + a1.HTTPAddr(), "-name=cmd"}
code := c.Run(args) code := c.Run(args)
if code != 0 { if code != 0 {

View File

@ -28,14 +28,13 @@ func TestExecCommand_implements(t *testing.T) {
} }
func TestExecCommandRun(t *testing.T) { func TestExecCommandRun(t *testing.T) {
a1 := testAgentWithConfig(t, func(cfg *agent.Config) { cfg := agent.TestConfig()
cfg.DisableRemoteExec = agent.Bool(false) cfg.DisableRemoteExec = agent.Bool(false)
}) a := agent.NewTestAgent(t.Name(), cfg)
defer a1.Shutdown() defer a.Shutdown()
waitForLeader(t, a1.httpAddr)
ui, c := testExecCommand(t) ui, c := testExecCommand(t)
args := []string{"-http-addr=" + a1.httpAddr, "-wait=500ms", "uptime"} args := []string{"-http-addr=" + a.HTTPAddr(), "-wait=500ms", "uptime"}
code := c.Run(args) code := c.Run(args)
if code != 0 { if code != 0 {
@ -48,20 +47,20 @@ func TestExecCommandRun(t *testing.T) {
} }
func TestExecCommandRun_CrossDC(t *testing.T) { func TestExecCommandRun_CrossDC(t *testing.T) {
a1 := testAgentWithConfig(t, func(cfg *agent.Config) { cfg1 := agent.TestConfig()
cfg.DisableRemoteExec = agent.Bool(false) cfg1.DisableRemoteExec = agent.Bool(false)
}) a1 := agent.NewTestAgent(t.Name(), cfg1)
defer a1.Shutdown() defer a1.Shutdown()
a2 := testAgentWithConfig(t, func(cfg *agent.Config) { cfg2 := agent.TestConfig()
cfg.Datacenter = "dc2" cfg2.Datacenter = "dc2"
cfg.DisableRemoteExec = agent.Bool(false) cfg2.DisableRemoteExec = agent.Bool(false)
}) a2 := agent.NewTestAgent(t.Name(), cfg2)
defer a2.Shutdown() defer a1.Shutdown()
// Join over the WAN // Join over the WAN
wanAddr := fmt.Sprintf("%s:%d", a1.config.BindAddr, a1.config.Ports.SerfWan) wanAddr := fmt.Sprintf("%s:%d", a1.Config.BindAddr, a1.Config.Ports.SerfWan)
n, err := a2.agent.JoinWAN([]string{wanAddr}) n, err := a2.JoinWAN([]string{wanAddr})
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -69,11 +68,8 @@ func TestExecCommandRun_CrossDC(t *testing.T) {
t.Fatalf("bad %d", n) t.Fatalf("bad %d", n)
} }
waitForLeader(t, a1.httpAddr)
waitForLeader(t, a2.httpAddr)
ui, c := testExecCommand(t) ui, c := testExecCommand(t)
args := []string{"-http-addr=" + a1.httpAddr, "-wait=500ms", "-datacenter=dc2", "uptime"} args := []string{"-http-addr=" + a1.HTTPAddr(), "-wait=500ms", "-datacenter=dc2", "uptime"}
code := c.Run(args) code := c.Run(args)
if code != 0 { if code != 0 {
@ -85,28 +81,6 @@ func TestExecCommandRun_CrossDC(t *testing.T) {
} }
} }
func waitForLeader(t *testing.T, httpAddr string) {
client, err := httpClient(httpAddr)
if err != nil {
t.Fatalf("err: %v", err)
}
retry.Run(t, func(r *retry.R) {
_, qm, err := client.Catalog().Nodes(nil)
if err != nil {
r.Fatal(err)
}
if !qm.KnownLeader || qm.LastIndex == 0 {
r.Fatal("not leader")
}
})
}
func httpClient(addr string) (*consulapi.Client, error) {
conf := consulapi.DefaultConfig()
conf.Address = addr
return consulapi.NewClient(conf)
}
func TestExecCommand_Validate(t *testing.T) { func TestExecCommand_Validate(t *testing.T) {
conf := &rExecConf{} conf := &rExecConf{}
err := conf.validate() err := conf.validate()
@ -143,17 +117,12 @@ func TestExecCommand_Validate(t *testing.T) {
} }
func TestExecCommand_Sessions(t *testing.T) { func TestExecCommand_Sessions(t *testing.T) {
a1 := testAgentWithConfig(t, func(cfg *agent.Config) { cfg := agent.TestConfig()
cfg.DisableRemoteExec = agent.Bool(false) cfg.DisableRemoteExec = agent.Bool(false)
}) a := agent.NewTestAgent(t.Name(), cfg)
defer a1.Shutdown() defer a.Shutdown()
waitForLeader(t, a1.httpAddr)
client, err := httpClient(a1.httpAddr)
if err != nil {
t.Fatalf("err: %v", err)
}
client := a.Client()
_, c := testExecCommand(t) _, c := testExecCommand(t)
c.client = client c.client = client
@ -186,17 +155,12 @@ func TestExecCommand_Sessions(t *testing.T) {
} }
func TestExecCommand_Sessions_Foreign(t *testing.T) { func TestExecCommand_Sessions_Foreign(t *testing.T) {
a1 := testAgentWithConfig(t, func(cfg *agent.Config) { cfg := agent.TestConfig()
cfg.DisableRemoteExec = agent.Bool(false) cfg.DisableRemoteExec = agent.Bool(false)
}) a := agent.NewTestAgent(t.Name(), cfg)
defer a1.Shutdown() defer a.Shutdown()
waitForLeader(t, a1.httpAddr)
client, err := httpClient(a1.httpAddr)
if err != nil {
t.Fatalf("err: %v", err)
}
client := a.Client()
_, c := testExecCommand(t) _, c := testExecCommand(t)
c.client = client c.client = client
@ -206,6 +170,7 @@ func TestExecCommand_Sessions_Foreign(t *testing.T) {
var id string var id string
retry.Run(t, func(r *retry.R) { retry.Run(t, func(r *retry.R) {
var err error
id, err = c.createSession() id, err = c.createSession()
if err != nil { if err != nil {
r.Fatal(err) r.Fatal(err)
@ -239,17 +204,12 @@ func TestExecCommand_Sessions_Foreign(t *testing.T) {
} }
func TestExecCommand_UploadDestroy(t *testing.T) { func TestExecCommand_UploadDestroy(t *testing.T) {
a1 := testAgentWithConfig(t, func(cfg *agent.Config) { cfg := agent.TestConfig()
cfg.DisableRemoteExec = agent.Bool(false) cfg.DisableRemoteExec = agent.Bool(false)
}) a := agent.NewTestAgent(t.Name(), cfg)
defer a1.Shutdown() defer a.Shutdown()
waitForLeader(t, a1.httpAddr)
client, err := httpClient(a1.httpAddr)
if err != nil {
t.Fatalf("err: %v", err)
}
client := a.Client()
_, c := testExecCommand(t) _, c := testExecCommand(t)
c.client = client c.client = client
@ -298,17 +258,12 @@ func TestExecCommand_UploadDestroy(t *testing.T) {
} }
func TestExecCommand_StreamResults(t *testing.T) { func TestExecCommand_StreamResults(t *testing.T) {
a1 := testAgentWithConfig(t, func(cfg *agent.Config) { cfg := agent.TestConfig()
cfg.DisableRemoteExec = agent.Bool(false) cfg.DisableRemoteExec = agent.Bool(false)
}) a := agent.NewTestAgent(t.Name(), cfg)
defer a1.Shutdown() defer a.Shutdown()
waitForLeader(t, a1.httpAddr)
client, err := httpClient(a1.httpAddr)
if err != nil {
t.Fatalf("err: %v", err)
}
client := a.Client()
_, c := testExecCommand(t) _, c := testExecCommand(t)
c.client = client c.client = client
c.conf.prefix = "_rexec" c.conf.prefix = "_rexec"

View File

@ -5,6 +5,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/hashicorp/consul/testutil/retry" "github.com/hashicorp/consul/testutil/retry"
"github.com/hashicorp/serf/serf" "github.com/hashicorp/serf/serf"
@ -26,13 +27,13 @@ func TestForceLeaveCommand_implements(t *testing.T) {
} }
func TestForceLeaveCommandRun(t *testing.T) { func TestForceLeaveCommandRun(t *testing.T) {
a1 := testAgent(t) a1 := agent.NewTestAgent(t.Name(), nil)
a2 := testAgent(t) a2 := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a1.Shutdown()
defer a2.Shutdown() defer a2.Shutdown()
addr := fmt.Sprintf("127.0.0.1:%d", a2.config.Ports.SerfLan) addr := fmt.Sprintf("127.0.0.1:%d", a2.Config.Ports.SerfLan)
_, err := a1.agent.JoinLAN([]string{addr}) _, err := a1.JoinLAN([]string{addr})
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -42,8 +43,8 @@ func TestForceLeaveCommandRun(t *testing.T) {
ui, c := testForceLeaveCommand(t) ui, c := testForceLeaveCommand(t)
args := []string{ args := []string{
"-http-addr=" + a1.httpAddr, "-http-addr=" + a1.HTTPAddr(),
a2.config.NodeName, a2.Config.NodeName,
} }
code := c.Run(args) code := c.Run(args)
@ -51,12 +52,12 @@ func TestForceLeaveCommandRun(t *testing.T) {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
} }
m := a1.agent.LANMembers() m := a1.LANMembers()
if len(m) != 2 { if len(m) != 2 {
t.Fatalf("should have 2 members: %#v", m) t.Fatalf("should have 2 members: %#v", m)
} }
retry.Run(t, func(r *retry.R) { retry.Run(t, func(r *retry.R) {
m = a1.agent.LANMembers() m = a1.LANMembers()
if got, want := m[1].Status, serf.StatusLeft; got != want { if got, want := m[1].Status, serf.StatusLeft; got != want {
r.Fatalf("got status %q want %q", got, want) r.Fatalf("got status %q want %q", got, want)
} }

View File

@ -4,6 +4,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
@ -13,7 +14,7 @@ func TestInfoCommand_implements(t *testing.T) {
} }
func TestInfoCommandRun(t *testing.T) { func TestInfoCommandRun(t *testing.T) {
a1 := testAgent(t) a1 := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a1.Shutdown()
ui := new(cli.MockUi) ui := new(cli.MockUi)
@ -23,7 +24,7 @@ func TestInfoCommandRun(t *testing.T) {
Flags: base.FlagSetClientHTTP, Flags: base.FlagSetClientHTTP,
}, },
} }
args := []string{"-http-addr=" + a1.httpAddr} args := []string{"-http-addr=" + a1.HTTPAddr()}
code := c.Run(args) code := c.Run(args)
if code != 0 { if code != 0 {

View File

@ -5,6 +5,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
@ -24,15 +25,15 @@ func TestJoinCommand_implements(t *testing.T) {
} }
func TestJoinCommandRun(t *testing.T) { func TestJoinCommandRun(t *testing.T) {
a1 := testAgent(t) a1 := agent.NewTestAgent(t.Name(), nil)
a2 := testAgent(t) a2 := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a1.Shutdown()
defer a2.Shutdown() defer a2.Shutdown()
ui, c := testJoinCommand(t) ui, c := testJoinCommand(t)
args := []string{ args := []string{
"-http-addr=" + a1.httpAddr, "-http-addr=" + a1.HTTPAddr(),
fmt.Sprintf("127.0.0.1:%d", a2.config.Ports.SerfLan), fmt.Sprintf("127.0.0.1:%d", a2.Config.Ports.SerfLan),
} }
code := c.Run(args) code := c.Run(args)
@ -40,22 +41,22 @@ func TestJoinCommandRun(t *testing.T) {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
} }
if len(a1.agent.LANMembers()) != 2 { if len(a1.LANMembers()) != 2 {
t.Fatalf("bad: %#v", a1.agent.LANMembers()) t.Fatalf("bad: %#v", a1.LANMembers())
} }
} }
func TestJoinCommandRun_wan(t *testing.T) { func TestJoinCommandRun_wan(t *testing.T) {
a1 := testAgent(t) a1 := agent.NewTestAgent(t.Name(), nil)
a2 := testAgent(t) a2 := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a1.Shutdown()
defer a2.Shutdown() defer a2.Shutdown()
ui, c := testJoinCommand(t) ui, c := testJoinCommand(t)
args := []string{ args := []string{
"-http-addr=" + a1.httpAddr, "-http-addr=" + a1.HTTPAddr(),
"-wan", "-wan",
fmt.Sprintf("127.0.0.1:%d", a2.config.Ports.SerfWan), fmt.Sprintf("127.0.0.1:%d", a2.Config.Ports.SerfWan),
} }
code := c.Run(args) code := c.Run(args)
@ -63,8 +64,8 @@ func TestJoinCommandRun_wan(t *testing.T) {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
} }
if len(a1.agent.WANMembers()) != 2 { if len(a1.WANMembers()) != 2 {
t.Fatalf("bad: %#v", a1.agent.WANMembers()) t.Fatalf("bad: %#v", a1.WANMembers())
} }
} }

View File

@ -28,13 +28,13 @@ func TestKeyringCommandRun(t *testing.T) {
key2 := "kZyFABeAmc64UMTrm9XuKA==" key2 := "kZyFABeAmc64UMTrm9XuKA=="
// Begin with a single key // Begin with a single key
a1 := testAgentWithConfig(t, func(cfg *agent.Config) { cfg := agent.TestConfig()
cfg.EncryptKey = key1 cfg.EncryptKey = key1
}) a1 := agent.NewTestAgent(t.Name(), cfg)
defer a1.Shutdown() defer a1.Shutdown()
// The LAN and WAN keyrings were initialized with key1 // The LAN and WAN keyrings were initialized with key1
out := listKeys(t, a1.httpAddr) out := listKeys(t, a1.HTTPAddr())
if !strings.Contains(out, "dc1 (LAN):\n "+key1) { if !strings.Contains(out, "dc1 (LAN):\n "+key1) {
t.Fatalf("bad: %#v", out) t.Fatalf("bad: %#v", out)
} }
@ -46,10 +46,10 @@ func TestKeyringCommandRun(t *testing.T) {
} }
// Install the second key onto the keyring // Install the second key onto the keyring
installKey(t, a1.httpAddr, key2) installKey(t, a1.HTTPAddr(), key2)
// Both keys should be present // Both keys should be present
out = listKeys(t, a1.httpAddr) out = listKeys(t, a1.HTTPAddr())
for _, key := range []string{key1, key2} { for _, key := range []string{key1, key2} {
if !strings.Contains(out, key) { if !strings.Contains(out, key) {
t.Fatalf("bad: %#v", out) t.Fatalf("bad: %#v", out)
@ -57,11 +57,11 @@ func TestKeyringCommandRun(t *testing.T) {
} }
// Rotate to key2, remove key1 // Rotate to key2, remove key1
useKey(t, a1.httpAddr, key2) useKey(t, a1.HTTPAddr(), key2)
removeKey(t, a1.httpAddr, key1) removeKey(t, a1.HTTPAddr(), key1)
// Only key2 is present now // Only key2 is present now
out = listKeys(t, a1.httpAddr) out = listKeys(t, a1.HTTPAddr())
if !strings.Contains(out, "dc1 (LAN):\n "+key2) { if !strings.Contains(out, "dc1 (LAN):\n "+key2) {
t.Fatalf("bad: %#v", out) t.Fatalf("bad: %#v", out)
} }

View File

@ -6,6 +6,7 @@ import (
"testing" "testing"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
@ -79,9 +80,9 @@ func TestKVDeleteCommand_Validation(t *testing.T) {
} }
func TestKVDeleteCommand_Run(t *testing.T) { func TestKVDeleteCommand_Run(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui, c := testKVDeleteCommand(t) ui, c := testKVDeleteCommand(t)
@ -95,7 +96,7 @@ func TestKVDeleteCommand_Run(t *testing.T) {
} }
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"foo", "foo",
} }
@ -114,9 +115,9 @@ func TestKVDeleteCommand_Run(t *testing.T) {
} }
func TestKVDeleteCommand_Recurse(t *testing.T) { func TestKVDeleteCommand_Recurse(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui, c := testKVDeleteCommand(t) ui, c := testKVDeleteCommand(t)
@ -134,7 +135,7 @@ func TestKVDeleteCommand_Recurse(t *testing.T) {
} }
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-recurse", "-recurse",
"foo", "foo",
} }
@ -156,9 +157,9 @@ func TestKVDeleteCommand_Recurse(t *testing.T) {
} }
func TestKVDeleteCommand_CAS(t *testing.T) { func TestKVDeleteCommand_CAS(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui, c := testKVDeleteCommand(t) ui, c := testKVDeleteCommand(t)
@ -172,7 +173,7 @@ func TestKVDeleteCommand_CAS(t *testing.T) {
} }
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-cas", "-cas",
"-modify-index", "1", "-modify-index", "1",
"foo", "foo",
@ -193,7 +194,7 @@ func TestKVDeleteCommand_CAS(t *testing.T) {
ui.ErrorWriter.Reset() ui.ErrorWriter.Reset()
args = []string{ args = []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-cas", "-cas",
"-modify-index", strconv.FormatUint(data.ModifyIndex, 10), "-modify-index", strconv.FormatUint(data.ModifyIndex, 10),
"foo", "foo",

View File

@ -6,14 +6,15 @@ import (
"testing" "testing"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
func TestKVExportCommand_Run(t *testing.T) { func TestKVExportCommand_Run(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui := new(cli.MockUi) ui := new(cli.MockUi)
c := KVExportCommand{ c := KVExportCommand{
@ -37,7 +38,7 @@ func TestKVExportCommand_Run(t *testing.T) {
} }
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"foo", "foo",
} }

View File

@ -6,6 +6,7 @@ import (
"testing" "testing"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
@ -67,9 +68,9 @@ func TestKVGetCommand_Validation(t *testing.T) {
} }
func TestKVGetCommand_Run(t *testing.T) { func TestKVGetCommand_Run(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui, c := testKVGetCommand(t) ui, c := testKVGetCommand(t)
@ -83,7 +84,7 @@ func TestKVGetCommand_Run(t *testing.T) {
} }
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"foo", "foo",
} }
@ -99,14 +100,13 @@ func TestKVGetCommand_Run(t *testing.T) {
} }
func TestKVGetCommand_Missing(t *testing.T) { func TestKVGetCommand_Missing(t *testing.T) {
srv, _ := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr)
_, c := testKVGetCommand(t) _, c := testKVGetCommand(t)
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"not-a-real-key", "not-a-real-key",
} }
@ -117,9 +117,9 @@ func TestKVGetCommand_Missing(t *testing.T) {
} }
func TestKVGetCommand_Empty(t *testing.T) { func TestKVGetCommand_Empty(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui, c := testKVGetCommand(t) ui, c := testKVGetCommand(t)
@ -133,7 +133,7 @@ func TestKVGetCommand_Empty(t *testing.T) {
} }
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"empty", "empty",
} }
@ -144,9 +144,9 @@ func TestKVGetCommand_Empty(t *testing.T) {
} }
func TestKVGetCommand_Detailed(t *testing.T) { func TestKVGetCommand_Detailed(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui, c := testKVGetCommand(t) ui, c := testKVGetCommand(t)
@ -160,7 +160,7 @@ func TestKVGetCommand_Detailed(t *testing.T) {
} }
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-detailed", "-detailed",
"foo", "foo",
} }
@ -186,9 +186,9 @@ func TestKVGetCommand_Detailed(t *testing.T) {
} }
func TestKVGetCommand_Keys(t *testing.T) { func TestKVGetCommand_Keys(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui, c := testKVGetCommand(t) ui, c := testKVGetCommand(t)
@ -200,7 +200,7 @@ func TestKVGetCommand_Keys(t *testing.T) {
} }
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-keys", "-keys",
"foo/", "foo/",
} }
@ -219,9 +219,9 @@ func TestKVGetCommand_Keys(t *testing.T) {
} }
func TestKVGetCommand_Recurse(t *testing.T) { func TestKVGetCommand_Recurse(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui, c := testKVGetCommand(t) ui, c := testKVGetCommand(t)
@ -238,7 +238,7 @@ func TestKVGetCommand_Recurse(t *testing.T) {
} }
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-recurse", "-recurse",
"foo", "foo",
} }
@ -257,9 +257,9 @@ func TestKVGetCommand_Recurse(t *testing.T) {
} }
func TestKVGetCommand_RecurseBase64(t *testing.T) { func TestKVGetCommand_RecurseBase64(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui, c := testKVGetCommand(t) ui, c := testKVGetCommand(t)
@ -276,7 +276,7 @@ func TestKVGetCommand_RecurseBase64(t *testing.T) {
} }
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-recurse", "-recurse",
"-base64", "-base64",
"foo", "foo",
@ -296,9 +296,9 @@ func TestKVGetCommand_RecurseBase64(t *testing.T) {
} }
func TestKVGetCommand_DetailedBase64(t *testing.T) { func TestKVGetCommand_DetailedBase64(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui, c := testKVGetCommand(t) ui, c := testKVGetCommand(t)
@ -312,7 +312,7 @@ func TestKVGetCommand_DetailedBase64(t *testing.T) {
} }
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-detailed", "-detailed",
"-base64", "-base64",
"foo", "foo",

View File

@ -4,14 +4,15 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
func TestKVImportCommand_Run(t *testing.T) { func TestKVImportCommand_Run(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
const json = `[ const json = `[
{ {
@ -36,7 +37,7 @@ func TestKVImportCommand_Run(t *testing.T) {
} }
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-", "-",
} }

View File

@ -10,6 +10,7 @@ import (
"testing" "testing"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/hashicorp/consul/testutil" "github.com/hashicorp/consul/testutil"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
@ -84,14 +85,14 @@ func TestKVPutCommand_Validation(t *testing.T) {
} }
func TestKVPutCommand_Run(t *testing.T) { func TestKVPutCommand_Run(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui, c := testKVPutCommand(t) ui, c := testKVPutCommand(t)
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"foo", "bar", "foo", "bar",
} }
@ -111,14 +112,14 @@ func TestKVPutCommand_Run(t *testing.T) {
} }
func TestKVPutCommand_RunEmptyDataQuoted(t *testing.T) { func TestKVPutCommand_RunEmptyDataQuoted(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui, c := testKVPutCommand(t) ui, c := testKVPutCommand(t)
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"foo", "", "foo", "",
} }
@ -138,16 +139,16 @@ func TestKVPutCommand_RunEmptyDataQuoted(t *testing.T) {
} }
func TestKVPutCommand_RunBase64(t *testing.T) { func TestKVPutCommand_RunBase64(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui, c := testKVPutCommand(t) ui, c := testKVPutCommand(t)
const encodedString = "aGVsbG8gd29ybGQK" const encodedString = "aGVsbG8gd29ybGQK"
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-base64", "-base64",
"foo", encodedString, "foo", encodedString,
} }
@ -173,9 +174,9 @@ func TestKVPutCommand_RunBase64(t *testing.T) {
} }
func TestKVPutCommand_File(t *testing.T) { func TestKVPutCommand_File(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui, c := testKVPutCommand(t) ui, c := testKVPutCommand(t)
@ -186,7 +187,7 @@ func TestKVPutCommand_File(t *testing.T) {
} }
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"foo", "@" + f.Name(), "foo", "@" + f.Name(),
} }
@ -224,9 +225,9 @@ func TestKVPutCommand_FileNoExist(t *testing.T) {
} }
func TestKVPutCommand_Stdin(t *testing.T) { func TestKVPutCommand_Stdin(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
stdinR, stdinW := io.Pipe() stdinR, stdinW := io.Pipe()
@ -239,7 +240,7 @@ func TestKVPutCommand_Stdin(t *testing.T) {
}() }()
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"foo", "-", "foo", "-",
} }
@ -259,14 +260,14 @@ func TestKVPutCommand_Stdin(t *testing.T) {
} }
func TestKVPutCommand_NegativeVal(t *testing.T) { func TestKVPutCommand_NegativeVal(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui, c := testKVPutCommand(t) ui, c := testKVPutCommand(t)
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"foo", "-2", "foo", "-2",
} }
@ -286,14 +287,14 @@ func TestKVPutCommand_NegativeVal(t *testing.T) {
} }
func TestKVPutCommand_Flags(t *testing.T) { func TestKVPutCommand_Flags(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui, c := testKVPutCommand(t) ui, c := testKVPutCommand(t)
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-flags", "12345", "-flags", "12345",
"foo", "foo",
} }
@ -314,9 +315,9 @@ func TestKVPutCommand_Flags(t *testing.T) {
} }
func TestKVPutCommand_CAS(t *testing.T) { func TestKVPutCommand_CAS(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
// Create the initial pair so it has a ModifyIndex. // Create the initial pair so it has a ModifyIndex.
pair := &api.KVPair{ pair := &api.KVPair{
@ -330,7 +331,7 @@ func TestKVPutCommand_CAS(t *testing.T) {
ui, c := testKVPutCommand(t) ui, c := testKVPutCommand(t)
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-cas", "-cas",
"-modify-index", "123", "-modify-index", "123",
"foo", "a", "foo", "a",
@ -351,7 +352,7 @@ func TestKVPutCommand_CAS(t *testing.T) {
ui.ErrorWriter.Reset() ui.ErrorWriter.Reset()
args = []string{ args = []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-cas", "-cas",
"-modify-index", strconv.FormatUint(data.ModifyIndex, 10), "-modify-index", strconv.FormatUint(data.ModifyIndex, 10),
"foo", "a", "foo", "a",

View File

@ -4,6 +4,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
@ -23,11 +24,11 @@ func TestLeaveCommand_implements(t *testing.T) {
} }
func TestLeaveCommandRun(t *testing.T) { func TestLeaveCommandRun(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
ui, c := testLeaveCommand(t) ui, c := testLeaveCommand(t)
args := []string{"-http-addr=" + a1.httpAddr} args := []string{"-http-addr=" + a.HTTPAddr()}
code := c.Run(args) code := c.Run(args)
if code != 0 { if code != 0 {
@ -40,11 +41,11 @@ func TestLeaveCommandRun(t *testing.T) {
} }
func TestLeaveCommandFailOnNonFlagArgs(t *testing.T) { func TestLeaveCommandFailOnNonFlagArgs(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
_, c := testLeaveCommand(t) _, c := testLeaveCommand(t)
args := []string{"-http-addr=" + a1.httpAddr, "appserver1"} args := []string{"-http-addr=" + a.HTTPAddr(), "appserver1"}
code := c.Run(args) code := c.Run(args)
if code == 0 { if code == 0 {

View File

@ -9,6 +9,7 @@ import (
"time" "time"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
@ -45,14 +46,13 @@ func TestLockCommand_BadArgs(t *testing.T) {
} }
func TestLockCommand_Run(t *testing.T) { func TestLockCommand_Run(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
waitForLeader(t, a1.httpAddr)
ui, c := testLockCommand(t) ui, c := testLockCommand(t)
filePath := filepath.Join(a1.dir, "test_touch") filePath := filepath.Join(a.Config.DataDir, "test_touch")
touchCmd := fmt.Sprintf("touch '%s'", filePath) touchCmd := fmt.Sprintf("touch '%s'", filePath)
args := []string{"-http-addr=" + a1.httpAddr, "test/prefix", touchCmd} args := []string{"-http-addr=" + a.HTTPAddr(), "test/prefix", touchCmd}
code := c.Run(args) code := c.Run(args)
if code != 0 { if code != 0 {
@ -67,14 +67,13 @@ func TestLockCommand_Run(t *testing.T) {
} }
func TestLockCommand_Try_Lock(t *testing.T) { func TestLockCommand_Try_Lock(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
waitForLeader(t, a1.httpAddr)
ui, c := testLockCommand(t) ui, c := testLockCommand(t)
filePath := filepath.Join(a1.dir, "test_touch") filePath := filepath.Join(a.Config.DataDir, "test_touch")
touchCmd := fmt.Sprintf("touch '%s'", filePath) touchCmd := fmt.Sprintf("touch '%s'", filePath)
args := []string{"-http-addr=" + a1.httpAddr, "-try=10s", "test/prefix", touchCmd} args := []string{"-http-addr=" + a.HTTPAddr(), "-try=10s", "test/prefix", touchCmd}
// Run the command. // Run the command.
var lu *LockUnlock var lu *LockUnlock
@ -98,14 +97,13 @@ func TestLockCommand_Try_Lock(t *testing.T) {
} }
func TestLockCommand_Try_Semaphore(t *testing.T) { func TestLockCommand_Try_Semaphore(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
waitForLeader(t, a1.httpAddr)
ui, c := testLockCommand(t) ui, c := testLockCommand(t)
filePath := filepath.Join(a1.dir, "test_touch") filePath := filepath.Join(a.Config.DataDir, "test_touch")
touchCmd := fmt.Sprintf("touch '%s'", filePath) touchCmd := fmt.Sprintf("touch '%s'", filePath)
args := []string{"-http-addr=" + a1.httpAddr, "-n=3", "-try=10s", "test/prefix", touchCmd} args := []string{"-http-addr=" + a.HTTPAddr(), "-n=3", "-try=10s", "test/prefix", touchCmd}
// Run the command. // Run the command.
var lu *LockUnlock var lu *LockUnlock
@ -129,14 +127,13 @@ func TestLockCommand_Try_Semaphore(t *testing.T) {
} }
func TestLockCommand_MonitorRetry_Lock_Default(t *testing.T) { func TestLockCommand_MonitorRetry_Lock_Default(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
waitForLeader(t, a1.httpAddr)
ui, c := testLockCommand(t) ui, c := testLockCommand(t)
filePath := filepath.Join(a1.dir, "test_touch") filePath := filepath.Join(a.Config.DataDir, "test_touch")
touchCmd := fmt.Sprintf("touch '%s'", filePath) touchCmd := fmt.Sprintf("touch '%s'", filePath)
args := []string{"-http-addr=" + a1.httpAddr, "test/prefix", touchCmd} args := []string{"-http-addr=" + a.HTTPAddr(), "test/prefix", touchCmd}
// Run the command. // Run the command.
var lu *LockUnlock var lu *LockUnlock
@ -161,14 +158,13 @@ func TestLockCommand_MonitorRetry_Lock_Default(t *testing.T) {
} }
func TestLockCommand_MonitorRetry_Semaphore_Default(t *testing.T) { func TestLockCommand_MonitorRetry_Semaphore_Default(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
waitForLeader(t, a1.httpAddr)
ui, c := testLockCommand(t) ui, c := testLockCommand(t)
filePath := filepath.Join(a1.dir, "test_touch") filePath := filepath.Join(a.Config.DataDir, "test_touch")
touchCmd := fmt.Sprintf("touch '%s'", filePath) touchCmd := fmt.Sprintf("touch '%s'", filePath)
args := []string{"-http-addr=" + a1.httpAddr, "-n=3", "test/prefix", touchCmd} args := []string{"-http-addr=" + a.HTTPAddr(), "-n=3", "test/prefix", touchCmd}
// Run the command. // Run the command.
var lu *LockUnlock var lu *LockUnlock
@ -193,14 +189,13 @@ func TestLockCommand_MonitorRetry_Semaphore_Default(t *testing.T) {
} }
func TestLockCommand_MonitorRetry_Lock_Arg(t *testing.T) { func TestLockCommand_MonitorRetry_Lock_Arg(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
waitForLeader(t, a1.httpAddr)
ui, c := testLockCommand(t) ui, c := testLockCommand(t)
filePath := filepath.Join(a1.dir, "test_touch") filePath := filepath.Join(a.Config.DataDir, "test_touch")
touchCmd := fmt.Sprintf("touch '%s'", filePath) touchCmd := fmt.Sprintf("touch '%s'", filePath)
args := []string{"-http-addr=" + a1.httpAddr, "-monitor-retry=9", "test/prefix", touchCmd} args := []string{"-http-addr=" + a.HTTPAddr(), "-monitor-retry=9", "test/prefix", touchCmd}
// Run the command. // Run the command.
var lu *LockUnlock var lu *LockUnlock
@ -225,14 +220,13 @@ func TestLockCommand_MonitorRetry_Lock_Arg(t *testing.T) {
} }
func TestLockCommand_MonitorRetry_Semaphore_Arg(t *testing.T) { func TestLockCommand_MonitorRetry_Semaphore_Arg(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
waitForLeader(t, a1.httpAddr)
ui, c := testLockCommand(t) ui, c := testLockCommand(t)
filePath := filepath.Join(a1.dir, "test_touch") filePath := filepath.Join(a.Config.DataDir, "test_touch")
touchCmd := fmt.Sprintf("touch '%s'", filePath) touchCmd := fmt.Sprintf("touch '%s'", filePath)
args := []string{"-http-addr=" + a1.httpAddr, "-n=3", "-monitor-retry=9", "test/prefix", touchCmd} args := []string{"-http-addr=" + a.HTTPAddr(), "-n=3", "-monitor-retry=9", "test/prefix", touchCmd}
// Run the command. // Run the command.
var lu *LockUnlock var lu *LockUnlock

View File

@ -4,6 +4,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/consul/structs"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
@ -44,28 +45,28 @@ func TestMaintCommandRun_ConflictingArgs(t *testing.T) {
} }
func TestMaintCommandRun_NoArgs(t *testing.T) { func TestMaintCommandRun_NoArgs(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
// Register the service and put it into maintenance mode // Register the service and put it into maintenance mode
service := &structs.NodeService{ service := &structs.NodeService{
ID: "test", ID: "test",
Service: "test", Service: "test",
} }
if err := a1.agent.AddService(service, nil, false, ""); err != nil { if err := a.AddService(service, nil, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
if err := a1.agent.EnableServiceMaintenance("test", "broken 1", ""); err != nil { if err := a.EnableServiceMaintenance("test", "broken 1", ""); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
// Enable node maintenance // Enable node maintenance
a1.agent.EnableNodeMaintenance("broken 2", "") a.EnableNodeMaintenance("broken 2", "")
// Run consul maint with no args (list mode) // Run consul maint with no args (list mode)
ui, c := testMaintCommand(t) ui, c := testMaintCommand(t)
args := []string{"-http-addr=" + a1.httpAddr} args := []string{"-http-addr=" + a.HTTPAddr()}
code := c.Run(args) code := c.Run(args)
if code != 0 { if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
@ -81,7 +82,7 @@ func TestMaintCommandRun_NoArgs(t *testing.T) {
} }
// Ensure the node shows up in the list // Ensure the node shows up in the list
if !strings.Contains(out, a1.config.NodeName) { if !strings.Contains(out, a.Config.NodeName) {
t.Fatalf("bad:\n%s", out) t.Fatalf("bad:\n%s", out)
} }
if !strings.Contains(out, "broken 2") { if !strings.Contains(out, "broken 2") {
@ -90,13 +91,13 @@ func TestMaintCommandRun_NoArgs(t *testing.T) {
} }
func TestMaintCommandRun_EnableNodeMaintenance(t *testing.T) { func TestMaintCommandRun_EnableNodeMaintenance(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
ui, c := testMaintCommand(t) ui, c := testMaintCommand(t)
args := []string{ args := []string{
"-http-addr=" + a1.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-enable", "-enable",
"-reason=broken", "-reason=broken",
} }
@ -111,13 +112,13 @@ func TestMaintCommandRun_EnableNodeMaintenance(t *testing.T) {
} }
func TestMaintCommandRun_DisableNodeMaintenance(t *testing.T) { func TestMaintCommandRun_DisableNodeMaintenance(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
ui, c := testMaintCommand(t) ui, c := testMaintCommand(t)
args := []string{ args := []string{
"-http-addr=" + a1.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-disable", "-disable",
} }
code := c.Run(args) code := c.Run(args)
@ -131,22 +132,22 @@ func TestMaintCommandRun_DisableNodeMaintenance(t *testing.T) {
} }
func TestMaintCommandRun_EnableServiceMaintenance(t *testing.T) { func TestMaintCommandRun_EnableServiceMaintenance(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
// Register the service // Register the service
service := &structs.NodeService{ service := &structs.NodeService{
ID: "test", ID: "test",
Service: "test", Service: "test",
} }
if err := a1.agent.AddService(service, nil, false, ""); err != nil { if err := a.AddService(service, nil, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
ui, c := testMaintCommand(t) ui, c := testMaintCommand(t)
args := []string{ args := []string{
"-http-addr=" + a1.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-enable", "-enable",
"-service=test", "-service=test",
"-reason=broken", "-reason=broken",
@ -162,22 +163,22 @@ func TestMaintCommandRun_EnableServiceMaintenance(t *testing.T) {
} }
func TestMaintCommandRun_DisableServiceMaintenance(t *testing.T) { func TestMaintCommandRun_DisableServiceMaintenance(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
// Register the service // Register the service
service := &structs.NodeService{ service := &structs.NodeService{
ID: "test", ID: "test",
Service: "test", Service: "test",
} }
if err := a1.agent.AddService(service, nil, false, ""); err != nil { if err := a.AddService(service, nil, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
ui, c := testMaintCommand(t) ui, c := testMaintCommand(t)
args := []string{ args := []string{
"-http-addr=" + a1.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-disable", "-disable",
"-service=test", "-service=test",
} }
@ -192,13 +193,13 @@ func TestMaintCommandRun_DisableServiceMaintenance(t *testing.T) {
} }
func TestMaintCommandRun_ServiceMaintenance_NoService(t *testing.T) { func TestMaintCommandRun_ServiceMaintenance_NoService(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
ui, c := testMaintCommand(t) ui, c := testMaintCommand(t)
args := []string{ args := []string{
"-http-addr=" + a1.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-enable", "-enable",
"-service=redis", "-service=redis",
"-reason=broken", "-reason=broken",

View File

@ -5,6 +5,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
@ -24,11 +25,11 @@ func TestMembersCommand_implements(t *testing.T) {
} }
func TestMembersCommandRun(t *testing.T) { func TestMembersCommandRun(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
ui, c := testMembersCommand(t) ui, c := testMembersCommand(t)
args := []string{"-http-addr=" + a1.httpAddr} args := []string{"-http-addr=" + a.HTTPAddr()}
code := c.Run(args) code := c.Run(args)
if code != 0 { if code != 0 {
@ -36,7 +37,7 @@ func TestMembersCommandRun(t *testing.T) {
} }
// Name // Name
if !strings.Contains(ui.OutputWriter.String(), a1.config.NodeName) { if !strings.Contains(ui.OutputWriter.String(), a.Config.NodeName) {
t.Fatalf("bad: %#v", ui.OutputWriter.String()) t.Fatalf("bad: %#v", ui.OutputWriter.String())
} }
@ -52,29 +53,29 @@ func TestMembersCommandRun(t *testing.T) {
} }
func TestMembersCommandRun_WAN(t *testing.T) { func TestMembersCommandRun_WAN(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
ui, c := testMembersCommand(t) ui, c := testMembersCommand(t)
args := []string{"-http-addr=" + a1.httpAddr, "-wan"} args := []string{"-http-addr=" + a.HTTPAddr(), "-wan"}
code := c.Run(args) code := c.Run(args)
if code != 0 { if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
} }
if !strings.Contains(ui.OutputWriter.String(), fmt.Sprintf("%d", a1.config.Ports.SerfWan)) { if !strings.Contains(ui.OutputWriter.String(), fmt.Sprintf("%d", a.Config.Ports.SerfWan)) {
t.Fatalf("bad: %#v", ui.OutputWriter.String()) t.Fatalf("bad: %#v", ui.OutputWriter.String())
} }
} }
func TestMembersCommandRun_statusFilter(t *testing.T) { func TestMembersCommandRun_statusFilter(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
ui, c := testMembersCommand(t) ui, c := testMembersCommand(t)
args := []string{ args := []string{
"-http-addr=" + a1.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-status=a.*e", "-status=a.*e",
} }
@ -83,18 +84,18 @@ func TestMembersCommandRun_statusFilter(t *testing.T) {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
} }
if !strings.Contains(ui.OutputWriter.String(), a1.config.NodeName) { if !strings.Contains(ui.OutputWriter.String(), a.Config.NodeName) {
t.Fatalf("bad: %#v", ui.OutputWriter.String()) t.Fatalf("bad: %#v", ui.OutputWriter.String())
} }
} }
func TestMembersCommandRun_statusFilter_failed(t *testing.T) { func TestMembersCommandRun_statusFilter_failed(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
ui, c := testMembersCommand(t) ui, c := testMembersCommand(t)
args := []string{ args := []string{
"-http-addr=" + a1.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-status=(fail|left)", "-status=(fail|left)",
} }
@ -103,7 +104,7 @@ func TestMembersCommandRun_statusFilter_failed(t *testing.T) {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
} }
if strings.Contains(ui.OutputWriter.String(), a1.config.NodeName) { if strings.Contains(ui.OutputWriter.String(), a.Config.NodeName) {
t.Fatalf("bad: %#v", ui.OutputWriter.String()) t.Fatalf("bad: %#v", ui.OutputWriter.String())
} }

View File

@ -4,6 +4,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
@ -13,9 +14,8 @@ func TestOperator_Autopilot_Get_Implements(t *testing.T) {
} }
func TestOperator_Autopilot_Get(t *testing.T) { func TestOperator_Autopilot_Get(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
waitForLeader(t, a1.httpAddr)
ui := new(cli.MockUi) ui := new(cli.MockUi)
c := OperatorAutopilotGetCommand{ c := OperatorAutopilotGetCommand{
@ -24,7 +24,7 @@ func TestOperator_Autopilot_Get(t *testing.T) {
Flags: base.FlagSetHTTP, Flags: base.FlagSetHTTP,
}, },
} }
args := []string{"-http-addr=" + a1.httpAddr} args := []string{"-http-addr=" + a.HTTPAddr()}
code := c.Run(args) code := c.Run(args)
if code != 0 { if code != 0 {

View File

@ -5,6 +5,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/consul/structs"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
@ -15,9 +16,8 @@ func TestOperator_Autopilot_Set_Implements(t *testing.T) {
} }
func TestOperator_Autopilot_Set(t *testing.T) { func TestOperator_Autopilot_Set(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
waitForLeader(t, a1.httpAddr)
ui := new(cli.MockUi) ui := new(cli.MockUi)
c := OperatorAutopilotSetCommand{ c := OperatorAutopilotSetCommand{
@ -27,7 +27,7 @@ func TestOperator_Autopilot_Set(t *testing.T) {
}, },
} }
args := []string{ args := []string{
"-http-addr=" + a1.httpAddr, "-http-addr=" + a.HTTPAddr(),
"-cleanup-dead-servers=false", "-cleanup-dead-servers=false",
"-max-trailing-logs=99", "-max-trailing-logs=99",
"-last-contact-threshold=123ms", "-last-contact-threshold=123ms",
@ -47,7 +47,7 @@ func TestOperator_Autopilot_Set(t *testing.T) {
Datacenter: "dc1", Datacenter: "dc1",
} }
var reply structs.AutopilotConfig var reply structs.AutopilotConfig
if err := a1.agent.RPC("Operator.AutopilotGetConfiguration", &req, &reply); err != nil { if err := a.RPC("Operator.AutopilotGetConfiguration", &req, &reply); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }

View File

@ -5,6 +5,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
@ -14,17 +15,16 @@ func TestOperator_Raft_ListPeers_Implements(t *testing.T) {
} }
func TestOperator_Raft_ListPeers(t *testing.T) { func TestOperator_Raft_ListPeers(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
waitForLeader(t, a1.httpAddr)
expected := fmt.Sprintf("%s 127.0.0.1:%d 127.0.0.1:%d leader true 2", expected := fmt.Sprintf("%s 127.0.0.1:%d 127.0.0.1:%d leader true 2",
a1.config.NodeName, a1.config.Ports.Server, a1.config.Ports.Server) a.Config.NodeName, a.Config.Ports.Server, a.Config.Ports.Server)
// Test the legacy mode with 'consul operator raft -list-peers' // Test the legacy mode with 'consul operator raft -list-peers'
{ {
ui, c := testOperatorRaftCommand(t) ui, c := testOperatorRaftCommand(t)
args := []string{"-http-addr=" + a1.httpAddr, "-list-peers"} args := []string{"-http-addr=" + a.HTTPAddr(), "-list-peers"}
code := c.Run(args) code := c.Run(args)
if code != 0 { if code != 0 {
@ -45,7 +45,7 @@ func TestOperator_Raft_ListPeers(t *testing.T) {
Flags: base.FlagSetHTTP, Flags: base.FlagSetHTTP,
}, },
} }
args := []string{"-http-addr=" + a1.httpAddr} args := []string{"-http-addr=" + a.HTTPAddr()}
code := c.Run(args) code := c.Run(args)
if code != 0 { if code != 0 {

View File

@ -4,6 +4,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
@ -13,14 +14,13 @@ func TestOperator_Raft_RemovePeer_Implements(t *testing.T) {
} }
func TestOperator_Raft_RemovePeer(t *testing.T) { func TestOperator_Raft_RemovePeer(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
waitForLeader(t, a1.httpAddr)
// Test the legacy mode with 'consul operator raft -remove-peer' // Test the legacy mode with 'consul operator raft -remove-peer'
{ {
ui, c := testOperatorRaftCommand(t) ui, c := testOperatorRaftCommand(t)
args := []string{"-http-addr=" + a1.httpAddr, "-remove-peer", "-address=nope"} args := []string{"-http-addr=" + a.HTTPAddr(), "-remove-peer", "-address=nope"}
code := c.Run(args) code := c.Run(args)
if code != 1 { if code != 1 {
@ -43,7 +43,7 @@ func TestOperator_Raft_RemovePeer(t *testing.T) {
Flags: base.FlagSetHTTP, Flags: base.FlagSetHTTP,
}, },
} }
args := []string{"-http-addr=" + a1.httpAddr, "-address=nope"} args := []string{"-http-addr=" + a.HTTPAddr(), "-address=nope"}
code := c.Run(args) code := c.Run(args)
if code != 1 { if code != 1 {
@ -66,7 +66,7 @@ func TestOperator_Raft_RemovePeer(t *testing.T) {
Flags: base.FlagSetHTTP, Flags: base.FlagSetHTTP,
}, },
} }
args := []string{"-http-addr=" + a1.httpAddr, "-id=nope"} args := []string{"-http-addr=" + a.HTTPAddr(), "-id=nope"}
code := c.Run(args) code := c.Run(args)
if code != 1 { if code != 1 {

View File

@ -4,6 +4,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
@ -13,12 +14,12 @@ func TestReloadCommand_implements(t *testing.T) {
} }
func TestReloadCommandRun(t *testing.T) { func TestReloadCommandRun(t *testing.T) {
a1 := testAgentWithConfig(t, nil) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
// Setup a dummy response to errCh to simulate a successful reload // Setup a dummy response to errCh to simulate a successful reload
go func() { go func() {
errCh := <-a1.agent.ReloadCh() errCh := <-a.ReloadCh()
errCh <- nil errCh <- nil
}() }()
@ -29,7 +30,7 @@ func TestReloadCommandRun(t *testing.T) {
Flags: base.FlagSetClientHTTP, Flags: base.FlagSetClientHTTP,
}, },
} }
args := []string{"-http-addr=" + a1.httpAddr} args := []string{"-http-addr=" + a.HTTPAddr()}
code := c.Run(args) code := c.Run(args)
if code != 0 { if code != 0 {

View File

@ -54,11 +54,10 @@ func TestRTTCommand_Run_BadArgs(t *testing.T) {
func TestRTTCommand_Run_LAN(t *testing.T) { func TestRTTCommand_Run_LAN(t *testing.T) {
updatePeriod := 10 * time.Millisecond updatePeriod := 10 * time.Millisecond
a := testAgentWithConfig(t, func(cfg *agent.Config) { cfg := agent.TestConfig()
cfg.ConsulConfig.CoordinateUpdatePeriod = updatePeriod cfg.ConsulConfig.CoordinateUpdatePeriod = updatePeriod
}) a := agent.NewTestAgent(t.Name(), cfg)
defer a.Shutdown() defer a.Shutdown()
waitForLeader(t, a.httpAddr)
// Inject some known coordinates. // Inject some known coordinates.
c1 := coordinate.NewCoordinate(coordinate.DefaultConfig()) c1 := coordinate.NewCoordinate(coordinate.DefaultConfig())
@ -67,34 +66,34 @@ func TestRTTCommand_Run_LAN(t *testing.T) {
dist_str := fmt.Sprintf("%.3f ms", c1.DistanceTo(c2).Seconds()*1000.0) dist_str := fmt.Sprintf("%.3f ms", c1.DistanceTo(c2).Seconds()*1000.0)
{ {
req := structs.CoordinateUpdateRequest{ req := structs.CoordinateUpdateRequest{
Datacenter: a.config.Datacenter, Datacenter: a.Config.Datacenter,
Node: a.config.NodeName, Node: a.Config.NodeName,
Coord: c1, Coord: c1,
} }
var reply struct{} var reply struct{}
if err := a.agent.RPC("Coordinate.Update", &req, &reply); err != nil { if err := a.RPC("Coordinate.Update", &req, &reply); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
} }
{ {
req := structs.RegisterRequest{ req := structs.RegisterRequest{
Datacenter: a.config.Datacenter, Datacenter: a.Config.Datacenter,
Node: "dogs", Node: "dogs",
Address: "127.0.0.2", Address: "127.0.0.2",
} }
var reply struct{} var reply struct{}
if err := a.agent.RPC("Catalog.Register", &req, &reply); err != nil { if err := a.RPC("Catalog.Register", &req, &reply); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
} }
{ {
var reply struct{} var reply struct{}
req := structs.CoordinateUpdateRequest{ req := structs.CoordinateUpdateRequest{
Datacenter: a.config.Datacenter, Datacenter: a.Config.Datacenter,
Node: "dogs", Node: "dogs",
Coord: c2, Coord: c2,
} }
if err := a.agent.RPC("Coordinate.Update", &req, &reply); err != nil { if err := a.RPC("Coordinate.Update", &req, &reply); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
} }
@ -102,8 +101,8 @@ func TestRTTCommand_Run_LAN(t *testing.T) {
// Ask for the RTT of two known nodes // Ask for the RTT of two known nodes
ui, c := testRTTCommand(t) ui, c := testRTTCommand(t)
args := []string{ args := []string{
"-http-addr=" + a.httpAddr, "-http-addr=" + a.HTTPAddr(),
a.config.NodeName, a.Config.NodeName,
"dogs", "dogs",
} }
// Wait for the updates to get flushed to the data store. // Wait for the updates to get flushed to the data store.
@ -124,7 +123,7 @@ func TestRTTCommand_Run_LAN(t *testing.T) {
{ {
ui, c := testRTTCommand(t) ui, c := testRTTCommand(t)
args := []string{ args := []string{
"-http-addr=" + a.httpAddr, "-http-addr=" + a.HTTPAddr(),
"dogs", "dogs",
} }
code := c.Run(args) code := c.Run(args)
@ -143,8 +142,8 @@ func TestRTTCommand_Run_LAN(t *testing.T) {
{ {
ui, c := testRTTCommand(t) ui, c := testRTTCommand(t)
args := []string{ args := []string{
"-http-addr=" + a.httpAddr, "-http-addr=" + a.HTTPAddr(),
a.config.NodeName, a.Config.NodeName,
"nope", "nope",
} }
code := c.Run(args) code := c.Run(args)
@ -155,11 +154,10 @@ func TestRTTCommand_Run_LAN(t *testing.T) {
} }
func TestRTTCommand_Run_WAN(t *testing.T) { func TestRTTCommand_Run_WAN(t *testing.T) {
a := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a.Shutdown() defer a.Shutdown()
waitForLeader(t, a.httpAddr)
node := fmt.Sprintf("%s.%s", a.config.NodeName, a.config.Datacenter) node := fmt.Sprintf("%s.%s", a.Config.NodeName, a.Config.Datacenter)
// We can't easily inject WAN coordinates, so we will just query the // We can't easily inject WAN coordinates, so we will just query the
// node with itself. // node with itself.
@ -167,7 +165,7 @@ func TestRTTCommand_Run_WAN(t *testing.T) {
ui, c := testRTTCommand(t) ui, c := testRTTCommand(t)
args := []string{ args := []string{
"-wan", "-wan",
"-http-addr=" + a.httpAddr, "-http-addr=" + a.HTTPAddr(),
node, node,
node, node,
} }
@ -187,7 +185,7 @@ func TestRTTCommand_Run_WAN(t *testing.T) {
ui, c := testRTTCommand(t) ui, c := testRTTCommand(t)
args := []string{ args := []string{
"-wan", "-wan",
"-http-addr=" + a.httpAddr, "-http-addr=" + a.HTTPAddr(),
node, node,
} }
code := c.Run(args) code := c.Run(args)
@ -206,7 +204,7 @@ func TestRTTCommand_Run_WAN(t *testing.T) {
ui, c := testRTTCommand(t) ui, c := testRTTCommand(t)
args := []string{ args := []string{
"-wan", "-wan",
"-http-addr=" + a.httpAddr, "-http-addr=" + a.HTTPAddr(),
node, node,
"dc1.nope", "dc1.nope",
} }

View File

@ -7,6 +7,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/hashicorp/consul/testutil" "github.com/hashicorp/consul/testutil"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
@ -69,9 +70,9 @@ func TestSnapshotInspectCommand_Validation(t *testing.T) {
} }
func TestSnapshotInspectCommand_Run(t *testing.T) { func TestSnapshotInspectCommand_Run(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
dir := testutil.TempDir(t, "snapshot") dir := testutil.TempDir(t, "snapshot")
defer os.RemoveAll(dir) defer os.RemoveAll(dir)

View File

@ -7,6 +7,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/hashicorp/consul/testutil" "github.com/hashicorp/consul/testutil"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
@ -69,9 +70,9 @@ func TestSnapshotRestoreCommand_Validation(t *testing.T) {
} }
func TestSnapshotRestoreCommand_Run(t *testing.T) { func TestSnapshotRestoreCommand_Run(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui, c := testSnapshotRestoreCommand(t) ui, c := testSnapshotRestoreCommand(t)
@ -80,7 +81,7 @@ func TestSnapshotRestoreCommand_Run(t *testing.T) {
file := path.Join(dir, "backup.tgz") file := path.Join(dir, "backup.tgz")
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
file, file,
} }

View File

@ -6,6 +6,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/hashicorp/consul/testutil" "github.com/hashicorp/consul/testutil"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
@ -68,9 +69,9 @@ func TestSnapshotSaveCommand_Validation(t *testing.T) {
} }
func TestSnapshotSaveCommand_Run(t *testing.T) { func TestSnapshotSaveCommand_Run(t *testing.T) {
srv, client := testAgentWithAPIClient(t) a := agent.NewTestAgent(t.Name(), nil)
defer srv.Shutdown() defer a.Shutdown()
waitForLeader(t, srv.httpAddr) client := a.Client()
ui, c := testSnapshotSaveCommand(t) ui, c := testSnapshotSaveCommand(t)
@ -79,7 +80,7 @@ func TestSnapshotSaveCommand_Run(t *testing.T) {
file := path.Join(dir, "backup.tgz") file := path.Join(dir, "backup.tgz")
args := []string{ args := []string{
"-http-addr=" + srv.httpAddr, "-http-addr=" + a.HTTPAddr(),
file, file,
} }

View File

@ -1,125 +1,17 @@
package command package command
import ( import (
"fmt"
"math/rand"
"os"
"strings" "strings"
"sync/atomic"
"testing" "testing"
"time"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/consul"
"github.com/hashicorp/consul/logger"
"github.com/hashicorp/consul/testutil"
"github.com/hashicorp/consul/types"
"github.com/hashicorp/consul/version" "github.com/hashicorp/consul/version"
"github.com/hashicorp/go-uuid"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
func init() { func init() {
// Seed the random number generator
rand.Seed(time.Now().UnixNano())
version.Version = "0.8.0" version.Version = "0.8.0"
} }
type server struct {
agent *agent.Agent
config *agent.Config
httpAddr string
dir string
}
func (a *server) Shutdown() {
a.agent.Shutdown()
os.RemoveAll(a.dir)
}
func testAgent(t *testing.T) *server {
return testAgentWithConfig(t, nil)
}
func testAgentWithAPIClient(t *testing.T) (*server, *api.Client) {
agent := testAgentWithConfig(t, func(cfg *agent.Config) {})
client, err := api.NewClient(&api.Config{Address: agent.httpAddr})
if err != nil {
t.Fatalf("consul client: %#v", err)
}
return agent, client
}
func testAgentWithConfig(t *testing.T, cb func(cfg *agent.Config)) *server {
conf := nextConfig()
if cb != nil {
cb(conf)
}
conf.DataDir = testutil.TempDir(t, "agent")
a, err := agent.NewAgent(conf)
if err != nil {
os.RemoveAll(conf.DataDir)
t.Fatal("Error creating agent:", err)
}
a.LogOutput = logger.NewLogWriter(512)
if err := a.Start(); err != nil {
os.RemoveAll(conf.DataDir)
t.Fatalf("Error starting agent: %v", err)
}
conf.Addresses.HTTP = "127.0.0.1"
addr := fmt.Sprintf("%s:%d", conf.Addresses.HTTP, conf.Ports.HTTP)
return &server{agent: a, config: conf, httpAddr: addr, dir: conf.DataDir}
}
var nextPort uint64 = 10000
func nextConfig() *agent.Config {
nodeID, err := uuid.GenerateUUID()
if err != nil {
panic(err)
}
port := int(atomic.AddUint64(&nextPort, 10))
conf := agent.DefaultConfig()
conf.Bootstrap = true
conf.Datacenter = "dc1"
conf.NodeName = fmt.Sprintf("Node %d", port)
conf.NodeID = types.NodeID(nodeID)
conf.BindAddr = "127.0.0.1"
conf.Server = true
conf.Version = version.Version
conf.Ports = agent.PortConfig{
DNS: port + 1,
HTTP: port + 2,
HTTPS: port + 3,
SerfLan: port + 4,
SerfWan: port + 5,
Server: port + 6,
}
cons := consul.DefaultConfig()
conf.ConsulConfig = cons
cons.SerfLANConfig.MemberlistConfig.ProbeTimeout = 100 * time.Millisecond
cons.SerfLANConfig.MemberlistConfig.ProbeInterval = 100 * time.Millisecond
cons.SerfLANConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond
cons.SerfWANConfig.MemberlistConfig.ProbeTimeout = 100 * time.Millisecond
cons.SerfWANConfig.MemberlistConfig.ProbeInterval = 100 * time.Millisecond
cons.SerfWANConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond
cons.RaftConfig.LeaderLeaseTimeout = 20 * time.Millisecond
cons.RaftConfig.HeartbeatTimeout = 40 * time.Millisecond
cons.RaftConfig.ElectionTimeout = 40 * time.Millisecond
return conf
}
func assertNoTabs(t *testing.T, c cli.Command) { func assertNoTabs(t *testing.T, c cli.Command) {
if strings.ContainsRune(c.Help(), '\t') { if strings.ContainsRune(c.Help(), '\t') {
t.Errorf("%#v help output contains tabs", c) t.Errorf("%#v help output contains tabs", c)

View File

@ -4,6 +4,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
@ -13,8 +14,8 @@ func TestWatchCommand_implements(t *testing.T) {
} }
func TestWatchCommandRun(t *testing.T) { func TestWatchCommandRun(t *testing.T) {
a1 := testAgent(t) a := agent.NewTestAgent(t.Name(), nil)
defer a1.Shutdown() defer a.Shutdown()
ui := new(cli.MockUi) ui := new(cli.MockUi)
c := &WatchCommand{ c := &WatchCommand{
@ -23,14 +24,14 @@ func TestWatchCommandRun(t *testing.T) {
Flags: base.FlagSetHTTP, Flags: base.FlagSetHTTP,
}, },
} }
args := []string{"-http-addr=" + a1.httpAddr, "-type=nodes"} args := []string{"-http-addr=" + a.HTTPAddr(), "-type=nodes"}
code := c.Run(args) code := c.Run(args)
if code != 0 { if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
} }
if !strings.Contains(ui.OutputWriter.String(), a1.config.NodeName) { if !strings.Contains(ui.OutputWriter.String(), a.Config.NodeName) {
t.Fatalf("bad: %#v", ui.OutputWriter.String()) t.Fatalf("bad: %#v", ui.OutputWriter.String())
} }
} }