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

@ -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,
}
}

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