open-consul/command/kv_export_test.go
Frank Schroeder 846fe5d630
test: use cli.NewMockUi instead of new(cli.MockUi)
Use the constructor function to prevent a nil panic
if there was no write to the output buffers.
2017-05-31 00:29:26 +02:00

69 lines
1.3 KiB
Go

package command
import (
"encoding/base64"
"encoding/json"
"testing"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/agent"
"github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli"
)
func TestKVExportCommand_Run(t *testing.T) {
t.Parallel()
a := agent.NewTestAgent(t.Name(), nil)
defer a.Shutdown()
client := a.Client()
ui := cli.NewMockUi()
c := KVExportCommand{
Command: base.Command{
UI: ui,
Flags: base.FlagSetHTTP,
},
}
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{
"-http-addr=" + a.HTTPAddr(),
"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 {
t.Fatalf("bad: %d", code)
}
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)
}
}
}