command/watch: Adding tests

This commit is contained in:
Armon Dadgar 2014-08-21 16:08:21 -07:00
parent dc5dee5ce4
commit 0cd9faf3a2
2 changed files with 52 additions and 10 deletions

View File

@ -26,12 +26,15 @@ type agentWrapper struct {
config *agent.Config config *agent.Config
agent *agent.Agent agent *agent.Agent
rpc *agent.AgentRPC rpc *agent.AgentRPC
http *agent.HTTPServer
addr string addr string
httpAddr string
} }
func (a *agentWrapper) Shutdown() { func (a *agentWrapper) Shutdown() {
a.rpc.Shutdown() a.rpc.Shutdown()
a.agent.Shutdown() a.agent.Shutdown()
a.http.Shutdown()
os.RemoveAll(a.dir) os.RemoveAll(a.dir)
} }
@ -59,12 +62,22 @@ func testAgent(t *testing.T) *agentWrapper {
} }
rpc := agent.NewAgentRPC(a, l, mult, lw) 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{ return &agentWrapper{
dir: dir, dir: dir,
config: conf, config: conf,
agent: a, agent: a,
rpc: rpc, rpc: rpc,
http: http,
addr: l.Addr().String(), addr: l.Addr().String(),
httpAddr: httpAddr,
} }
} }

29
command/watch_test.go Normal file
View File

@ -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())
}
}