github.hscsec.cn/hashicorp/consul@v1.4.5/command/kv/exp/kv_export_test.go (about) 1 package exp 2 3 import ( 4 "encoding/base64" 5 "encoding/json" 6 "strings" 7 "testing" 8 9 "github.com/hashicorp/consul/agent" 10 "github.com/hashicorp/consul/api" 11 "github.com/hashicorp/consul/command/kv/impexp" 12 "github.com/mitchellh/cli" 13 ) 14 15 func TestKVExportCommand_noTabs(t *testing.T) { 16 t.Parallel() 17 if strings.ContainsRune(New(nil).Help(), '\t') { 18 t.Fatal("help has tabs") 19 } 20 } 21 22 func TestKVExportCommand(t *testing.T) { 23 t.Parallel() 24 a := agent.NewTestAgent(t, t.Name(), ``) 25 defer a.Shutdown() 26 client := a.Client() 27 28 ui := cli.NewMockUi() 29 c := New(ui) 30 31 keys := map[string]string{ 32 "foo/a": "a", 33 "foo/b": "b", 34 "foo/c": "c", 35 "bar": "d", 36 } 37 for k, v := range keys { 38 pair := &api.KVPair{Key: k, Value: []byte(v)} 39 if _, err := client.KV().Put(pair, nil); err != nil { 40 t.Fatalf("err: %#v", err) 41 } 42 } 43 44 args := []string{ 45 "-http-addr=" + a.HTTPAddr(), 46 "foo", 47 } 48 49 code := c.Run(args) 50 if code != 0 { 51 t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) 52 } 53 54 output := ui.OutputWriter.String() 55 56 var exported []*impexp.Entry 57 err := json.Unmarshal([]byte(output), &exported) 58 if err != nil { 59 t.Fatalf("bad: %d", code) 60 } 61 62 if len(exported) != 3 { 63 t.Fatalf("bad: expected 3, got %d", len(exported)) 64 } 65 66 for _, entry := range exported { 67 if base64.StdEncoding.EncodeToString([]byte(keys[entry.Key])) != entry.Value { 68 t.Fatalf("bad: expected %s, got %s", keys[entry.Key], entry.Value) 69 } 70 } 71 }