2020-05-15 17:59:37 +03:00
|
|
|
// +build kvdb_etcd
|
|
|
|
|
2020-02-18 21:35:53 +03:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"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 TestBucketCreation(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 {
|
|
|
|
// empty bucket name
|
|
|
|
b, err := tx.CreateTopLevelBucket(nil)
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Error(t, walletdb.ErrBucketNameRequired, err)
|
|
|
|
require.Nil(t, b)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// empty bucket name
|
|
|
|
b, err = tx.CreateTopLevelBucket([]byte(""))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Error(t, walletdb.ErrBucketNameRequired, err)
|
|
|
|
require.Nil(t, b)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// "apple"
|
|
|
|
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, apple)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Check bucket tx.
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, tx, apple.Tx())
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// "apple" already created
|
|
|
|
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
|
|
|
|
|
|
|
// "apple/banana"
|
|
|
|
banana, err := apple.CreateBucket([]byte("banana"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, banana)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
banana, err = apple.CreateBucketIfNotExists([]byte("banana"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, banana)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Try creating "apple/banana" again
|
|
|
|
b, err = apple.CreateBucket([]byte("banana"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Error(t, walletdb.ErrBucketExists, err)
|
|
|
|
require.Nil(t, b)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// "apple/mango"
|
|
|
|
mango, err := apple.CreateBucket([]byte("mango"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, mango)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// "apple/banana/pear"
|
|
|
|
pear, err := banana.CreateBucket([]byte("pear"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, pear)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// empty bucket
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, apple.NestedReadWriteBucket(nil))
|
|
|
|
require.Nil(t, apple.NestedReadWriteBucket([]byte("")))
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// "apple/pear" doesn't exist
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, apple.NestedReadWriteBucket([]byte("pear")))
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// "apple/banana" exits
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NotNil(t, apple.NestedReadWriteBucket([]byte("banana")))
|
|
|
|
require.NotNil(t, apple.NestedReadBucket([]byte("banana")))
|
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.Nil(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
expected := map[string]string{
|
|
|
|
bkey("apple"): bval("apple"),
|
|
|
|
bkey("apple", "banana"): bval("apple", "banana"),
|
|
|
|
bkey("apple", "mango"): bval("apple", "mango"),
|
|
|
|
bkey("apple", "banana", "pear"): bval("apple", "banana", "pear"),
|
|
|
|
}
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, expected, f.Dump())
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBucketDeletion(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 {
|
|
|
|
// "apple"
|
|
|
|
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, apple)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// "apple/banana"
|
|
|
|
banana, err := apple.CreateBucket([]byte("banana"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, banana)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
kvs := []KV{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}}
|
|
|
|
|
|
|
|
for _, kv := range kvs {
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, banana.Put([]byte(kv.key), []byte(kv.val)))
|
|
|
|
require.Equal(t, []byte(kv.val), banana.Get([]byte(kv.key)))
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete a k/v from "apple/banana"
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, banana.Delete([]byte("key2")))
|
2020-02-18 21:35:53 +03:00
|
|
|
// Try getting/putting/deleting invalid k/v's.
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, banana.Get(nil))
|
|
|
|
require.Error(t, walletdb.ErrKeyRequired, banana.Put(nil, []byte("val")))
|
|
|
|
require.Error(t, walletdb.ErrKeyRequired, banana.Delete(nil))
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Try deleting a k/v that doesn't exist.
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, banana.Delete([]byte("nokey")))
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// "apple/pear"
|
|
|
|
pear, err := apple.CreateBucket([]byte("pear"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, pear)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Put some values into "apple/pear"
|
|
|
|
for _, kv := range kvs {
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, pear.Put([]byte(kv.key), []byte(kv.val)))
|
|
|
|
require.Equal(t, []byte(kv.val), pear.Get([]byte(kv.key)))
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create nested bucket "apple/pear/cherry"
|
|
|
|
cherry, err := pear.CreateBucket([]byte("cherry"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, cherry)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Put some values into "apple/pear/cherry"
|
|
|
|
for _, kv := range kvs {
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, cherry.Put([]byte(kv.key), []byte(kv.val)))
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read back values in "apple/pear/cherry" trough a read bucket.
|
|
|
|
cherryReadBucket := pear.NestedReadBucket([]byte("cherry"))
|
|
|
|
for _, kv := range kvs {
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(
|
2020-02-18 21:35:53 +03:00
|
|
|
t, []byte(kv.val),
|
|
|
|
cherryReadBucket.Get([]byte(kv.key)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try deleting some invalid buckets.
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Error(t,
|
2020-02-18 21:35:53 +03:00
|
|
|
walletdb.ErrBucketNameRequired, apple.DeleteNestedBucket(nil),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Try deleting a non existing bucket.
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Error(
|
2020-02-18 21:35:53 +03:00
|
|
|
t,
|
|
|
|
walletdb.ErrBucketNotFound,
|
|
|
|
apple.DeleteNestedBucket([]byte("missing")),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Delete "apple/pear"
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, apple.DeleteNestedBucket([]byte("pear")))
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// "apple/pear" deleted
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, apple.NestedReadWriteBucket([]byte("pear")))
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// "apple/pear/cherry" deleted
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, pear.NestedReadWriteBucket([]byte("cherry")))
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Values deleted too.
|
|
|
|
for _, kv := range kvs {
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, pear.Get([]byte(kv.key)))
|
|
|
|
require.Nil(t, cherry.Get([]byte(kv.key)))
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// "aple/banana" exists
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NotNil(t, apple.NestedReadWriteBucket([]byte("banana")))
|
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.Nil(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
expected := map[string]string{
|
|
|
|
bkey("apple"): bval("apple"),
|
|
|
|
bkey("apple", "banana"): bval("apple", "banana"),
|
|
|
|
vkey("key1", "apple", "banana"): "val1",
|
|
|
|
vkey("key3", "apple", "banana"): "val3",
|
|
|
|
}
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, expected, f.Dump())
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBucketForEach(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 {
|
|
|
|
// "apple"
|
|
|
|
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, apple)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// "apple/banana"
|
|
|
|
banana, err := apple.CreateBucket([]byte("banana"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, banana)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
kvs := []KV{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}}
|
|
|
|
|
|
|
|
// put some values into "apple" and "apple/banana" too
|
|
|
|
for _, kv := range kvs {
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, apple.Put([]byte(kv.key), []byte(kv.val)))
|
|
|
|
require.Equal(t, []byte(kv.val), apple.Get([]byte(kv.key)))
|
2020-02-18 21:35:53 +03:00
|
|
|
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, banana.Put([]byte(kv.key), []byte(kv.val)))
|
|
|
|
require.Equal(t, []byte(kv.val), banana.Get([]byte(kv.key)))
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
got := make(map[string]string)
|
|
|
|
err = apple.ForEach(func(key, val []byte) error {
|
|
|
|
got[string(key)] = string(val)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := map[string]string{
|
|
|
|
"key1": "val1",
|
|
|
|
"key2": "val2",
|
|
|
|
"key3": "val3",
|
|
|
|
"banana": "",
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, got)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
got = make(map[string]string)
|
|
|
|
err = banana.ForEach(func(key, val []byte) error {
|
|
|
|
got[string(key)] = string(val)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
// remove the sub-bucket key
|
|
|
|
delete(expected, "banana")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, expected, got)
|
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.Nil(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
expected := map[string]string{
|
|
|
|
bkey("apple"): bval("apple"),
|
|
|
|
bkey("apple", "banana"): bval("apple", "banana"),
|
|
|
|
vkey("key1", "apple"): "val1",
|
|
|
|
vkey("key2", "apple"): "val2",
|
|
|
|
vkey("key3", "apple"): "val3",
|
|
|
|
vkey("key1", "apple", "banana"): "val1",
|
|
|
|
vkey("key2", "apple", "banana"): "val2",
|
|
|
|
vkey("key3", "apple", "banana"): "val3",
|
|
|
|
}
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, expected, f.Dump())
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBucketForEachWithError(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 {
|
|
|
|
// "apple"
|
|
|
|
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, apple)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// "apple/banana"
|
|
|
|
banana, err := apple.CreateBucket([]byte("banana"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, banana)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// "apple/pear"
|
|
|
|
pear, err := apple.CreateBucket([]byte("pear"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, pear)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
kvs := []KV{{"key1", "val1"}, {"key2", "val2"}}
|
|
|
|
|
|
|
|
// Put some values into "apple" and "apple/banana" too.
|
|
|
|
for _, kv := range kvs {
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, apple.Put([]byte(kv.key), []byte(kv.val)))
|
|
|
|
require.Equal(t, []byte(kv.val), apple.Get([]byte(kv.key)))
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
got := make(map[string]string)
|
|
|
|
i := 0
|
|
|
|
// Error while iterating value keys.
|
|
|
|
err = apple.ForEach(func(key, val []byte) error {
|
2020-06-26 21:08:08 +03:00
|
|
|
if i == 2 {
|
2020-02-18 21:35:53 +03:00
|
|
|
return fmt.Errorf("error")
|
|
|
|
}
|
|
|
|
|
|
|
|
got[string(key)] = string(val)
|
|
|
|
i++
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := map[string]string{
|
2020-06-26 21:08:08 +03:00
|
|
|
"banana": "",
|
|
|
|
"key1": "val1",
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, expected, got)
|
|
|
|
require.Error(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
got = make(map[string]string)
|
|
|
|
i = 0
|
|
|
|
// Erro while iterating buckets.
|
|
|
|
err = apple.ForEach(func(key, val []byte) error {
|
|
|
|
if i == 3 {
|
|
|
|
return fmt.Errorf("error")
|
|
|
|
}
|
|
|
|
|
|
|
|
got[string(key)] = string(val)
|
|
|
|
i++
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
expected = map[string]string{
|
2020-06-26 21:08:08 +03:00
|
|
|
"banana": "",
|
2020-02-18 21:35:53 +03:00
|
|
|
"key1": "val1",
|
|
|
|
"key2": "val2",
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, expected, got)
|
|
|
|
require.Error(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
|
|
|
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
expected := map[string]string{
|
|
|
|
bkey("apple"): bval("apple"),
|
|
|
|
bkey("apple", "banana"): bval("apple", "banana"),
|
|
|
|
bkey("apple", "pear"): bval("apple", "pear"),
|
|
|
|
vkey("key1", "apple"): "val1",
|
|
|
|
vkey("key2", "apple"): "val2",
|
|
|
|
}
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, expected, f.Dump())
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBucketSequence(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 {
|
|
|
|
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, apple)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
banana, err := apple.CreateBucket([]byte("banana"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, banana)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, uint64(0), apple.Sequence())
|
|
|
|
require.Equal(t, uint64(0), banana.Sequence())
|
2020-02-18 21:35:53 +03:00
|
|
|
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, apple.SetSequence(math.MaxUint64))
|
|
|
|
require.Equal(t, uint64(math.MaxUint64), apple.Sequence())
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
for i := uint64(0); i < uint64(5); i++ {
|
|
|
|
s, err := apple.NextSequence()
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, i, s)
|
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.Nil(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
2020-07-21 19:26:13 +03:00
|
|
|
|
|
|
|
// TestKeyClash tests that one cannot create a bucket if a value with the same
|
|
|
|
// key exists and the same is true in reverse: that a value cannot be put if
|
|
|
|
// a bucket with the same key exists.
|
|
|
|
func TestKeyClash(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f := NewEtcdTestFixture(t)
|
|
|
|
defer f.Cleanup()
|
|
|
|
|
|
|
|
db, err := newEtcdBackend(f.BackendConfig())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// First:
|
|
|
|
// put: /apple/key -> val
|
|
|
|
// create bucket: /apple/banana
|
|
|
|
err = db.Update(func(tx walletdb.ReadWriteTx) error {
|
|
|
|
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, apple)
|
|
|
|
|
|
|
|
require.NoError(t, apple.Put([]byte("key"), []byte("val")))
|
|
|
|
|
|
|
|
banana, err := apple.CreateBucket([]byte("banana"))
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, banana)
|
|
|
|
|
|
|
|
return nil
|
2020-10-26 16:06:32 +03:00
|
|
|
}, func() {})
|
2020-07-21 19:26:13 +03:00
|
|
|
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
// Next try to:
|
|
|
|
// put: /apple/banana -> val => will fail (as /apple/banana is a bucket)
|
|
|
|
// create bucket: /apple/key => will fail (as /apple/key is a value)
|
|
|
|
err = db.Update(func(tx walletdb.ReadWriteTx) error {
|
|
|
|
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, apple)
|
|
|
|
|
|
|
|
require.Error(t,
|
|
|
|
walletdb.ErrIncompatibleValue,
|
|
|
|
apple.Put([]byte("banana"), []byte("val")),
|
|
|
|
)
|
|
|
|
|
|
|
|
b, err := apple.CreateBucket([]byte("key"))
|
|
|
|
require.Nil(t, b)
|
|
|
|
require.Error(t, walletdb.ErrIncompatibleValue, b)
|
|
|
|
|
|
|
|
b, err = apple.CreateBucketIfNotExists([]byte("key"))
|
|
|
|
require.Nil(t, b)
|
|
|
|
require.Error(t, walletdb.ErrIncompatibleValue, b)
|
|
|
|
|
|
|
|
return nil
|
2020-10-26 16:06:32 +03:00
|
|
|
}, func() {})
|
2020-07-21 19:26:13 +03:00
|
|
|
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
// Except that the only existing items in the db are:
|
|
|
|
// bucket: /apple
|
|
|
|
// bucket: /apple/banana
|
|
|
|
// value: /apple/key -> val
|
|
|
|
expected := map[string]string{
|
|
|
|
bkey("apple"): bval("apple"),
|
|
|
|
bkey("apple", "banana"): bval("apple", "banana"),
|
|
|
|
vkey("key", "apple"): "val",
|
|
|
|
}
|
|
|
|
require.Equal(t, expected, f.Dump())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestBucketCreateDelete tests that creating then deleting then creating a
|
|
|
|
// bucket suceeds.
|
|
|
|
func TestBucketCreateDelete(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
f := NewEtcdTestFixture(t)
|
|
|
|
defer f.Cleanup()
|
|
|
|
|
|
|
|
db, err := newEtcdBackend(f.BackendConfig())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = db.Update(func(tx walletdb.ReadWriteTx) error {
|
|
|
|
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, apple)
|
|
|
|
|
|
|
|
banana, err := apple.CreateBucket([]byte("banana"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, banana)
|
|
|
|
|
|
|
|
return nil
|
2020-10-26 16:06:32 +03:00
|
|
|
}, func() {})
|
2020-07-21 19:26:13 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = db.Update(func(tx walletdb.ReadWriteTx) error {
|
|
|
|
apple := tx.ReadWriteBucket([]byte("apple"))
|
|
|
|
require.NotNil(t, apple)
|
|
|
|
require.NoError(t, apple.DeleteNestedBucket([]byte("banana")))
|
|
|
|
|
|
|
|
return nil
|
2020-10-26 16:06:32 +03:00
|
|
|
}, func() {})
|
2020-07-21 19:26:13 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = db.Update(func(tx walletdb.ReadWriteTx) error {
|
|
|
|
apple := tx.ReadWriteBucket([]byte("apple"))
|
|
|
|
require.NotNil(t, apple)
|
|
|
|
require.NoError(t, apple.Put([]byte("banana"), []byte("value")))
|
|
|
|
|
|
|
|
return nil
|
2020-10-26 16:06:32 +03:00
|
|
|
}, func() {})
|
2020-07-21 19:26:13 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := map[string]string{
|
|
|
|
vkey("banana", "apple"): "value",
|
|
|
|
bkey("apple"): bval("apple"),
|
|
|
|
}
|
|
|
|
require.Equal(t, expected, f.Dump())
|
|
|
|
}
|