Add config CLI command and encrypt/decrypt sub-commands (#77)
This commit is contained in:
1 parent
8de5371499
commit
8afcbd4c8e
12 files changed
+509
-49
No files matched your search
@@ -0,0 +1,75 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/watchtower/internal/cmd/base"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const (
|
||||
configEncryptPath = "./fixtures/configEncrypt.hcl"
|
||||
configDecryptPath = "./fixtures/configDecrypt.hcl"
|
||||
)
|
||||
|
||||
func TestEncryptDecrypt(t *testing.T) {
|
||||
|
||||
cases := []struct {
|
||||
encrypt bool
|
||||
config string
|
||||
exp string
|
||||
}{
|
||||
{
|
||||
encrypt: true,
|
||||
config: configEncryptPath,
|
||||
exp: "",
|
||||
},
|
||||
{
|
||||
encrypt: false,
|
||||
config: configDecryptPath,
|
||||
exp: configEncryptPath,
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(fmt.Sprintf("Test encrypt %v", c.encrypt), func(t *testing.T) {
|
||||
|
||||
var b bytes.Buffer
|
||||
|
||||
ui := &cli.BasicUi{
|
||||
Reader: bufio.NewReader(os.Stdin),
|
||||
Writer: &b,
|
||||
ErrorWriter: &b,
|
||||
}
|
||||
|
||||
cmd := &EncryptDecryptCommand{
|
||||
Command: base.New(ui),
|
||||
Encrypt: c.encrypt}
|
||||
|
||||
if err := cmd.Run([]string{c.config}); err != 0 {
|
||||
assert.Equal(t, err, 0)
|
||||
}
|
||||
|
||||
got := b.String()
|
||||
assert.Greater(t, len(got), 0)
|
||||
fmt.Printf("%s\n", got)
|
||||
|
||||
// If it's encrypting, then we can assume that it did the right thing since
|
||||
// there are many tests on the underlying codebase for that. If it's not
|
||||
// encrypting, compare it to the cleartext to verify because we can.
|
||||
if !c.encrypt {
|
||||
expected, err := ioutil.ReadFile(c.exp)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, strings.TrimSpace(string(expected)), strings.TrimSpace(string(got)))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user