Merge pull request #523 from ChrisHines/windows-api-tests

api: fix tests on Windows.
This commit is contained in:
Alex Dadgar 2015-12-08 19:07:40 -08:00
commit 44788e8694
3 changed files with 66 additions and 15 deletions

View File

@ -1,7 +1,9 @@
package api package api
import ( import (
"encoding/json"
"net/http" "net/http"
"net/http/httptest"
"os" "os"
"testing" "testing"
"time" "time"
@ -46,6 +48,54 @@ func makeClient(t *testing.T, cb1 configCallback,
return client, server return client, server
} }
func TestRequestTime(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(100 * time.Millisecond)
d, err := json.Marshal(struct{ Done bool }{true})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(d)
}))
defer srv.Close()
conf := DefaultConfig()
conf.Address = srv.URL
client, err := NewClient(conf)
if err != nil {
t.Fatalf("err: %v", err)
}
var out interface{}
qm, err := client.query("/", &out, nil)
if err != nil {
t.Fatalf("query err: %v", err)
}
if qm.RequestTime == 0 {
t.Errorf("bad request time: %d", qm.RequestTime)
}
wm, err := client.write("/", struct{ S string }{"input"}, &out, nil)
if err != nil {
t.Fatalf("write err: %v", err)
}
if wm.RequestTime == 0 {
t.Errorf("bad request time: %d", wm.RequestTime)
}
wm, err = client.delete("/", &out, nil)
if err != nil {
t.Fatalf("delete err: %v", err)
}
if wm.RequestTime == 0 {
t.Errorf("bad request time: %d", wm.RequestTime)
}
}
func TestDefaultConfig_env(t *testing.T) { func TestDefaultConfig_env(t *testing.T) {
t.Parallel() t.Parallel()
url := "http://1.2.3.4:5678" url := "http://1.2.3.4:5678"

View File

@ -8,9 +8,6 @@ func assertQueryMeta(t *testing.T, qm *QueryMeta) {
if qm.LastIndex == 0 { if qm.LastIndex == 0 {
t.Fatalf("bad index: %d", qm.LastIndex) t.Fatalf("bad index: %d", qm.LastIndex)
} }
if qm.RequestTime == 0 {
t.Fatalf("bad request time: %d", qm.RequestTime)
}
if !qm.KnownLeader { if !qm.KnownLeader {
t.Fatalf("expected known leader, got none") t.Fatalf("expected known leader, got none")
} }

View File

@ -43,7 +43,7 @@ type TestServerConfig struct {
Stdout, Stderr io.Writer `json:"-"` Stdout, Stderr io.Writer `json:"-"`
} }
// Ports is used to configure the network ports we use. // PortsConfig is used to configure the network ports we use.
type PortsConfig struct { type PortsConfig struct {
HTTP int `json:"http,omitempty"` HTTP int `json:"http,omitempty"`
RPC int `json:"rpc,omitempty"` RPC int `json:"rpc,omitempty"`
@ -91,16 +91,16 @@ func defaultServerConfig() *TestServerConfig {
// TestServer is the main server wrapper struct. // TestServer is the main server wrapper struct.
type TestServer struct { type TestServer struct {
PID int cmd *exec.Cmd
Config *TestServerConfig Config *TestServerConfig
t *testing.T t *testing.T
HTTPAddr string HTTPAddr string
SerfAddr string SerfAddr string
HttpClient *http.Client HTTPClient *http.Client
} }
// NewTestServerConfig creates a new TestServer, and makes a call to // NewTestServer creates a new TestServer, and makes a call to
// an optional callback function to modify the configuration. // an optional callback function to modify the configuration.
func NewTestServer(t *testing.T, cb ServerConfigCallback) *TestServer { func NewTestServer(t *testing.T, cb ServerConfigCallback) *TestServer {
if path, err := exec.LookPath("nomad"); err != nil || path == "" { if path, err := exec.LookPath("nomad"); err != nil || path == "" {
@ -117,6 +117,7 @@ func NewTestServer(t *testing.T, cb ServerConfigCallback) *TestServer {
defer os.RemoveAll(dataDir) defer os.RemoveAll(dataDir)
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
defer configFile.Close()
nomadConfig := defaultServerConfig() nomadConfig := defaultServerConfig()
nomadConfig.DataDir = dataDir nomadConfig.DataDir = dataDir
@ -162,12 +163,12 @@ func NewTestServer(t *testing.T, cb ServerConfigCallback) *TestServer {
server := &TestServer{ server := &TestServer{
Config: nomadConfig, Config: nomadConfig,
PID: cmd.Process.Pid, cmd: cmd,
t: t, t: t,
HTTPAddr: fmt.Sprintf("127.0.0.1:%d", nomadConfig.Ports.HTTP), HTTPAddr: fmt.Sprintf("127.0.0.1:%d", nomadConfig.Ports.HTTP),
SerfAddr: fmt.Sprintf("127.0.0.1:%d", nomadConfig.Ports.Serf), SerfAddr: fmt.Sprintf("127.0.0.1:%d", nomadConfig.Ports.Serf),
HttpClient: client, HTTPClient: client,
} }
// Wait for the server to be ready // Wait for the server to be ready
@ -184,10 +185,13 @@ func NewTestServer(t *testing.T, cb ServerConfigCallback) *TestServer {
func (s *TestServer) Stop() { func (s *TestServer) Stop() {
defer os.RemoveAll(s.Config.DataDir) defer os.RemoveAll(s.Config.DataDir)
cmd := exec.Command("kill", "-9", fmt.Sprintf("%d", s.PID)) if err := s.cmd.Process.Kill(); err != nil {
if err := cmd.Run(); err != nil {
s.t.Errorf("err: %s", err) s.t.Errorf("err: %s", err)
} }
// wait for the process to exit to be sure that the data dir can be
// deleted on all platforms.
s.cmd.Wait()
} }
// waitForAPI waits for only the agent HTTP endpoint to start // waitForAPI waits for only the agent HTTP endpoint to start
@ -195,7 +199,7 @@ func (s *TestServer) Stop() {
// but will likely return before a leader is elected. // but will likely return before a leader is elected.
func (s *TestServer) waitForAPI() { func (s *TestServer) waitForAPI() {
WaitForResult(func() (bool, error) { WaitForResult(func() (bool, error) {
resp, err := s.HttpClient.Get(s.url("/v1/agent/self")) resp, err := s.HTTPClient.Get(s.url("/v1/agent/self"))
if err != nil { if err != nil {
return false, err return false, err
} }
@ -216,7 +220,7 @@ func (s *TestServer) waitForAPI() {
func (s *TestServer) waitForLeader() { func (s *TestServer) waitForLeader() {
WaitForResult(func() (bool, error) { WaitForResult(func() (bool, error) {
// Query the API and check the status code // Query the API and check the status code
resp, err := s.HttpClient.Get(s.url("/v1/jobs")) resp, err := s.HTTPClient.Get(s.url("/v1/jobs"))
if err != nil { if err != nil {
return false, err return false, err
} }
@ -256,7 +260,7 @@ func (s *TestServer) put(path string, body io.Reader) *http.Response {
if err != nil { if err != nil {
s.t.Fatalf("err: %s", err) s.t.Fatalf("err: %s", err)
} }
resp, err := s.HttpClient.Do(req) resp, err := s.HTTPClient.Do(req)
if err != nil { if err != nil {
s.t.Fatalf("err: %s", err) s.t.Fatalf("err: %s", err)
} }
@ -269,7 +273,7 @@ func (s *TestServer) put(path string, body io.Reader) *http.Response {
// get performs a new HTTP GET request. // get performs a new HTTP GET request.
func (s *TestServer) get(path string) *http.Response { func (s *TestServer) get(path string) *http.Response {
resp, err := s.HttpClient.Get(s.url(path)) resp, err := s.HTTPClient.Get(s.url(path))
if err != nil { if err != nil {
s.t.Fatalf("err: %s", err) s.t.Fatalf("err: %s", err)
} }