2018-04-23 16:54:22 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-07-09 18:43:13 +01:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-06-20 15:28:46 +01:00
|
|
|
"log"
|
2018-07-09 18:43:13 +01:00
|
|
|
"net"
|
2018-06-20 15:28:46 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-04-23 16:54:22 +01:00
|
|
|
"testing"
|
2018-06-13 19:22:32 +01:00
|
|
|
"time"
|
2018-04-23 16:54:22 +01:00
|
|
|
|
2018-07-19 01:09:38 +01:00
|
|
|
"github.com/spf13/viper"
|
2018-04-23 16:54:22 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-06-20 15:28:46 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
2018-07-09 18:43:13 +01:00
|
|
|
"google.golang.org/grpc"
|
2018-06-20 15:28:46 +01:00
|
|
|
"gopkg.in/spacemonkeygo/monkit.v2"
|
2018-04-23 16:54:22 +01:00
|
|
|
|
2018-06-13 19:22:32 +01:00
|
|
|
"storj.io/storj/internal/test"
|
2018-07-09 18:43:13 +01:00
|
|
|
"storj.io/storj/pkg/peertls"
|
2018-06-20 15:28:46 +01:00
|
|
|
"storj.io/storj/pkg/process"
|
2018-07-09 18:43:13 +01:00
|
|
|
proto "storj.io/storj/protos/overlay" // naming proto to avoid confusion with this package
|
2018-04-23 16:54:22 +01:00
|
|
|
)
|
|
|
|
|
2018-06-20 15:28:46 +01:00
|
|
|
func newTestService(t *testing.T) Service {
|
|
|
|
return Service{
|
|
|
|
logger: zap.NewNop(),
|
|
|
|
metrics: monkit.Default,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 18:43:13 +01:00
|
|
|
func TestNewServer(t *testing.T) {
|
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 0))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
srv := newMockServer()
|
|
|
|
assert.NotNil(t, srv)
|
|
|
|
|
|
|
|
go srv.Serve(lis)
|
|
|
|
srv.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewClient_CreateTLS(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
tmpPath, err := ioutil.TempDir("", "TestNewClient")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpPath)
|
|
|
|
|
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 0))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
basePath := filepath.Join(tmpPath, "TestNewClient_CreateTLS")
|
|
|
|
srv, tlsOpts := newMockTLSServer(t, basePath, true)
|
|
|
|
go srv.Serve(lis)
|
|
|
|
defer srv.Stop()
|
|
|
|
|
|
|
|
address := lis.Addr().String()
|
|
|
|
c, err := NewClient(&address, tlsOpts.DialOption())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
r, err := c.Lookup(context.Background(), &proto.LookupRequest{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewClient_LoadTLS(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
tmpPath, err := ioutil.TempDir("", "TestNewClient")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpPath)
|
|
|
|
|
|
|
|
basePath := filepath.Join(tmpPath, "TestNewClient_LoadTLS")
|
|
|
|
_, err = peertls.NewTLSFileOptions(
|
|
|
|
basePath,
|
|
|
|
basePath,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 0))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// NB: do NOT create a cert, it should be loaded from disk
|
|
|
|
srv, tlsOpts := newMockTLSServer(t, basePath, false)
|
|
|
|
|
|
|
|
go srv.Serve(lis)
|
|
|
|
defer srv.Stop()
|
|
|
|
|
|
|
|
address := lis.Addr().String()
|
|
|
|
c, err := NewClient(&address, tlsOpts.DialOption())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
r, err := c.Lookup(context.Background(), &proto.LookupRequest{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewClient_IndependentTLS(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
tmpPath, err := ioutil.TempDir("", "TestNewClient_IndependentTLS")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpPath)
|
|
|
|
|
|
|
|
clientBasePath := filepath.Join(tmpPath, "client")
|
|
|
|
serverBasePath := filepath.Join(tmpPath, "server")
|
|
|
|
|
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 0))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
srv, _ := newMockTLSServer(t, serverBasePath, true)
|
|
|
|
|
|
|
|
go srv.Serve(lis)
|
|
|
|
defer srv.Stop()
|
|
|
|
|
|
|
|
clientTLSOps, err := peertls.NewTLSFileOptions(
|
|
|
|
clientBasePath,
|
|
|
|
clientBasePath,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
address := lis.Addr().String()
|
|
|
|
c, err := NewClient(&address, clientTLSOps.DialOption())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
r, err := c.Lookup(context.Background(), &proto.LookupRequest{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, r)
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:28:46 +01:00
|
|
|
func TestProcess_redis(t *testing.T) {
|
2018-07-09 18:43:13 +01:00
|
|
|
tempPath, err := ioutil.TempDir("", "TestProcess_redis")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tempPath)
|
|
|
|
|
2018-07-19 01:09:38 +01:00
|
|
|
viper.Set("localPort", "0")
|
|
|
|
viper.Set("redisaddress", "127.0.0.1:6379")
|
|
|
|
defer viper.Set("redisaddress", "")
|
|
|
|
|
2018-06-13 19:22:32 +01:00
|
|
|
done := test.EnsureRedis(t)
|
|
|
|
defer done()
|
|
|
|
|
2018-06-20 15:28:46 +01:00
|
|
|
o := newTestService(t)
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
2018-07-19 01:09:38 +01:00
|
|
|
|
2018-07-09 18:43:13 +01:00
|
|
|
err = o.Process(ctx, nil, nil)
|
2018-06-13 19:22:32 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2018-06-20 15:28:46 +01:00
|
|
|
|
|
|
|
func TestProcess_bolt(t *testing.T) {
|
2018-07-09 18:43:13 +01:00
|
|
|
tempPath, err := ioutil.TempDir("", "TestProcess_bolt")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tempPath)
|
|
|
|
|
2018-06-20 15:28:46 +01:00
|
|
|
boltdbPath, err := filepath.Abs("test_bolt.db")
|
2018-07-19 01:09:38 +01:00
|
|
|
viper.Set("localport", "0")
|
|
|
|
viper.Set("boltdbpath", boltdbPath)
|
|
|
|
defer viper.Set("boltdbpath", "")
|
|
|
|
defer viper.Set("redisaddress", "")
|
|
|
|
|
2018-06-20 15:28:46 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
defer func() {
|
|
|
|
if err := os.Remove(boltdbPath); err != nil {
|
2018-07-09 18:43:13 +01:00
|
|
|
log.Printf("%s\n", errs.New("error while removing test bolt db: %s", err))
|
2018-06-20 15:28:46 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
o := newTestService(t)
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
2018-07-19 01:09:38 +01:00
|
|
|
process.ConfigEnvironment()
|
2018-06-27 09:02:49 +01:00
|
|
|
err = o.Process(ctx, nil, nil)
|
2018-06-20 15:28:46 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2018-07-19 01:09:38 +01:00
|
|
|
func TestProcess_default(t *testing.T) {
|
2018-07-09 18:43:13 +01:00
|
|
|
tempPath, err := ioutil.TempDir("", "TestProcess_error")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tempPath)
|
|
|
|
|
2018-07-19 01:09:38 +01:00
|
|
|
viper.Set("localPort", "0")
|
|
|
|
viper.Set("boltdbpath", defaultBoltDBPath())
|
|
|
|
defer viper.Set("boltdbpath", "")
|
|
|
|
defer viper.Set("redisaddress", "")
|
2018-06-20 15:28:46 +01:00
|
|
|
|
|
|
|
o := newTestService(t)
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
2018-07-09 18:43:13 +01:00
|
|
|
err = o.Process(ctx, nil, nil)
|
2018-07-19 01:09:38 +01:00
|
|
|
assert.Nil(t, err)
|
2018-06-20 15:28:46 +01:00
|
|
|
}
|
2018-07-09 18:43:13 +01:00
|
|
|
|
|
|
|
func newMockServer(opts ...grpc.ServerOption) *grpc.Server {
|
|
|
|
grpcServer := grpc.NewServer(opts...)
|
|
|
|
proto.RegisterOverlayServer(grpcServer, &MockOverlay{})
|
|
|
|
|
|
|
|
return grpcServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockTLSServer(t *testing.T, tlsBasePath string, create bool) (*grpc.Server, *peertls.TLSFileOptions) {
|
|
|
|
tlsOpts, err := peertls.NewTLSFileOptions(
|
|
|
|
tlsBasePath,
|
|
|
|
tlsBasePath,
|
|
|
|
create,
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, tlsOpts)
|
|
|
|
|
|
|
|
grpcServer := newMockServer(tlsOpts.ServerOption())
|
|
|
|
return grpcServer, tlsOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
type MockOverlay struct{}
|
|
|
|
|
|
|
|
func (o *MockOverlay) FindStorageNodes(ctx context.Context, req *proto.FindStorageNodesRequest) (*proto.FindStorageNodesResponse, error) {
|
|
|
|
return &proto.FindStorageNodesResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *MockOverlay) Lookup(ctx context.Context, req *proto.LookupRequest) (*proto.LookupResponse, error) {
|
|
|
|
return &proto.LookupResponse{}, nil
|
|
|
|
}
|