Skip to content

Commit

Permalink
testutil: exposing the API address from the test server
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanuber committed Mar 20, 2015
1 parent 37f6301 commit 78f9f53
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
11 changes: 5 additions & 6 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ import (
type configCallback func(c *Config)

func makeClient(t *testing.T) (*Client, *testutil.TestServer) {
return makeClientWithConfig(t, func(c *Config) {
c.Address = "127.0.0.1:18800"
}, nil)
return makeClientWithConfig(t, nil, nil)
}

func makeClientWithConfig(
Expand All @@ -33,15 +31,16 @@ func makeClientWithConfig(
cb1(conf)
}

// Create server
server := testutil.NewTestServerConfig(t, cb2)
conf.Address = server.APIAddr

// Create client
client, err := NewClient(conf)
if err != nil {
t.Fatalf("err: %v", err)
}

// Create server
server := testutil.NewTestServerConfig(t, cb2)

return client, server
}

Expand Down
36 changes: 19 additions & 17 deletions testutil/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ func defaultServerConfig() *TestServerConfig {
Server: true,
LogLevel: "debug",
Ports: &TestPortConfig{
DNS: 19000 + idx,
HTTP: 18800 + idx,
RPC: 18600 + idx,
SerfLan: 18200 + idx,
SerfWan: 18400 + idx,
Server: 18000 + idx,
DNS: 20000 + idx,
HTTP: 21000 + idx,
RPC: 22000 + idx,
SerfLan: 23000 + idx,
SerfWan: 24000 + idx,
Server: 25000 + idx,
},
}
}

type TestServer struct {
pid int
dataDir string
config *TestServerConfig
PID int
Config *TestServerConfig
APIAddr string
}

func NewTestServer(t *testing.T) *TestServer {
Expand All @@ -67,8 +67,7 @@ func NewTestServer(t *testing.T) *TestServer {

func NewTestServerConfig(t *testing.T, cb ServerConfigCallback) *TestServer {
if path, err := exec.LookPath("consul"); err != nil || path == "" {
t.Log("consul not found on $PATH, skipping")
t.SkipNow()
t.Skip("consul not found on $PATH, skipping")
}

dataDir, err := ioutil.TempDir("", "consul")
Expand Down Expand Up @@ -108,28 +107,30 @@ func NewTestServerConfig(t *testing.T, cb ServerConfigCallback) *TestServer {
}

server := &TestServer{
config: consulConfig,
pid: cmd.Process.Pid,
dataDir: dataDir,
Config: consulConfig,
PID: cmd.Process.Pid,
APIAddr: fmt.Sprintf("127.0.0.1:%d", consulConfig.Ports.HTTP),
}

// Wait for the server to be ready
if err := server.waitForLeader(); err != nil {
t.Fatalf("err: %s", err)
}

return server
}

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 {
panic(err)
}
}

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) {
resp, err := http.Get(url)
Expand All @@ -149,6 +150,7 @@ func (s *TestServer) waitForLeader() error {

return true, nil
}, func(err error) {
s.Stop()
panic(err)
})

Expand Down

0 comments on commit 78f9f53

Please sign in to comment.