2017-01-05 00:24:09 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
2017-05-22 11:59:36 +00:00
|
|
|
"github.com/hashicorp/consul/command/agent"
|
2017-02-09 00:26:24 +00:00
|
|
|
"github.com/hashicorp/consul/command/base"
|
2017-01-05 00:24:09 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestKVExportCommand_Run(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2017-05-22 11:59:36 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
client := a.Client()
|
2017-01-05 00:24:09 +00:00
|
|
|
|
2017-05-22 23:26:30 +00:00
|
|
|
ui := cli.NewMockUi()
|
2017-02-09 00:26:24 +00:00
|
|
|
c := KVExportCommand{
|
|
|
|
Command: base.Command{
|
2017-04-21 00:02:42 +00:00
|
|
|
UI: ui,
|
2017-02-09 00:26:24 +00:00
|
|
|
Flags: base.FlagSetHTTP,
|
|
|
|
},
|
|
|
|
}
|
2017-01-05 00:24:09 +00:00
|
|
|
|
|
|
|
keys := map[string]string{
|
|
|
|
"foo/a": "a",
|
|
|
|
"foo/b": "b",
|
|
|
|
"foo/c": "c",
|
|
|
|
"bar": "d",
|
|
|
|
}
|
|
|
|
for k, v := range keys {
|
|
|
|
pair := &api.KVPair{Key: k, Value: []byte(v)}
|
|
|
|
if _, err := client.KV().Put(pair, nil); err != nil {
|
|
|
|
t.Fatalf("err: %#v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2017-01-05 00:24:09 +00:00
|
|
|
"foo",
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
output := ui.OutputWriter.String()
|
|
|
|
|
|
|
|
var exported []*kvExportEntry
|
|
|
|
err := json.Unmarshal([]byte(output), &exported)
|
|
|
|
if err != nil {
|
2017-01-05 15:28:12 +00:00
|
|
|
t.Fatalf("bad: %d", code)
|
2017-01-05 00:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(exported) != 3 {
|
|
|
|
t.Fatalf("bad: expected 3, got %d", len(exported))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range exported {
|
|
|
|
if base64.StdEncoding.EncodeToString([]byte(keys[entry.Key])) != entry.Value {
|
|
|
|
t.Fatalf("bad: expected %s, got %s", keys[entry.Key], entry.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|