Unit test for kv_put with negative values

This commit is contained in:
mckennajones 2016-11-29 22:29:31 -08:00
parent 40a8f13cd9
commit 6bc1164825
2 changed files with 30 additions and 2 deletions

View File

@ -228,15 +228,15 @@ func (c *KVPutCommand) dataFromArgs(args []string) (string, string, error) {
} }
return key, string(data), nil return key, string(data), nil
case '-': case '-':
var b bytes.Buffer
if len(data) > 1 { if len(data) > 1 {
return key, data, nil return key, data, nil
} else { } else {
var b bytes.Buffer
if _, err := io.Copy(&b, stdin); err != nil { if _, err := io.Copy(&b, stdin); err != nil {
return "", "", fmt.Errorf("Failed to read stdin: %s", err) return "", "", fmt.Errorf("Failed to read stdin: %s", err)
} }
}
return key, b.String(), nil return key, b.String(), nil
}
default: default:
return key, data, nil return key, data, nil
} }

View File

@ -194,6 +194,34 @@ func TestKVPutCommand_Stdin(t *testing.T) {
} }
} }
func TestKVPutCommand_NegativeVal(t *testing.T) {
srv, client := testAgentWithAPIClient(t)
defer srv.Shutdown()
waitForLeader(t, srv.httpAddr)
ui := new(cli.MockUi)
c := &KVPutCommand{Ui: ui}
args := []string{
"-http-addr=" + srv.httpAddr,
"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)
}
}
func TestKVPutCommand_Flags(t *testing.T) { func TestKVPutCommand_Flags(t *testing.T) {
srv, client := testAgentWithAPIClient(t) srv, client := testAgentWithAPIClient(t)
defer srv.Shutdown() defer srv.Shutdown()