2018-06-13 19:22:32 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-08-07 00:32:47 +01:00
|
|
|
"math/rand"
|
2018-10-16 16:34:05 +01:00
|
|
|
"net"
|
2018-06-13 19:22:32 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-08-07 00:32:47 +01:00
|
|
|
"strconv"
|
2018-06-13 19:22:32 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/zeebo/errs"
|
2018-09-10 10:52:53 +01:00
|
|
|
"go.uber.org/zap/zaptest"
|
2018-06-13 19:22:32 +01:00
|
|
|
|
2018-08-07 00:32:47 +01:00
|
|
|
"storj.io/storj/pkg/dht"
|
|
|
|
"storj.io/storj/pkg/kademlia"
|
2018-10-08 16:09:37 +01:00
|
|
|
"storj.io/storj/pkg/node"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-10-08 16:09:37 +01:00
|
|
|
"storj.io/storj/pkg/provider"
|
2018-06-13 19:22:32 +01:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
"storj.io/storj/storage/boltdb"
|
|
|
|
"storj.io/storj/storage/redis"
|
2018-09-07 08:33:33 +01:00
|
|
|
"storj.io/storj/storage/redis/redisserver"
|
2018-09-10 10:52:53 +01:00
|
|
|
"storj.io/storj/storage/storelogger"
|
2018-09-11 08:27:12 +01:00
|
|
|
"storj.io/storj/storage/teststore"
|
2018-06-13 19:22:32 +01:00
|
|
|
)
|
|
|
|
|
2018-06-19 19:03:46 +01:00
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
)
|
|
|
|
|
2018-06-13 19:22:32 +01:00
|
|
|
type dbClient int
|
2018-09-18 05:39:06 +01:00
|
|
|
type responses map[dbClient]*pb.Node
|
|
|
|
type responsesB map[dbClient][]*pb.Node
|
2018-06-19 19:03:46 +01:00
|
|
|
type errors map[dbClient]*errs.Class
|
2018-06-13 19:22:32 +01:00
|
|
|
|
|
|
|
const (
|
|
|
|
mock dbClient = iota
|
|
|
|
bolt
|
|
|
|
_redis
|
2018-08-07 00:32:47 +01:00
|
|
|
testNetSize = 30
|
2018-06-13 19:22:32 +01:00
|
|
|
)
|
|
|
|
|
2018-10-09 17:47:05 +01:00
|
|
|
// helper function to get kademlia base configs without root Config struct
|
|
|
|
func kadconfig() kademlia.KadConfig {
|
|
|
|
return kademlia.KadConfig{
|
|
|
|
Alpha: 5,
|
|
|
|
DefaultIDLength: 256,
|
|
|
|
DefaultBucketSize: 20,
|
|
|
|
DefaultReplacementCacheSize: 5,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
func newTestKademlia(t *testing.T, ip, port string, d dht.DHT, b pb.Node) *kademlia.Kademlia {
|
2018-10-09 17:47:05 +01:00
|
|
|
kc := kadconfig()
|
2018-10-12 22:10:50 +01:00
|
|
|
fid, err := node.NewFullIdentity(ctx, 12, 4)
|
2018-08-07 00:32:47 +01:00
|
|
|
assert.NoError(t, err)
|
2018-09-18 05:39:06 +01:00
|
|
|
n := []pb.Node{b}
|
2018-10-16 16:34:05 +01:00
|
|
|
kad, err := kademlia.NewKademlia(fid.ID, n, net.JoinHostPort(ip, port), fid, "db", kc)
|
2018-08-07 00:32:47 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
return kad
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
func bootstrapTestNetwork(t *testing.T, ip, port string) ([]dht.DHT, pb.Node) {
|
2018-10-09 17:47:05 +01:00
|
|
|
kc := kadconfig()
|
2018-10-12 22:10:50 +01:00
|
|
|
bid, err := node.NewFullIdentity(ctx, 12, 4)
|
2018-08-07 00:32:47 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
dhts := []dht.DHT{}
|
|
|
|
|
|
|
|
p, err := strconv.Atoi(port)
|
|
|
|
pm := strconv.Itoa(p)
|
|
|
|
assert.NoError(t, err)
|
2018-10-16 16:34:05 +01:00
|
|
|
intro, err := kademlia.GetIntroNode(net.JoinHostPort(ip, pm))
|
2018-10-08 16:09:37 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
ca, err := provider.NewCA(ctx, 12, 4)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
identity, err := ca.NewIdentity()
|
2018-08-07 00:32:47 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-10-16 16:34:05 +01:00
|
|
|
boot, err := kademlia.NewKademlia(bid.ID, []pb.Node{*intro}, net.JoinHostPort(ip, pm), identity, "db", kc)
|
2018-08-07 00:32:47 +01:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rt, err := boot.GetRoutingTable(context.Background())
|
2018-08-27 18:28:16 +01:00
|
|
|
assert.NoError(t, err)
|
2018-08-07 00:32:47 +01:00
|
|
|
bootNode := rt.Local()
|
|
|
|
|
|
|
|
err = boot.ListenAndServe()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
p++
|
|
|
|
|
|
|
|
err = boot.Bootstrap(context.Background())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
for i := 0; i < testNetSize; i++ {
|
|
|
|
gg := strconv.Itoa(p)
|
2018-10-09 17:47:05 +01:00
|
|
|
kc := kadconfig()
|
2018-08-07 00:32:47 +01:00
|
|
|
|
2018-10-12 22:10:50 +01:00
|
|
|
fid, err := node.NewFullIdentity(ctx, 12, 4)
|
2018-10-08 16:09:37 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-10-16 16:34:05 +01:00
|
|
|
dht, err := kademlia.NewKademlia(fid.ID, []pb.Node{bootNode}, net.JoinHostPort(ip, gg), fid, "db", kc)
|
2018-08-07 00:32:47 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
p++
|
|
|
|
dhts = append(dhts, dht)
|
|
|
|
err = dht.ListenAndServe()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = dht.Bootstrap(context.Background())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dhts, bootNode
|
|
|
|
}
|
|
|
|
|
2018-06-13 19:22:32 +01:00
|
|
|
var (
|
|
|
|
getCases = []struct {
|
|
|
|
testID string
|
|
|
|
expectedTimesCalled int
|
|
|
|
key string
|
|
|
|
expectedResponses responses
|
|
|
|
expectedErrors errors
|
2018-09-11 08:27:12 +01:00
|
|
|
data []storage.ListItem
|
2018-06-13 19:22:32 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
testID: "valid Get",
|
|
|
|
expectedTimesCalled: 1,
|
|
|
|
key: "foo",
|
|
|
|
expectedResponses: func() responses {
|
2018-10-15 13:04:21 +01:00
|
|
|
na := &pb.Node{Address: &pb.NodeAddress{Transport: pb.NodeTransport_TCP_TLS_GRPC, Address: "127.0.0.1:9999"}}
|
2018-06-13 19:22:32 +01:00
|
|
|
return responses{
|
|
|
|
mock: na,
|
|
|
|
bolt: na,
|
|
|
|
_redis: na,
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
expectedErrors: errors{
|
|
|
|
mock: nil,
|
|
|
|
bolt: nil,
|
|
|
|
_redis: nil,
|
|
|
|
},
|
2018-09-11 08:27:12 +01:00
|
|
|
data: []storage.ListItem{{
|
|
|
|
Key: storage.Key("foo"),
|
|
|
|
Value: func() storage.Value {
|
2018-10-15 13:04:21 +01:00
|
|
|
na := &pb.Node{Address: &pb.NodeAddress{Transport: pb.NodeTransport_TCP_TLS_GRPC, Address: "127.0.0.1:9999"}}
|
2018-09-11 08:27:12 +01:00
|
|
|
d, err := proto.Marshal(na)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}(),
|
|
|
|
}},
|
|
|
|
}, {
|
2018-06-13 19:22:32 +01:00
|
|
|
testID: "forced get error",
|
|
|
|
expectedTimesCalled: 1,
|
|
|
|
key: "error",
|
|
|
|
expectedResponses: func() responses {
|
2018-10-15 13:04:21 +01:00
|
|
|
na := &pb.Node{Address: &pb.NodeAddress{Transport: pb.NodeTransport_TCP_TLS_GRPC, Address: "127.0.0.1:9999"}}
|
2018-06-13 19:22:32 +01:00
|
|
|
return responses{
|
|
|
|
mock: nil,
|
|
|
|
bolt: na,
|
|
|
|
_redis: na,
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
expectedErrors: errors{
|
2018-06-29 21:06:25 +01:00
|
|
|
mock: nil,
|
2018-06-13 19:22:32 +01:00
|
|
|
bolt: nil,
|
|
|
|
_redis: nil,
|
|
|
|
},
|
2018-09-11 08:27:12 +01:00
|
|
|
data: []storage.ListItem{{
|
|
|
|
Key: storage.Key("error"),
|
|
|
|
Value: func() storage.Value {
|
2018-10-15 13:04:21 +01:00
|
|
|
na := &pb.Node{Address: &pb.NodeAddress{Transport: pb.NodeTransport_TCP_TLS_GRPC, Address: "127.0.0.1:9999"}}
|
2018-09-11 08:27:12 +01:00
|
|
|
d, err := proto.Marshal(na)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}(),
|
|
|
|
}},
|
2018-06-13 19:22:32 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testID: "get missing key",
|
|
|
|
expectedTimesCalled: 1,
|
|
|
|
key: "bar",
|
|
|
|
expectedResponses: responses{
|
|
|
|
mock: nil,
|
|
|
|
bolt: nil,
|
|
|
|
_redis: nil,
|
|
|
|
},
|
|
|
|
expectedErrors: errors{
|
2018-09-11 08:27:12 +01:00
|
|
|
mock: &storage.ErrKeyNotFound,
|
2018-08-23 08:05:56 +01:00
|
|
|
bolt: &storage.ErrKeyNotFound,
|
2018-08-14 16:22:29 +01:00
|
|
|
_redis: &storage.ErrKeyNotFound,
|
2018-06-13 19:22:32 +01:00
|
|
|
},
|
2018-09-11 08:27:12 +01:00
|
|
|
data: []storage.ListItem{{
|
|
|
|
Key: storage.Key("foo"),
|
|
|
|
Value: func() storage.Value {
|
2018-10-15 13:04:21 +01:00
|
|
|
na := &pb.Node{Address: &pb.NodeAddress{Transport: pb.NodeTransport_TCP_TLS_GRPC, Address: "127.0.0.1:9999"}}
|
2018-09-11 08:27:12 +01:00
|
|
|
d, err := proto.Marshal(na)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}(),
|
|
|
|
}},
|
2018-06-13 19:22:32 +01:00
|
|
|
},
|
|
|
|
}
|
2018-09-11 05:52:14 +01:00
|
|
|
getAllCases = []struct {
|
|
|
|
testID string
|
|
|
|
expectedTimesCalled int
|
|
|
|
keys []string
|
|
|
|
expectedResponses responsesB
|
|
|
|
expectedErrors errors
|
2018-09-11 08:27:12 +01:00
|
|
|
data []storage.ListItem
|
2018-09-11 05:52:14 +01:00
|
|
|
}{
|
|
|
|
{testID: "valid GetAll",
|
|
|
|
expectedTimesCalled: 1,
|
|
|
|
keys: []string{"key1"},
|
|
|
|
expectedResponses: func() responsesB {
|
2018-10-15 13:04:21 +01:00
|
|
|
n1 := &pb.Node{Address: &pb.NodeAddress{Transport: pb.NodeTransport_TCP_TLS_GRPC, Address: "127.0.0.1:9999"}}
|
2018-09-18 05:39:06 +01:00
|
|
|
ns := []*pb.Node{n1}
|
2018-09-11 05:52:14 +01:00
|
|
|
return responsesB{
|
|
|
|
mock: ns,
|
|
|
|
bolt: ns,
|
|
|
|
_redis: ns,
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
expectedErrors: errors{
|
|
|
|
mock: nil,
|
|
|
|
bolt: nil,
|
|
|
|
_redis: nil,
|
|
|
|
},
|
2018-09-11 08:27:12 +01:00
|
|
|
data: []storage.ListItem{
|
|
|
|
{
|
|
|
|
Key: storage.Key("key1"),
|
|
|
|
Value: func() storage.Value {
|
2018-10-15 13:04:21 +01:00
|
|
|
na := &pb.Node{Address: &pb.NodeAddress{Transport: pb.NodeTransport_TCP_TLS_GRPC, Address: "127.0.0.1:9999"}}
|
2018-09-11 08:27:12 +01:00
|
|
|
d, err := proto.Marshal(na)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}(),
|
|
|
|
},
|
2018-09-11 05:52:14 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{testID: "valid GetAll",
|
|
|
|
expectedTimesCalled: 1,
|
|
|
|
keys: []string{"key1", "key2"},
|
|
|
|
expectedResponses: func() responsesB {
|
2018-10-15 13:04:21 +01:00
|
|
|
n1 := &pb.Node{Address: &pb.NodeAddress{Transport: pb.NodeTransport_TCP_TLS_GRPC, Address: "127.0.0.1:9999"}}
|
|
|
|
n2 := &pb.Node{Address: &pb.NodeAddress{Transport: pb.NodeTransport_TCP_TLS_GRPC, Address: "127.0.0.1:9998"}}
|
2018-09-18 05:39:06 +01:00
|
|
|
ns := []*pb.Node{n1, n2}
|
2018-09-11 05:52:14 +01:00
|
|
|
return responsesB{
|
|
|
|
mock: ns,
|
|
|
|
bolt: ns,
|
|
|
|
_redis: ns,
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
expectedErrors: errors{
|
|
|
|
mock: nil,
|
|
|
|
bolt: nil,
|
|
|
|
_redis: nil,
|
|
|
|
},
|
2018-09-11 08:27:12 +01:00
|
|
|
data: []storage.ListItem{
|
|
|
|
{
|
|
|
|
Key: storage.Key("key1"),
|
|
|
|
Value: func() storage.Value {
|
2018-10-15 13:04:21 +01:00
|
|
|
na := &pb.Node{Address: &pb.NodeAddress{Transport: pb.NodeTransport_TCP_TLS_GRPC, Address: "127.0.0.1:9999"}}
|
2018-09-11 08:27:12 +01:00
|
|
|
d, err := proto.Marshal(na)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}(),
|
|
|
|
}, {
|
|
|
|
Key: storage.Key("key2"),
|
|
|
|
Value: func() storage.Value {
|
2018-10-15 13:04:21 +01:00
|
|
|
na := &pb.Node{Address: &pb.NodeAddress{Transport: pb.NodeTransport_TCP_TLS_GRPC, Address: "127.0.0.1:9998"}}
|
2018-09-11 08:27:12 +01:00
|
|
|
d, err := proto.Marshal(na)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}(),
|
|
|
|
},
|
2018-09-11 05:52:14 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{testID: "mix of valid and nil nodes returned",
|
|
|
|
expectedTimesCalled: 1,
|
|
|
|
keys: []string{"key1", "key3"},
|
|
|
|
expectedResponses: func() responsesB {
|
2018-10-15 13:04:21 +01:00
|
|
|
n1 := &pb.Node{Address: &pb.NodeAddress{Transport: pb.NodeTransport_TCP_TLS_GRPC, Address: "127.0.0.1:9999"}}
|
2018-09-18 05:39:06 +01:00
|
|
|
ns := []*pb.Node{n1, nil}
|
2018-09-11 05:52:14 +01:00
|
|
|
return responsesB{
|
|
|
|
mock: ns,
|
|
|
|
bolt: ns,
|
|
|
|
_redis: ns,
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
expectedErrors: errors{
|
|
|
|
mock: nil,
|
|
|
|
bolt: nil,
|
|
|
|
_redis: nil,
|
|
|
|
},
|
2018-09-11 08:27:12 +01:00
|
|
|
data: []storage.ListItem{
|
|
|
|
{
|
|
|
|
Key: storage.Key("key1"),
|
|
|
|
Value: func() storage.Value {
|
2018-10-15 13:04:21 +01:00
|
|
|
na := &pb.Node{Address: &pb.NodeAddress{Transport: pb.NodeTransport_TCP_TLS_GRPC, Address: "127.0.0.1:9999"}}
|
2018-09-11 08:27:12 +01:00
|
|
|
d, err := proto.Marshal(na)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}(),
|
|
|
|
},
|
2018-09-11 05:52:14 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{testID: "empty string keys",
|
|
|
|
expectedTimesCalled: 1,
|
|
|
|
keys: []string{"", ""},
|
|
|
|
expectedResponses: func() responsesB {
|
2018-09-18 05:39:06 +01:00
|
|
|
ns := []*pb.Node{nil, nil}
|
2018-09-11 05:52:14 +01:00
|
|
|
return responsesB{
|
|
|
|
mock: ns,
|
|
|
|
bolt: ns,
|
|
|
|
_redis: ns,
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
expectedErrors: errors{
|
|
|
|
mock: nil,
|
|
|
|
bolt: nil,
|
|
|
|
_redis: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{testID: "empty keys",
|
|
|
|
expectedTimesCalled: 0,
|
|
|
|
keys: []string{},
|
|
|
|
expectedResponses: func() responsesB {
|
|
|
|
return responsesB{
|
|
|
|
mock: nil,
|
|
|
|
bolt: nil,
|
|
|
|
_redis: nil,
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
expectedErrors: errors{
|
|
|
|
mock: &OverlayError,
|
|
|
|
bolt: &OverlayError,
|
|
|
|
_redis: &OverlayError,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-06-13 19:22:32 +01:00
|
|
|
putCases = []struct {
|
|
|
|
testID string
|
|
|
|
expectedTimesCalled int
|
|
|
|
key string
|
2018-09-18 05:39:06 +01:00
|
|
|
value pb.Node
|
2018-06-13 19:22:32 +01:00
|
|
|
expectedErrors errors
|
2018-09-11 08:27:12 +01:00
|
|
|
data []storage.ListItem
|
2018-06-13 19:22:32 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
testID: "valid Put",
|
|
|
|
expectedTimesCalled: 1,
|
|
|
|
key: "foo",
|
2018-10-15 13:04:21 +01:00
|
|
|
value: pb.Node{Id: "foo", Address: &pb.NodeAddress{Transport: pb.NodeTransport_TCP_TLS_GRPC, Address: "127.0.0.1:9999"}},
|
2018-06-13 19:22:32 +01:00
|
|
|
expectedErrors: errors{
|
|
|
|
mock: nil,
|
|
|
|
bolt: nil,
|
|
|
|
_redis: nil,
|
|
|
|
},
|
2018-09-11 08:27:12 +01:00
|
|
|
data: []storage.ListItem{},
|
2018-06-13 19:22:32 +01:00
|
|
|
},
|
|
|
|
}
|
2018-08-07 00:32:47 +01:00
|
|
|
|
|
|
|
refreshCases = []struct {
|
|
|
|
testID string
|
|
|
|
expectedTimesCalled int
|
|
|
|
expectedErr error
|
2018-09-11 08:27:12 +01:00
|
|
|
data []storage.ListItem
|
2018-08-07 00:32:47 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
testID: "valid update",
|
|
|
|
expectedTimesCalled: 1,
|
|
|
|
expectedErr: nil,
|
2018-09-11 08:27:12 +01:00
|
|
|
data: []storage.ListItem{},
|
2018-08-07 00:32:47 +01:00
|
|
|
},
|
|
|
|
}
|
2018-06-13 19:22:32 +01:00
|
|
|
)
|
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
func redisTestClient(t *testing.T, addr string, items []storage.ListItem) storage.KeyValueStore {
|
2018-09-07 08:33:33 +01:00
|
|
|
client, err := redis.NewClient(addr, "", 1)
|
2018-06-19 19:03:46 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-06-13 19:22:32 +01:00
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
if err := storage.PutAll(client, items...); err != nil {
|
|
|
|
t.Fatal(err)
|
2018-06-20 15:28:46 +01:00
|
|
|
}
|
2018-06-13 19:22:32 +01:00
|
|
|
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
func boltTestClient(t *testing.T, items []storage.ListItem) (_ storage.KeyValueStore, _ func()) {
|
2018-06-13 19:22:32 +01:00
|
|
|
boltPath, err := filepath.Abs("test_bolt.db")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-09-10 10:52:53 +01:00
|
|
|
client, err := boltdb.New(boltPath, "testBoltdb")
|
2018-06-13 19:22:32 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
cleanup := func() {
|
2018-08-23 08:05:56 +01:00
|
|
|
assert.NoError(t, client.Close())
|
|
|
|
assert.NoError(t, os.Remove(boltPath))
|
2018-06-13 19:22:32 +01:00
|
|
|
}
|
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
if err := storage.PutAll(client, items...); err != nil {
|
|
|
|
t.Fatal(err)
|
2018-06-20 15:28:46 +01:00
|
|
|
}
|
2018-06-13 19:22:32 +01:00
|
|
|
|
2018-09-10 10:52:53 +01:00
|
|
|
return storelogger.New(zaptest.NewLogger(t), client), cleanup
|
2018-06-13 19:22:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRedisGet(t *testing.T) {
|
2018-09-07 08:33:33 +01:00
|
|
|
redisAddr, cleanup, err := redisserver.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cleanup()
|
2018-06-13 19:22:32 +01:00
|
|
|
|
|
|
|
for _, c := range getCases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
2018-09-07 08:33:33 +01:00
|
|
|
db := redisTestClient(t, redisAddr, c.data)
|
2018-06-13 19:22:32 +01:00
|
|
|
oc := Cache{DB: db}
|
|
|
|
|
2018-06-19 19:03:46 +01:00
|
|
|
resp, err := oc.Get(ctx, c.key)
|
|
|
|
assertErrClass(t, c.expectedErrors[_redis], err)
|
2018-06-13 19:22:32 +01:00
|
|
|
assert.Equal(t, c.expectedResponses[_redis], resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 05:52:14 +01:00
|
|
|
func TestRedisGetAll(t *testing.T) {
|
|
|
|
redisAddr, cleanup, err := redisserver.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cleanup()
|
|
|
|
for _, c := range getAllCases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
|
|
|
db := redisTestClient(t, redisAddr, c.data)
|
|
|
|
oc := Cache{DB: db}
|
|
|
|
|
|
|
|
resp, err := oc.GetAll(ctx, c.keys)
|
|
|
|
assertErrClass(t, c.expectedErrors[_redis], err)
|
|
|
|
assert.Equal(t, c.expectedResponses[_redis], resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-19 19:03:46 +01:00
|
|
|
func assertErrClass(t *testing.T, class *errs.Class, err error) {
|
2018-09-11 08:27:12 +01:00
|
|
|
t.Helper()
|
2018-06-19 19:03:46 +01:00
|
|
|
if class != nil {
|
|
|
|
assert.True(t, class.Has(err))
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-13 19:22:32 +01:00
|
|
|
func TestRedisPut(t *testing.T) {
|
2018-09-07 08:33:33 +01:00
|
|
|
redisAddr, cleanup, err := redisserver.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cleanup()
|
2018-06-13 19:22:32 +01:00
|
|
|
|
|
|
|
for _, c := range putCases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
2018-09-07 08:33:33 +01:00
|
|
|
db := redisTestClient(t, redisAddr, c.data)
|
2018-06-13 19:22:32 +01:00
|
|
|
oc := Cache{DB: db}
|
|
|
|
|
|
|
|
err := oc.Put(c.key, c.value)
|
2018-06-19 19:03:46 +01:00
|
|
|
assertErrClass(t, c.expectedErrors[_redis], err)
|
2018-06-13 19:22:32 +01:00
|
|
|
|
|
|
|
v, err := db.Get([]byte(c.key))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
na := &pb.Node{}
|
2018-06-13 19:22:32 +01:00
|
|
|
assert.NoError(t, proto.Unmarshal(v, na))
|
2018-08-23 08:05:56 +01:00
|
|
|
assert.True(t, proto.Equal(na, &c.value))
|
2018-06-13 19:22:32 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBoltGet(t *testing.T) {
|
|
|
|
for _, c := range getCases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
|
|
|
db, cleanup := boltTestClient(t, c.data)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
oc := Cache{DB: db}
|
|
|
|
|
2018-06-19 19:03:46 +01:00
|
|
|
resp, err := oc.Get(ctx, c.key)
|
|
|
|
assertErrClass(t, c.expectedErrors[bolt], err)
|
2018-06-13 19:22:32 +01:00
|
|
|
assert.Equal(t, c.expectedResponses[bolt], resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 05:52:14 +01:00
|
|
|
func TestBoltGetAll(t *testing.T) {
|
|
|
|
for _, c := range getAllCases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
|
|
|
db, cleanup := boltTestClient(t, c.data)
|
|
|
|
defer cleanup()
|
|
|
|
oc := Cache{DB: db}
|
|
|
|
resp, err := oc.GetAll(ctx, c.keys)
|
|
|
|
assertErrClass(t, c.expectedErrors[bolt], err)
|
|
|
|
assert.Equal(t, c.expectedResponses[bolt], resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-06-13 19:22:32 +01:00
|
|
|
func TestBoltPut(t *testing.T) {
|
|
|
|
for _, c := range putCases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
|
|
|
db, cleanup := boltTestClient(t, c.data)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
oc := Cache{DB: db}
|
|
|
|
|
|
|
|
err := oc.Put(c.key, c.value)
|
2018-06-19 19:03:46 +01:00
|
|
|
assertErrClass(t, c.expectedErrors[_redis], err)
|
2018-06-13 19:22:32 +01:00
|
|
|
|
|
|
|
v, err := db.Get([]byte(c.key))
|
|
|
|
assert.NoError(t, err)
|
2018-09-18 05:39:06 +01:00
|
|
|
na := &pb.Node{}
|
2018-06-13 19:22:32 +01:00
|
|
|
|
|
|
|
assert.NoError(t, proto.Unmarshal(v, na))
|
2018-08-23 08:05:56 +01:00
|
|
|
assert.True(t, proto.Equal(na, &c.value))
|
2018-06-13 19:22:32 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMockGet(t *testing.T) {
|
|
|
|
for _, c := range getCases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
db := teststore.New()
|
|
|
|
if err := storage.PutAll(db, c.data...); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-06-13 19:22:32 +01:00
|
|
|
oc := Cache{DB: db}
|
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
if c.key == "error" {
|
|
|
|
db.ForceError = 1
|
|
|
|
}
|
|
|
|
assert.Equal(t, 0, db.CallCount.Get)
|
2018-06-13 19:22:32 +01:00
|
|
|
|
2018-06-19 19:03:46 +01:00
|
|
|
resp, err := oc.Get(ctx, c.key)
|
2018-09-11 08:27:12 +01:00
|
|
|
if c.key == "error" {
|
|
|
|
assert.Error(t, err)
|
|
|
|
} else {
|
|
|
|
assertErrClass(t, c.expectedErrors[mock], err)
|
|
|
|
}
|
2018-06-13 19:22:32 +01:00
|
|
|
assert.Equal(t, c.expectedResponses[mock], resp)
|
2018-09-11 08:27:12 +01:00
|
|
|
assert.Equal(t, c.expectedTimesCalled, db.CallCount.Get)
|
2018-06-13 19:22:32 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 05:52:14 +01:00
|
|
|
func TestMockGetAll(t *testing.T) {
|
|
|
|
for _, c := range getAllCases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
db := teststore.New()
|
|
|
|
if err := storage.PutAll(db, c.data...); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-09-11 05:52:14 +01:00
|
|
|
oc := Cache{DB: db}
|
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
assert.Equal(t, 0, db.CallCount.GetAll)
|
2018-09-11 05:52:14 +01:00
|
|
|
|
|
|
|
resp, err := oc.GetAll(ctx, c.keys)
|
|
|
|
assertErrClass(t, c.expectedErrors[mock], err)
|
|
|
|
assert.Equal(t, c.expectedResponses[mock], resp)
|
2018-09-11 08:27:12 +01:00
|
|
|
assert.Equal(t, c.expectedTimesCalled, db.CallCount.GetAll)
|
2018-09-11 05:52:14 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-13 19:22:32 +01:00
|
|
|
func TestMockPut(t *testing.T) {
|
|
|
|
for _, c := range putCases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
2018-09-11 08:27:12 +01:00
|
|
|
db := teststore.New()
|
|
|
|
if err := storage.PutAll(db, c.data...); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
db.CallCount.Put = 0
|
2018-06-13 19:22:32 +01:00
|
|
|
|
|
|
|
oc := Cache{DB: db}
|
|
|
|
|
|
|
|
err := oc.Put(c.key, c.value)
|
2018-06-19 19:03:46 +01:00
|
|
|
assertErrClass(t, c.expectedErrors[mock], err)
|
2018-09-11 08:27:12 +01:00
|
|
|
assert.Equal(t, c.expectedTimesCalled, db.CallCount.Put)
|
2018-06-13 19:22:32 +01:00
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
v, err := db.Get(storage.Key(c.key))
|
|
|
|
assert.NoError(t, err)
|
2018-06-13 19:22:32 +01:00
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
na := &pb.Node{}
|
2018-06-13 19:22:32 +01:00
|
|
|
assert.NoError(t, proto.Unmarshal(v, na))
|
2018-08-23 08:05:56 +01:00
|
|
|
assert.True(t, proto.Equal(na, &c.value))
|
2018-06-13 19:22:32 +01:00
|
|
|
})
|
2018-08-07 00:32:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRefresh(t *testing.T) {
|
2018-08-09 20:20:39 +01:00
|
|
|
t.Skip()
|
2018-08-07 00:32:47 +01:00
|
|
|
for _, c := range refreshCases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
2018-09-11 08:27:12 +01:00
|
|
|
dhts, b := bootstrapTestNetwork(t, "127.0.0.1", "0")
|
2018-08-07 00:32:47 +01:00
|
|
|
ctx := context.Background()
|
2018-09-11 08:27:12 +01:00
|
|
|
|
|
|
|
db := teststore.New()
|
|
|
|
if err := storage.PutAll(db, c.data...); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dht := newTestKademlia(t, "127.0.0.1", "0", dhts[rand.Intn(testNetSize)], b)
|
2018-08-07 00:32:47 +01:00
|
|
|
|
|
|
|
_cache := &Cache{
|
|
|
|
DB: db,
|
|
|
|
DHT: dht,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := _cache.Refresh(ctx)
|
|
|
|
assert.Equal(t, err, c.expectedErr)
|
|
|
|
})
|
2018-06-13 19:22:32 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-01 13:57:37 +01:00
|
|
|
|
|
|
|
func TestNewRedisOverlayCache(t *testing.T) {
|
2018-09-07 08:33:33 +01:00
|
|
|
redisAddr, cleanup, err := redisserver.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cleanup()
|
|
|
|
|
2018-08-01 13:57:37 +01:00
|
|
|
cases := []struct {
|
|
|
|
testName, address string
|
|
|
|
testFunc func(string)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
testName: "NewRedisOverlayCache valid",
|
2018-09-07 08:33:33 +01:00
|
|
|
address: redisAddr,
|
2018-08-01 13:57:37 +01:00
|
|
|
testFunc: func(address string) {
|
|
|
|
cache, err := NewRedisOverlayCache(address, "", 1, nil)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, cache)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testName: "NewRedisOverlayCache fail",
|
|
|
|
address: "",
|
|
|
|
testFunc: func(address string) {
|
|
|
|
cache, err := NewRedisOverlayCache(address, "", 1, nil)
|
|
|
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, cache)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testName, func(t *testing.T) {
|
|
|
|
c.testFunc(c.address)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|