2016-09-26 15:12:40 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2016-09-26 23:00:00 +00:00
|
|
|
"strconv"
|
2016-09-26 15:12:40 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
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"
|
2016-09-26 15:12:40 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2017-02-09 00:26:24 +00:00
|
|
|
func testKVDeleteCommand(t *testing.T) (*cli.MockUi, *KVDeleteCommand) {
|
2017-05-22 23:26:30 +00:00
|
|
|
ui := cli.NewMockUi()
|
2017-02-09 00:26:24 +00:00
|
|
|
return ui, &KVDeleteCommand{
|
|
|
|
Command: base.Command{
|
2017-04-21 00:02:42 +00:00
|
|
|
UI: ui,
|
2017-02-09 00:26:24 +00:00
|
|
|
Flags: base.FlagSetHTTP,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 15:12:40 +00:00
|
|
|
func TestKVDeleteCommand_implements(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2016-09-26 15:12:40 +00:00
|
|
|
var _ cli.Command = &KVDeleteCommand{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKVDeleteCommand_noTabs(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2016-09-26 15:12:40 +00:00
|
|
|
assertNoTabs(t, new(KVDeleteCommand))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKVDeleteCommand_Validation(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2017-02-09 00:26:24 +00:00
|
|
|
ui, c := testKVDeleteCommand(t)
|
2016-09-26 15:12:40 +00:00
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
args []string
|
|
|
|
output string
|
|
|
|
}{
|
|
|
|
"-cas and -recurse": {
|
2016-09-26 23:00:00 +00:00
|
|
|
[]string{"-cas", "-modify-index", "2", "-recurse", "foo"},
|
2016-09-26 15:12:40 +00:00
|
|
|
"Cannot specify both",
|
|
|
|
},
|
2016-09-26 23:00:00 +00:00
|
|
|
"-cas no -modify-index": {
|
|
|
|
[]string{"-cas", "foo"},
|
|
|
|
"Must specify -modify-index",
|
|
|
|
},
|
|
|
|
"-modify-index no -cas": {
|
|
|
|
[]string{"-modify-index", "2", "foo"},
|
|
|
|
"Cannot specify -modify-index without",
|
2016-09-26 15:12:40 +00:00
|
|
|
},
|
|
|
|
"no key": {
|
|
|
|
[]string{},
|
|
|
|
"Missing KEY argument",
|
|
|
|
},
|
|
|
|
"extra args": {
|
|
|
|
[]string{"foo", "bar", "baz"},
|
|
|
|
"Too many arguments",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
// Ensure our buffer is always clear
|
|
|
|
if ui.ErrorWriter != nil {
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
}
|
|
|
|
if ui.OutputWriter != nil {
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(tc.args)
|
|
|
|
if code == 0 {
|
|
|
|
t.Errorf("%s: expected non-zero exit", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
output := ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(output, tc.output) {
|
|
|
|
t.Errorf("%s: expected %q to contain %q", name, output, tc.output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKVDeleteCommand_Run(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2017-05-22 11:59:36 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
client := a.Client()
|
2016-09-26 15:12:40 +00:00
|
|
|
|
2017-02-09 00:26:24 +00:00
|
|
|
ui, c := testKVDeleteCommand(t)
|
2016-09-26 15:12:40 +00:00
|
|
|
|
|
|
|
pair := &api.KVPair{
|
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
}
|
|
|
|
_, err := client.KV().Put(pair, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2016-09-26 15:12:40 +00:00
|
|
|
"foo",
|
|
|
|
}
|
|
|
|
|
|
|
|
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.Fatalf("err: %#v", err)
|
|
|
|
}
|
|
|
|
if pair != nil {
|
|
|
|
t.Fatalf("bad: %#v", pair)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKVDeleteCommand_Recurse(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2017-05-22 11:59:36 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
client := a.Client()
|
2016-09-26 15:12:40 +00:00
|
|
|
|
2017-02-09 00:26:24 +00:00
|
|
|
ui, c := testKVDeleteCommand(t)
|
2016-09-26 15:12:40 +00:00
|
|
|
|
|
|
|
keys := []string{"foo/a", "foo/b", "food"}
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
pair := &api.KVPair{
|
|
|
|
Key: k,
|
|
|
|
Value: []byte("bar"),
|
|
|
|
}
|
|
|
|
_, err := client.KV().Put(pair, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %#v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2016-09-26 15:12:40 +00:00
|
|
|
"-recurse",
|
|
|
|
"foo",
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
pair, _, err := client.KV().Get(k, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %#v", err)
|
|
|
|
}
|
|
|
|
if pair != nil {
|
|
|
|
t.Fatalf("bad: %#v", pair)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-26 23:00:00 +00:00
|
|
|
|
|
|
|
func TestKVDeleteCommand_CAS(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2017-05-22 11:59:36 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
client := a.Client()
|
2016-09-26 23:00:00 +00:00
|
|
|
|
2017-02-09 00:26:24 +00:00
|
|
|
ui, c := testKVDeleteCommand(t)
|
2016-09-26 23:00:00 +00:00
|
|
|
|
|
|
|
pair := &api.KVPair{
|
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
}
|
|
|
|
_, err := client.KV().Put(pair, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2016-09-26 23:00:00 +00:00
|
|
|
"-cas",
|
|
|
|
"-modify-index", "1",
|
|
|
|
"foo",
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code == 0 {
|
|
|
|
t.Fatalf("bad: expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
data, _, err := client.KV().Get("foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset buffers
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
|
|
|
args = []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2016-09-26 23:00:00 +00:00
|
|
|
"-cas",
|
|
|
|
"-modify-index", strconv.FormatUint(data.ModifyIndex, 10),
|
|
|
|
"foo",
|
|
|
|
}
|
|
|
|
|
|
|
|
code = c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
data, _, err = client.KV().Get("foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if data != nil {
|
|
|
|
t.Fatalf("bad: %#v", data)
|
|
|
|
}
|
|
|
|
}
|