From 77519a5439407240c00a190e008e16922f4910b8 Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Sat, 13 Sep 2014 11:47:37 -0700 Subject: [PATCH] command/keyring: add tests for init --- command/keyring_test.go | 69 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/command/keyring_test.go b/command/keyring_test.go index d3f01ade92..b98c3c5971 100644 --- a/command/keyring_test.go +++ b/command/keyring_test.go @@ -1,6 +1,9 @@ package command import ( + "io/ioutil" + "os" + "path/filepath" "strings" "testing" @@ -84,6 +87,72 @@ func TestKeyringCommandRun_failedConnection(t *testing.T) { } } +func TestKeyCommandRun_initKeyringFail(t *testing.T) { + ui := new(cli.MockUi) + c := &KeyringCommand{Ui: ui} + + // Should error if no data-dir given + args := []string{"-init=HS5lJ+XuTlYKWaeGYyG+/A=="} + code := c.Run(args) + if code != 1 { + t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) + } + + // Errors on invalid key + args = []string{"-init=xyz", "-data-dir=/tmp"} + code = c.Run(args) + if code != 1 { + t.Fatalf("should have errored") + } +} + +func TestKeyCommandRun_initKeyring(t *testing.T) { + ui := new(cli.MockUi) + c := &KeyringCommand{Ui: ui} + + tempDir, err := ioutil.TempDir("", "consul") + if err != nil { + t.Fatalf("err: %s", err) + } + defer os.RemoveAll(tempDir) + + args := []string{ + "-init=HS5lJ+XuTlYKWaeGYyG+/A==", + "-data-dir=" + tempDir, + } + code := c.Run(args) + if code != 0 { + t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) + } + + fileLAN := filepath.Join(tempDir, agent.SerfLANKeyring) + fileWAN := filepath.Join(tempDir, agent.SerfWANKeyring) + if _, err := os.Stat(fileLAN); err != nil { + t.Fatalf("err: %s", err) + } + if _, err := os.Stat(fileWAN); err != nil { + t.Fatalf("err: %s", err) + } + + expected := "[\n \"HS5lJ+XuTlYKWaeGYyG+/A==\"\n]" + + contentLAN, err := ioutil.ReadFile(fileLAN) + if err != nil { + t.Fatalf("err: %s", err) + } + if string(contentLAN) != expected { + t.Fatalf("bad: %#v", string(contentLAN)) + } + + contentWAN, err := ioutil.ReadFile(fileWAN) + if err != nil { + t.Fatalf("err: %s", err) + } + if string(contentWAN) != expected { + t.Fatalf("bad: %#v", string(contentWAN)) + } +} + func listKeys(t *testing.T, addr string) string { ui := new(cli.MockUi) c := &KeyringCommand{Ui: ui}