github.com/kjdelisle/consul@v1.4.5/command/kv/imp/kv_import_test.go (about) 1 package imp 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/hashicorp/consul/agent" 8 "github.com/mitchellh/cli" 9 ) 10 11 func TestKVImportCommand_noTabs(t *testing.T) { 12 t.Parallel() 13 if strings.ContainsRune(New(nil).Help(), '\t') { 14 t.Fatal("help has tabs") 15 } 16 } 17 18 func TestKVImportCommand(t *testing.T) { 19 t.Parallel() 20 a := agent.NewTestAgent(t, t.Name(), ``) 21 defer a.Shutdown() 22 client := a.Client() 23 24 const json = `[ 25 { 26 "key": "foo", 27 "flags": 0, 28 "value": "YmFyCg==" 29 }, 30 { 31 "key": "foo/a", 32 "flags": 0, 33 "value": "YmF6Cg==" 34 } 35 ]` 36 37 ui := cli.NewMockUi() 38 c := New(ui) 39 c.testStdin = strings.NewReader(json) 40 41 args := []string{ 42 "-http-addr=" + a.HTTPAddr(), 43 "-", 44 } 45 46 code := c.Run(args) 47 if code != 0 { 48 t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) 49 } 50 51 pair, _, err := client.KV().Get("foo", nil) 52 if err != nil { 53 t.Fatal(err) 54 } 55 56 if strings.TrimSpace(string(pair.Value)) != "bar" { 57 t.Fatalf("bad: expected: bar, got %s", pair.Value) 58 } 59 60 pair, _, err = client.KV().Get("foo/a", nil) 61 if err != nil { 62 t.Fatal(err) 63 } 64 65 if strings.TrimSpace(string(pair.Value)) != "baz" { 66 t.Fatalf("bad: expected: baz, got %s", pair.Value) 67 } 68 }