testutil: exposing the API address from the test server

pull/881/head
Ryan Uber 2015-03-02 19:20:13 -08:00
parent 37f6301856
commit 78f9f53bf1
2 changed files with 24 additions and 23 deletions

View File

@ -17,9 +17,7 @@ import (
type configCallback func(c *Config) type configCallback func(c *Config)
func makeClient(t *testing.T) (*Client, *testutil.TestServer) { func makeClient(t *testing.T) (*Client, *testutil.TestServer) {
return makeClientWithConfig(t, func(c *Config) { return makeClientWithConfig(t, nil, nil)
c.Address = "127.0.0.1:18800"
}, nil)
} }
func makeClientWithConfig( func makeClientWithConfig(
@ -33,15 +31,16 @@ func makeClientWithConfig(
cb1(conf) cb1(conf)
} }
// Create server
server := testutil.NewTestServerConfig(t, cb2)
conf.Address = server.APIAddr
// Create client // Create client
client, err := NewClient(conf) client, err := NewClient(conf)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// Create server
server := testutil.NewTestServerConfig(t, cb2)
return client, server return client, server
} }

View File

@ -45,20 +45,20 @@ func defaultServerConfig() *TestServerConfig {
Server: true, Server: true,
LogLevel: "debug", LogLevel: "debug",
Ports: &TestPortConfig{ Ports: &TestPortConfig{
DNS: 19000 + idx, DNS: 20000 + idx,
HTTP: 18800 + idx, HTTP: 21000 + idx,
RPC: 18600 + idx, RPC: 22000 + idx,
SerfLan: 18200 + idx, SerfLan: 23000 + idx,
SerfWan: 18400 + idx, SerfWan: 24000 + idx,
Server: 18000 + idx, Server: 25000 + idx,
}, },
} }
} }
type TestServer struct { type TestServer struct {
pid int PID int
dataDir string Config *TestServerConfig
config *TestServerConfig APIAddr string
} }
func NewTestServer(t *testing.T) *TestServer { func NewTestServer(t *testing.T) *TestServer {
@ -67,8 +67,7 @@ func NewTestServer(t *testing.T) *TestServer {
func NewTestServerConfig(t *testing.T, cb ServerConfigCallback) *TestServer { func NewTestServerConfig(t *testing.T, cb ServerConfigCallback) *TestServer {
if path, err := exec.LookPath("consul"); err != nil || path == "" { if path, err := exec.LookPath("consul"); err != nil || path == "" {
t.Log("consul not found on $PATH, skipping") t.Skip("consul not found on $PATH, skipping")
t.SkipNow()
} }
dataDir, err := ioutil.TempDir("", "consul") dataDir, err := ioutil.TempDir("", "consul")
@ -108,28 +107,30 @@ func NewTestServerConfig(t *testing.T, cb ServerConfigCallback) *TestServer {
} }
server := &TestServer{ server := &TestServer{
config: consulConfig, Config: consulConfig,
pid: cmd.Process.Pid, PID: cmd.Process.Pid,
dataDir: dataDir, APIAddr: fmt.Sprintf("127.0.0.1:%d", consulConfig.Ports.HTTP),
} }
// Wait for the server to be ready
if err := server.waitForLeader(); err != nil { if err := server.waitForLeader(); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
return server return server
} }
func (s *TestServer) Stop() { func (s *TestServer) Stop() {
defer os.RemoveAll(s.dataDir) defer os.RemoveAll(s.Config.DataDir)
cmd := exec.Command("kill", "-9", fmt.Sprintf("%d", s.pid)) cmd := exec.Command("kill", "-9", fmt.Sprintf("%d", s.PID))
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
panic(err) panic(err)
} }
} }
func (s *TestServer) waitForLeader() error { func (s *TestServer) waitForLeader() error {
url := fmt.Sprintf("http://127.0.0.1:%d/v1/catalog/nodes", s.config.Ports.HTTP) url := fmt.Sprintf("http://127.0.0.1:%d/v1/catalog/nodes", s.Config.Ports.HTTP)
WaitForResult(func() (bool, error) { WaitForResult(func() (bool, error) {
resp, err := http.Get(url) resp, err := http.Get(url)
@ -149,6 +150,7 @@ func (s *TestServer) waitForLeader() error {
return true, nil return true, nil
}, func(err error) { }, func(err error) {
s.Stop()
panic(err) panic(err)
}) })