2016-05-11 04:41:47 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
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"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2016-05-11 04:41:47 +00:00
|
|
|
"github.com/hashicorp/net-rpc-msgpackrpc"
|
|
|
|
)
|
|
|
|
|
2017-04-21 00:50:52 +00:00
|
|
|
func TestTxn_CheckNotExists(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
apply := func(arg *structs.TxnRequest) (*structs.TxnResponse, error) {
|
|
|
|
out := new(structs.TxnResponse)
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Txn.Apply", arg, out)
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
|
|
|
|
checkKeyNotExists := &structs.TxnRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Ops: structs.TxnOps{
|
|
|
|
{
|
|
|
|
KV: &structs.TxnKVOp{
|
|
|
|
Verb: api.KVCheckNotExists,
|
|
|
|
DirEnt: structs.DirEntry{Key: "test"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
createKey := &structs.TxnRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Ops: structs.TxnOps{
|
|
|
|
{
|
|
|
|
KV: &structs.TxnKVOp{
|
|
|
|
Verb: api.KVSet,
|
|
|
|
DirEnt: structs.DirEntry{Key: "test"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := apply(checkKeyNotExists); err != nil {
|
|
|
|
t.Fatalf("testing for non-existent key failed: %s", err)
|
|
|
|
}
|
|
|
|
if _, err := apply(createKey); err != nil {
|
|
|
|
t.Fatalf("creating new key failed: %s", err)
|
|
|
|
}
|
|
|
|
out, err := apply(checkKeyNotExists)
|
|
|
|
if err != nil || out == nil || len(out.Errors) != 1 || out.Errors[0].Error() != `op 0: key "test" exists` {
|
|
|
|
t.Fatalf("testing for existent key failed: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:41:47 +00:00
|
|
|
func TestTxn_Apply(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2016-05-11 04:41:47 +00:00
|
|
|
|
|
|
|
// Do a super basic request. The state store test covers the details so
|
|
|
|
// we just need to be sure that the transaction is sent correctly and
|
|
|
|
// the results are converted appropriately.
|
2016-05-11 08:35:27 +00:00
|
|
|
arg := structs.TxnRequest{
|
2016-05-11 04:41:47 +00:00
|
|
|
Datacenter: "dc1",
|
2016-05-11 08:35:27 +00:00
|
|
|
Ops: structs.TxnOps{
|
|
|
|
&structs.TxnOp{
|
2016-05-11 17:58:27 +00:00
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVSet,
|
2016-05-11 08:35:27 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "test",
|
|
|
|
Flags: 42,
|
|
|
|
Value: []byte("test"),
|
|
|
|
},
|
2016-05-11 04:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
2016-05-11 08:35:27 +00:00
|
|
|
&structs.TxnOp{
|
2016-05-11 17:58:27 +00:00
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVGet,
|
2016-05-11 08:35:27 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "test",
|
|
|
|
},
|
2016-05-11 04:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-05-11 08:35:27 +00:00
|
|
|
var out structs.TxnResponse
|
2016-05-11 04:41:47 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Txn.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the state store directly.
|
|
|
|
state := s1.fsm.State()
|
2017-01-24 19:20:51 +00:00
|
|
|
_, d, err := state.KVSGet(nil, "test")
|
2016-05-11 04:41:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if d == nil {
|
|
|
|
t.Fatalf("should not be nil")
|
|
|
|
}
|
|
|
|
if d.Flags != 42 ||
|
|
|
|
!bytes.Equal(d.Value, []byte("test")) {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the transaction's return value.
|
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: "test",
|
|
|
|
Flags: 42,
|
|
|
|
Value: nil,
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: d.CreateIndex,
|
|
|
|
ModifyIndex: d.ModifyIndex,
|
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: "test",
|
|
|
|
Flags: 42,
|
|
|
|
Value: []byte("test"),
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: d.CreateIndex,
|
|
|
|
ModifyIndex: d.ModifyIndex,
|
2016-05-11 08:35:27 +00:00
|
|
|
},
|
2016-05-11 04:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(out, expected) {
|
|
|
|
t.Fatalf("bad %v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTxn_Apply_ACLDeny(t *testing.T) {
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2016-05-11 04:41:47 +00:00
|
|
|
|
2016-05-13 22:58:55 +00:00
|
|
|
// Put in a key to read back.
|
|
|
|
state := s1.fsm.State()
|
|
|
|
d := &structs.DirEntry{
|
|
|
|
Key: "nope",
|
|
|
|
Value: []byte("hello"),
|
|
|
|
}
|
|
|
|
if err := state.KVSSet(1, d); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:41:47 +00:00
|
|
|
// Create the ACL.
|
|
|
|
var id string
|
|
|
|
{
|
|
|
|
arg := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
Name: "User token",
|
|
|
|
Type: structs.ACLTypeClient,
|
|
|
|
Rules: testListRules,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up a transaction where every operation should get blocked due to
|
|
|
|
// ACLs.
|
2016-05-11 08:35:27 +00:00
|
|
|
arg := structs.TxnRequest{
|
2016-05-11 04:41:47 +00:00
|
|
|
Datacenter: "dc1",
|
2016-05-11 08:35:27 +00:00
|
|
|
Ops: structs.TxnOps{
|
|
|
|
&structs.TxnOp{
|
2016-05-11 17:58:27 +00:00
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVSet,
|
2016-05-11 08:35:27 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
2016-05-13 22:58:55 +00:00
|
|
|
Key: "nope",
|
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.TxnOp{
|
2016-05-11 17:58:27 +00:00
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVDelete,
|
2016-05-11 08:35:27 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
2016-05-13 22:58:55 +00:00
|
|
|
Key: "nope",
|
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.TxnOp{
|
2016-05-11 17:58:27 +00:00
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVDeleteCAS,
|
2016-05-11 08:35:27 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
2016-05-13 22:58:55 +00:00
|
|
|
Key: "nope",
|
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.TxnOp{
|
2016-05-11 17:58:27 +00:00
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVDeleteTree,
|
2016-05-11 08:35:27 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
2016-05-13 22:58:55 +00:00
|
|
|
Key: "nope",
|
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.TxnOp{
|
2016-05-11 17:58:27 +00:00
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVCAS,
|
2016-05-11 08:35:27 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
2016-05-13 22:58:55 +00:00
|
|
|
Key: "nope",
|
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.TxnOp{
|
2016-05-11 17:58:27 +00:00
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVLock,
|
2016-05-11 08:35:27 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
2016-05-13 22:58:55 +00:00
|
|
|
Key: "nope",
|
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.TxnOp{
|
2016-05-11 17:58:27 +00:00
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVUnlock,
|
2016-05-11 08:35:27 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
2016-05-13 22:58:55 +00:00
|
|
|
Key: "nope",
|
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.TxnOp{
|
2016-05-11 17:58:27 +00:00
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVGet,
|
2016-05-11 08:35:27 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "nope",
|
|
|
|
},
|
2016-05-11 04:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
2016-05-13 23:57:39 +00:00
|
|
|
&structs.TxnOp{
|
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVGetTree,
|
2016-05-13 23:57:39 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "nope",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-05-11 08:35:27 +00:00
|
|
|
&structs.TxnOp{
|
2016-05-11 17:58:27 +00:00
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVCheckSession,
|
2016-05-11 08:35:27 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "nope",
|
|
|
|
},
|
2016-05-11 04:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
2016-05-11 08:35:27 +00:00
|
|
|
&structs.TxnOp{
|
2016-05-11 17:58:27 +00:00
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVCheckIndex,
|
2016-05-11 08:35:27 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "nope",
|
|
|
|
},
|
2016-05-11 04:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
2017-04-21 00:50:52 +00:00
|
|
|
&structs.TxnOp{
|
|
|
|
KV: &structs.TxnKVOp{
|
|
|
|
Verb: api.KVCheckNotExists,
|
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "nope",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-05-11 04:41:47 +00:00
|
|
|
},
|
2016-05-13 00:38:25 +00:00
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Token: id,
|
|
|
|
},
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
2016-05-11 08:35:27 +00:00
|
|
|
var out structs.TxnResponse
|
2016-05-11 04:41:47 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Txn.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the transaction's return value.
|
2016-05-11 08:35:27 +00:00
|
|
|
var expected structs.TxnResponse
|
2016-05-13 22:58:55 +00:00
|
|
|
for i, op := range arg.Ops {
|
|
|
|
switch op.KV.Verb {
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVGet, api.KVGetTree:
|
2016-05-13 22:58:55 +00:00
|
|
|
// These get filtered but won't result in an error.
|
|
|
|
|
|
|
|
default:
|
2017-03-23 23:05:35 +00:00
|
|
|
expected.Errors = append(expected.Errors, &structs.TxnError{
|
|
|
|
OpIndex: i,
|
2017-04-21 00:02:42 +00:00
|
|
|
What: errPermissionDenied.Error(),
|
2017-03-23 23:05:35 +00:00
|
|
|
})
|
2016-05-13 22:58:55 +00:00
|
|
|
}
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(out, expected) {
|
|
|
|
t.Fatalf("bad %v", out)
|
|
|
|
}
|
|
|
|
}
|
2016-05-13 00:38:25 +00:00
|
|
|
|
2016-05-11 04:41:47 +00:00
|
|
|
func TestTxn_Apply_LockDelay(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2016-05-11 04:41:47 +00:00
|
|
|
|
|
|
|
// Create and invalidate a session with a lock.
|
|
|
|
state := s1.fsm.State()
|
|
|
|
if err := state.EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
session := &structs.Session{
|
|
|
|
ID: generateUUID(),
|
|
|
|
Node: "foo",
|
|
|
|
LockDelay: 50 * time.Millisecond,
|
|
|
|
}
|
|
|
|
if err := state.SessionCreate(2, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
id := session.ID
|
|
|
|
d := &structs.DirEntry{
|
|
|
|
Key: "test",
|
|
|
|
Session: id,
|
|
|
|
}
|
|
|
|
if ok, err := state.KVSLock(3, d); err != nil || !ok {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if err := state.SessionDestroy(4, id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a new session that is valid.
|
|
|
|
if err := state.SessionCreate(5, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-04-21 00:02:42 +00:00
|
|
|
validID := session.ID
|
2016-05-11 04:41:47 +00:00
|
|
|
|
|
|
|
// Make a lock request via an atomic transaction.
|
2016-05-11 08:35:27 +00:00
|
|
|
arg := structs.TxnRequest{
|
2016-05-11 04:41:47 +00:00
|
|
|
Datacenter: "dc1",
|
2016-05-11 08:35:27 +00:00
|
|
|
Ops: structs.TxnOps{
|
|
|
|
&structs.TxnOp{
|
2016-05-11 17:58:27 +00:00
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVLock,
|
2016-05-11 08:35:27 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "test",
|
2017-04-21 00:02:42 +00:00
|
|
|
Session: validID,
|
2016-05-11 08:35:27 +00:00
|
|
|
},
|
2016-05-11 04:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
{
|
2016-05-11 08:35:27 +00:00
|
|
|
var out structs.TxnResponse
|
2016-05-11 04:41:47 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Txn.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(out.Results) != 0 ||
|
|
|
|
len(out.Errors) != 1 ||
|
|
|
|
out.Errors[0].OpIndex != 0 ||
|
|
|
|
!strings.Contains(out.Errors[0].What, "due to lock delay") {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for lock-delay.
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
|
|
|
// Should acquire.
|
|
|
|
{
|
2016-05-11 08:35:27 +00:00
|
|
|
var out structs.TxnResponse
|
2016-05-11 04:41:47 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Txn.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(out.Results) != 1 ||
|
|
|
|
len(out.Errors) != 0 ||
|
2016-05-11 20:48:03 +00:00
|
|
|
out.Results[0].KV.LockIndex != 2 {
|
2016-05-11 04:41:47 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-13 00:38:25 +00:00
|
|
|
|
|
|
|
func TestTxn_Read(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2016-05-13 00:38:25 +00:00
|
|
|
|
|
|
|
// Put in a key to read back.
|
|
|
|
state := s1.fsm.State()
|
|
|
|
d := &structs.DirEntry{
|
|
|
|
Key: "test",
|
|
|
|
Value: []byte("hello"),
|
|
|
|
}
|
|
|
|
if err := state.KVSSet(1, d); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a super basic request. The state store test covers the details so
|
|
|
|
// we just need to be sure that the transaction is sent correctly and
|
|
|
|
// the results are converted appropriately.
|
|
|
|
arg := structs.TxnReadRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Ops: structs.TxnOps{
|
|
|
|
&structs.TxnOp{
|
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVGet,
|
2016-05-13 00:38:25 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out structs.TxnReadResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Txn.Read", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the transaction's return value.
|
|
|
|
expected := structs.TxnReadResponse{
|
|
|
|
TxnResponse: structs.TxnResponse{
|
|
|
|
Results: structs.TxnResults{
|
|
|
|
&structs.TxnResult{
|
|
|
|
KV: &structs.DirEntry{
|
|
|
|
Key: "test",
|
|
|
|
Value: []byte("hello"),
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: 1,
|
|
|
|
ModifyIndex: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
QueryMeta: structs.QueryMeta{
|
|
|
|
KnownLeader: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(out, expected) {
|
|
|
|
t.Fatalf("bad %v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTxn_Read_ACLDeny(t *testing.T) {
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2016-05-13 00:38:25 +00:00
|
|
|
|
2016-05-13 22:58:55 +00:00
|
|
|
// Put in a key to read back.
|
|
|
|
state := s1.fsm.State()
|
|
|
|
d := &structs.DirEntry{
|
|
|
|
Key: "nope",
|
|
|
|
Value: []byte("hello"),
|
|
|
|
}
|
|
|
|
if err := state.KVSSet(1, d); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-05-13 00:38:25 +00:00
|
|
|
// Create the ACL.
|
|
|
|
var id string
|
|
|
|
{
|
|
|
|
arg := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
Name: "User token",
|
|
|
|
Type: structs.ACLTypeClient,
|
|
|
|
Rules: testListRules,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up a transaction where every operation should get blocked due to
|
|
|
|
// ACLs.
|
|
|
|
arg := structs.TxnReadRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Ops: structs.TxnOps{
|
|
|
|
&structs.TxnOp{
|
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVGet,
|
2016-05-13 00:38:25 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "nope",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-05-13 23:57:39 +00:00
|
|
|
&structs.TxnOp{
|
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVGetTree,
|
2016-05-13 23:57:39 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "nope",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-05-13 00:38:25 +00:00
|
|
|
&structs.TxnOp{
|
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVCheckSession,
|
2016-05-13 00:38:25 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "nope",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.TxnOp{
|
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVCheckIndex,
|
2016-05-13 00:38:25 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "nope",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Token: id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out structs.TxnReadResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Txn.Read", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the transaction's return value.
|
|
|
|
expected := structs.TxnReadResponse{
|
|
|
|
QueryMeta: structs.QueryMeta{
|
|
|
|
KnownLeader: true,
|
|
|
|
},
|
|
|
|
}
|
2016-05-13 22:58:55 +00:00
|
|
|
for i, op := range arg.Ops {
|
|
|
|
switch op.KV.Verb {
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVGet, api.KVGetTree:
|
2016-05-13 22:58:55 +00:00
|
|
|
// These get filtered but won't result in an error.
|
|
|
|
|
|
|
|
default:
|
2017-03-23 23:05:35 +00:00
|
|
|
expected.Errors = append(expected.Errors, &structs.TxnError{
|
|
|
|
OpIndex: i,
|
2017-04-21 00:02:42 +00:00
|
|
|
What: errPermissionDenied.Error(),
|
2017-03-23 23:05:35 +00:00
|
|
|
})
|
2016-05-13 22:58:55 +00:00
|
|
|
}
|
2016-05-13 00:38:25 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(out, expected) {
|
|
|
|
t.Fatalf("bad %v", out)
|
|
|
|
}
|
|
|
|
}
|