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"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-04-21 00:54:18 +01:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2018-04-10 22:46:48 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func tempfile() string {
|
|
|
|
f, _ := ioutil.TempFile("", "TempBolt-")
|
|
|
|
f.Close()
|
|
|
|
os.Remove(f.Name())
|
|
|
|
return f.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetState(t *testing.T) {
|
2018-04-21 00:54:18 +01:00
|
|
|
logger, _ := zap.NewDevelopment()
|
|
|
|
c, err := New(logger, tempfile())
|
2018-04-10 22:46:48 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Failed to create test db")
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
c.Close()
|
2018-04-17 04:50:20 +01:00
|
|
|
os.Remove(c.Path)
|
2018-04-10 22:46:48 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
testFile := File{
|
|
|
|
Path: `test/path`,
|
2018-04-21 00:54:18 +01:00
|
|
|
Value: []byte(`test value`),
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
testFile2 := File{
|
|
|
|
Path: `test/path2`,
|
2018-04-21 00:54:18 +01:00
|
|
|
Value: []byte(`value2`),
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// tests Put function
|
|
|
|
if err := c.Put(testFile); err != nil {
|
2018-04-17 04:50:20 +01:00
|
|
|
t.Error("Failed to save testFile to files bucket")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// tests Get function
|
|
|
|
retrvFile, err := c.Get([]byte("test/path"))
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Failed to get saved test value")
|
|
|
|
}
|
2018-04-21 00:54:18 +01:00
|
|
|
if !bytes.Equal(retrvFile.Value, testFile.Value) {
|
2018-04-10 22:46:48 +01:00
|
|
|
t.Error("Retrieved file was not same as original file")
|
|
|
|
}
|
|
|
|
|
|
|
|
// tests Delete function
|
|
|
|
if err := c.Delete([]byte("test/path")); err != nil {
|
2018-04-17 04:50:20 +01:00
|
|
|
t.Error("Failed to delete testfile")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// tests List function
|
|
|
|
if err := c.Put(testFile2); err != nil {
|
2018-04-17 04:50:20 +01:00
|
|
|
t.Error("Failed to save testFile2 to files bucket")
|
2018-04-10 22:46:48 +01:00
|
|
|
}
|
2018-04-17 04:50:20 +01:00
|
|
|
testFiles, err := c.List()
|
2018-04-10 22:46:48 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Failed to list file keys")
|
|
|
|
}
|
|
|
|
|
|
|
|
// tests List + Delete function
|
|
|
|
testString := strings.Join(testFiles, "")
|
|
|
|
if testString != "test/path2" {
|
|
|
|
t.Error("Expected only testFile2 in list")
|
|
|
|
}
|
|
|
|
}
|