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-13 19:22:32 +01:00
|
|
|
"storj.io/storj/pkg/netstate"
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetState(t *testing.T) {
|
2018-04-21 00:54:18 +01:00
|
|
|
logger, _ := zap.NewDevelopment()
|
2018-06-13 19:22:32 +01:00
|
|
|
c, err := NewClient(logger, tempfile(), "test_bucket")
|
2018-04-10 22:46:48 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Failed to create test db")
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
c.Close()
|
2018-06-13 19:22:32 +01:00
|
|
|
switch client := c.(type) {
|
|
|
|
case *boltClient:
|
|
|
|
os.Remove(client.Path)
|
|
|
|
}
|
2018-04-10 22:46:48 +01:00
|
|
|
}()
|
|
|
|
|
2018-06-13 19:22:32 +01:00
|
|
|
testEntry1 := netstate.PointerEntry{
|
2018-05-30 03:47:40 +01:00
|
|
|
Path: []byte(`test/path`),
|
|
|
|
Pointer: []byte(`pointer1`),
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-06-13 19:22:32 +01:00
|
|
|
testEntry2 := netstate.PointerEntry{
|
2018-05-30 03:47:40 +01:00
|
|
|
Path: []byte(`test/path2`),
|
|
|
|
Pointer: []byte(`pointer2`),
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// tests Put function
|
2018-06-13 19:22:32 +01:00
|
|
|
if err := c.Put(testEntry1.Path, testEntry1.Pointer); err != nil {
|
2018-05-30 03:47:40 +01:00
|
|
|
t.Error("Failed to save testFile to pointers bucket")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// tests Get function
|
2018-05-15 01:31:26 +01:00
|
|
|
retrvValue, err := c.Get([]byte("test/path"))
|
2018-04-10 22:46:48 +01:00
|
|
|
if err != nil {
|
2018-05-30 03:47:40 +01:00
|
|
|
t.Error("Failed to get saved test pointer")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
2018-05-30 03:47:40 +01:00
|
|
|
if !bytes.Equal(retrvValue, testEntry1.Pointer) {
|
|
|
|
t.Error("Retrieved pointer was not same as put pointer")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// tests Delete function
|
|
|
|
if err := c.Delete([]byte("test/path")); err != nil {
|
2018-05-30 03:47:40 +01:00
|
|
|
t.Error("Failed to delete test entry")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// tests List function
|
2018-06-13 19:22:32 +01:00
|
|
|
if err := c.Put(testEntry2.Path, testEntry2.Pointer); err != nil {
|
2018-05-30 03:47:40 +01:00
|
|
|
t.Error("Failed to put testEntry2 to pointers bucket")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
2018-05-30 03:47:40 +01:00
|
|
|
testPaths, err := c.List()
|
2018-04-10 22:46:48 +01:00
|
|
|
if err != nil {
|
2018-05-30 03:47:40 +01:00
|
|
|
t.Error("Failed to list Path keys in pointers bucket")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// tests List + Delete function
|
2018-05-30 03:47:40 +01:00
|
|
|
if !bytes.Equal(testPaths[0], []byte("test/path2")) {
|
|
|
|
t.Error("Expected only testEntry2 path in list")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
|
|
|
}
|