2018-08-03 14:15:52 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
2018-08-01 13:57:37 +01:00
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/url"
|
2018-08-23 08:05:56 +01:00
|
|
|
"os"
|
2018-08-01 13:57:37 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-08-23 08:05:56 +01:00
|
|
|
"github.com/zeebo/errs"
|
2018-08-01 13:57:37 +01:00
|
|
|
"storj.io/storj/pkg/kademlia"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRun(t *testing.T) {
|
|
|
|
config := Config{}
|
|
|
|
bctx := context.Background()
|
|
|
|
kad := &kademlia.Kademlia{}
|
|
|
|
var key kademlia.CtxKey
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
testName string
|
2018-08-23 08:05:56 +01:00
|
|
|
testFunc func(t *testing.T)
|
2018-08-01 13:57:37 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
testName: "Run with nil",
|
2018-08-23 08:05:56 +01:00
|
|
|
testFunc: func(t *testing.T) {
|
2018-08-01 13:57:37 +01:00
|
|
|
err := config.Run(bctx, nil)
|
|
|
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, err.Error(), "overlay error: programmer error: kademlia responsibility unstarted")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testName: "Run with nil, pass pointer to Kademlia in context",
|
2018-08-23 08:05:56 +01:00
|
|
|
testFunc: func(t *testing.T) {
|
2018-08-01 13:57:37 +01:00
|
|
|
ctx := context.WithValue(bctx, key, kad)
|
|
|
|
err := config.Run(ctx, nil)
|
|
|
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, err.Error(), "overlay error: database scheme not supported: ")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testName: "db scheme redis conn fail",
|
2018-08-23 08:05:56 +01:00
|
|
|
testFunc: func(t *testing.T) {
|
2018-08-01 13:57:37 +01:00
|
|
|
ctx := context.WithValue(bctx, key, kad)
|
|
|
|
var config = Config{DatabaseURL: "redis://somedir/overlay.db/?db=1"}
|
|
|
|
err := config.Run(ctx, nil)
|
|
|
|
|
|
|
|
assert.Error(t, err)
|
2018-08-22 07:39:57 +01:00
|
|
|
assert.Equal(t, err.Error(), "redis error: ping failed: dial tcp: address somedir: missing port in address")
|
2018-08-01 13:57:37 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testName: "db scheme bolt conn fail",
|
2018-08-23 08:05:56 +01:00
|
|
|
testFunc: func(t *testing.T) {
|
2018-08-01 13:57:37 +01:00
|
|
|
ctx := context.WithValue(bctx, key, kad)
|
|
|
|
var config = Config{DatabaseURL: "bolt://somedir/overlay.db"}
|
|
|
|
err := config.Run(ctx, nil)
|
|
|
|
|
|
|
|
assert.Error(t, err)
|
2018-08-23 08:05:56 +01:00
|
|
|
if !os.IsNotExist(errs.Unwrap(err)) {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
2018-08-01 13:57:37 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2018-08-23 08:05:56 +01:00
|
|
|
t.Run(c.testName, c.testFunc)
|
2018-08-01 13:57:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUrlPwd(t *testing.T) {
|
2018-08-27 18:28:16 +01:00
|
|
|
res := GetUserPassword(nil)
|
2018-08-01 13:57:37 +01:00
|
|
|
|
|
|
|
assert.Equal(t, res, "")
|
|
|
|
|
|
|
|
uinfo := url.UserPassword("testUser", "testPassword")
|
|
|
|
|
|
|
|
uri := url.URL{User: uinfo}
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
res = GetUserPassword(&uri)
|
2018-08-01 13:57:37 +01:00
|
|
|
|
|
|
|
assert.Equal(t, res, "testPassword")
|
|
|
|
}
|