2018-04-17 04:50:20 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-04-10 22:46:48 +01:00
|
|
|
package boltdb
|
|
|
|
|
|
|
|
import (
|
2018-04-21 00:54:18 +01:00
|
|
|
"bytes"
|
2018-04-10 22:46:48 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2018-04-21 00:54:18 +01:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2018-06-29 21:06:25 +01:00
|
|
|
"storj.io/storj/storage"
|
2018-04-10 22:46:48 +01:00
|
|
|
)
|
|
|
|
|
2018-06-29 21:06:25 +01:00
|
|
|
type BoltClientTest struct {
|
|
|
|
*testing.T
|
|
|
|
c storage.KeyValueStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBoltClientTest(t *testing.T) *BoltClientTest {
|
|
|
|
logger, _ := zap.NewDevelopment()
|
|
|
|
dbName := tempfile()
|
|
|
|
|
|
|
|
c, err := NewClient(logger, dbName, "test_bucket")
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Failed to create test db")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &BoltClientTest{
|
|
|
|
T: t,
|
|
|
|
c: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bt *BoltClientTest) Close() {
|
|
|
|
bt.c.Close()
|
|
|
|
switch client := bt.c.(type) {
|
2018-08-01 15:15:38 +01:00
|
|
|
case *Client:
|
2018-06-29 21:06:25 +01:00
|
|
|
os.Remove(client.Path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bt *BoltClientTest) HandleErr(err error, msg string) {
|
|
|
|
bt.Error(msg)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
panic(msg)
|
|
|
|
}
|
|
|
|
|
2018-04-10 22:46:48 +01:00
|
|
|
func tempfile() string {
|
2018-05-07 18:01:53 +01:00
|
|
|
f, err := ioutil.TempFile("", "TempBolt-")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-04-10 22:46:48 +01:00
|
|
|
f.Close()
|
2018-05-08 23:02:01 +01:00
|
|
|
err = os.Remove(f.Name())
|
2018-05-07 18:01:53 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-04-10 22:46:48 +01:00
|
|
|
return f.Name()
|
|
|
|
}
|
|
|
|
|
2018-06-29 21:06:25 +01:00
|
|
|
func TestPut(t *testing.T) {
|
|
|
|
bt := NewBoltClientTest(t)
|
|
|
|
defer bt.Close()
|
|
|
|
|
|
|
|
if err := bt.c.Put([]byte("test/path/1"), []byte("pointer1")); err != nil {
|
|
|
|
bt.HandleErr(err, "Failed to save pointer1 to pointers bucket")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
|
|
|
bt := NewBoltClientTest(t)
|
|
|
|
defer bt.Close()
|
|
|
|
|
|
|
|
if err := bt.c.Put([]byte("test/path/1"), []byte("pointer1")); err != nil {
|
|
|
|
bt.HandleErr(err, "Failed to save pointer1 to pointers bucket")
|
|
|
|
}
|
|
|
|
|
|
|
|
retrvValue, err := bt.c.Get([]byte("test/path/1"))
|
2018-04-10 22:46:48 +01:00
|
|
|
if err != nil {
|
2018-06-29 21:06:25 +01:00
|
|
|
bt.HandleErr(err, "Failed to get")
|
|
|
|
}
|
|
|
|
if retrvValue.IsZero() {
|
|
|
|
bt.HandleErr(nil, "Failed to get saved test pointer")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(retrvValue, []byte("pointer1")) {
|
|
|
|
bt.HandleErr(nil, "Retrieved pointer was not same as put pointer")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-06-29 21:06:25 +01:00
|
|
|
// tests Get non-existent path
|
|
|
|
getRes, err := bt.c.Get([]byte("fake/path"))
|
|
|
|
if err != nil {
|
|
|
|
bt.HandleErr(err, "Failed to get")
|
|
|
|
}
|
|
|
|
if !getRes.IsZero() {
|
|
|
|
bt.HandleErr(nil, "Expected zero-value response for getting fake path")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
2018-06-29 21:06:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDelete(t *testing.T) {
|
|
|
|
bt := NewBoltClientTest(t)
|
|
|
|
defer bt.Close()
|
2018-04-10 22:46:48 +01:00
|
|
|
|
2018-06-29 21:06:25 +01:00
|
|
|
if err := bt.c.Put([]byte("test/path/1"), []byte("pointer1")); err != nil {
|
|
|
|
bt.HandleErr(err, "Failed to save pointer1 to pointers bucket")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-06-29 21:06:25 +01:00
|
|
|
if err := bt.c.Delete([]byte("test/path/1")); err != nil {
|
|
|
|
bt.HandleErr(err, "Failed to delete test entry")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
2018-06-29 21:06:25 +01:00
|
|
|
}
|
2018-04-10 22:46:48 +01:00
|
|
|
|
2018-06-29 21:06:25 +01:00
|
|
|
func TestList(t *testing.T) {
|
|
|
|
bt := NewBoltClientTest(t)
|
|
|
|
defer bt.Close()
|
|
|
|
|
|
|
|
if err := bt.c.Put([]byte("test/path/2"), []byte("pointer2")); err != nil {
|
|
|
|
bt.HandleErr(err, "Failed to put pointer2 to pointers bucket")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
2018-06-29 21:06:25 +01:00
|
|
|
testPaths, err := bt.c.List([]byte("test/path/2"), storage.Limit(1))
|
|
|
|
if err != nil {
|
|
|
|
bt.HandleErr(err, "Failed to list Path keys in pointers bucket")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-06-29 21:06:25 +01:00
|
|
|
if !bytes.Equal(testPaths[0], []byte("test/path/2")) {
|
|
|
|
bt.HandleErr(nil, "Expected only test/path/2 in list")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
2018-06-29 21:06:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListNoStartingKey(t *testing.T) {
|
|
|
|
bt := NewBoltClientTest(t)
|
|
|
|
defer bt.Close()
|
2018-04-10 22:46:48 +01:00
|
|
|
|
2018-06-29 21:06:25 +01:00
|
|
|
if err := bt.c.Put([]byte("test/path/1"), []byte("pointer1")); err != nil {
|
|
|
|
bt.HandleErr(err, "Failed to save pointer1 to pointers bucket")
|
|
|
|
}
|
|
|
|
if err := bt.c.Put([]byte("test/path/2"), []byte("pointer2")); err != nil {
|
|
|
|
bt.HandleErr(err, "Failed to save pointer2 to pointers bucket")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
2018-06-29 21:06:25 +01:00
|
|
|
if err := bt.c.Put([]byte("test/path/3"), []byte("pointer3")); err != nil {
|
|
|
|
bt.HandleErr(err, "Failed to save pointer3 to pointers bucket")
|
|
|
|
}
|
|
|
|
|
|
|
|
testPaths, err := bt.c.List(nil, storage.Limit(3))
|
2018-04-10 22:46:48 +01:00
|
|
|
if err != nil {
|
2018-06-29 21:06:25 +01:00
|
|
|
bt.HandleErr(err, "Failed to list Paths")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-06-29 21:06:25 +01:00
|
|
|
if !bytes.Equal(testPaths[2], []byte("test/path/3")) {
|
|
|
|
bt.HandleErr(nil, "Expected test/path/3 to be last in list")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
|
|
|
}
|