github.hscsec.cn/hashicorp/consul@v1.4.5/command/kv/del/kv_delete_test.go (about) 1 package del 2 3 import ( 4 "strconv" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/consul/agent" 9 "github.com/hashicorp/consul/api" 10 "github.com/mitchellh/cli" 11 ) 12 13 func TestKVDeleteCommand_noTabs(t *testing.T) { 14 t.Parallel() 15 if strings.ContainsRune(New(nil).Help(), '\t') { 16 t.Fatal("help has tabs") 17 } 18 } 19 20 func TestKVDeleteCommand_Validation(t *testing.T) { 21 t.Parallel() 22 ui := cli.NewMockUi() 23 c := New(ui) 24 25 cases := map[string]struct { 26 args []string 27 output string 28 }{ 29 "-cas and -recurse": { 30 []string{"-cas", "-modify-index", "2", "-recurse", "foo"}, 31 "Cannot specify both", 32 }, 33 "-cas no -modify-index": { 34 []string{"-cas", "foo"}, 35 "Must specify -modify-index", 36 }, 37 "-modify-index no -cas": { 38 []string{"-modify-index", "2", "foo"}, 39 "Cannot specify -modify-index without", 40 }, 41 "no key": { 42 []string{}, 43 "Missing KEY argument", 44 }, 45 "extra args": { 46 []string{"foo", "bar", "baz"}, 47 "Too many arguments", 48 }, 49 } 50 51 for name, tc := range cases { 52 c.init() 53 // Ensure our buffer is always clear 54 if ui.ErrorWriter != nil { 55 ui.ErrorWriter.Reset() 56 } 57 if ui.OutputWriter != nil { 58 ui.OutputWriter.Reset() 59 } 60 61 code := c.Run(tc.args) 62 if code == 0 { 63 t.Errorf("%s: expected non-zero exit", name) 64 } 65 66 output := ui.ErrorWriter.String() 67 if !strings.Contains(output, tc.output) { 68 t.Errorf("%s: expected %q to contain %q", name, output, tc.output) 69 } 70 } 71 } 72 73 func TestKVDeleteCommand(t *testing.T) { 74 t.Parallel() 75 a := agent.NewTestAgent(t, t.Name(), ``) 76 defer a.Shutdown() 77 client := a.Client() 78 79 ui := cli.NewMockUi() 80 c := New(ui) 81 82 pair := &api.KVPair{ 83 Key: "foo", 84 Value: []byte("bar"), 85 } 86 _, err := client.KV().Put(pair, nil) 87 if err != nil { 88 t.Fatalf("err: %#v", err) 89 } 90 91 args := []string{ 92 "-http-addr=" + a.HTTPAddr(), 93 "foo", 94 } 95 96 code := c.Run(args) 97 if code != 0 { 98 t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) 99 } 100 101 pair, _, err = client.KV().Get("foo", nil) 102 if err != nil { 103 t.Fatalf("err: %#v", err) 104 } 105 if pair != nil { 106 t.Fatalf("bad: %#v", pair) 107 } 108 } 109 110 func TestKVDeleteCommand_Recurse(t *testing.T) { 111 t.Parallel() 112 a := agent.NewTestAgent(t, t.Name(), ``) 113 defer a.Shutdown() 114 client := a.Client() 115 116 ui := cli.NewMockUi() 117 c := New(ui) 118 119 keys := []string{"foo/a", "foo/b", "food"} 120 121 for _, k := range keys { 122 pair := &api.KVPair{ 123 Key: k, 124 Value: []byte("bar"), 125 } 126 _, err := client.KV().Put(pair, nil) 127 if err != nil { 128 t.Fatalf("err: %#v", err) 129 } 130 } 131 132 args := []string{ 133 "-http-addr=" + a.HTTPAddr(), 134 "-recurse", 135 "foo", 136 } 137 138 code := c.Run(args) 139 if code != 0 { 140 t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) 141 } 142 143 for _, k := range keys { 144 pair, _, err := client.KV().Get(k, nil) 145 if err != nil { 146 t.Fatalf("err: %#v", err) 147 } 148 if pair != nil { 149 t.Fatalf("bad: %#v", pair) 150 } 151 } 152 } 153 154 func TestKVDeleteCommand_CAS(t *testing.T) { 155 t.Parallel() 156 a := agent.NewTestAgent(t, t.Name(), ``) 157 defer a.Shutdown() 158 client := a.Client() 159 160 ui := cli.NewMockUi() 161 c := New(ui) 162 163 pair := &api.KVPair{ 164 Key: "foo", 165 Value: []byte("bar"), 166 } 167 _, err := client.KV().Put(pair, nil) 168 if err != nil { 169 t.Fatalf("err: %#v", err) 170 } 171 172 args := []string{ 173 "-http-addr=" + a.HTTPAddr(), 174 "-cas", 175 "-modify-index", "1", 176 "foo", 177 } 178 179 code := c.Run(args) 180 if code == 0 { 181 t.Fatalf("bad: expected error") 182 } 183 184 data, _, err := client.KV().Get("foo", nil) 185 if err != nil { 186 t.Fatal(err) 187 } 188 189 // Reset buffers 190 ui.OutputWriter.Reset() 191 ui.ErrorWriter.Reset() 192 193 args = []string{ 194 "-http-addr=" + a.HTTPAddr(), 195 "-cas", 196 "-modify-index", strconv.FormatUint(data.ModifyIndex, 10), 197 "foo", 198 } 199 200 code = c.Run(args) 201 if code != 0 { 202 t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) 203 } 204 205 data, _, err = client.KV().Get("foo", nil) 206 if err != nil { 207 t.Fatal(err) 208 } 209 if data != nil { 210 t.Fatalf("bad: %#v", data) 211 } 212 }