mirror of https://github.com/status-im/consul.git
command: test generated keyring file content and conflicting args for agent
This commit is contained in:
parent
621aafa9b4
commit
5ab4a590d7
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue