From 633877458a33d285e0e652061985c2cf156c5ca5 Mon Sep 17 00:00:00 2001 From: nkryuchkov Date: Mon, 2 Sep 2019 11:01:07 +0300 Subject: [PATCH 1/4] [WIP] Fix failing tests --- cmd/skywire-cli/commands/node/routes.go | 2 +- go.sum | 1 + pkg/app/app.go | 7 ++ pkg/router/route_manager_test.go | 8 +- pkg/router/router.go | 4 +- pkg/router/router_test.go | 128 +++++++++++++----------- pkg/routing/rule.go | 7 +- pkg/routing/rule_test.go | 2 +- pkg/setup/node.go | 2 +- pkg/setup/node_test.go | 5 +- pkg/visor/config.go | 1 - pkg/visor/rpc_client.go | 2 +- pkg/visor/rpc_test.go | 115 +++++++++++---------- pkg/visor/visor.go | 1 - pkg/visor/visor_test.go | 30 ++++-- 15 files changed, 176 insertions(+), 139 deletions(-) diff --git a/cmd/skywire-cli/commands/node/routes.go b/cmd/skywire-cli/commands/node/routes.go index 0320d7018a..7cff7a5295 100644 --- a/cmd/skywire-cli/commands/node/routes.go +++ b/cmd/skywire-cli/commands/node/routes.go @@ -100,7 +100,7 @@ var addRuleCmd = &cobra.Command{ remotePort = routing.Port(parseUint("remote-port", args[3], 16)) localPort = routing.Port(parseUint("local-port", args[4], 16)) ) - rule = routing.AppRule(time.Now().Add(expire), routeID, remotePK, remotePort, localPort, 0) + rule = routing.AppRule(time.Now().Add(expire), 0, routeID, remotePK, localPort, remotePort) case "fwd": var ( nextRouteID = routing.RouteID(parseUint("next-route-id", args[1], 32)) diff --git a/go.sum b/go.sum index 1e5468a790..37962e0f0a 100644 --- a/go.sum +++ b/go.sum @@ -143,6 +143,7 @@ golang.org/x/sys v0.0.0-20190804053845-51ab0e2deafa h1:KIDDMLT1O0Nr7TSxp8xM5tJcd golang.org/x/sys v0.0.0-20190804053845-51ab0e2deafa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190825160603-fb81701db80f h1:LCxigP8q3fPRGNVYndYsyHnF0zRrvcoVwZMfb8iQZe4= golang.org/x/sys v0.0.0-20190825160603-fb81701db80f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= diff --git a/pkg/app/app.go b/pkg/app/app.go index ac67f1030f..16ec17f114 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -151,7 +151,9 @@ func (app *App) Close() error { // Accept awaits for incoming loop confirmation request from a Node and // returns net.Conn for received loop. func (app *App) Accept() (net.Conn, error) { + fmt.Println("!!! [ACCEPT] start !!!") addrs := <-app.acceptChan + fmt.Println("!!! [ACCEPT] read from ch !!!") laddr := addrs[0] raddr := addrs[1] @@ -187,6 +189,7 @@ func (app *App) Addr() net.Addr { func (app *App) handleProto() { err := app.proto.Serve(func(frame Frame, payload []byte) (res interface{}, err error) { + fmt.Printf("!!! app received frame: %s\n", frame) switch frame { case FrameConfirmLoop: err = app.confirmLoop(payload) @@ -242,6 +245,8 @@ func (app *App) forwardPacket(data []byte) error { return err } + fmt.Printf("!!! packet loop: %s\n", packet.Loop) + app.mu.Lock() conn := app.conns[packet.Loop] app.mu.Unlock() @@ -272,6 +277,7 @@ func (app *App) closeConn(data []byte) error { } func (app *App) confirmLoop(data []byte) error { + fmt.Println("!!! [confirmLoop] !!!") var addrs [2]routing.Addr if err := json.Unmarshal(data, &addrs); err != nil { return err @@ -288,6 +294,7 @@ func (app *App) confirmLoop(data []byte) error { return errors.New("loop is already created") } + fmt.Println("!!! [confirmLoop] selecting !!!") select { case app.acceptChan <- addrs: default: diff --git a/pkg/router/route_manager_test.go b/pkg/router/route_manager_test.go index bd371ad470..69e63f59d2 100644 --- a/pkg/router/route_manager_test.go +++ b/pkg/router/route_manager_test.go @@ -67,7 +67,7 @@ func TestNewRouteManager(t *testing.T) { defer clearRules() pk, _ := cipher.GenerateKeyPair() - rule := routing.AppRule(time.Now(), 3, pk, 3, 2, 1) + rule := routing.AppRule(time.Now(), 1, 3, pk, 2, 3) _, err := rt.AddRule(rule) require.NoError(t, err) @@ -98,6 +98,8 @@ func TestNewRouteManager(t *testing.T) { errCh <- rm.handleSetupConn(delOut) // Receive DeleteRule request. close(errCh) }() + + // TODO: remove defer from for loop defer func() { require.NoError(t, requestIDIn.Close()) require.NoError(t, addIn.Close()) @@ -186,7 +188,7 @@ func TestNewRouteManager(t *testing.T) { proto := setup.NewSetupProtocol(in) pk, _ := cipher.GenerateKeyPair() - rule := routing.AppRule(time.Now(), 3, pk, 3, 2, 2) + rule := routing.AppRule(time.Now(), 2, 3, pk, 2, 3) require.NoError(t, rt.SetRule(2, rule)) rule = routing.ForwardRule(time.Now(), 3, uuid.New(), 1) @@ -238,7 +240,7 @@ func TestNewRouteManager(t *testing.T) { proto := setup.NewSetupProtocol(in) pk, _ := cipher.GenerateKeyPair() - rule := routing.AppRule(time.Now(), 3, pk, 3, 2, 0) + rule := routing.AppRule(time.Now(), 0, 3, pk, 2, 3) require.NoError(t, rt.SetRule(2, rule)) rule = routing.ForwardRule(time.Now(), 3, uuid.New(), 1) diff --git a/pkg/router/router.go b/pkg/router/router.go index 57f96d7c52..3a657d44da 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -147,6 +147,8 @@ func (r *Router) handlePacket(ctx context.Context, packet routing.Packet) error // ServeApp handles App packets from the App connection on provided port. func (r *Router) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) error { + fmt.Println("!!! [ServeApp] start !!!") + r.wg.Add(1) defer r.wg.Done() @@ -229,7 +231,7 @@ func (r *Router) consumePacket(payload []byte, rule routing.Rule) error { } fmt.Println("got it!") if err := b.conn.Send(app.FrameSend, p, nil); err != nil { // TODO: Stuck here. - fmt.Println("err:", err) + fmt.Println("!!! Send err:", err) return err } fmt.Println("done") diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index f705805c42..d17041fb5f 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -2,7 +2,9 @@ package router import ( "context" + "encoding/json" "fmt" + "net" "os" "testing" "time" @@ -11,6 +13,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/skycoin/skywire/pkg/app" "github.com/skycoin/skywire/pkg/routing" "github.com/skycoin/skywire/pkg/snet" "github.com/skycoin/skywire/pkg/snet/snettest" @@ -36,7 +39,6 @@ func TestMain(m *testing.M) { // Ensure that received packets are handled properly in `(*Router).Serve()`. func TestRouter_Serve(t *testing.T) { - // We are generating two key pairs - one for the a `Router`, the other to send packets to `Router`. keys := snettest.GenKeyPairs(2) @@ -49,10 +51,10 @@ func TestRouter_Serve(t *testing.T) { // Create routers r0, err := New(nEnv.Nets[0], rEnv.GenRouterConfig(0)) require.NoError(t, err) - //go r0.Serve(context.TODO()) + // go r0.Serve(context.TODO()) r1, err := New(nEnv.Nets[1], rEnv.GenRouterConfig(1)) require.NoError(t, err) - //go r1.Serve(context.TODO()) + // go r1.Serve(context.TODO()) // Create dmsg transport between two `snet.Network` entities. tp1, err := rEnv.TpMngrs[1].SaveTransport(context.TODO(), keys[0].PK, dmsg.Type) @@ -94,62 +96,70 @@ func TestRouter_Serve(t *testing.T) { }) // TODO(evanlinjin): I'm having so much trouble with this I officially give up. - //t.Run("handlePacket_appRule", func(t *testing.T) { - // defer clearRules(r0, r1) - // - // // prepare mock-app - // localPort := routing.Port(9) - // cConn, sConn := net.Pipe() - // - // // mock-app config - // appConf := &app.Config{ - // AppName: "test_app", - // AppVersion: "1.0", - // ProtocolVersion: supportedProtocolVersion, - // } - // - // // serve mock-app - // sErrCh := make(chan error, 1) - // go func() { - // sErrCh <- r0.ServeApp(sConn, localPort, appConf) - // close(sErrCh) - // }() - // defer func() { - // assert.NoError(t, cConn.Close()) - // assert.NoError(t, <-sErrCh) - // }() - // - // a, err := app.New(cConn, appConf) - // require.NoError(t, err) - // cErrCh := make(chan error, 1) - // go func() { - // conn, err := a.Accept() - // if err == nil { - // fmt.Println("ACCEPTED:", conn.RemoteAddr()) - // } - // fmt.Println("FAILED TO ACCEPT") - // cErrCh <- err - // close(cErrCh) - // }() - // defer func() { - // assert.NoError(t, <-cErrCh) - // }() - // - // // Add a APP rule for r0. - // appRule := routing.AppRule(time.Now().Add(time.Hour), routing.RouteID(7), keys[1].PK, routing.Port(8), localPort) - // appRtID, err := r0.rm.rt.AddRule(appRule) - // require.NoError(t, err) - // - // // Call handlePacket for r0. - // - // // payload is prepended with two bytes to satisfy app.Proto. - // // payload[0] = frame type, payload[1] = id - // rAddr := routing.Addr{PubKey: keys[1].PK, Port: localPort} - // rawRAddr, _ := json.Marshal(rAddr) - // //payload := append([]byte{byte(app.FrameClose), 0}, rawRAddr...) - // packet := routing.MakePacket(appRtID, rawRAddr) - // require.NoError(t, r0.handlePacket(context.TODO(), packet)) - //}) + t.Run("handlePacket_appRule", func(t *testing.T) { + const duration = 10 * time.Second + // time.AfterFunc(duration, func() { + // panic("timeout") + // }) + + defer clearRules(r0, r1) + + // prepare mock-app + localPort := routing.Port(9) + cConn, sConn := net.Pipe() + + // mock-app config + appConf := &app.Config{ + AppName: "test_app", + AppVersion: "1.0", + ProtocolVersion: supportedProtocolVersion, + } + + // serve mock-app + // sErrCh := make(chan error, 1) + go func() { + // sErrCh <- r0.ServeApp(sConn, localPort, appConf) + _ = r0.ServeApp(sConn, localPort, appConf) + // close(sErrCh) + }() + // defer func() { + // assert.NoError(t, cConn.Close()) + // assert.NoError(t, <-sErrCh) + // }() + + a, err := app.New(cConn, appConf) + require.NoError(t, err) + // cErrCh := make(chan error, 1) + go func() { + conn, err := a.Accept() + if err == nil { + fmt.Println("ACCEPTED:", conn.RemoteAddr()) + } + fmt.Println("FAILED TO ACCEPT") + // cErrCh <- err + // close(cErrCh) + }() + a.Dial(a.Addr().(routing.Addr)) + // defer func() { + // assert.NoError(t, <-cErrCh) + // }() + + // Add a APP rule for r0. + // port8 := routing.Port(8) + appRule := routing.AppRule(time.Now().Add(time.Hour), 0, routing.RouteID(7), keys[1].PK, localPort, localPort) + appRtID, err := r0.rm.rt.AddRule(appRule) + require.NoError(t, err) + + // Call handlePacket for r0. + + // payload is prepended with two bytes to satisfy app.Proto. + // payload[0] = frame type, payload[1] = id + rAddr := routing.Addr{PubKey: keys[1].PK, Port: localPort} + rawRAddr, _ := json.Marshal(rAddr) + // payload := append([]byte{byte(app.FrameClose), 0}, rawRAddr...) + packet := routing.MakePacket(appRtID, rawRAddr) + require.NoError(t, r0.handlePacket(context.TODO(), packet)) + }) } type TestEnv struct { diff --git a/pkg/routing/rule.go b/pkg/routing/rule.go index f6d9cc4819..217bb2dbe3 100644 --- a/pkg/routing/rule.go +++ b/pkg/routing/rule.go @@ -150,7 +150,7 @@ type RuleSummary struct { func (rs *RuleSummary) ToRule() (Rule, error) { if rs.Type == RuleApp && rs.AppFields != nil && rs.ForwardFields == nil { f := rs.AppFields - return AppRule(rs.ExpireAt, f.RespRID, f.RemotePK, f.RemotePort, f.LocalPort, rs.RequestRouteID), nil + return AppRule(rs.ExpireAt, rs.RequestRouteID, f.RespRID, f.RemotePK, f.LocalPort, f.RemotePort), nil } if rs.Type == RuleForward && rs.AppFields == nil && rs.ForwardFields != nil { f := rs.ForwardFields @@ -183,8 +183,7 @@ func (r Rule) Summary() *RuleSummary { } // AppRule constructs a new consume RoutingRule. -func AppRule(expireAt time.Time, respRoute RouteID, remotePK cipher.PubKey, remotePort, localPort Port, - requestRouteID RouteID) Rule { +func AppRule(expireAt time.Time, reqRoute, respRoute RouteID, remotePK cipher.PubKey, localPort, remotePort Port) Rule { rule := make([]byte, RuleHeaderSize) if expireAt.Unix() <= time.Now().Unix() { binary.BigEndian.PutUint64(rule[0:], 0) @@ -198,7 +197,7 @@ func AppRule(expireAt time.Time, respRoute RouteID, remotePK cipher.PubKey, remo rule = append(rule, bytes.Repeat([]byte{0}, 8)...) binary.BigEndian.PutUint16(rule[46:], uint16(remotePort)) binary.BigEndian.PutUint16(rule[48:], uint16(localPort)) - binary.BigEndian.PutUint32(rule[50:], uint32(requestRouteID)) + binary.BigEndian.PutUint32(rule[50:], uint32(reqRoute)) return rule } diff --git a/pkg/routing/rule_test.go b/pkg/routing/rule_test.go index b7715bac7b..62cb46468a 100644 --- a/pkg/routing/rule_test.go +++ b/pkg/routing/rule_test.go @@ -12,7 +12,7 @@ import ( func TestAppRule(t *testing.T) { expireAt := time.Now().Add(2 * time.Minute) pk, _ := cipher.GenerateKeyPair() - rule := AppRule(expireAt, 2, pk, 3, 4, 1) + rule := AppRule(expireAt, 1, 2, pk, 4, 3) assert.Equal(t, expireAt.Unix(), rule.Expiry().Unix()) assert.Equal(t, RuleApp, rule.Type()) diff --git a/pkg/setup/node.go b/pkg/setup/node.go index e2a6c0aa66..6bd9069399 100644 --- a/pkg/setup/node.go +++ b/pkg/setup/node.go @@ -267,7 +267,7 @@ func (sn *Node) createRoute(ctx context.Context, expireAt time.Time, route routi nextTpID = r[i+1].Transport rule = routing.ForwardRule(expireAt, 0, nextTpID, 0) } else { - rule = routing.AppRule(expireAt, 0, init, lport, rport, 0) + rule = routing.AppRule(expireAt, 0, 0, init, rport, lport) } go func(i int, pk cipher.PubKey, rule routing.Rule, reqIDChIn <-chan routing.RouteID, diff --git a/pkg/setup/node_test.go b/pkg/setup/node_test.go index 0d1b1e2eb7..7153f3c814 100644 --- a/pkg/setup/node_test.go +++ b/pkg/setup/node_test.go @@ -176,7 +176,8 @@ func TestNode(t *testing.T) { routeID := atomic.AddUint32(&nextRouteID, 1) - err = proto.WritePacket(RespSuccess, []routing.RouteID{routing.RouteID(routeID)}) + // TODO: This error is not checked due to a bug in dmsg. + _ = proto.WritePacket(RespSuccess, []routing.RouteID{routing.RouteID(routeID)}) // nolint:errcheck assert.NoError(t, err) fmt.Printf("client %v:%v responded to with registration ID: %v\n", client, clients[client].Addr, routeID) @@ -204,7 +205,7 @@ func TestNode(t *testing.T) { } // TODO: This error is not checked due to a bug in dmsg. - _ = proto.WritePacket(RespSuccess, nil) //nolint:errcheck + _ = proto.WritePacket(RespSuccess, nil) // nolint:errcheck fmt.Printf("client %v:%v responded for PacketAddRules\n", client, clients[client].Addr) diff --git a/pkg/visor/config.go b/pkg/visor/config.go index fdbfef03f8..4342c90f4a 100644 --- a/pkg/visor/config.go +++ b/pkg/visor/config.go @@ -68,7 +68,6 @@ type Config struct { // MessagingConfig returns config for dmsg client. func (c *Config) MessagingConfig() (*DmsgConfig, error) { - msgConfig := c.Messaging if msgConfig.Discovery == "" { diff --git a/pkg/visor/rpc_client.go b/pkg/visor/rpc_client.go index f9dee8b6ae..438bbbf0f3 100644 --- a/pkg/visor/rpc_client.go +++ b/pkg/visor/rpc_client.go @@ -231,7 +231,7 @@ func NewMockRPCClient(r *rand.Rand, maxTps int, maxRules int) (cipher.PubKey, RP if err != nil { panic(err) } - appRule := routing.AppRule(ruleExp, fwdRID, remotePK, rp, lp, appRID) + appRule := routing.AppRule(ruleExp, appRID, fwdRID, remotePK, lp, rp) if err := rt.SetRule(appRID, appRule); err != nil { panic(err) } diff --git a/pkg/visor/rpc_test.go b/pkg/visor/rpc_test.go index 9f68f65596..613b8ffc45 100644 --- a/pkg/visor/rpc_test.go +++ b/pkg/visor/rpc_test.go @@ -1,16 +1,23 @@ package visor import ( + "context" + "encoding/json" + "net" + "net/rpc" "os" "testing" "time" + "github.com/google/uuid" "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skycoin/src/util/logging" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/skycoin/skywire/pkg/routing" + "github.com/skycoin/skywire/pkg/snet" + "github.com/skycoin/skywire/pkg/transport" "github.com/skycoin/skywire/pkg/util/pathutil" ) @@ -90,8 +97,6 @@ func TestStartStopApp(t *testing.T) { node.startedMu.Unlock() } -// TODO(nkryuchkov): fix and uncomment -/* func TestRPC(t *testing.T) { r := new(mockRouter) executer := new(MockExecuter) @@ -151,6 +156,7 @@ func TestRPC(t *testing.T) { go svr.ServeConn(sConn) // client := RPCClient{Client: rpc.NewClient(cConn)} + client := NewRPCClient(rpc.NewClient(cConn), "") printFunc := func(t *testing.T, name string, v interface{}) { j, err := json.MarshalIndent(v, name+": ", " ") @@ -170,11 +176,11 @@ func TestRPC(t *testing.T) { require.NoError(t, gateway.Summary(&struct{}{}, &summary)) test(t, &summary) }) - // t.Run("RPCClient", func(t *testing.T) { - // summary, err := client.Summary() - // require.NoError(t, err) - // test(t, summary) - // }) + t.Run("RPCClient", func(t *testing.T) { + summary, err := client.Summary() + require.NoError(t, err) + test(t, summary) + }) }) t.Run("Exec", func(t *testing.T) { @@ -186,11 +192,11 @@ func TestRPC(t *testing.T) { assert.Equal(t, []byte("1\n"), out) }) - // t.Run("RPCClient", func(t *testing.T) { - // out, err := client.Exec(command) - // require.NoError(t, err) - // assert.Equal(t, []byte("1\n"), out) - // }) + t.Run("RPCClient", func(t *testing.T) { + out, err := client.Exec(command) + require.NoError(t, err) + assert.Equal(t, []byte("1\n"), out) + }) }) t.Run("Apps", func(t *testing.T) { @@ -203,21 +209,21 @@ func TestRPC(t *testing.T) { require.NoError(t, gateway.Apps(&struct{}{}, &apps)) test(t, apps) }) - // t.Run("RPCClient", func(t *testing.T) { - // apps, err := client.Apps() - // require.NoError(t, err) - // test(t, apps) - // }) + t.Run("RPCClient", func(t *testing.T) { + apps, err := client.Apps() + require.NoError(t, err) + test(t, apps) + }) }) // TODO(evanlinjin): For some reason, this freezes. - // t.Run("StopStartApp", func(t *testing.T) { - // appName := "foo" - // require.NoError(t, gateway.StopApp(&appName, &struct{}{})) - // require.NoError(t, gateway.StartApp(&appName, &struct{}{})) - // require.NoError(t, client.StopApp(appName)) - // require.NoError(t, client.StartApp(appName)) - // }) + t.Run("StopStartApp", func(t *testing.T) { + appName := "foo" + require.NoError(t, gateway.StopApp(&appName, &struct{}{})) + require.NoError(t, gateway.StartApp(&appName, &struct{}{})) + require.NoError(t, client.StopApp(appName)) + require.NoError(t, client.StartApp(appName)) + }) t.Run("SetAutoStart", func(t *testing.T) { unknownAppName := "whoAmI" @@ -241,15 +247,15 @@ func TestRPC(t *testing.T) { // Test with RPC Client - // err = client.SetAutoStart(in1.AppName, in1.AutoStart) - // require.Error(t, err) - // assert.Equal(t, ErrUnknownApp.Error(), err.Error()) - // - // require.NoError(t, client.SetAutoStart(in2.AppName, in2.AutoStart)) - // assert.True(t, node.appsConf[0].AutoStart) - // - // require.NoError(t, client.SetAutoStart(in3.AppName, in3.AutoStart)) - // assert.False(t, node.appsConf[0].AutoStart) + err = client.SetAutoStart(in1.AppName, in1.AutoStart) + require.Error(t, err) + assert.Equal(t, ErrUnknownApp.Error(), err.Error()) + + require.NoError(t, client.SetAutoStart(in2.AppName, in2.AutoStart)) + assert.True(t, node.appsConf[0].AutoStart) + + require.NoError(t, client.SetAutoStart(in3.AppName, in3.AutoStart)) + assert.False(t, node.appsConf[0].AutoStart) }) t.Run("TransportTypes", func(t *testing.T) { @@ -257,33 +263,32 @@ func TestRPC(t *testing.T) { var out []*TransportSummary require.NoError(t, gateway.Transports(&in, &out)) - assert.Len(t, out, 1) + require.Len(t, out, 1) assert.Equal(t, "mock", out[0].Type) - // out2, err := client.Transports(in.FilterTypes, in.FilterPubKeys, in.ShowLogs) - // require.NoError(t, err) - // assert.Equal(t, out, out2) + out2, err := client.Transports(in.FilterTypes, in.FilterPubKeys, in.ShowLogs) + require.NoError(t, err) + assert.Equal(t, out, out2) }) - //t.Run("Transport", func(t *testing.T) { - // var ids []uuid.UUID - // node.tm.WalkTransports(func(tp *transport.ManagedTransport) bool { - // ids = append(ids, tp.Entry.ID) - // return true - // }) - // - // for _, id := range ids { - // id := id - // var summary TransportSummary - // require.NoError(t, gateway.Transport(&id, &summary)) - // - // summary2, err := client.Transport(id) - // require.NoError(t, err) - // require.Equal(t, summary, *summary2) - // } - //}) + t.Run("Transport", func(t *testing.T) { + var ids []uuid.UUID + node.tm.WalkTransports(func(tp *transport.ManagedTransport) bool { + ids = append(ids, tp.Entry.ID) + return true + }) + + for _, id := range ids { + id := id + var summary TransportSummary + require.NoError(t, gateway.Transport(&id, &summary)) + + summary2, err := client.Transport(id) + require.NoError(t, err) + require.Equal(t, summary, *summary2) + } + }) // TODO: Test add/remove transports } -*/ diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index 2d70283a14..6d49c89d7c 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -23,7 +23,6 @@ import ( "github.com/skycoin/dmsg" "github.com/skycoin/dmsg/cipher" - "github.com/skycoin/dmsg/noise" "github.com/skycoin/skycoin/src/util/logging" diff --git a/pkg/visor/visor_test.go b/pkg/visor/visor_test.go index 6f194cd035..0f9eaf9740 100644 --- a/pkg/visor/visor_test.go +++ b/pkg/visor/visor_test.go @@ -2,22 +2,29 @@ package visor import ( "context" + "encoding/json" "errors" "io/ioutil" "net" + "net/http" + "net/http/httptest" "os" "os/exec" "sync" "testing" "time" + "github.com/skycoin/dmsg" "github.com/skycoin/dmsg/cipher" + "github.com/skycoin/dmsg/disc" "github.com/skycoin/skycoin/src/util/logging" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/skycoin/skywire/internal/httpauth" "github.com/skycoin/skywire/pkg/app" "github.com/skycoin/skywire/pkg/routing" + "github.com/skycoin/skywire/pkg/snet" "github.com/skycoin/skywire/pkg/transport" "github.com/skycoin/skywire/pkg/util/pathutil" ) @@ -41,7 +48,6 @@ func TestMain(m *testing.M) { } // TODO(nkryuchkov): fix and uncomment -/* func TestNewNode(t *testing.T) { pk, sk := cipher.GenerateKeyPair() srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -73,10 +79,8 @@ func TestNewNode(t *testing.T) { assert.NotNil(t, node.localPath) assert.NotNil(t, node.startedApps) } -*/ -// TODO(Darkren): fix test -/*func TestNodeStartClose(t *testing.T) { +func TestNodeStartClose(t *testing.T) { r := new(mockRouter) executer := &MockExecuter{} conf := []AppConfig{ @@ -90,13 +94,21 @@ func TestNewNode(t *testing.T) { node := &Node{config: &Config{}, router: r, executer: executer, appsConf: conf, startedApps: map[string]*appBind{}, logger: logging.MustGetLogger("test")} - mConf := &dmsg.Config{PubKey: cipher.PubKey{}, SecKey: cipher.SecKey{}, Discovery: disc.NewMock()} - node.messenger = dmsg.NewClient(mConf.PubKey, mConf.SecKey, mConf.Discovery) - var err error + dmsgC := dmsg.NewClient(cipher.PubKey{}, cipher.SecKey{}, disc.NewMock()) + netConf := snet.Config{ + PubKey: cipher.PubKey{}, + SecKey: cipher.SecKey{}, + TpNetworks: nil, + DmsgDiscAddr: "", + DmsgMinSrvs: 0, + } + network := snet.NewRaw(netConf, dmsgC) tmConf := &transport.ManagerConfig{PubKey: cipher.PubKey{}, DiscoveryClient: transport.NewDiscoveryMock()} - node.tm, err = transport.NewManager(tmConf, nil, node.messenger) + + tm, err := transport.NewManager(network, tmConf) + node.tm = tm require.NoError(t, err) errCh := make(chan error) @@ -112,7 +124,7 @@ func TestNewNode(t *testing.T) { require.Len(t, executer.cmds, 1) assert.Equal(t, "skychat.v1.0", executer.cmds[0].Path) assert.Equal(t, "skychat/v1.0", executer.cmds[0].Dir) -}*/ +} func TestNodeSpawnApp(t *testing.T) { pk, _ := cipher.GenerateKeyPair() From 7a218cba27320117ea0677477f3867a5fdff090e Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Tue, 3 Sep 2019 22:58:46 +0300 Subject: [PATCH 2/4] Attempt to fix RPC tests --- pkg/transport/mock.go | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/pkg/transport/mock.go b/pkg/transport/mock.go index 57f3c62c09..643807630a 100644 --- a/pkg/transport/mock.go +++ b/pkg/transport/mock.go @@ -10,6 +10,7 @@ import ( "github.com/skycoin/dmsg" "github.com/skycoin/dmsg/cipher" "github.com/skycoin/dmsg/disc" + "golang.org/x/net/nettest" "github.com/skycoin/skywire/pkg/snet" ) @@ -174,9 +175,11 @@ func MockTransportManagersPair() (pk1, pk2 cipher.PubKey, m1, m2 *Manager, errCh discovery := NewDiscoveryMock() logs := InMemoryTransportLogStore() - var sk1, sk2 cipher.SecKey + var pk3 cipher.PubKey + var sk1, sk2, sk3 cipher.SecKey pk1, sk1 = cipher.GenerateKeyPair() pk2, sk2 = cipher.GenerateKeyPair() + pk3, sk3 = cipher.GenerateKeyPair() mc1 := &ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: discovery, LogStore: logs} mc2 := &ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: discovery, LogStore: logs} @@ -186,23 +189,31 @@ func MockTransportManagersPair() (pk1, pk2 cipher.PubKey, m1, m2 *Manager, errCh dmsgD := disc.NewMock() - if err = dmsgD.SetEntry(context.TODO(), disc.NewClientEntry(pk1, 0, []cipher.PubKey{})); err != nil { + l, err := nettest.NewLocalListener("tcp") + if err != nil { + return + } + srv, err := dmsg.NewServer(pk3, sk3, "", l, dmsgD) + if err != nil { + return + } + + go func() { + errCh <- srv.Serve() + close(errCh) + }() + + if err = dmsgD.SetEntry(context.TODO(), disc.NewServerEntry(pk3, 0, srv.Addr(), 0)); err != nil { return } - // l, err := nettest.NewLocalListener("tcp") - // if err != nil { - // return - // } - // srv, err := dmsg.NewServer(pk1, sk1, "", l, dmsgD) - // if err != nil { - // return - // } - // - // go func() { - // errCh <- srv.Serve() - // close(errCh) - // }() + if err = dmsgD.SetEntry(context.TODO(), disc.NewClientEntry(pk1, 0, []cipher.PubKey{pk3})); err != nil { + return + } + + if err = dmsgD.SetEntry(context.TODO(), disc.NewClientEntry(pk2, 0, []cipher.PubKey{pk3})); err != nil { + return + } dmsgC1 := dmsg.NewClient(pk1, sk1, dmsgD) dmsgC2 := dmsg.NewClient(pk2, sk2, dmsgD) From 253e89bf165df6137b7861b6b452e88361461b3e Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Wed, 4 Sep 2019 11:44:38 +0300 Subject: [PATCH 3/4] Use route finder in router config for tests --- pkg/router/router_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index d17041fb5f..978450877b 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -14,6 +14,7 @@ import ( "github.com/stretchr/testify/require" "github.com/skycoin/skywire/pkg/app" + routeFinder "github.com/skycoin/skywire/pkg/route-finder/client" "github.com/skycoin/skywire/pkg/routing" "github.com/skycoin/skywire/pkg/snet" "github.com/skycoin/skywire/pkg/snet/snettest" @@ -214,7 +215,7 @@ func (e *TestEnv) GenRouterConfig(i int) *Config { SecKey: e.TpMngrConfs[i].SecKey, TransportManager: e.TpMngrs[i], RoutingTable: routing.InMemoryRoutingTable(), - RouteFinder: nil, // TODO + RouteFinder: routeFinder.NewMock(), SetupNodes: nil, // TODO GarbageCollectDuration: DefaultGarbageCollectDuration, } From 7678e2c03ee1d4f4488d75c3625a6c1871bfb28b Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Wed, 4 Sep 2019 15:11:27 +0300 Subject: [PATCH 4/4] Fix a compilation error --- pkg/setup/node.go | 2 +- pkg/visor/rpc_client.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/setup/node.go b/pkg/setup/node.go index 510e0aa06f..5577b2b2af 100644 --- a/pkg/setup/node.go +++ b/pkg/setup/node.go @@ -268,7 +268,7 @@ func (sn *Node) createRoute(ctx context.Context, keepAlive time.Duration, route nextTpID = r[i+1].Transport rule = routing.ForwardRule(keepAlive, 0, nextTpID, 0) } else { - rule = routing.AppRule(keepAlive, 0, 0, init, rport, lport) + rule = routing.AppRule(keepAlive, 0, 0, init, lport, rport) } go func(i int, pk cipher.PubKey, rule routing.Rule, reqIDChIn <-chan routing.RouteID, diff --git a/pkg/visor/rpc_client.go b/pkg/visor/rpc_client.go index 8fda4a4f27..33a01cc14d 100644 --- a/pkg/visor/rpc_client.go +++ b/pkg/visor/rpc_client.go @@ -232,7 +232,7 @@ func NewMockRPCClient(r *rand.Rand, maxTps int, maxRules int) (cipher.PubKey, RP if err != nil { panic(err) } - appRule := routing.AppRule(ruleKeepAlive, fwdRID, remotePK, rp, lp, appRID) + appRule := routing.AppRule(ruleKeepAlive, appRID, fwdRID, remotePK, lp, rp) if err := rt.SetRule(appRID, appRule); err != nil { panic(err) }