2018-04-23 16:54:22 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-06-20 15:28:46 +01:00
|
|
|
"flag"
|
2018-04-23 16:54:22 +01:00
|
|
|
"fmt"
|
2018-06-20 15:28:46 +01:00
|
|
|
"log"
|
2018-04-23 16:54:22 +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
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-06-20 15:28:46 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
2018-04-23 16:54:22 +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-06-20 15:28:46 +01:00
|
|
|
"storj.io/storj/pkg/process"
|
2018-05-01 18:24:32 +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-04-23 16:54:22 +01:00
|
|
|
func TestNewServer(t *testing.T) {
|
2018-06-05 22:06:37 +01:00
|
|
|
t.SkipNow()
|
2018-04-23 16:54:22 +01:00
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 0))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-06-05 22:06:37 +01:00
|
|
|
srv := NewServer(nil, nil, nil, nil)
|
2018-04-23 16:54:22 +01:00
|
|
|
assert.NotNil(t, srv)
|
|
|
|
|
|
|
|
go srv.Serve(lis)
|
|
|
|
srv.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewClient(t *testing.T) {
|
2018-06-20 15:28:46 +01:00
|
|
|
// a := "35.232.202.229:8080"
|
|
|
|
// c, err := NewClient(&a, grpc.WithInsecure())
|
2018-06-05 22:06:37 +01:00
|
|
|
t.SkipNow()
|
2018-04-23 16:54:22 +01:00
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 0))
|
|
|
|
assert.NoError(t, err)
|
2018-06-05 22:06:37 +01:00
|
|
|
srv := NewServer(nil, nil, nil, nil)
|
2018-04-23 16:54:22 +01:00
|
|
|
go srv.Serve(lis)
|
|
|
|
defer srv.Stop()
|
|
|
|
|
|
|
|
address := lis.Addr().String()
|
|
|
|
c, err := NewClient(&address, grpc.WithInsecure())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
r, err := c.Lookup(context.Background(), &proto.LookupRequest{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, r)
|
|
|
|
}
|
2018-06-13 19:22:32 +01:00
|
|
|
|
2018-06-20 15:28:46 +01:00
|
|
|
func TestProcess_redis(t *testing.T) {
|
|
|
|
flag.Set("localPort", "0")
|
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-06-13 19:22:32 +01:00
|
|
|
err := o.Process(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2018-06-20 15:28:46 +01:00
|
|
|
|
|
|
|
func TestProcess_bolt(t *testing.T) {
|
|
|
|
flag.Set("localPort", "0")
|
|
|
|
flag.Set("redisAddress", "")
|
|
|
|
boltdbPath, err := filepath.Abs("test_bolt.db")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
defer func() {
|
|
|
|
if err := os.Remove(boltdbPath); err != nil {
|
|
|
|
log.Println(errs.New("error while removing test bolt db: %s", err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
flag.Set("boltdbPath", boltdbPath)
|
|
|
|
|
|
|
|
o := newTestService(t)
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
|
|
|
err = o.Process(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcess_error(t *testing.T) {
|
|
|
|
flag.Set("localPort", "0")
|
|
|
|
flag.Set("boltdbPath", "")
|
|
|
|
flag.Set("redisAddress", "")
|
|
|
|
|
|
|
|
o := newTestService(t)
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
|
|
|
err := o.Process(ctx)
|
|
|
|
assert.True(t, process.ErrUsage.Has(err))
|
|
|
|
}
|