storj/pkg/overlay/config_test.go
Egon Elbre 0f5a2f4ef5 Enable more linters (#272)
* enable more linters

* Run gofmt -s

* run goimports

* run unconvert

* fix naked return

* fix misspellings

* fix ineffectual assigments

* fix missing declaration

* don't use deprecated grpc.Errof

* check errors in tests

* run gofmt -w -r "assert.Nil(err) -> assert.NoError(err)"

* fix directory permissions

* don't use nil Context

* simplify boolean expressions

* use bytes.Equal instead of bytes.Compare

* merge variable declarations, remove redundant returns

* fix some golint errors

* run goimports

* handle more errors

* delete empty TestMain

* delete empty TestMain

* ignore examples for now

* fix lint errors

* remove unused values

* more fixes

* run gofmt -w -s .

* add more comments

* fix naming

* more lint fixes

* try switching travis to go1.11

* fix unnecessary conversions

* fix deprecated methods

* use go1.10 and disable gofmt/goimports for now

* switch to 1.10

* don't re-enable gofmt and goimports

* switch covermode to atomic because of -race

* gofmt
2018-08-27 11:28:16 -06:00

89 lines
2.0 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package overlay
import (
"context"
"net/url"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/zeebo/errs"
"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
testFunc func(t *testing.T)
}{
{
testName: "Run with nil",
testFunc: func(t *testing.T) {
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",
testFunc: func(t *testing.T) {
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",
testFunc: func(t *testing.T) {
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)
assert.Equal(t, err.Error(), "redis error: ping failed: dial tcp: address somedir: missing port in address")
},
},
{
testName: "db scheme bolt conn fail",
testFunc: func(t *testing.T) {
ctx := context.WithValue(bctx, key, kad)
var config = Config{DatabaseURL: "bolt://somedir/overlay.db"}
err := config.Run(ctx, nil)
assert.Error(t, err)
if !os.IsNotExist(errs.Unwrap(err)) {
t.Fatal(err.Error())
}
},
},
}
for _, c := range cases {
t.Run(c.testName, c.testFunc)
}
}
func TestUrlPwd(t *testing.T) {
res := GetUserPassword(nil)
assert.Equal(t, res, "")
uinfo := url.UserPassword("testUser", "testPassword")
uri := url.URL{User: uinfo}
res = GetUserPassword(&uri)
assert.Equal(t, res, "testPassword")
}