2016-05-11 04:41:47 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-07-24 21:06:39 +00:00
|
|
|
"encoding/base64"
|
2016-05-11 04:41:47 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2016-05-11 08:35:27 +00:00
|
|
|
"strings"
|
2016-05-11 04:41:47 +00:00
|
|
|
"testing"
|
2019-03-25 23:58:41 +00:00
|
|
|
"time"
|
2016-05-11 04:41:47 +00:00
|
|
|
|
2022-01-17 20:09:08 +00:00
|
|
|
"github.com/hashicorp/raft"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-04-29 17:42:49 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-01-17 20:09:08 +00:00
|
|
|
|
2019-11-25 17:57:35 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-03-25 23:58:41 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2018-08-14 16:08:33 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2016-05-11 04:41:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTxnEndpoint_Bad_JSON(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer([]byte("{"))
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
|
|
|
|
resp := httptest.NewRecorder()
|
2022-01-31 16:17:35 +00:00
|
|
|
_, err := a.srv.Txn(resp, req)
|
2022-04-29 17:42:49 +00:00
|
|
|
require.True(t, isHTTPBadRequest(err), fmt.Sprintf("Expected bad request HTTP error but got %v", err))
|
2022-01-31 16:17:35 +00:00
|
|
|
if !strings.Contains(err.Error(), "Failed to parse") {
|
2017-05-21 18:31:20 +00:00
|
|
|
t.Fatalf("expected conflicting args error")
|
|
|
|
}
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 19:17:12 +00:00
|
|
|
func TestTxnEndpoint_Bad_Size_Item(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-01-28 21:22:40 +00:00
|
|
|
testIt := func(t *testing.T, agent *TestAgent, wantPass bool) {
|
2019-07-24 21:06:39 +00:00
|
|
|
value := strings.Repeat("X", 3*raft.SuggestedMaxDataSize)
|
|
|
|
value = base64.StdEncoding.EncodeToString([]byte(value))
|
|
|
|
buf := bytes.NewBuffer([]byte(fmt.Sprintf(`
|
2017-09-25 18:40:42 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"KV": {
|
|
|
|
"Verb": "set",
|
|
|
|
"Key": "key",
|
|
|
|
"Value": %q
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2019-07-24 21:06:39 +00:00
|
|
|
`, value)))
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
|
|
|
|
resp := httptest.NewRecorder()
|
2022-01-31 16:17:35 +00:00
|
|
|
_, err := agent.srv.Txn(resp, req)
|
2022-04-29 17:42:49 +00:00
|
|
|
|
|
|
|
if wantPass {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err, ok := err.(HTTPError); ok {
|
|
|
|
if err.StatusCode != 413 {
|
|
|
|
t.Fatalf("expected 413 but got %d", err.StatusCode)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Fatalf("excected HTTP error but got %v", err)
|
|
|
|
}
|
2019-07-24 21:06:39 +00:00
|
|
|
}
|
2017-05-21 18:31:20 +00:00
|
|
|
}
|
2019-07-24 21:06:39 +00:00
|
|
|
|
2020-03-05 21:42:37 +00:00
|
|
|
t.Run("exceeds default limits", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2020-01-28 21:22:40 +00:00
|
|
|
testIt(t, a, false)
|
2019-07-24 21:06:39 +00:00
|
|
|
a.Shutdown()
|
|
|
|
})
|
|
|
|
|
2020-03-05 21:42:37 +00:00
|
|
|
t.Run("exceeds configured max txn len", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "limits = { txn_max_req_len = 700000 }")
|
2020-03-05 21:42:37 +00:00
|
|
|
testIt(t, a, false)
|
|
|
|
a.Shutdown()
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("exceeds default max kv value size", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "limits = { txn_max_req_len = 123456789 }")
|
2020-03-05 21:42:37 +00:00
|
|
|
testIt(t, a, false)
|
|
|
|
a.Shutdown()
|
|
|
|
})
|
|
|
|
|
2019-07-24 21:06:39 +00:00
|
|
|
t.Run("allowed", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, `
|
2020-03-05 21:42:37 +00:00
|
|
|
limits = {
|
|
|
|
txn_max_req_len = 123456789
|
|
|
|
kv_max_value_size = 123456789
|
|
|
|
}`)
|
2020-01-28 21:22:40 +00:00
|
|
|
testIt(t, a, true)
|
2019-07-24 21:06:39 +00:00
|
|
|
a.Shutdown()
|
|
|
|
})
|
2016-05-11 08:35:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 19:17:12 +00:00
|
|
|
func TestTxnEndpoint_Bad_Size_Net(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 18:31:20 +00:00
|
|
|
|
2019-07-24 21:06:39 +00:00
|
|
|
testIt := func(agent *TestAgent, wantPass bool) {
|
|
|
|
value := strings.Repeat("X", 3*raft.SuggestedMaxDataSize)
|
|
|
|
value = base64.StdEncoding.EncodeToString([]byte(value))
|
|
|
|
buf := bytes.NewBuffer([]byte(fmt.Sprintf(`
|
2017-09-25 18:40:42 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"KV": {
|
|
|
|
"Verb": "set",
|
|
|
|
"Key": "key1",
|
|
|
|
"Value": %q
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"KV": {
|
|
|
|
"Verb": "set",
|
|
|
|
"Key": "key1",
|
|
|
|
"Value": %q
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"KV": {
|
|
|
|
"Verb": "set",
|
|
|
|
"Key": "key1",
|
|
|
|
"Value": %q
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
`, value, value, value)))
|
2019-07-24 21:06:39 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
|
|
|
|
resp := httptest.NewRecorder()
|
2022-01-31 16:17:35 +00:00
|
|
|
_, err := agent.srv.Txn(resp, req)
|
2022-04-29 17:42:49 +00:00
|
|
|
|
|
|
|
if wantPass {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err, ok := err.(HTTPError); ok {
|
|
|
|
if err.StatusCode != 413 {
|
|
|
|
t.Fatalf("expected 413 but got %d", err.StatusCode)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Fatalf("excected HTTP error but got %v", err)
|
|
|
|
}
|
2019-07-24 21:06:39 +00:00
|
|
|
}
|
2017-05-21 18:31:20 +00:00
|
|
|
}
|
2019-07-24 21:06:39 +00:00
|
|
|
|
2020-03-05 21:42:37 +00:00
|
|
|
t.Run("exceeds default limits", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2019-07-24 21:06:39 +00:00
|
|
|
testIt(a, false)
|
|
|
|
a.Shutdown()
|
|
|
|
})
|
|
|
|
|
2020-03-05 21:42:37 +00:00
|
|
|
t.Run("exceeds configured max txn len", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "limits = { txn_max_req_len = 700000 }")
|
2020-03-05 21:42:37 +00:00
|
|
|
testIt(a, false)
|
|
|
|
a.Shutdown()
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("exceeds default max kv value size", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "limits = { txn_max_req_len = 123456789 }")
|
2020-03-05 21:42:37 +00:00
|
|
|
testIt(a, false)
|
|
|
|
a.Shutdown()
|
|
|
|
})
|
|
|
|
|
2019-07-24 21:06:39 +00:00
|
|
|
t.Run("allowed", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, `
|
2020-03-05 21:42:37 +00:00
|
|
|
limits = {
|
|
|
|
txn_max_req_len = 123456789
|
|
|
|
kv_max_value_size = 123456789
|
|
|
|
}`)
|
|
|
|
testIt(a, true)
|
|
|
|
a.Shutdown()
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("allowed kv max backward compatible", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "limits = { kv_max_value_size = 123456789 }")
|
2019-07-24 21:06:39 +00:00
|
|
|
testIt(a, true)
|
|
|
|
a.Shutdown()
|
|
|
|
})
|
2016-05-13 19:17:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTxnEndpoint_Bad_Size_Ops(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer([]byte(fmt.Sprintf(`
|
2017-09-25 18:40:42 +00:00
|
|
|
[
|
|
|
|
%s
|
|
|
|
{
|
|
|
|
"KV": {
|
|
|
|
"Verb": "set",
|
|
|
|
"Key": "key",
|
|
|
|
"Value": ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
`, strings.Repeat(`{ "KV": { "Verb": "get", "Key": "key" } },`, 2*maxTxnOps))))
|
2017-05-21 18:31:20 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
|
|
|
|
resp := httptest.NewRecorder()
|
2022-01-31 16:17:35 +00:00
|
|
|
_, err := a.srv.Txn(resp, req)
|
2022-04-29 17:42:49 +00:00
|
|
|
|
|
|
|
if err, ok := err.(HTTPError); ok {
|
|
|
|
if err.StatusCode != 413 {
|
|
|
|
t.Fatalf("expected 413 but got %d", err.StatusCode)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Fatalf("expected HTTP error but got %v", err)
|
2017-05-21 18:31:20 +00:00
|
|
|
}
|
2016-05-13 19:17:12 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 17:58:27 +00:00
|
|
|
func TestTxnEndpoint_KV_Actions(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 18:31:20 +00:00
|
|
|
t.Run("", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2018-08-14 16:08:33 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-05-21 18:31:20 +00:00
|
|
|
|
2016-05-11 04:41:47 +00:00
|
|
|
// Make sure all incoming fields get converted properly to the internal
|
|
|
|
// RPC format.
|
|
|
|
var index uint64
|
2017-05-21 18:31:20 +00:00
|
|
|
id := makeTestSession(t, a.srv)
|
2016-05-11 04:41:47 +00:00
|
|
|
{
|
|
|
|
buf := bytes.NewBuffer([]byte(fmt.Sprintf(`
|
2017-09-25 18:40:42 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"KV": {
|
|
|
|
"Verb": "lock",
|
|
|
|
"Key": "key",
|
|
|
|
"Value": "aGVsbG8gd29ybGQ=",
|
|
|
|
"Flags": 23,
|
|
|
|
"Session": %q
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"KV": {
|
|
|
|
"Verb": "get",
|
|
|
|
"Key": "key"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
`, id)))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
|
2016-05-11 04:41:47 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 18:31:20 +00:00
|
|
|
obj, err := a.srv.Txn(resp, req)
|
2016-05-11 04:41:47 +00:00
|
|
|
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
|
|
|
}
|
2019-11-25 17:57:35 +00:00
|
|
|
|
2016-05-11 20:48:03 +00:00
|
|
|
index = txnResp.Results[0].KV.ModifyIndex
|
2019-11-25 17:57:35 +00:00
|
|
|
entMeta := txnResp.Results[0].KV.EnterpriseMeta
|
|
|
|
|
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
|
|
|
},
|
2019-11-25 17:57:35 +00:00
|
|
|
EnterpriseMeta: entMeta,
|
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
|
|
|
},
|
2019-11-25 17:57:35 +00:00
|
|
|
EnterpriseMeta: entMeta,
|
2016-05-11 04:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2019-11-25 17:57:35 +00:00
|
|
|
assert.Equal(t, expected, txnResp)
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 00:38:25 +00:00
|
|
|
// Do a read-only transaction that should get routed to the
|
|
|
|
// fast-path endpoint.
|
|
|
|
{
|
2016-05-13 19:17:12 +00:00
|
|
|
buf := bytes.NewBuffer([]byte(`
|
2017-09-25 18:40:42 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"KV": {
|
|
|
|
"Verb": "get",
|
|
|
|
"Key": "key"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"KV": {
|
|
|
|
"Verb": "get-tree",
|
|
|
|
"Key": "key"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
`))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
|
2016-05-13 00:38:25 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 18:31:20 +00:00
|
|
|
obj, err := a.srv.Txn(resp, req)
|
2016-05-13 00:38:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf("expected 200, got %d", resp.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
header := resp.Header().Get("X-Consul-KnownLeader")
|
|
|
|
if header != "true" {
|
|
|
|
t.Fatalf("bad: %v", header)
|
|
|
|
}
|
|
|
|
header = resp.Header().Get("X-Consul-LastContact")
|
|
|
|
if header != "0" {
|
|
|
|
t.Fatalf("bad: %v", header)
|
|
|
|
}
|
|
|
|
|
|
|
|
txnResp, ok := obj.(structs.TxnReadResponse)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("bad type: %T", obj)
|
|
|
|
}
|
2019-11-25 17:57:35 +00:00
|
|
|
entMeta := txnResp.Results[0].KV.EnterpriseMeta
|
2016-05-13 00:38:25 +00:00
|
|
|
expected := structs.TxnReadResponse{
|
|
|
|
TxnResponse: structs.TxnResponse{
|
|
|
|
Results: structs.TxnResults{
|
|
|
|
&structs.TxnResult{
|
|
|
|
KV: &structs.DirEntry{
|
|
|
|
Key: "key",
|
|
|
|
Value: []byte("hello world"),
|
|
|
|
Flags: 23,
|
|
|
|
Session: id,
|
|
|
|
LockIndex: 1,
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: index,
|
|
|
|
ModifyIndex: index,
|
|
|
|
},
|
2019-11-25 17:57:35 +00:00
|
|
|
EnterpriseMeta: entMeta,
|
2016-05-13 00:38:25 +00:00
|
|
|
},
|
|
|
|
},
|
2016-05-13 23:57:39 +00:00
|
|
|
&structs.TxnResult{
|
|
|
|
KV: &structs.DirEntry{
|
|
|
|
Key: "key",
|
|
|
|
Value: []byte("hello world"),
|
|
|
|
Flags: 23,
|
|
|
|
Session: id,
|
|
|
|
LockIndex: 1,
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: index,
|
|
|
|
ModifyIndex: index,
|
|
|
|
},
|
2019-11-25 17:57:35 +00:00
|
|
|
EnterpriseMeta: entMeta,
|
2016-05-13 23:57:39 +00:00
|
|
|
},
|
|
|
|
},
|
2016-05-13 00:38:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
QueryMeta: structs.QueryMeta{
|
|
|
|
KnownLeader: true,
|
2022-01-17 20:09:08 +00:00
|
|
|
Index: 1,
|
2016-05-13 00:38:25 +00:00
|
|
|
},
|
|
|
|
}
|
2019-11-25 17:57:35 +00:00
|
|
|
assert.Equal(t, expected, txnResp)
|
2016-05-13 00:38:25 +00:00
|
|
|
}
|
|
|
|
|
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(`
|
2017-09-25 18:40:42 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"KV": {
|
|
|
|
"Verb": "cas",
|
|
|
|
"Key": "key",
|
|
|
|
"Value": "Z29vZGJ5ZSB3b3JsZA==",
|
|
|
|
"Index": %d
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"KV": {
|
|
|
|
"Verb": "get",
|
|
|
|
"Key": "key"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
`, index)))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
|
2016-05-11 04:41:47 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 18:31:20 +00:00
|
|
|
obj, err := a.srv.Txn(resp, req)
|
2016-05-11 04:41:47 +00:00
|
|
|
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
|
|
|
}
|
2019-11-25 17:57:35 +00:00
|
|
|
|
2016-05-11 20:48:03 +00:00
|
|
|
modIndex := txnResp.Results[0].KV.ModifyIndex
|
2019-11-25 17:57:35 +00:00
|
|
|
entMeta := txnResp.Results[0].KV.EnterpriseMeta
|
|
|
|
|
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
|
|
|
},
|
2019-11-25 17:57:35 +00:00
|
|
|
EnterpriseMeta: entMeta,
|
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
|
|
|
},
|
2019-11-25 17:57:35 +00:00
|
|
|
EnterpriseMeta: entMeta,
|
2016-05-11 04:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2019-11-25 17:57:35 +00:00
|
|
|
assert.Equal(t, expected, txnResp)
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Verify an error inside a transaction.
|
2017-05-21 18:31:20 +00:00
|
|
|
t.Run("", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
2016-05-11 04:41:47 +00:00
|
|
|
buf := bytes.NewBuffer([]byte(`
|
2017-09-25 18:40:42 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"KV": {
|
|
|
|
"Verb": "lock",
|
|
|
|
"Key": "key",
|
|
|
|
"Value": "aGVsbG8gd29ybGQ=",
|
|
|
|
"Session": "nope"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"KV": {
|
|
|
|
"Verb": "get",
|
|
|
|
"Key": "key"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
`))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
|
2016-05-11 04:41:47 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 18:31:20 +00:00
|
|
|
if _, err := a.srv.Txn(resp, req); err != nil {
|
2016-05-11 04:41:47 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 409 {
|
|
|
|
t.Fatalf("expected 409, got %d", resp.Code)
|
|
|
|
}
|
2021-11-08 21:20:50 +00:00
|
|
|
if !bytes.Contains(resp.Body.Bytes(), []byte("invalid session")) {
|
2016-05-11 04:41:47 +00:00
|
|
|
t.Fatalf("bad: %s", resp.Body.String())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-03-25 23:58:41 +00:00
|
|
|
|
|
|
|
func TestTxnEndpoint_UpdateCheck(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2019-03-25 23:58:41 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2019-03-25 23:58:41 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
2019-03-26 17:44:02 +00:00
|
|
|
// Make sure the fields of a check are handled correctly when both creating and
|
|
|
|
// updating, and test both sets of duration fields to ensure backwards compatibility.
|
2019-03-25 23:58:41 +00:00
|
|
|
buf := bytes.NewBuffer([]byte(fmt.Sprintf(`
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"Check": {
|
|
|
|
"Verb": "set",
|
|
|
|
"Check": {
|
|
|
|
"Node": "%s",
|
|
|
|
"CheckID": "nodecheck",
|
|
|
|
"Name": "Node http check",
|
|
|
|
"Status": "critical",
|
|
|
|
"Notes": "Http based health check",
|
|
|
|
"Output": "",
|
|
|
|
"ServiceID": "",
|
|
|
|
"ServiceName": "",
|
|
|
|
"Definition": {
|
|
|
|
"Interval": "6s",
|
|
|
|
"Timeout": "6s",
|
|
|
|
"DeregisterCriticalServiceAfter": "6s",
|
|
|
|
"HTTP": "http://localhost:8000",
|
|
|
|
"TLSSkipVerify": true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Check": {
|
|
|
|
"Verb": "set",
|
|
|
|
"Check": {
|
|
|
|
"Node": "%s",
|
|
|
|
"CheckID": "nodecheck",
|
|
|
|
"Name": "Node http check",
|
|
|
|
"Status": "passing",
|
|
|
|
"Notes": "Http based health check",
|
|
|
|
"Output": "success",
|
|
|
|
"ServiceID": "",
|
|
|
|
"ServiceName": "",
|
|
|
|
"Definition": {
|
|
|
|
"Interval": "10s",
|
|
|
|
"Timeout": "10s",
|
|
|
|
"DeregisterCriticalServiceAfter": "15m",
|
|
|
|
"HTTP": "http://localhost:9000",
|
|
|
|
"TLSSkipVerify": false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 17:44:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"Check": {
|
|
|
|
"Verb": "set",
|
|
|
|
"Check": {
|
|
|
|
"Node": "%s",
|
|
|
|
"CheckID": "nodecheck",
|
|
|
|
"Name": "Node http check",
|
|
|
|
"Status": "passing",
|
|
|
|
"Notes": "Http based health check",
|
|
|
|
"Output": "success",
|
|
|
|
"ServiceID": "",
|
|
|
|
"ServiceName": "",
|
2022-09-02 18:28:05 +00:00
|
|
|
"ExposedPort": 5678,
|
2019-03-26 17:44:02 +00:00
|
|
|
"Definition": {
|
|
|
|
"IntervalDuration": "15s",
|
|
|
|
"TimeoutDuration": "15s",
|
|
|
|
"DeregisterCriticalServiceAfterDuration": "30m",
|
|
|
|
"HTTP": "http://localhost:9000",
|
|
|
|
"TLSSkipVerify": false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 23:58:41 +00:00
|
|
|
}
|
|
|
|
]
|
2019-03-26 17:44:02 +00:00
|
|
|
`, a.config.NodeName, a.config.NodeName, a.config.NodeName)))
|
2019-03-25 23:58:41 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.Txn(resp, req)
|
2022-09-02 18:28:05 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 200, resp.Code, resp.Body)
|
2019-03-25 23:58:41 +00:00
|
|
|
|
|
|
|
txnResp, ok := obj.(structs.TxnResponse)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("bad type: %T", obj)
|
|
|
|
}
|
2019-03-26 17:44:02 +00:00
|
|
|
if len(txnResp.Results) != 3 {
|
2019-03-25 23:58:41 +00:00
|
|
|
t.Fatalf("bad: %v", txnResp)
|
|
|
|
}
|
|
|
|
index := txnResp.Results[0].Check.ModifyIndex
|
|
|
|
expected := structs.TxnResponse{
|
|
|
|
Results: structs.TxnResults{
|
|
|
|
&structs.TxnResult{
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
Node: a.config.NodeName,
|
|
|
|
CheckID: "nodecheck",
|
|
|
|
Name: "Node http check",
|
|
|
|
Status: api.HealthCritical,
|
|
|
|
Notes: "Http based health check",
|
|
|
|
Definition: structs.HealthCheckDefinition{
|
2019-04-25 16:26:33 +00:00
|
|
|
Interval: 6 * time.Second,
|
|
|
|
Timeout: 6 * time.Second,
|
2019-03-25 23:58:41 +00:00
|
|
|
DeregisterCriticalServiceAfter: 6 * time.Second,
|
2019-04-25 16:26:33 +00:00
|
|
|
HTTP: "http://localhost:8000",
|
|
|
|
TLSSkipVerify: true,
|
2019-03-25 23:58:41 +00:00
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: index,
|
|
|
|
ModifyIndex: index,
|
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2019-03-25 23:58:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.TxnResult{
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
Node: a.config.NodeName,
|
|
|
|
CheckID: "nodecheck",
|
|
|
|
Name: "Node http check",
|
|
|
|
Status: api.HealthPassing,
|
|
|
|
Notes: "Http based health check",
|
|
|
|
Output: "success",
|
|
|
|
Definition: structs.HealthCheckDefinition{
|
2019-04-25 16:26:33 +00:00
|
|
|
Interval: 10 * time.Second,
|
|
|
|
Timeout: 10 * time.Second,
|
2019-03-25 23:58:41 +00:00
|
|
|
DeregisterCriticalServiceAfter: 15 * time.Minute,
|
2019-04-25 16:26:33 +00:00
|
|
|
HTTP: "http://localhost:9000",
|
|
|
|
TLSSkipVerify: false,
|
2019-03-26 17:44:02 +00:00
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: index,
|
|
|
|
ModifyIndex: index,
|
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2019-03-26 17:44:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.TxnResult{
|
|
|
|
Check: &structs.HealthCheck{
|
2022-09-02 18:28:05 +00:00
|
|
|
Node: a.config.NodeName,
|
|
|
|
CheckID: "nodecheck",
|
|
|
|
Name: "Node http check",
|
|
|
|
Status: api.HealthPassing,
|
|
|
|
Notes: "Http based health check",
|
|
|
|
Output: "success",
|
|
|
|
ExposedPort: 5678,
|
2019-03-26 17:44:02 +00:00
|
|
|
Definition: structs.HealthCheckDefinition{
|
2019-04-25 16:26:33 +00:00
|
|
|
Interval: 15 * time.Second,
|
|
|
|
Timeout: 15 * time.Second,
|
2019-03-26 17:44:02 +00:00
|
|
|
DeregisterCriticalServiceAfter: 30 * time.Minute,
|
2019-04-25 16:26:33 +00:00
|
|
|
HTTP: "http://localhost:9000",
|
|
|
|
TLSSkipVerify: false,
|
2019-03-25 23:58:41 +00:00
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: index,
|
|
|
|
ModifyIndex: index,
|
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2019-03-25 23:58:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2019-11-25 17:57:35 +00:00
|
|
|
assert.Equal(t, expected, txnResp)
|
2019-03-25 23:58:41 +00:00
|
|
|
}
|
2022-09-02 18:28:05 +00:00
|
|
|
|
|
|
|
func TestTxnEndpoint_NodeService(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
a := NewTestAgent(t, "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Make sure the fields of a check are handled correctly when both creating and
|
|
|
|
// updating, and test both sets of duration fields to ensure backwards compatibility.
|
|
|
|
buf := bytes.NewBuffer([]byte(fmt.Sprintf(`
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"Service": {
|
|
|
|
"Verb": "set",
|
|
|
|
"Node": "%s",
|
|
|
|
"Service": {
|
|
|
|
"Service": "test",
|
|
|
|
"Port": 4444
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Service": {
|
|
|
|
"Verb": "set",
|
|
|
|
"Node": "%s",
|
|
|
|
"Service": {
|
|
|
|
"Service": "test-sidecar-proxy",
|
|
|
|
"Port": 20000,
|
|
|
|
"Kind": "connect-proxy",
|
|
|
|
"Proxy": {
|
|
|
|
"DestinationServiceName": "test",
|
|
|
|
"DestinationServiceID": "test",
|
|
|
|
"LocalServiceAddress": "127.0.0.1",
|
|
|
|
"LocalServicePort": 4444,
|
|
|
|
"upstreams": [
|
|
|
|
{
|
|
|
|
"DestinationName": "fake-backend",
|
|
|
|
"LocalBindPort": 25001
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
`, a.config.NodeName, a.config.NodeName)))
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.Txn(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 200, resp.Code)
|
|
|
|
|
|
|
|
txnResp, ok := obj.(structs.TxnResponse)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("bad type: %T", obj)
|
|
|
|
}
|
|
|
|
require.Equal(t, 2, len(txnResp.Results))
|
|
|
|
|
|
|
|
index := txnResp.Results[0].Service.ModifyIndex
|
|
|
|
expected := structs.TxnResponse{
|
|
|
|
Results: structs.TxnResults{
|
|
|
|
&structs.TxnResult{
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "test",
|
|
|
|
ID: "test",
|
|
|
|
Port: 4444,
|
|
|
|
Weights: &structs.Weights{
|
|
|
|
Passing: 1,
|
|
|
|
Warning: 1,
|
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: index,
|
|
|
|
ModifyIndex: index,
|
|
|
|
},
|
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.TxnResult{
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "test-sidecar-proxy",
|
|
|
|
ID: "test-sidecar-proxy",
|
|
|
|
Port: 20000,
|
|
|
|
Kind: "connect-proxy",
|
|
|
|
Weights: &structs.Weights{
|
|
|
|
Passing: 1,
|
|
|
|
Warning: 1,
|
|
|
|
},
|
|
|
|
Proxy: structs.ConnectProxyConfig{
|
|
|
|
DestinationServiceName: "test",
|
|
|
|
DestinationServiceID: "test",
|
|
|
|
LocalServiceAddress: "127.0.0.1",
|
|
|
|
LocalServicePort: 4444,
|
|
|
|
},
|
|
|
|
TaggedAddresses: map[string]structs.ServiceAddress{
|
|
|
|
"consul-virtual": {
|
|
|
|
Address: "240.0.0.1",
|
|
|
|
Port: 20000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: index,
|
|
|
|
ModifyIndex: index,
|
|
|
|
},
|
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Equal(t, expected, txnResp)
|
|
|
|
}
|