diff --git a/command/agent/agent_test.go b/command/agent/agent_test.go index 10ee331b8c..1e4aec000c 100644 --- a/command/agent/agent_test.go +++ b/command/agent/agent_test.go @@ -81,11 +81,11 @@ func makeAgentKeyring(t *testing.T, conf *Config, key string) (string, *Agent) { conf.DataDir = dir fileLAN := filepath.Join(dir, serfLANKeyring) - if err := testutil.InitKeyring(fileLAN, key); err != nil { + if err := initKeyring(fileLAN, key); err != nil { t.Fatalf("err: %s", err) } fileWAN := filepath.Join(dir, serfWANKeyring) - if err := testutil.InitKeyring(fileWAN, key); err != nil { + if err := initKeyring(fileWAN, key); err != nil { t.Fatalf("err: %s", err) } diff --git a/command/keyring_test.go b/command/keyring_test.go index cc75d9797e..7f2d4c0748 100644 --- a/command/keyring_test.go +++ b/command/keyring_test.go @@ -1,9 +1,6 @@ package command import ( - "io/ioutil" - "os" - "path/filepath" "strings" "testing" @@ -110,53 +107,6 @@ func TestKeyringCommandRun_initKeyringFail(t *testing.T) { } } -func TestKeyringCommandRun_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 := `["HS5lJ+XuTlYKWaeGYyG+/A=="]` - - 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} diff --git a/testutil/keyring.go b/testutil/keyring.go deleted file mode 100644 index 60486bbb88..0000000000 --- a/testutil/keyring.go +++ /dev/null @@ -1,43 +0,0 @@ -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 deleted file mode 100644 index 7e5b3e63fe..0000000000 --- a/testutil/keyring_test.go +++ /dev/null @@ -1,42 +0,0 @@ -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)) - } -}