2016-05-11 04:41:47 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"reflect"
|
2016-05-11 08:35:27 +00:00
|
|
|
"strings"
|
2016-05-11 04:41:47 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTxnEndpoint_Bad_JSON(t *testing.T) {
|
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
|
|
|
buf := bytes.NewBuffer([]byte("{"))
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/txn", buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
if _, err := srv.Txn(resp, req); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 400 {
|
|
|
|
t.Fatalf("expected 400, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
if !bytes.Contains(resp.Body.Bytes(), []byte("Failed to parse")) {
|
|
|
|
t.Fatalf("expected conflicting args error")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTxnEndpoint_Bad_Method(t *testing.T) {
|
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
|
|
|
buf := bytes.NewBuffer([]byte("{}"))
|
|
|
|
req, err := http.NewRequest("GET", "/v1/txn", buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
if _, err := srv.Txn(resp, req); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 405 {
|
|
|
|
t.Fatalf("expected 405, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-11 08:35:27 +00:00
|
|
|
func TestTxnEndpoint_Bad_Size(t *testing.T) {
|
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
|
|
|
buf := bytes.NewBuffer([]byte(fmt.Sprintf(`
|
|
|
|
[
|
|
|
|
{
|
2016-05-11 17:58:27 +00:00
|
|
|
"KV": {
|
2016-05-11 08:35:27 +00:00
|
|
|
"Verb": "set",
|
|
|
|
"Key": "key",
|
|
|
|
"Value": %q
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
`, strings.Repeat("bad", 2*maxKVSize))))
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/txn", buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
if _, err := srv.Txn(resp, req); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 413 {
|
|
|
|
t.Fatalf("expected 413, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-11 17:58:27 +00:00
|
|
|
func TestTxnEndpoint_KV_Actions(t *testing.T) {
|
2016-05-11 04:41:47 +00:00
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
|
|
|
// Make sure all incoming fields get converted properly to the internal
|
|
|
|
// RPC format.
|
|
|
|
var index uint64
|
|
|
|
id := makeTestSession(t, srv)
|
|
|
|
{
|
|
|
|
buf := bytes.NewBuffer([]byte(fmt.Sprintf(`
|
|
|
|
[
|
|
|
|
{
|
2016-05-11 17:58:27 +00:00
|
|
|
"KV": {
|
2016-05-11 08:35:27 +00:00
|
|
|
"Verb": "lock",
|
|
|
|
"Key": "key",
|
|
|
|
"Value": "aGVsbG8gd29ybGQ=",
|
|
|
|
"Flags": 23,
|
|
|
|
"Session": %q
|
|
|
|
}
|
2016-05-11 04:41:47 +00:00
|
|
|
},
|
|
|
|
{
|
2016-05-11 17:58:27 +00:00
|
|
|
"KV": {
|
2016-05-11 08:35:27 +00:00
|
|
|
"Verb": "get",
|
|
|
|
"Key": "key"
|
|
|
|
}
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
`, id)))
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/txn", buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.Txn(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf("expected 200, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
|
2016-05-11 08:35:27 +00:00
|
|
|
txnResp, ok := obj.(structs.TxnResponse)
|
2016-05-11 04:41:47 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("bad type: %T", obj)
|
|
|
|
}
|
2016-05-11 08:35:27 +00:00
|
|
|
if len(txnResp.Results) != 2 {
|
|
|
|
t.Fatalf("bad: %v", txnResp)
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
2016-05-11 20:48:03 +00:00
|
|
|
index = txnResp.Results[0].KV.ModifyIndex
|
2016-05-11 08:35:27 +00:00
|
|
|
expected := structs.TxnResponse{
|
|
|
|
Results: structs.TxnResults{
|
|
|
|
&structs.TxnResult{
|
2016-05-11 20:48:03 +00:00
|
|
|
KV: &structs.DirEntry{
|
|
|
|
Key: "key",
|
|
|
|
Value: nil,
|
|
|
|
Flags: 23,
|
|
|
|
Session: id,
|
|
|
|
LockIndex: 1,
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: index,
|
|
|
|
ModifyIndex: index,
|
2016-05-11 08:35:27 +00:00
|
|
|
},
|
2016-05-11 04:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
2016-05-11 08:35:27 +00:00
|
|
|
&structs.TxnResult{
|
2016-05-11 20:48:03 +00:00
|
|
|
KV: &structs.DirEntry{
|
|
|
|
Key: "key",
|
|
|
|
Value: []byte("hello world"),
|
|
|
|
Flags: 23,
|
|
|
|
Session: id,
|
|
|
|
LockIndex: 1,
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: index,
|
|
|
|
ModifyIndex: index,
|
2016-05-11 08:35:27 +00:00
|
|
|
},
|
2016-05-11 04:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-05-11 08:35:27 +00:00
|
|
|
if !reflect.DeepEqual(txnResp, expected) {
|
|
|
|
t.Fatalf("bad: %v", txnResp)
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have an index we can do a CAS to make sure the
|
|
|
|
// index field gets translated to the RPC format.
|
|
|
|
{
|
|
|
|
buf := bytes.NewBuffer([]byte(fmt.Sprintf(`
|
|
|
|
[
|
|
|
|
{
|
2016-05-11 17:58:27 +00:00
|
|
|
"KV": {
|
2016-05-11 08:35:27 +00:00
|
|
|
"Verb": "cas",
|
|
|
|
"Key": "key",
|
|
|
|
"Value": "Z29vZGJ5ZSB3b3JsZA==",
|
|
|
|
"Index": %d
|
|
|
|
}
|
2016-05-11 04:41:47 +00:00
|
|
|
},
|
|
|
|
{
|
2016-05-11 17:58:27 +00:00
|
|
|
"KV": {
|
2016-05-11 08:35:27 +00:00
|
|
|
"Verb": "get",
|
|
|
|
"Key": "key"
|
|
|
|
}
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
`, index)))
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/txn", buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.Txn(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf("expected 200, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
|
2016-05-11 08:35:27 +00:00
|
|
|
txnResp, ok := obj.(structs.TxnResponse)
|
2016-05-11 04:41:47 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("bad type: %T", obj)
|
|
|
|
}
|
2016-05-11 08:35:27 +00:00
|
|
|
if len(txnResp.Results) != 2 {
|
|
|
|
t.Fatalf("bad: %v", txnResp)
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
2016-05-11 20:48:03 +00:00
|
|
|
modIndex := txnResp.Results[0].KV.ModifyIndex
|
2016-05-11 08:35:27 +00:00
|
|
|
expected := structs.TxnResponse{
|
|
|
|
Results: structs.TxnResults{
|
|
|
|
&structs.TxnResult{
|
2016-05-11 20:48:03 +00:00
|
|
|
KV: &structs.DirEntry{
|
|
|
|
Key: "key",
|
|
|
|
Value: nil,
|
|
|
|
Session: id,
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: index,
|
|
|
|
ModifyIndex: modIndex,
|
2016-05-11 08:35:27 +00:00
|
|
|
},
|
2016-05-11 04:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
2016-05-11 08:35:27 +00:00
|
|
|
&structs.TxnResult{
|
2016-05-11 20:48:03 +00:00
|
|
|
KV: &structs.DirEntry{
|
|
|
|
Key: "key",
|
|
|
|
Value: []byte("goodbye world"),
|
|
|
|
Session: id,
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: index,
|
|
|
|
ModifyIndex: modIndex,
|
2016-05-11 08:35:27 +00:00
|
|
|
},
|
2016-05-11 04:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-05-11 08:35:27 +00:00
|
|
|
if !reflect.DeepEqual(txnResp, expected) {
|
|
|
|
t.Fatalf("bad: %v", txnResp)
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Verify an error inside a transaction.
|
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
|
|
|
buf := bytes.NewBuffer([]byte(`
|
|
|
|
[
|
|
|
|
{
|
2016-05-11 17:58:27 +00:00
|
|
|
"KV": {
|
2016-05-11 08:35:27 +00:00
|
|
|
"Verb": "lock",
|
|
|
|
"Key": "key",
|
|
|
|
"Value": "aGVsbG8gd29ybGQ=",
|
|
|
|
"Session": "nope"
|
|
|
|
}
|
2016-05-11 04:41:47 +00:00
|
|
|
},
|
|
|
|
{
|
2016-05-11 17:58:27 +00:00
|
|
|
"KV": {
|
2016-05-11 08:35:27 +00:00
|
|
|
"Verb": "get",
|
|
|
|
"Key": "key"
|
|
|
|
}
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
`))
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/txn", buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
if _, err = srv.Txn(resp, req); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 409 {
|
|
|
|
t.Fatalf("expected 409, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
if !bytes.Contains(resp.Body.Bytes(), []byte("failed session lookup")) {
|
|
|
|
t.Fatalf("bad: %s", resp.Body.String())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|