From 416ed7692edf6b72528a6238e69fc17a40cf77a2 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Tue, 23 Aug 2016 11:17:31 -0700 Subject: [PATCH] Fix command tests that wait for client to be registered --- testutil/server.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/testutil/server.go b/testutil/server.go index 24b63c780..6bba71122 100644 --- a/testutil/server.go +++ b/testutil/server.go @@ -186,6 +186,11 @@ func NewTestServer(t *testing.T, cb ServerConfigCallback) *TestServer { } else { server.waitForAPI() } + + // Wait for the client to be ready + if nomadConfig.DevMode { + server.waitForClient() + } return server } @@ -249,6 +254,44 @@ func (s *TestServer) waitForLeader() { }) } +// waitForClient waits for the Nomad client to be ready. The function returns +// immediately if the server is not in dev mode. +func (s *TestServer) waitForClient() { + if !s.Config.DevMode { + return + } + + WaitForResult(func() (bool, error) { + resp, err := s.HTTPClient.Get(s.url("/v1/nodes")) + if err != nil { + return false, err + } + defer resp.Body.Close() + if err := s.requireOK(resp); err != nil { + return false, err + } + + var decoded []struct { + ID string + Status string + } + + dec := json.NewDecoder(resp.Body) + if err := dec.Decode(&decoded); err != nil { + return false, err + } + + if len(decoded) != 1 || decoded[0].Status != "ready" { + return false, fmt.Errorf("Node not ready: %v", decoded) + } + + return true, nil + }, func(err error) { + defer s.Stop() + s.t.Fatalf("err: %s", err) + }) +} + // url is a helper function which takes a relative URL and // makes it into a proper URL against the local Nomad server. func (s *TestServer) url(path string) string {