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"
|
|
|
|
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/structs"
|
2016-05-11 04:41:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTxnEndpoint_Bad_JSON(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 18:31:20 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer([]byte("{"))
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
if _, err := a.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")
|
|
|
|
}
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTxnEndpoint_Bad_Method(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 18:31:20 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer([]byte("{}"))
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/txn", buf)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
if _, err := a.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 04:41:47 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 19:17:12 +00:00
|
|
|
func TestTxnEndpoint_Bad_Size_Item(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 18:31:20 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer([]byte(fmt.Sprintf(`
|
2016-05-11 08:35:27 +00:00
|
|
|
[
|
|
|
|
{
|
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))))
|
2017-05-21 18:31:20 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
if _, err := a.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 08:35:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 19:17:12 +00:00
|
|
|
func TestTxnEndpoint_Bad_Size_Net(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 18:31:20 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
value := strings.Repeat("X", maxKVSize/2)
|
|
|
|
buf := bytes.NewBuffer([]byte(fmt.Sprintf(`
|
2016-05-13 19:17:12 +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)))
|
2017-05-21 18:31:20 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
if _, err := a.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-13 19:17:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTxnEndpoint_Bad_Size_Ops(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 18:31:20 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer([]byte(fmt.Sprintf(`
|
2016-05-13 19:17:12 +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()
|
|
|
|
if _, err := a.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-13 19:17:12 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 17:58:27 +00:00
|
|
|
func TestTxnEndpoint_KV_Actions(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 18:31:20 +00:00
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
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(`
|
|
|
|
[
|
|
|
|
{
|
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)))
|
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
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(`
|
2016-05-13 00:38:25 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"KV": {
|
|
|
|
"Verb": "get",
|
|
|
|
"Key": "key"
|
|
|
|
}
|
2016-05-13 23:57:39 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"KV": {
|
|
|
|
"Verb": "get-tree",
|
|
|
|
"Key": "key"
|
|
|
|
}
|
2016-05-13 00:38:25 +00:00
|
|
|
}
|
|
|
|
]
|
2016-05-13 19:17:12 +00:00
|
|
|
`))
|
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)
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-05-13 00:38:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
QueryMeta: structs.QueryMeta{
|
|
|
|
KnownLeader: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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)))
|
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
|
|
|
}
|
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.
|
2017-05-21 18:31:20 +00:00
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
2016-05-11 04:41:47 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
]
|
|
|
|
`))
|
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)
|
|
|
|
}
|
|
|
|
if !bytes.Contains(resp.Body.Bytes(), []byte("failed session lookup")) {
|
|
|
|
t.Fatalf("bad: %s", resp.Body.String())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|