From 90de483871836f0beb1a1a2e3b7fa659f58cf310 Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Wed, 10 Sep 2014 08:49:16 -0700 Subject: [PATCH] command/keys: begin tests --- command/agent/rpc.go | 4 ++++ command/keys_test.go | 33 +++++++++++++++++++++++++++++++++ command/util_test.go | 7 +++++++ 3 files changed, 44 insertions(+) create mode 100644 command/keys_test.go diff --git a/command/agent/rpc.go b/command/agent/rpc.go index 2382bee488..bf9b42d527 100644 --- a/command/agent/rpc.go +++ b/command/agent/rpc.go @@ -665,6 +665,10 @@ func (i *AgentRPC) handleGossipKeyChange(client *rpcClient, seq uint64, cmd stri queryResp, err = i.agent.RemoveKeyWAN(req.Key) case removeKeyLANCommand: queryResp, err = i.agent.RemoveKeyLAN(req.Key) + default: + respHeader := responseHeader{Seq: seq, Error: unsupportedCommand} + client.Send(&respHeader, nil) + return fmt.Errorf("command '%s' not recognized", cmd) } header := responseHeader{ diff --git a/command/keys_test.go b/command/keys_test.go new file mode 100644 index 0000000000..0d1c464b4b --- /dev/null +++ b/command/keys_test.go @@ -0,0 +1,33 @@ +package command + +import ( + "strings" + "testing" + + "github.com/hashicorp/consul/command/agent" + "github.com/mitchellh/cli" +) + +func TestKeysCommand_implements(t *testing.T) { + var _ cli.Command = &KeysCommand{} +} + +func TestKeysCommand_list(t *testing.T) { + conf := agent.Config{EncryptKey: "HS5lJ+XuTlYKWaeGYyG+/A=="} + + a1 := testAgentWithConfig(&conf, t) + defer a1.Shutdown() + + ui := new(cli.MockUi) + c := &KeysCommand{Ui: ui} + args := []string{"-list", "-rpc-addr=" + a1.addr} + + code := c.Run(args) + if code != 0 { + t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) + } + + if !strings.Contains(ui.OutputWriter.String(), conf.EncryptKey) { + t.Fatalf("bad: %#v", ui.OutputWriter.String()) + } +} diff --git a/command/util_test.go b/command/util_test.go index cd201139bc..388c1e62b5 100644 --- a/command/util_test.go +++ b/command/util_test.go @@ -39,6 +39,10 @@ func (a *agentWrapper) Shutdown() { } func testAgent(t *testing.T) *agentWrapper { + return testAgentWithConfig(nil, t) +} + +func testAgentWithConfig(c *agent.Config, t *testing.T) *agentWrapper { l, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatalf("err: %s", err) @@ -48,6 +52,9 @@ func testAgent(t *testing.T) *agentWrapper { mult := io.MultiWriter(os.Stderr, lw) conf := nextConfig() + if c != nil { + conf = agent.MergeConfig(conf, c) + } dir, err := ioutil.TempDir("", "agent") if err != nil {