2016-09-26 15:12:29 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-01-04 22:05:08 +00:00
|
|
|
"encoding/base64"
|
2016-09-26 15:12:29 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"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"
|
2017-05-12 13:41:13 +00:00
|
|
|
"github.com/hashicorp/consul/testutil"
|
2016-09-26 15:12:29 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2017-02-09 00:26:24 +00:00
|
|
|
func testKVPutCommand(t *testing.T) (*cli.MockUi, *KVPutCommand) {
|
2017-05-22 23:26:30 +00:00
|
|
|
ui := cli.NewMockUi()
|
2017-02-09 00:26:24 +00:00
|
|
|
return ui, &KVPutCommand{
|
|
|
|
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:29 +00:00
|
|
|
func TestKVPutCommand_implements(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2016-09-26 15:12:29 +00:00
|
|
|
var _ cli.Command = &KVPutCommand{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKVPutCommand_noTabs(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2017-02-09 00:26:24 +00:00
|
|
|
assertNoTabs(t, new(KVDeleteCommand))
|
2016-09-26 15:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestKVPutCommand_Validation(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2017-02-09 00:26:24 +00:00
|
|
|
ui, c := testKVPutCommand(t)
|
2016-09-26 15:12:29 +00:00
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
args []string
|
|
|
|
output string
|
|
|
|
}{
|
|
|
|
"-acquire without -session": {
|
|
|
|
[]string{"-acquire", "foo"},
|
|
|
|
"Missing -session",
|
|
|
|
},
|
|
|
|
"-release without -session": {
|
|
|
|
[]string{"-release", "foo"},
|
|
|
|
"Missing -session",
|
|
|
|
},
|
2016-09-26 23:06:32 +00:00
|
|
|
"-cas no -modify-index": {
|
|
|
|
[]string{"-cas", "foo"},
|
|
|
|
"Must specify -modify-index",
|
|
|
|
},
|
2016-09-26 15:12:29 +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 TestKVPutCommand_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:29 +00:00
|
|
|
|
2017-02-09 00:26:24 +00:00
|
|
|
ui, c := testKVPutCommand(t)
|
2016-09-26 15:12:29 +00:00
|
|
|
|
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2016-09-26 15:12:29 +00:00
|
|
|
"foo", "bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
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 !bytes.Equal(data.Value, []byte("bar")) {
|
|
|
|
t.Errorf("bad: %#v", data.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-05 13:58:22 +00:00
|
|
|
func TestKVPutCommand_RunEmptyDataQuoted(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()
|
2017-01-05 13:58:22 +00:00
|
|
|
|
2017-02-09 00:26:24 +00:00
|
|
|
ui, c := testKVPutCommand(t)
|
2017-01-05 13:58:22 +00:00
|
|
|
|
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2017-01-05 13:58:22 +00:00
|
|
|
"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.Value != nil {
|
|
|
|
t.Errorf("bad: %#v", data.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 22:05:08 +00:00
|
|
|
func TestKVPutCommand_RunBase64(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()
|
2017-01-04 22:05:08 +00:00
|
|
|
|
2017-02-09 00:26:24 +00:00
|
|
|
ui, c := testKVPutCommand(t)
|
2017-01-04 22:05:08 +00:00
|
|
|
|
|
|
|
const encodedString = "aGVsbG8gd29ybGQK"
|
|
|
|
|
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2017-01-04 22:05:08 +00:00
|
|
|
"-base64",
|
|
|
|
"foo", encodedString,
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected, err := base64.StdEncoding.DecodeString(encodedString)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(data.Value, []byte(expected)) {
|
|
|
|
t.Errorf("bad: %#v, %s", data.Value, data.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 15:12:29 +00:00
|
|
|
func TestKVPutCommand_File(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:29 +00:00
|
|
|
|
2017-02-09 00:26:24 +00:00
|
|
|
ui, c := testKVPutCommand(t)
|
2016-09-26 15:12:29 +00:00
|
|
|
|
2017-05-12 13:41:13 +00:00
|
|
|
f := testutil.TempFile(t, "kv-put-command-file")
|
2016-09-26 15:12:29 +00:00
|
|
|
defer os.Remove(f.Name())
|
|
|
|
if _, err := f.WriteString("bar"); 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:29 +00:00
|
|
|
"foo", "@" + f.Name(),
|
|
|
|
}
|
|
|
|
|
|
|
|
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 !bytes.Equal(data.Value, []byte("bar")) {
|
|
|
|
t.Errorf("bad: %#v", data.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKVPutCommand_FileNoExist(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2017-02-09 00:26:24 +00:00
|
|
|
ui, c := testKVPutCommand(t)
|
2016-09-26 15:12:29 +00:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"foo", "@/nope/definitely/not-a-real-file.txt",
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code == 0 {
|
|
|
|
t.Fatal("bad: expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
output := ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(output, "Failed to read file") {
|
|
|
|
t.Errorf("bad: %#v", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKVPutCommand_Stdin(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:29 +00:00
|
|
|
|
|
|
|
stdinR, stdinW := io.Pipe()
|
|
|
|
|
2017-02-09 00:26:24 +00:00
|
|
|
ui, c := testKVPutCommand(t)
|
|
|
|
c.testStdin = stdinR
|
2016-09-26 15:12:29 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
stdinW.Write([]byte("bar"))
|
|
|
|
stdinW.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2016-09-26 15:12:29 +00:00
|
|
|
"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 !bytes.Equal(data.Value, []byte("bar")) {
|
|
|
|
t.Errorf("bad: %#v", data.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 06:29:31 +00:00
|
|
|
func TestKVPutCommand_NegativeVal(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-11-30 06:29:31 +00:00
|
|
|
|
2017-02-09 00:26:24 +00:00
|
|
|
ui, c := testKVPutCommand(t)
|
2016-11-30 06:29:31 +00:00
|
|
|
|
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2016-11-30 06:29:31 +00:00
|
|
|
"foo", "-2",
|
|
|
|
}
|
|
|
|
|
|
|
|
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 !bytes.Equal(data.Value, []byte("-2")) {
|
|
|
|
t.Errorf("bad: %#v", data.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 15:12:29 +00:00
|
|
|
func TestKVPutCommand_Flags(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:29 +00:00
|
|
|
|
2017-02-09 00:26:24 +00:00
|
|
|
ui, c := testKVPutCommand(t)
|
2016-09-26 15:12:29 +00:00
|
|
|
|
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2016-09-26 15:12:29 +00:00
|
|
|
"-flags", "12345",
|
|
|
|
"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.Flags != 12345 {
|
|
|
|
t.Errorf("bad: %#v", data.Flags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKVPutCommand_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 15:12:29 +00:00
|
|
|
|
|
|
|
// Create the initial pair so it has a ModifyIndex.
|
|
|
|
pair := &api.KVPair{
|
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
}
|
|
|
|
if _, err := client.KV().Put(pair, nil); err != nil {
|
|
|
|
t.Fatalf("err: %#v", err)
|
|
|
|
}
|
|
|
|
|
2017-02-09 00:26:24 +00:00
|
|
|
ui, c := testKVPutCommand(t)
|
2016-09-26 15:12:29 +00:00
|
|
|
|
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2016-09-26 15:12:29 +00:00
|
|
|
"-cas",
|
|
|
|
"-modify-index", "123",
|
|
|
|
"foo", "a",
|
|
|
|
}
|
|
|
|
|
|
|
|
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 15:12:29 +00:00
|
|
|
"-cas",
|
|
|
|
"-modify-index", strconv.FormatUint(data.ModifyIndex, 10),
|
|
|
|
"foo", "a",
|
|
|
|
}
|
|
|
|
|
|
|
|
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 !bytes.Equal(data.Value, []byte("a")) {
|
|
|
|
t.Errorf("bad: %#v", data.Value)
|
|
|
|
}
|
|
|
|
}
|