Skip to content

Commit

Permalink
Merge 95d449f into 900f67e
Browse files Browse the repository at this point in the history
  • Loading branch information
aligeti committed Jun 29, 2018
2 parents 900f67e + 95d449f commit 6ead510
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pkg/transport/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.

package transport

import (
"context"

"github.com/zeebo/errs"
"google.golang.org/grpc"
monkit "gopkg.in/spacemonkeygo/monkit.v2"

proto "storj.io/storj/protos/overlay"
)

var (
mon = monkit.Package()
//Error is the errs class of standard Transport Client errors
Error = errs.Class("transport error")
)

// Client defines the interface to an transport client.
type Client interface {
DialUnauthenticated(ctx context.Context, addr proto.NodeAddress) (*grpc.ClientConn, error)
DialNode(ctx context.Context, node proto.Node) (*grpc.ClientConn, error)
}
38 changes: 38 additions & 0 deletions pkg/transport/transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.

package transport

import (
"context"

"google.golang.org/grpc"

proto "storj.io/storj/protos/overlay"
)

// Transport interface structure
type Transport struct {
}

// DialNode using the authenticated mode
func (o *Transport) DialNode(ctx context.Context, node proto.Node) (conn *grpc.ClientConn, err error) {
defer mon.Task()(&ctx)(&err)

if node.Address == nil {
return nil, Error.New("no address")
}
/* TODO@ASK security feature under development */
return o.DialUnauthenticated(ctx, *node.Address)
}

// DialUnauthenticated using unauthenticated mode
func (o *Transport) DialUnauthenticated(ctx context.Context, addr proto.NodeAddress) (conn *grpc.ClientConn, err error) {
defer mon.Task()(&ctx)(&err)

if addr.Address == "" {
return nil, Error.New("no address")
}

return grpc.Dial(addr.Address, grpc.WithInsecure())
}
48 changes: 48 additions & 0 deletions pkg/transport/transport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package transport

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
proto "storj.io/storj/protos/overlay"
)

func TestDialNode(t *testing.T) {
oc := Transport{}

// node.Address.Address == "" condition test
node := proto.Node{
Id: "DUMMYID1",
Address: &proto.NodeAddress{
Transport: proto.NodeTransport_TCP,
Address: "",
},
}
conn, err := oc.DialNode(context.Background(), node)
assert.Error(t, err)
assert.Nil(t, conn)

// node.Address == nil condition test
node = proto.Node{
Id: "DUMMYID2",
Address: nil,
}
conn, err = oc.DialNode(context.Background(), node)
assert.Error(t, err)
assert.Nil(t, conn)

// node is valid argument condition test
node = proto.Node{
Id: "DUMMYID3",
Address: &proto.NodeAddress{
Transport: proto.NodeTransport_TCP,
Address: "127.0.0.0:9000",
},
}
conn, err = oc.DialNode(context.Background(), node)
assert.NoError(t, err)
assert.NotNil(t, conn)
}

0 comments on commit 6ead510

Please sign in to comment.