2020-05-15 17:59:37 +03:00
|
|
|
// +build kvdb_etcd
|
|
|
|
|
2020-02-18 21:35:53 +03:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
2020-06-29 15:53:38 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-02-18 21:35:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadCursorEmptyInterval(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f := NewEtcdTestFixture(t)
|
|
|
|
defer f.Cleanup()
|
|
|
|
|
|
|
|
db, err := newEtcdBackend(f.BackendConfig())
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
err = db.Update(func(tx walletdb.ReadWriteTx) error {
|
2020-06-29 16:09:35 +03:00
|
|
|
b, err := tx.CreateTopLevelBucket([]byte("apple"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, b)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
return nil
|
2020-10-26 16:06:32 +03:00
|
|
|
}, func() {})
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
err = db.View(func(tx walletdb.ReadTx) error {
|
2020-06-29 16:09:35 +03:00
|
|
|
b := tx.ReadBucket([]byte("apple"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NotNil(t, b)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
cursor := b.ReadCursor()
|
|
|
|
k, v := cursor.First()
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, k)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
k, v = cursor.Next()
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, k)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
k, v = cursor.Last()
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, k)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
k, v = cursor.Prev()
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, k)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
return nil
|
2020-10-20 17:18:40 +03:00
|
|
|
}, func() {})
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadCursorNonEmptyInterval(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f := NewEtcdTestFixture(t)
|
|
|
|
defer f.Cleanup()
|
|
|
|
|
|
|
|
db, err := newEtcdBackend(f.BackendConfig())
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
testKeyValues := []KV{
|
|
|
|
{"b", "1"},
|
|
|
|
{"c", "2"},
|
|
|
|
{"da", "3"},
|
|
|
|
{"e", "4"},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Update(func(tx walletdb.ReadWriteTx) error {
|
2020-06-29 16:09:35 +03:00
|
|
|
b, err := tx.CreateTopLevelBucket([]byte("apple"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, b)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
for _, kv := range testKeyValues {
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, b.Put([]byte(kv.key), []byte(kv.val)))
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
return nil
|
2020-10-26 16:06:32 +03:00
|
|
|
}, func() {})
|
2020-02-18 21:35:53 +03:00
|
|
|
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
err = db.View(func(tx walletdb.ReadTx) error {
|
2020-06-29 16:09:35 +03:00
|
|
|
b := tx.ReadBucket([]byte("apple"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NotNil(t, b)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Iterate from the front.
|
|
|
|
var kvs []KV
|
|
|
|
cursor := b.ReadCursor()
|
|
|
|
k, v := cursor.First()
|
|
|
|
|
|
|
|
for k != nil && v != nil {
|
|
|
|
kvs = append(kvs, KV{string(k), string(v)})
|
|
|
|
k, v = cursor.Next()
|
|
|
|
}
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, testKeyValues, kvs)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Iterate from the back.
|
|
|
|
kvs = []KV{}
|
|
|
|
k, v = cursor.Last()
|
|
|
|
|
|
|
|
for k != nil && v != nil {
|
|
|
|
kvs = append(kvs, KV{string(k), string(v)})
|
|
|
|
k, v = cursor.Prev()
|
|
|
|
}
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, reverseKVs(testKeyValues), kvs)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Random access
|
|
|
|
perm := []int{3, 0, 2, 1}
|
|
|
|
for _, i := range perm {
|
|
|
|
k, v := cursor.Seek([]byte(testKeyValues[i].key))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, []byte(testKeyValues[i].key), k)
|
|
|
|
require.Equal(t, []byte(testKeyValues[i].val), v)
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Seek to nonexisting key.
|
|
|
|
k, v = cursor.Seek(nil)
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, k)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
k, v = cursor.Seek([]byte("x"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, k)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
return nil
|
2020-10-20 17:18:40 +03:00
|
|
|
}, func() {})
|
2020-02-18 21:35:53 +03:00
|
|
|
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadWriteCursor(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f := NewEtcdTestFixture(t)
|
|
|
|
defer f.Cleanup()
|
|
|
|
|
|
|
|
db, err := newEtcdBackend(f.BackendConfig())
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
testKeyValues := []KV{
|
|
|
|
{"b", "1"},
|
|
|
|
{"c", "2"},
|
|
|
|
{"da", "3"},
|
|
|
|
{"e", "4"},
|
|
|
|
}
|
|
|
|
|
|
|
|
count := len(testKeyValues)
|
|
|
|
|
|
|
|
// Pre-store the first half of the interval.
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, db.Update(func(tx walletdb.ReadWriteTx) error {
|
2020-02-18 21:35:53 +03:00
|
|
|
b, err := tx.CreateTopLevelBucket([]byte("apple"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, b)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
for i := 0; i < count/2; i++ {
|
|
|
|
err = b.Put(
|
|
|
|
[]byte(testKeyValues[i].key),
|
|
|
|
[]byte(testKeyValues[i].val),
|
|
|
|
)
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
return nil
|
2020-10-26 16:06:32 +03:00
|
|
|
}, func() {}))
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
err = db.Update(func(tx walletdb.ReadWriteTx) error {
|
|
|
|
b := tx.ReadWriteBucket([]byte("apple"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NotNil(t, b)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Store the second half of the interval.
|
|
|
|
for i := count / 2; i < count; i++ {
|
|
|
|
err = b.Put(
|
|
|
|
[]byte(testKeyValues[i].key),
|
|
|
|
[]byte(testKeyValues[i].val),
|
|
|
|
)
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cursor := b.ReadWriteCursor()
|
|
|
|
|
|
|
|
// First on valid interval.
|
|
|
|
fk, fv := cursor.First()
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, []byte("b"), fk)
|
|
|
|
require.Equal(t, []byte("1"), fv)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Prev(First()) = nil
|
|
|
|
k, v := cursor.Prev()
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, k)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Last on valid interval.
|
|
|
|
lk, lv := cursor.Last()
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, []byte("e"), lk)
|
|
|
|
require.Equal(t, []byte("4"), lv)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Next(Last()) = nil
|
|
|
|
k, v = cursor.Next()
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, k)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Delete first item, then add an item before the
|
|
|
|
// deleted one. Check that First/Next will "jump"
|
|
|
|
// over the deleted item and return the new first.
|
|
|
|
_, _ = cursor.First()
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, cursor.Delete())
|
|
|
|
require.NoError(t, b.Put([]byte("a"), []byte("0")))
|
2020-02-18 21:35:53 +03:00
|
|
|
fk, fv = cursor.First()
|
|
|
|
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, []byte("a"), fk)
|
|
|
|
require.Equal(t, []byte("0"), fv)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
k, v = cursor.Next()
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, []byte("c"), k)
|
|
|
|
require.Equal(t, []byte("2"), v)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Similarly test that a new end is returned if
|
|
|
|
// the old end is deleted first.
|
|
|
|
_, _ = cursor.Last()
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, cursor.Delete())
|
|
|
|
require.NoError(t, b.Put([]byte("f"), []byte("5")))
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
lk, lv = cursor.Last()
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, []byte("f"), lk)
|
|
|
|
require.Equal(t, []byte("5"), lv)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
k, v = cursor.Prev()
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, []byte("da"), k)
|
|
|
|
require.Equal(t, []byte("3"), v)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Overwrite k/v in the middle of the interval.
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, b.Put([]byte("c"), []byte("3")))
|
2020-02-18 21:35:53 +03:00
|
|
|
k, v = cursor.Prev()
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, []byte("c"), k)
|
|
|
|
require.Equal(t, []byte("3"), v)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Insert new key/values.
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, b.Put([]byte("cx"), []byte("x")))
|
|
|
|
require.NoError(t, b.Put([]byte("cy"), []byte("y")))
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
k, v = cursor.Next()
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, []byte("cx"), k)
|
|
|
|
require.Equal(t, []byte("x"), v)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
k, v = cursor.Next()
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, []byte("cy"), k)
|
|
|
|
require.Equal(t, []byte("y"), v)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
expected := []KV{
|
|
|
|
{"a", "0"},
|
|
|
|
{"c", "3"},
|
|
|
|
{"cx", "x"},
|
|
|
|
{"cy", "y"},
|
|
|
|
{"da", "3"},
|
|
|
|
{"f", "5"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate from the front.
|
|
|
|
var kvs []KV
|
|
|
|
k, v = cursor.First()
|
|
|
|
|
|
|
|
for k != nil && v != nil {
|
|
|
|
kvs = append(kvs, KV{string(k), string(v)})
|
|
|
|
k, v = cursor.Next()
|
|
|
|
}
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, expected, kvs)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Iterate from the back.
|
|
|
|
kvs = []KV{}
|
|
|
|
k, v = cursor.Last()
|
|
|
|
|
|
|
|
for k != nil && v != nil {
|
|
|
|
kvs = append(kvs, KV{string(k), string(v)})
|
|
|
|
k, v = cursor.Prev()
|
|
|
|
}
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, reverseKVs(expected), kvs)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
return nil
|
2020-10-26 16:06:32 +03:00
|
|
|
}, func() {})
|
2020-02-18 21:35:53 +03:00
|
|
|
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
expected := map[string]string{
|
|
|
|
bkey("apple"): bval("apple"),
|
|
|
|
vkey("a", "apple"): "0",
|
|
|
|
vkey("c", "apple"): "3",
|
|
|
|
vkey("cx", "apple"): "x",
|
|
|
|
vkey("cy", "apple"): "y",
|
|
|
|
vkey("da", "apple"): "3",
|
|
|
|
vkey("f", "apple"): "5",
|
|
|
|
}
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, expected, f.Dump())
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
2020-06-26 21:08:08 +03:00
|
|
|
|
|
|
|
// TestReadWriteCursorWithBucketAndValue tests that cursors are able to iterate
|
|
|
|
// over both bucket and value keys if both are present in the iterated bucket.
|
|
|
|
func TestReadWriteCursorWithBucketAndValue(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f := NewEtcdTestFixture(t)
|
|
|
|
defer f.Cleanup()
|
|
|
|
|
|
|
|
db, err := newEtcdBackend(f.BackendConfig())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Pre-store the first half of the interval.
|
|
|
|
require.NoError(t, db.Update(func(tx walletdb.ReadWriteTx) error {
|
|
|
|
b, err := tx.CreateTopLevelBucket([]byte("apple"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, b)
|
|
|
|
|
|
|
|
require.NoError(t, b.Put([]byte("key"), []byte("val")))
|
|
|
|
|
|
|
|
b1, err := b.CreateBucket([]byte("banana"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, b1)
|
|
|
|
|
|
|
|
b2, err := b.CreateBucket([]byte("pear"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, b2)
|
|
|
|
|
|
|
|
return nil
|
2020-10-26 16:06:32 +03:00
|
|
|
}, func() {}))
|
2020-06-26 21:08:08 +03:00
|
|
|
|
|
|
|
err = db.View(func(tx walletdb.ReadTx) error {
|
|
|
|
b := tx.ReadBucket([]byte("apple"))
|
|
|
|
require.NotNil(t, b)
|
|
|
|
|
|
|
|
cursor := b.ReadCursor()
|
|
|
|
|
|
|
|
// First on valid interval.
|
|
|
|
k, v := cursor.First()
|
|
|
|
require.Equal(t, []byte("banana"), k)
|
|
|
|
require.Nil(t, v)
|
|
|
|
|
|
|
|
k, v = cursor.Next()
|
|
|
|
require.Equal(t, []byte("key"), k)
|
|
|
|
require.Equal(t, []byte("val"), v)
|
|
|
|
|
|
|
|
k, v = cursor.Last()
|
|
|
|
require.Equal(t, []byte("pear"), k)
|
|
|
|
require.Nil(t, v)
|
|
|
|
|
|
|
|
k, v = cursor.Seek([]byte("k"))
|
|
|
|
require.Equal(t, []byte("key"), k)
|
|
|
|
require.Equal(t, []byte("val"), v)
|
|
|
|
|
|
|
|
k, v = cursor.Seek([]byte("banana"))
|
|
|
|
require.Equal(t, []byte("banana"), k)
|
|
|
|
require.Nil(t, v)
|
|
|
|
|
|
|
|
k, v = cursor.Next()
|
|
|
|
require.Equal(t, []byte("key"), k)
|
|
|
|
require.Equal(t, []byte("val"), v)
|
|
|
|
|
|
|
|
return nil
|
2020-10-20 17:18:40 +03:00
|
|
|
}, func() {})
|
2020-06-26 21:08:08 +03:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := map[string]string{
|
|
|
|
bkey("apple"): bval("apple"),
|
|
|
|
bkey("apple", "banana"): bval("apple", "banana"),
|
|
|
|
bkey("apple", "pear"): bval("apple", "pear"),
|
|
|
|
vkey("key", "apple"): "val",
|
|
|
|
}
|
|
|
|
require.Equal(t, expected, f.Dump())
|
|
|
|
}
|