From 4c3ec248a561a085acb061a38e84d695b1ba99a3 Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Fri, 16 Jan 2015 09:58:37 -0800 Subject: [PATCH] agent: fixing up tests --- api/api_test.go | 1 - command/agent/agent_test.go | 1 - command/agent/http_test.go | 58 ++++++++++++++++++++++++++++++ command/agent/rpc_client_test.go | 62 +++++++++++++++++--------------- 4 files changed, 91 insertions(+), 31 deletions(-) diff --git a/api/api_test.go b/api/api_test.go index a8c826ed20..722d78b56b 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -139,7 +139,6 @@ func makeClientWithConfig(t *testing.T, cb1 configCallback, cb2 serverConfigCall // Make client config conf := DefaultConfig() cb1(conf) - fmt.Printf("%#v\n", conf.HttpClient.Transport) // Create client client, err := NewClient(conf) diff --git a/command/agent/agent_test.go b/command/agent/agent_test.go index d7b7d91539..0add36702c 100644 --- a/command/agent/agent_test.go +++ b/command/agent/agent_test.go @@ -9,7 +9,6 @@ import ( "os" "path/filepath" "reflect" - "runtime" "sync/atomic" "testing" "time" diff --git a/command/agent/http_test.go b/command/agent/http_test.go index eca844f1d3..fc5067b46d 100644 --- a/command/agent/http_test.go +++ b/command/agent/http_test.go @@ -6,10 +6,12 @@ import ( "fmt" "io" "io/ioutil" + "net" "net/http" "net/http/httptest" "os" "path/filepath" + "runtime" "strconv" "testing" "time" @@ -19,7 +21,15 @@ import ( ) func makeHTTPServer(t *testing.T) (string, *HTTPServer) { + return makeHTTPServerWithConfig(t, nil) +} + +func makeHTTPServerWithConfig(t *testing.T, cb func(c *Config)) (string, *HTTPServer) { conf := nextConfig() + if cb != nil { + cb(conf) + } + dir, agent := makeAgent(t, conf) uiDir := filepath.Join(dir, "ui") if err := os.Mkdir(uiDir, 755); err != nil { @@ -43,6 +53,54 @@ func encodeReq(obj interface{}) io.ReadCloser { return ioutil.NopCloser(buf) } +func TestHTTPServer_UnixSocket(t *testing.T) { + if runtime.GOOS == "windows" { + t.SkipNow() + } + + tempDir, err := ioutil.TempDir("", "consul") + if err != nil { + t.Fatalf("err: %s", err) + } + defer os.RemoveAll(tempDir) + socket := filepath.Join(tempDir, "test.sock") + + dir, srv := makeHTTPServerWithConfig(t, func(c *Config) { + c.Addresses.HTTP = "unix://" + socket + }) + defer os.RemoveAll(dir) + defer srv.Shutdown() + defer srv.agent.Shutdown() + + // Ensure the socket was created + if _, err := os.Stat(socket); err != nil { + t.Fatalf("err: %s", err) + } + + // Ensure we can get a response from the socket. + path, _ := unixSocketAddr(srv.agent.config.Addresses.HTTP) + client := &http.Client{ + Transport: &http.Transport{ + Dial: func(_, _ string) (net.Conn, error) { + return net.Dial("unix", path) + }, + }, + } + + // This URL doesn't look like it makes sense, but the scheme (http://) and + // the host (127.0.0.1) are required by the HTTP client library. In reality + // this will just use the custom dialer and talk to the socket. + resp, err := client.Get("http://127.0.0.1/v1/agent/self") + if err != nil { + t.Fatalf("err: %s", err) + } + defer resp.Body.Close() + + if body, err := ioutil.ReadAll(resp.Body); err != nil || len(body) == 0 { + t.Fatalf("bad: %s %v", body, err) + } +} + func TestSetIndex(t *testing.T) { resp := httptest.NewRecorder() setIndex(resp, 1000) diff --git a/command/agent/rpc_client_test.go b/command/agent/rpc_client_test.go index 8516943a18..48d8335649 100644 --- a/command/agent/rpc_client_test.go +++ b/command/agent/rpc_client_test.go @@ -9,6 +9,7 @@ import ( "io/ioutil" "net" "os" + "path/filepath" "runtime" "strings" "testing" @@ -68,6 +69,38 @@ func testRPCClientWithConfig(t *testing.T, cb func(c *Config)) *rpcParts { } } +func TestRPCClient_UnixSocket(t *testing.T) { + if runtime.GOOS == "windows" { + t.SkipNow() + } + + tempDir, err := ioutil.TempDir("", "consul") + if err != nil { + t.Fatalf("err: %s", err) + } + defer os.RemoveAll(tempDir) + socket := filepath.Join(tempDir, "test.sock") + + p1 := testRPCClientWithConfig(t, func(c *Config) { + c.Addresses.RPC = "unix://" + socket + }) + defer p1.Close() + + // Ensure the socket was created + if _, err := os.Stat(socket); err != nil { + t.Fatalf("err: %s", err) + } + + // Ensure we can talk with the socket + mem, err := p1.client.LANMembers() + if err != nil { + t.Fatalf("err: %s", err) + } + if len(mem) != 1 { + t.Fatalf("bad: %#v", mem) + } +} + func TestRPCClientForceLeave(t *testing.T) { p1 := testRPCClient(t) p2 := testRPCClient(t) @@ -215,35 +248,6 @@ func TestRPCClientStats(t *testing.T) { } } -func TestRPCClientStatsUnix(t *testing.T) { - if runtime.GOOS == "windows" { - t.SkipNow() - } - - tempdir, err := ioutil.TempDir("", "consul-test-") - if err != nil { - t.Fatalf("err: %s", err) - } - - p1 := testRPCClientWithConfig(t, func(c *Config) { - c.Addresses.RPC = fmt.Sprintf("unix://%s/test.sock", tempdir) - }) - defer p1.Close() - - stats, err := p1.client.Stats() - if err != nil { - t.Fatalf("err: %s", err) - } - - if _, ok := stats["agent"]; !ok { - t.Fatalf("bad: %#v", stats) - } - - if _, ok := stats["consul"]; !ok { - t.Fatalf("bad: %#v", stats) - } -} - func TestRPCClientLeave(t *testing.T) { p1 := testRPCClient(t) defer p1.Close()