2017-01-05 00:24:09 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
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 TestKVImportCommand_Run(t *testing.T) {
|
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
|
|
|
|
|
|
|
const json = `[
|
|
|
|
{
|
|
|
|
"key": "foo",
|
|
|
|
"flags": 0,
|
|
|
|
"value": "YmFyCg=="
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"key": "foo/a",
|
|
|
|
"flags": 0,
|
|
|
|
"value": "YmF6Cg=="
|
|
|
|
}
|
|
|
|
]`
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &KVImportCommand{
|
2017-02-09 00:26:24 +00:00
|
|
|
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
|
|
|
testStdin: strings.NewReader(json),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2017-01-05 00:24:09 +00:00
|
|
|
"-",
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
pair, _, err := client.KV().Get("foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.TrimSpace(string(pair.Value)) != "bar" {
|
|
|
|
t.Fatalf("bad: expected: bar, got %s", pair.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
pair, _, err = client.KV().Get("foo/a", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.TrimSpace(string(pair.Value)) != "baz" {
|
|
|
|
t.Fatalf("bad: expected: baz, got %s", pair.Value)
|
|
|
|
}
|
|
|
|
}
|