2023-03-28 20:12:41 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"path"
|
2016-05-07 00:50:58 +00:00
|
|
|
"strings"
|
2015-01-06 18:40:00 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2020-06-05 20:52:31 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2015-01-06 18:40:00 +00:00
|
|
|
)
|
|
|
|
|
2017-06-30 20:58:55 +00:00
|
|
|
func TestAPI_ClientPutGetDelete(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
kv := c.KV()
|
|
|
|
|
2019-02-22 15:37:09 +00:00
|
|
|
s.WaitForSerfCheck(t)
|
2015-01-06 18:40:00 +00:00
|
|
|
// Get a get without a key
|
|
|
|
key := testKey()
|
|
|
|
pair, _, err := kv.Get(key, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if pair != nil {
|
|
|
|
t.Fatalf("unexpected value: %#v", pair)
|
|
|
|
}
|
|
|
|
|
|
|
|
value := []byte("test")
|
|
|
|
|
2015-06-09 19:01:20 +00:00
|
|
|
// Put a key that begins with a '/', this should fail
|
2015-04-01 03:08:06 +00:00
|
|
|
invalidKey := "/test"
|
2015-06-09 19:01:20 +00:00
|
|
|
p := &KVPair{Key: invalidKey, Flags: 42, Value: value}
|
2015-04-01 03:08:06 +00:00
|
|
|
if _, err := kv.Put(p, nil); err == nil {
|
|
|
|
t.Fatalf("Invalid key not detected: %s", invalidKey)
|
|
|
|
}
|
|
|
|
|
2015-06-09 19:01:20 +00:00
|
|
|
// Put the key
|
|
|
|
p = &KVPair{Key: key, Flags: 42, Value: value}
|
|
|
|
if _, err := kv.Put(p, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
// Get should work
|
|
|
|
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 !bytes.Equal(pair.Value, value) {
|
|
|
|
t.Fatalf("unexpected value: %#v", pair)
|
|
|
|
}
|
|
|
|
if pair.Flags != 42 {
|
|
|
|
t.Fatalf("unexpected value: %#v", pair)
|
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
|
|
|
t.Fatalf("unexpected value: %#v", meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete
|
|
|
|
if _, err := kv.Delete(key, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get should fail
|
|
|
|
pair, _, err = kv.Get(key, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if pair != nil {
|
|
|
|
t.Fatalf("unexpected value: %#v", pair)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_ClientList_DeleteRecurse(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
kv := c.KV()
|
|
|
|
|
|
|
|
// Generate some test keys
|
|
|
|
prefix := testKey()
|
|
|
|
var keys []string
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
keys = append(keys, path.Join(prefix, testKey()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set values
|
|
|
|
value := []byte("test")
|
|
|
|
for _, key := range keys {
|
|
|
|
p := &KVPair{Key: key, Value: value}
|
|
|
|
if _, err := kv.Put(p, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// List the values
|
|
|
|
pairs, meta, err := kv.List(prefix, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(pairs) != len(keys) {
|
|
|
|
t.Fatalf("got %d keys", len(pairs))
|
|
|
|
}
|
|
|
|
for _, pair := range pairs {
|
|
|
|
if !bytes.Equal(pair.Value, value) {
|
|
|
|
t.Fatalf("unexpected value: %#v", pair)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
|
|
|
t.Fatalf("unexpected value: %#v", meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all
|
|
|
|
if _, err := kv.DeleteTree(prefix, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List the values
|
|
|
|
pairs, _, err = kv.List(prefix, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(pairs) != 0 {
|
|
|
|
t.Fatalf("got %d keys", len(pairs))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_ClientDeleteCAS(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-13 21:57:48 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-13 21:57:48 +00:00
|
|
|
|
|
|
|
kv := c.KV()
|
|
|
|
|
|
|
|
// Put the key
|
|
|
|
key := testKey()
|
|
|
|
value := []byte("test")
|
|
|
|
p := &KVPair{Key: key, Value: value}
|
|
|
|
if work, _, err := kv.CAS(p, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
} else if !work {
|
|
|
|
t.Fatalf("CAS failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get should work
|
|
|
|
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 meta.LastIndex == 0 {
|
|
|
|
t.Fatalf("unexpected value: %#v", meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CAS update with bad index
|
|
|
|
p.ModifyIndex = 1
|
|
|
|
if work, _, err := kv.DeleteCAS(p, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
} else if work {
|
|
|
|
t.Fatalf("unexpected CAS")
|
|
|
|
}
|
|
|
|
|
|
|
|
// CAS update with valid index
|
|
|
|
p.ModifyIndex = meta.LastIndex
|
|
|
|
if work, _, err := kv.DeleteCAS(p, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
} else if !work {
|
|
|
|
t.Fatalf("unexpected CAS failure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_ClientCAS(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
kv := c.KV()
|
|
|
|
|
|
|
|
// Put the key
|
|
|
|
key := testKey()
|
|
|
|
value := []byte("test")
|
|
|
|
p := &KVPair{Key: key, Value: value}
|
|
|
|
if work, _, err := kv.CAS(p, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
} else if !work {
|
|
|
|
t.Fatalf("CAS failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get should work
|
|
|
|
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 meta.LastIndex == 0 {
|
|
|
|
t.Fatalf("unexpected value: %#v", meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CAS update with bad index
|
|
|
|
newVal := []byte("foo")
|
|
|
|
p.Value = newVal
|
|
|
|
p.ModifyIndex = 1
|
|
|
|
if work, _, err := kv.CAS(p, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
} else if work {
|
|
|
|
t.Fatalf("unexpected CAS")
|
|
|
|
}
|
|
|
|
|
|
|
|
// CAS update with valid index
|
|
|
|
p.ModifyIndex = meta.LastIndex
|
|
|
|
if work, _, err := kv.CAS(p, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
} else if !work {
|
|
|
|
t.Fatalf("unexpected CAS failure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_ClientWatchGet(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
kv := c.KV()
|
|
|
|
|
2019-02-22 15:37:09 +00:00
|
|
|
s.WaitForSerfCheck(t)
|
2015-01-06 18:40:00 +00:00
|
|
|
// Get a get without a key
|
|
|
|
key := testKey()
|
|
|
|
pair, meta, err := kv.Get(key, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if pair != nil {
|
|
|
|
t.Fatalf("unexpected value: %#v", pair)
|
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
|
|
|
t.Fatalf("unexpected value: %#v", meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the key
|
|
|
|
value := []byte("test")
|
2020-06-05 20:52:31 +00:00
|
|
|
doneCh := make(chan error)
|
2015-01-06 18:40:00 +00:00
|
|
|
go func() {
|
|
|
|
kv := c.KV()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
p := &KVPair{Key: key, Flags: 42, Value: value}
|
2020-06-05 20:52:31 +00:00
|
|
|
_, err := kv.Put(p, nil)
|
|
|
|
doneCh <- err
|
2015-01-06 18:40:00 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Get should work
|
|
|
|
options := &QueryOptions{WaitIndex: meta.LastIndex}
|
|
|
|
pair, meta2, err := kv.Get(key, options)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if pair == nil {
|
|
|
|
t.Fatalf("expected value: %#v", pair)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(pair.Value, value) {
|
|
|
|
t.Fatalf("unexpected value: %#v", pair)
|
|
|
|
}
|
|
|
|
if pair.Flags != 42 {
|
|
|
|
t.Fatalf("unexpected value: %#v", pair)
|
|
|
|
}
|
|
|
|
if meta2.LastIndex <= meta.LastIndex {
|
|
|
|
t.Fatalf("unexpected value: %#v", meta2)
|
|
|
|
}
|
2016-10-25 22:18:38 +00:00
|
|
|
|
|
|
|
// Block until put finishes to avoid a race between it and deferred s.Stop()
|
2020-06-05 20:52:31 +00:00
|
|
|
err = <-doneCh
|
|
|
|
require.NoError(t, err)
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_ClientWatchList(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
kv := c.KV()
|
|
|
|
|
|
|
|
// Get a get without a key
|
|
|
|
prefix := testKey()
|
|
|
|
key := path.Join(prefix, testKey())
|
|
|
|
pairs, meta, err := kv.List(prefix, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(pairs) != 0 {
|
|
|
|
t.Fatalf("unexpected value: %#v", pairs)
|
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
|
|
|
t.Fatalf("unexpected value: %#v", meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the key
|
|
|
|
value := []byte("test")
|
2020-06-05 20:52:31 +00:00
|
|
|
doneCh := make(chan error)
|
2015-01-06 18:40:00 +00:00
|
|
|
go func() {
|
|
|
|
kv := c.KV()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
p := &KVPair{Key: key, Flags: 42, Value: value}
|
2020-06-05 20:52:31 +00:00
|
|
|
_, err := kv.Put(p, nil)
|
|
|
|
doneCh <- err
|
2015-01-06 18:40:00 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Get should work
|
|
|
|
options := &QueryOptions{WaitIndex: meta.LastIndex}
|
|
|
|
pairs, meta2, err := kv.List(prefix, options)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(pairs) != 1 {
|
|
|
|
t.Fatalf("expected value: %#v", pairs)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(pairs[0].Value, value) {
|
|
|
|
t.Fatalf("unexpected value: %#v", pairs)
|
|
|
|
}
|
|
|
|
if pairs[0].Flags != 42 {
|
|
|
|
t.Fatalf("unexpected value: %#v", pairs)
|
|
|
|
}
|
|
|
|
if meta2.LastIndex <= meta.LastIndex {
|
|
|
|
t.Fatalf("unexpected value: %#v", meta2)
|
|
|
|
}
|
|
|
|
|
2016-10-25 22:18:38 +00:00
|
|
|
// Block until put finishes to avoid a race between it and deferred s.Stop()
|
2020-06-05 20:52:31 +00:00
|
|
|
err = <-doneCh
|
|
|
|
require.NoError(t, err)
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_ClientKeys_DeleteRecurse(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
kv := c.KV()
|
|
|
|
|
|
|
|
// Generate some test keys
|
|
|
|
prefix := testKey()
|
|
|
|
var keys []string
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
keys = append(keys, path.Join(prefix, testKey()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set values
|
|
|
|
value := []byte("test")
|
|
|
|
for _, key := range keys {
|
|
|
|
p := &KVPair{Key: key, Value: value}
|
|
|
|
if _, err := kv.Put(p, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// List the values
|
|
|
|
out, meta, err := kv.Keys(prefix, "", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(out) != len(keys) {
|
|
|
|
t.Fatalf("got %d keys", len(out))
|
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
|
|
|
t.Fatalf("unexpected value: %#v", meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all
|
|
|
|
if _, err := kv.DeleteTree(prefix, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List the values
|
|
|
|
out, _, err = kv.Keys(prefix, "", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(out) != 0 {
|
|
|
|
t.Fatalf("got %d keys", len(out))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_ClientAcquireRelease(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
s.WaitForSerfCheck(t)
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
session := c.Session()
|
|
|
|
kv := c.KV()
|
|
|
|
|
|
|
|
// Make a session
|
|
|
|
id, _, err := session.CreateNoChecks(nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer session.Destroy(id, nil)
|
|
|
|
|
|
|
|
// Acquire the key
|
|
|
|
key := testKey()
|
|
|
|
value := []byte("test")
|
|
|
|
p := &KVPair{Key: key, Value: value, Session: id}
|
|
|
|
if work, _, err := kv.Acquire(p, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
} else if !work {
|
|
|
|
t.Fatalf("Lock failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get should work
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release
|
|
|
|
if work, _, err := kv.Release(p, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
} else if !work {
|
|
|
|
t.Fatalf("Release fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get should work
|
|
|
|
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 != "" {
|
|
|
|
t.Fatalf("Expected unlock: %v", pair)
|
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
|
|
|
t.Fatalf("unexpected value: %#v", meta)
|
|
|
|
}
|
|
|
|
}
|
2016-05-07 00:50:58 +00:00
|
|
|
|
2018-12-12 17:14:02 +00:00
|
|
|
func TestAPI_KVClientTxn(t *testing.T) {
|
2016-05-07 00:50:58 +00:00
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
s.WaitForSerfCheck(t)
|
|
|
|
|
2016-05-07 00:50:58 +00:00
|
|
|
session := c.Session()
|
|
|
|
kv := c.KV()
|
|
|
|
|
|
|
|
// 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")
|
2016-05-11 08:35:27 +00:00
|
|
|
txn := KVTxnOps{
|
|
|
|
&KVTxnOp{
|
|
|
|
Verb: KVLock,
|
2016-05-07 00:50:58 +00:00
|
|
|
Key: key,
|
|
|
|
Value: value,
|
|
|
|
},
|
2016-05-11 08:35:27 +00:00
|
|
|
&KVTxnOp{
|
|
|
|
Verb: KVGet,
|
|
|
|
Key: key,
|
2016-05-07 00:50:58 +00:00
|
|
|
},
|
|
|
|
}
|
2016-05-11 08:35:27 +00:00
|
|
|
ok, ret, _, err := kv.Txn(txn, nil)
|
2016-05-07 00:50:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
} else if ok {
|
|
|
|
t.Fatalf("transaction should have failed")
|
|
|
|
}
|
|
|
|
|
2016-05-11 21:18:31 +00:00
|
|
|
if ret == nil || len(ret.Errors) != 2 || len(ret.Results) != 0 {
|
2016-05-07 00:50:58 +00:00
|
|
|
t.Fatalf("bad: %v", ret)
|
|
|
|
}
|
|
|
|
if ret.Errors[0].OpIndex != 0 ||
|
2016-05-11 21:18:31 +00:00
|
|
|
!strings.Contains(ret.Errors[0].What, "missing session") ||
|
|
|
|
!strings.Contains(ret.Errors[1].What, "doesn't exist") {
|
2016-05-07 00:50:58 +00:00
|
|
|
t.Fatalf("bad: %v", ret.Errors[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now poke in a real session and try again.
|
|
|
|
txn[0].Session = id
|
2016-05-11 08:35:27 +00:00
|
|
|
ok, ret, _, err = kv.Txn(txn, nil)
|
2016-05-07 00:50:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
} else if !ok {
|
|
|
|
t.Fatalf("transaction failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret == nil || len(ret.Errors) != 0 || len(ret.Results) != 2 {
|
|
|
|
t.Fatalf("bad: %v", ret)
|
|
|
|
}
|
|
|
|
for i, result := range ret.Results {
|
|
|
|
var expected []byte
|
|
|
|
if i == 1 {
|
|
|
|
expected = value
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.Key != key ||
|
|
|
|
!bytes.Equal(result.Value, expected) ||
|
|
|
|
result.Session != id ||
|
|
|
|
result.LockIndex != 1 {
|
|
|
|
t.Fatalf("bad: %v", result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-13 00:38:25 +00:00
|
|
|
// Run a read-only transaction.
|
|
|
|
txn = KVTxnOps{
|
|
|
|
&KVTxnOp{
|
|
|
|
Verb: KVGet,
|
|
|
|
Key: key,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ok, ret, _, err = kv.Txn(txn, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
} else if !ok {
|
|
|
|
t.Fatalf("transaction failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret == nil || len(ret.Errors) != 0 || len(ret.Results) != 1 {
|
|
|
|
t.Fatalf("bad: %v", ret)
|
|
|
|
}
|
|
|
|
for _, result := range ret.Results {
|
|
|
|
if result.Key != key ||
|
|
|
|
!bytes.Equal(result.Value, value) ||
|
|
|
|
result.Session != id ||
|
|
|
|
result.LockIndex != 1 {
|
|
|
|
t.Fatalf("bad: %v", result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-07 00:50:58 +00:00
|
|
|
// Sanity check using the regular GET API.
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|