2018-12-12 17:14:02 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
|
|
|
|
2018-12-12 17:14:02 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPI_ClientTxn(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2019-07-12 15:37:29 +00:00
|
|
|
s.WaitForSerfCheck(t)
|
|
|
|
|
2018-12-12 17:14:02 +00:00
|
|
|
session := c.Session()
|
|
|
|
txn := c.Txn()
|
|
|
|
|
|
|
|
// Set up a test service and health check.
|
|
|
|
nodeID, err := uuid.GenerateUUID()
|
2019-07-17 15:33:38 +00:00
|
|
|
require.NoError(t, err)
|
2018-12-12 17:14:02 +00:00
|
|
|
|
|
|
|
catalog := c.Catalog()
|
|
|
|
reg := &CatalogRegistration{
|
|
|
|
ID: nodeID,
|
|
|
|
Node: "foo",
|
|
|
|
Address: "2.2.2.2",
|
|
|
|
Service: &AgentService{
|
|
|
|
ID: "foo1",
|
|
|
|
Service: "foo",
|
|
|
|
},
|
2019-01-25 01:28:52 +00:00
|
|
|
Checks: HealthChecks{
|
|
|
|
{
|
|
|
|
CheckID: "bar",
|
|
|
|
Status: "critical",
|
|
|
|
Definition: HealthCheckDefinition{
|
|
|
|
TCP: "1.1.1.1",
|
|
|
|
IntervalDuration: 5 * time.Second,
|
|
|
|
TimeoutDuration: 10 * time.Second,
|
|
|
|
DeregisterCriticalServiceAfterDuration: 20 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CheckID: "baz",
|
|
|
|
Status: "passing",
|
|
|
|
Definition: HealthCheckDefinition{
|
|
|
|
TCP: "2.2.2.2",
|
|
|
|
Interval: ReadableDuration(40 * time.Second),
|
|
|
|
Timeout: ReadableDuration(80 * time.Second),
|
|
|
|
DeregisterCriticalServiceAfter: ReadableDuration(160 * time.Second),
|
|
|
|
},
|
2018-12-12 17:14:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err = catalog.Register(reg, nil)
|
2019-07-17 15:33:38 +00:00
|
|
|
require.NoError(t, err)
|
2018-12-12 17:14:02 +00:00
|
|
|
|
|
|
|
node, _, err := catalog.Node("foo", nil)
|
2019-07-17 15:33:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, nodeID, node.Node.ID)
|
2018-12-12 17:14:02 +00:00
|
|
|
|
|
|
|
// Make a session.
|
|
|
|
id, _, err := session.CreateNoChecks(nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer session.Destroy(id, nil)
|
|
|
|
|
|
|
|
// Acquire and get the key via a transaction, but don't supply a valid
|
|
|
|
// session.
|
|
|
|
key := testKey()
|
|
|
|
value := []byte("test")
|
|
|
|
ops := TxnOps{
|
|
|
|
&TxnOp{
|
|
|
|
KV: &KVTxnOp{
|
|
|
|
Verb: KVLock,
|
|
|
|
Key: key,
|
|
|
|
Value: value,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&TxnOp{
|
|
|
|
KV: &KVTxnOp{
|
|
|
|
Verb: KVGet,
|
|
|
|
Key: key,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&TxnOp{
|
|
|
|
Node: &NodeTxnOp{
|
|
|
|
Verb: NodeGet,
|
|
|
|
Node: Node{Node: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&TxnOp{
|
|
|
|
Service: &ServiceTxnOp{
|
|
|
|
Verb: ServiceGet,
|
|
|
|
Node: "foo",
|
2019-01-16 00:54:07 +00:00
|
|
|
Service: AgentService{ID: "foo1"},
|
2018-12-12 17:14:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&TxnOp{
|
|
|
|
Check: &CheckTxnOp{
|
|
|
|
Verb: CheckGet,
|
|
|
|
Check: HealthCheck{Node: "foo", CheckID: "bar"},
|
|
|
|
},
|
|
|
|
},
|
2019-01-25 01:28:52 +00:00
|
|
|
&TxnOp{
|
|
|
|
Check: &CheckTxnOp{
|
|
|
|
Verb: CheckGet,
|
|
|
|
Check: HealthCheck{Node: "foo", CheckID: "baz"},
|
|
|
|
},
|
|
|
|
},
|
2018-12-12 17:14:02 +00:00
|
|
|
}
|
|
|
|
ok, ret, _, err := txn.Txn(ops, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
} else if ok {
|
|
|
|
t.Fatalf("transaction should have failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret == nil || len(ret.Errors) != 2 || len(ret.Results) != 0 {
|
|
|
|
t.Fatalf("bad: %v", ret.Errors[2])
|
|
|
|
}
|
|
|
|
if ret.Errors[0].OpIndex != 0 ||
|
|
|
|
!strings.Contains(ret.Errors[0].What, "missing session") ||
|
|
|
|
!strings.Contains(ret.Errors[1].What, "doesn't exist") {
|
|
|
|
t.Fatalf("bad: %v", ret.Errors[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now poke in a real session and try again.
|
|
|
|
ops[0].KV.Session = id
|
|
|
|
ok, ret, _, err = txn.Txn(ops, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
} else if !ok {
|
|
|
|
t.Fatalf("transaction failure")
|
|
|
|
}
|
|
|
|
|
2019-01-25 01:28:52 +00:00
|
|
|
if ret == nil || len(ret.Errors) != 0 || len(ret.Results) != 6 {
|
2018-12-12 17:14:02 +00:00
|
|
|
t.Fatalf("bad: %v", ret)
|
|
|
|
}
|
|
|
|
expected := TxnResults{
|
|
|
|
&TxnResult{
|
|
|
|
KV: &KVPair{
|
|
|
|
Key: key,
|
|
|
|
Session: id,
|
|
|
|
LockIndex: 1,
|
|
|
|
CreateIndex: ret.Results[0].KV.CreateIndex,
|
|
|
|
ModifyIndex: ret.Results[0].KV.ModifyIndex,
|
2019-12-10 02:26:41 +00:00
|
|
|
Namespace: ret.Results[0].KV.Namespace,
|
2018-12-12 17:14:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&TxnResult{
|
|
|
|
KV: &KVPair{
|
|
|
|
Key: key,
|
|
|
|
Session: id,
|
|
|
|
Value: []byte("test"),
|
|
|
|
LockIndex: 1,
|
|
|
|
CreateIndex: ret.Results[1].KV.CreateIndex,
|
|
|
|
ModifyIndex: ret.Results[1].KV.ModifyIndex,
|
2019-12-10 02:26:41 +00:00
|
|
|
Namespace: ret.Results[0].KV.Namespace,
|
2018-12-12 17:14:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&TxnResult{
|
|
|
|
Node: &Node{
|
|
|
|
ID: nodeID,
|
|
|
|
Node: "foo",
|
2021-08-17 18:29:39 +00:00
|
|
|
Partition: defaultPartition,
|
2018-12-12 17:14:02 +00:00
|
|
|
Address: "2.2.2.2",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
CreateIndex: ret.Results[2].Node.CreateIndex,
|
|
|
|
ModifyIndex: ret.Results[2].Node.CreateIndex,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&TxnResult{
|
|
|
|
Service: &CatalogService{
|
|
|
|
ID: "foo1",
|
|
|
|
CreateIndex: ret.Results[3].Service.CreateIndex,
|
|
|
|
ModifyIndex: ret.Results[3].Service.CreateIndex,
|
2021-08-12 21:31:37 +00:00
|
|
|
Partition: defaultPartition,
|
2019-12-10 02:26:41 +00:00
|
|
|
Namespace: defaultNamespace,
|
2018-12-12 17:14:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&TxnResult{
|
|
|
|
Check: &HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "bar",
|
|
|
|
Status: "critical",
|
|
|
|
Definition: HealthCheckDefinition{
|
2019-01-25 01:28:52 +00:00
|
|
|
TCP: "1.1.1.1",
|
|
|
|
Interval: ReadableDuration(5 * time.Second),
|
|
|
|
IntervalDuration: 5 * time.Second,
|
|
|
|
Timeout: ReadableDuration(10 * time.Second),
|
|
|
|
TimeoutDuration: 10 * time.Second,
|
|
|
|
DeregisterCriticalServiceAfter: ReadableDuration(20 * time.Second),
|
|
|
|
DeregisterCriticalServiceAfterDuration: 20 * time.Second,
|
|
|
|
},
|
2019-10-17 18:33:11 +00:00
|
|
|
Type: "tcp",
|
2021-08-12 21:31:37 +00:00
|
|
|
Partition: defaultPartition,
|
2019-12-10 02:26:41 +00:00
|
|
|
Namespace: defaultNamespace,
|
2019-01-25 01:28:52 +00:00
|
|
|
CreateIndex: ret.Results[4].Check.CreateIndex,
|
|
|
|
ModifyIndex: ret.Results[4].Check.CreateIndex,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&TxnResult{
|
|
|
|
Check: &HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "baz",
|
|
|
|
Status: "passing",
|
|
|
|
Definition: HealthCheckDefinition{
|
|
|
|
TCP: "2.2.2.2",
|
|
|
|
Interval: ReadableDuration(40 * time.Second),
|
|
|
|
IntervalDuration: 40 * time.Second,
|
|
|
|
Timeout: ReadableDuration(80 * time.Second),
|
|
|
|
TimeoutDuration: 80 * time.Second,
|
|
|
|
DeregisterCriticalServiceAfter: ReadableDuration(160 * time.Second),
|
|
|
|
DeregisterCriticalServiceAfterDuration: 160 * time.Second,
|
2018-12-12 17:14:02 +00:00
|
|
|
},
|
2019-10-17 18:33:11 +00:00
|
|
|
Type: "tcp",
|
2021-08-12 21:31:37 +00:00
|
|
|
Partition: defaultPartition,
|
2019-12-10 02:26:41 +00:00
|
|
|
Namespace: defaultNamespace,
|
2018-12-12 17:14:02 +00:00
|
|
|
CreateIndex: ret.Results[4].Check.CreateIndex,
|
|
|
|
ModifyIndex: ret.Results[4].Check.CreateIndex,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-08-12 21:31:37 +00:00
|
|
|
require.Equal(t, expected, ret.Results)
|
2018-12-12 17:14:02 +00:00
|
|
|
|
2019-07-17 15:33:38 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
// Run a read-only transaction.
|
|
|
|
ops = TxnOps{
|
|
|
|
&TxnOp{
|
|
|
|
KV: &KVTxnOp{
|
|
|
|
Verb: KVGet,
|
|
|
|
Key: key,
|
|
|
|
},
|
2018-12-12 17:14:02 +00:00
|
|
|
},
|
2019-07-17 15:33:38 +00:00
|
|
|
&TxnOp{
|
|
|
|
Node: &NodeTxnOp{
|
|
|
|
Verb: NodeGet,
|
|
|
|
Node: Node{ID: s.Config.NodeID, Node: s.Config.NodeName},
|
|
|
|
},
|
2018-12-12 17:14:02 +00:00
|
|
|
},
|
2019-07-17 15:33:38 +00:00
|
|
|
}
|
|
|
|
ok, ret, _, err = txn.Txn(ops, nil)
|
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("err: %v", err)
|
|
|
|
} else if !ok {
|
|
|
|
r.Fatalf("transaction failure")
|
|
|
|
}
|
2018-12-12 17:14:02 +00:00
|
|
|
|
2019-07-17 15:33:38 +00:00
|
|
|
expected = TxnResults{
|
|
|
|
&TxnResult{
|
|
|
|
KV: &KVPair{
|
|
|
|
Key: key,
|
|
|
|
Session: id,
|
|
|
|
Value: []byte("test"),
|
|
|
|
LockIndex: 1,
|
|
|
|
CreateIndex: ret.Results[0].KV.CreateIndex,
|
|
|
|
ModifyIndex: ret.Results[0].KV.ModifyIndex,
|
2019-12-10 02:26:41 +00:00
|
|
|
Namespace: ret.Results[0].KV.Namespace,
|
2019-07-17 15:33:38 +00:00
|
|
|
},
|
2018-12-12 17:14:02 +00:00
|
|
|
},
|
2019-07-17 15:33:38 +00:00
|
|
|
&TxnResult{
|
|
|
|
Node: &Node{
|
|
|
|
ID: s.Config.NodeID,
|
|
|
|
Node: s.Config.NodeName,
|
2021-08-17 18:29:39 +00:00
|
|
|
Partition: defaultPartition,
|
2019-07-17 15:33:38 +00:00
|
|
|
Address: "127.0.0.1",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
TaggedAddresses: map[string]string{
|
2020-01-17 14:54:17 +00:00
|
|
|
"lan": s.Config.Bind,
|
|
|
|
"lan_ipv4": s.Config.Bind,
|
|
|
|
"wan": s.Config.Bind,
|
|
|
|
"wan_ipv4": s.Config.Bind,
|
2019-07-17 15:33:38 +00:00
|
|
|
},
|
|
|
|
Meta: map[string]string{"consul-network-segment": ""},
|
|
|
|
CreateIndex: ret.Results[1].Node.CreateIndex,
|
|
|
|
ModifyIndex: ret.Results[1].Node.ModifyIndex,
|
2018-12-12 17:14:02 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-17 15:33:38 +00:00
|
|
|
}
|
2021-08-17 18:29:39 +00:00
|
|
|
require.Equal(r, expected, ret.Results)
|
2019-07-17 15:33:38 +00:00
|
|
|
})
|
2018-12-12 17:14:02 +00:00
|
|
|
|
|
|
|
// Sanity check using the regular GET API.
|
|
|
|
kv := c.KV()
|
|
|
|
pair, meta, err := kv.Get(key, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if pair == nil {
|
|
|
|
t.Fatalf("expected value: %#v", pair)
|
|
|
|
}
|
|
|
|
if pair.LockIndex != 1 {
|
|
|
|
t.Fatalf("Expected lock: %v", pair)
|
|
|
|
}
|
|
|
|
if pair.Session != id {
|
|
|
|
t.Fatalf("Expected lock: %v", pair)
|
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
|
|
|
t.Fatalf("unexpected value: %#v", meta)
|
|
|
|
}
|
|
|
|
}
|