From 0cd9faf3a20adc9a8174f87dd407119c1bb68172 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Thu, 21 Aug 2014 16:08:21 -0700 Subject: [PATCH] command/watch: Adding tests --- command/util_test.go | 33 +++++++++++++++++++++++---------- command/watch_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 command/watch_test.go diff --git a/command/util_test.go b/command/util_test.go index 8d492b1733..0366f760bb 100644 --- a/command/util_test.go +++ b/command/util_test.go @@ -22,16 +22,19 @@ func init() { } type agentWrapper struct { - dir string - config *agent.Config - agent *agent.Agent - rpc *agent.AgentRPC - addr string + dir string + config *agent.Config + agent *agent.Agent + rpc *agent.AgentRPC + http *agent.HTTPServer + addr string + httpAddr string } func (a *agentWrapper) Shutdown() { a.rpc.Shutdown() a.agent.Shutdown() + a.http.Shutdown() os.RemoveAll(a.dir) } @@ -59,12 +62,22 @@ func testAgent(t *testing.T) *agentWrapper { } rpc := agent.NewAgentRPC(a, l, mult, lw) + + httpAddr := fmt.Sprintf("127.0.0.1:%d", conf.Ports.HTTP) + http, err := agent.NewHTTPServer(a, "", false, os.Stderr, httpAddr) + if err != nil { + os.RemoveAll(dir) + t.Fatalf(fmt.Sprintf("err: %v", err)) + } + return &agentWrapper{ - dir: dir, - config: conf, - agent: a, - rpc: rpc, - addr: l.Addr().String(), + dir: dir, + config: conf, + agent: a, + rpc: rpc, + http: http, + addr: l.Addr().String(), + httpAddr: httpAddr, } } diff --git a/command/watch_test.go b/command/watch_test.go new file mode 100644 index 0000000000..eaa9376d5a --- /dev/null +++ b/command/watch_test.go @@ -0,0 +1,29 @@ +package command + +import ( + "github.com/mitchellh/cli" + "strings" + "testing" +) + +func TestWatchCommand_implements(t *testing.T) { + var _ cli.Command = &WatchCommand{} +} + +func TestWatchCommandRun(t *testing.T) { + a1 := testAgent(t) + defer a1.Shutdown() + + ui := new(cli.MockUi) + c := &WatchCommand{Ui: ui} + args := []string{"-http-addr=" + a1.httpAddr, "-type=nodes"} + + code := c.Run(args) + if code != 0 { + t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) + } + + if !strings.Contains(ui.OutputWriter.String(), a1.config.NodeName) { + t.Fatalf("bad: %#v", ui.OutputWriter.String()) + } +}