2
0
mirror of https://github.com/status-im/consul.git synced 2025-01-13 07:14:37 +00:00

agent: fix test cases

This commit is contained in:
Ryan Uber 2014-10-04 15:45:26 -07:00
parent c945311baf
commit 0b8fe6cc83
4 changed files with 2 additions and 137 deletions

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

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

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

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