From 5ab4a590d763a8aa00fef628dd05e4638cca3a99 Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Wed, 17 Sep 2014 23:28:39 -0700 Subject: [PATCH] command: test generated keyring file content and conflicting args for agent --- command/agent/agent_test.go | 17 ++------------ command/agent/command_test.go | 36 +++++++++++++++++++++++++++++ command/keyring.go | 10 ++++---- command/keyring_test.go | 2 +- testutil/keyring.go | 43 +++++++++++++++++++++++++++++++++++ testutil/keyring_test.go | 42 ++++++++++++++++++++++++++++++++++ 6 files changed, 129 insertions(+), 21 deletions(-) create mode 100644 testutil/keyring.go create mode 100644 testutil/keyring_test.go diff --git a/command/agent/agent_test.go b/command/agent/agent_test.go index 1e806a9ce5..8240b4854d 100644 --- a/command/agent/agent_test.go +++ b/command/agent/agent_test.go @@ -1,7 +1,6 @@ package agent import ( - "encoding/json" "fmt" "io" "io/ioutil" @@ -74,11 +73,6 @@ func makeAgentLog(t *testing.T, conf *Config, l io.Writer) (string, *Agent) { } func makeAgentKeyring(t *testing.T, conf *Config, key string) (string, *Agent) { - keyBytes, err := json.Marshal([]string{key}) - if err != nil { - t.Fatalf("err: %s", err) - } - dir, err := ioutil.TempDir("", "agent") if err != nil { t.Fatalf("err: %v", err) @@ -87,18 +81,11 @@ func makeAgentKeyring(t *testing.T, conf *Config, key string) (string, *Agent) { conf.DataDir = dir fileLAN := filepath.Join(dir, SerfLANKeyring) - if err := os.MkdirAll(filepath.Dir(fileLAN), 0700); err != nil { + if err := testutil.InitKeyring(fileLAN, key); err != nil { t.Fatalf("err: %s", err) } - if err := ioutil.WriteFile(fileLAN, keyBytes, 0600); err != nil { - t.Fatalf("err: %s", err) - } - fileWAN := filepath.Join(dir, SerfWANKeyring) - if err := os.MkdirAll(filepath.Dir(fileWAN), 0700); err != nil { - t.Fatalf("err: %s", err) - } - if err := ioutil.WriteFile(fileWAN, keyBytes, 0600); err != nil { + if err := testutil.InitKeyring(fileWAN, key); err != nil { t.Fatalf("err: %s", err) } diff --git a/command/agent/command_test.go b/command/agent/command_test.go index 10557daa78..db7d1c9a5c 100644 --- a/command/agent/command_test.go +++ b/command/agent/command_test.go @@ -1,10 +1,17 @@ package agent import ( +<<<<<<< HEAD "fmt" "io/ioutil" "log" "os" +======= + "github.com/hashicorp/consul/testutil" + "github.com/mitchellh/cli" + "io/ioutil" + "path/filepath" +>>>>>>> command: test generated keyring file content and conflicting args for agent "testing" "github.com/hashicorp/consul/testutil" @@ -38,6 +45,7 @@ func TestValidDatacenter(t *testing.T) { } } +<<<<<<< HEAD func TestRetryJoin(t *testing.T) { dir, agent := makeAgent(t, nextConfig()) defer os.RemoveAll(dir) @@ -161,5 +169,33 @@ func TestRetryJoinWanFail(t *testing.T) { if code := cmd.Run(args); code == 0 { t.Fatalf("bad: %d", code) +======= +func TestArgConflict(t *testing.T) { + ui := new(cli.MockUi) + c := &Command{Ui: ui} + + dir, err := ioutil.TempDir("", "agent") + if err != nil { + t.Fatalf("err: %s", err) + } + + key := "HS5lJ+XuTlYKWaeGYyG+/A==" + + fileLAN := filepath.Join(dir, SerfLANKeyring) + if err := testutil.InitKeyring(fileLAN, key); err != nil { + t.Fatalf("err: %s", err) + } + + args := []string{ + "-encrypt=" + key, + "-data-dir=" + dir, + } + code := c.Run(args) + if code != 1 { + t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) + } + if !strings.Contains(ui.ErrorWriter.String(), "keyring files exist") { + t.Fatalf("bad: %#v", ui.ErrorWriter.String()) +>>>>>>> command: test generated keyring file content and conflicting args for agent } } diff --git a/command/keyring.go b/command/keyring.go index aed55c0397..83293a644c 100644 --- a/command/keyring.go +++ b/command/keyring.go @@ -69,12 +69,12 @@ func (c *KeyringCommand) Run(args []string) int { } fileLAN := filepath.Join(dataDir, agent.SerfLANKeyring) - if err := initializeKeyring(fileLAN, init); err != nil { + if err := initKeyring(fileLAN, init); err != nil { c.Ui.Error(fmt.Sprintf("Error: %s", err)) return 1 } fileWAN := filepath.Join(dataDir, agent.SerfWANKeyring) - if err := initializeKeyring(fileWAN, init); err != nil { + if err := initKeyring(fileWAN, init); err != nil { c.Ui.Error(fmt.Sprintf("Error: %s", err)) return 1 } @@ -214,14 +214,14 @@ func (c *KeyringCommand) listKeysOperation(fn listKeysFunc) int { return 0 } -// initializeKeyring will create a keyring file at a given path. -func initializeKeyring(path, key string) error { +// initKeyring will create a keyring file at a given path. +func initKeyring(path, key string) error { if _, err := base64.StdEncoding.DecodeString(key); err != nil { return fmt.Errorf("Invalid key: %s", err) } keys := []string{key} - keyringBytes, err := json.MarshalIndent(keys, "", " ") + keyringBytes, err := json.Marshal(keys) if err != nil { return err } diff --git a/command/keyring_test.go b/command/keyring_test.go index b98c3c5971..cc18ad7996 100644 --- a/command/keyring_test.go +++ b/command/keyring_test.go @@ -134,7 +134,7 @@ func TestKeyCommandRun_initKeyring(t *testing.T) { t.Fatalf("err: %s", err) } - expected := "[\n \"HS5lJ+XuTlYKWaeGYyG+/A==\"\n]" + expected := `["HS5lJ+XuTlYKWaeGYyG+/A=="]` contentLAN, err := ioutil.ReadFile(fileLAN) if err != nil { diff --git a/testutil/keyring.go b/testutil/keyring.go new file mode 100644 index 0000000000..60486bbb88 --- /dev/null +++ b/testutil/keyring.go @@ -0,0 +1,43 @@ +package testutil + +import ( + "encoding/base64" + "encoding/json" + "fmt" + "os" + "path/filepath" +) + +// InitKeyring will create a keyring file at a given path. +func InitKeyring(path, key string) error { + if _, err := base64.StdEncoding.DecodeString(key); err != nil { + return fmt.Errorf("Invalid key: %s", err) + } + + keys := []string{key} + keyringBytes, err := json.Marshal(keys) + if err != nil { + return err + } + + if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { + return err + } + + if _, err := os.Stat(path); err == nil { + return fmt.Errorf("File already exists: %s", path) + } + + fh, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600) + if err != nil { + return err + } + defer fh.Close() + + if _, err := fh.Write(keyringBytes); err != nil { + os.Remove(path) + return err + } + + return nil +} diff --git a/testutil/keyring_test.go b/testutil/keyring_test.go new file mode 100644 index 0000000000..7e5b3e63fe --- /dev/null +++ b/testutil/keyring_test.go @@ -0,0 +1,42 @@ +package testutil + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" +) + +func TestAgent_InitKeyring(t *testing.T) { + key := "tbLJg26ZJyJ9pK3qhc9jig==" + + dir, err := ioutil.TempDir("", "agent") + if err != nil { + t.Fatalf("err: %s", err) + } + defer os.RemoveAll(dir) + keyFile := filepath.Join(dir, "test/keyring") + + if err := InitKeyring(keyFile, key); err != nil { + t.Fatalf("err: %s", err) + } + + fi, err := os.Stat(filepath.Dir(keyFile)) + if err != nil { + t.Fatalf("err: %s", err) + } + + if !fi.IsDir() { + t.Fatalf("bad: %#v", fi) + } + + data, err := ioutil.ReadFile(keyFile) + if err != nil { + t.Fatalf("err: %s", err) + } + + expected := `["tbLJg26ZJyJ9pK3qhc9jig=="]` + if string(data) != expected { + t.Fatalf("bad: %#v", string(data)) + } +}