github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/lib/peers_test.go (about)

     1  package lib
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/qri-io/qfs"
     9  	"github.com/qri-io/qfs/muxfs"
    10  	"github.com/qri-io/qri/base/params"
    11  	"github.com/qri-io/qri/config"
    12  	testcfg "github.com/qri-io/qri/config/test"
    13  	"github.com/qri-io/qri/event"
    14  	"github.com/qri-io/qri/p2p"
    15  	p2ptest "github.com/qri-io/qri/p2p/test"
    16  	"github.com/qri-io/qri/profile"
    17  	"github.com/qri-io/qri/repo"
    18  )
    19  
    20  func TestPeerMethodsListNoConnection(t *testing.T) {
    21  	ctx, done := context.WithCancel(context.Background())
    22  	defer done()
    23  
    24  	node := newTestQriNode(t)
    25  	inst := NewInstanceFromConfigAndNode(ctx, testcfg.DefaultConfigForTesting(), node)
    26  	p := PeerListParams{}
    27  	_, err := inst.Peer().List(ctx, &p)
    28  	if err == nil {
    29  		t.Errorf("error: req.List should have failed and returned an error")
    30  	} else if !strings.HasPrefix(err.Error(), "error: not connected") {
    31  		t.Errorf("error: unexpected error message: %s", err.Error())
    32  	}
    33  }
    34  
    35  func TestPeerMethodsList(t *testing.T) {
    36  	ctx, done := context.WithCancel(context.Background())
    37  	defer done()
    38  
    39  	cases := []struct {
    40  		p   *PeerListParams
    41  		res []*profile.Profile
    42  		err string
    43  	}{
    44  		{&PeerListParams{}, nil, "error: not connected, run `qri connect` in another window"},
    45  		// {&ListParams{Data: badDataFile}, nil, "error determining dataset schema: no file extension provided"},
    46  		// {&ListParams{DataFilename: badDataFile.FileName(), Data: badDataFile}, nil, "error determining dataset schema: EOF"},
    47  		// {&ListParams{DataFilename: jobsByAutomationFile.FileName(), Data: jobsByAutomationFile}, nil, ""},
    48  		// TODO - need a test that confirms that this node's identity is never present in peers list
    49  	}
    50  
    51  	node := newTestQriNode(t)
    52  	inst := NewInstanceFromConfigAndNode(ctx, testcfg.DefaultConfigForTesting(), node)
    53  	m := inst.Peer()
    54  	for i, c := range cases {
    55  		_, err := m.List(ctx, c.p)
    56  
    57  		if !(err == nil && c.err == "" || err != nil && err.Error() == c.err) {
    58  			t.Errorf("case %d error mismatch: expected: %s, got: %s", i, c.err, err)
    59  			continue
    60  		}
    61  	}
    62  }
    63  
    64  func TestConnectedQriProfiles(t *testing.T) {
    65  	ctx, done := context.WithCancel(context.Background())
    66  	defer done()
    67  
    68  	cases := []struct {
    69  		params    *ConnectionsParams
    70  		peerCount int
    71  		err       string
    72  	}{
    73  		{&ConnectionsParams{List: params.List{Limit: 100}}, 0, ""},
    74  	}
    75  
    76  	node := newTestQriNode(t)
    77  	inst := NewInstanceFromConfigAndNode(ctx, testcfg.DefaultConfigForTesting(), node)
    78  	m := inst.Peer()
    79  	for i, c := range cases {
    80  		got, err := m.ConnectedQriProfiles(ctx, c.params)
    81  		if !(err == nil && c.err == "" || err != nil && err.Error() == c.err) {
    82  			t.Errorf("case %d error mismatch. expected: %s, got: %s", i, c.err, err)
    83  			continue
    84  		}
    85  		if len(got) != c.peerCount {
    86  			t.Errorf("case %d peer count mismatch. expected: %d, got: %d", i, c.peerCount, len(got))
    87  			continue
    88  		}
    89  	}
    90  }
    91  
    92  func TestConnections(t *testing.T) {
    93  	ctx, done := context.WithCancel(context.Background())
    94  	defer done()
    95  
    96  	cases := []struct {
    97  		params    *ConnectionsParams
    98  		peerCount int
    99  		err       string
   100  	}{
   101  		{&ConnectionsParams{List: params.List{Limit: 100}}, 0, ""},
   102  	}
   103  
   104  	node := newTestQriNode(t)
   105  	inst := NewInstanceFromConfigAndNode(ctx, testcfg.DefaultConfigForTesting(), node)
   106  	m := inst.Peer()
   107  	for i, c := range cases {
   108  		got, err := m.Connections(ctx, c.params)
   109  		if !(err == nil && c.err == "" || err != nil && err.Error() == c.err) {
   110  			t.Errorf("case %d error mismatch. expected: %s, got: %s", i, c.err, err)
   111  			continue
   112  		}
   113  		if len(got) != c.peerCount {
   114  			t.Errorf("case %d peer count mismatch. expected: %d, got: %d", i, c.peerCount, len(got))
   115  			continue
   116  		}
   117  	}
   118  }
   119  
   120  func TestInfo(t *testing.T) {
   121  	ctx, done := context.WithCancel(context.Background())
   122  	defer done()
   123  
   124  	cases := []struct {
   125  		p        PeerInfoParams
   126  		refCount int
   127  		err      string
   128  	}{
   129  		{PeerInfoParams{}, 0, "repo: not found"},
   130  		{PeerInfoParams{ProfileID: "QmY1PxkV9t9RoBwtXHfue1Qf6iYob19nL6rDHuXxooAVZa"}, 0, "repo: not found"},
   131  	}
   132  
   133  	node := newTestQriNode(t)
   134  	inst := NewInstanceFromConfigAndNode(ctx, testcfg.DefaultConfigForTesting(), node)
   135  	m := inst.Peer()
   136  	for i, c := range cases {
   137  		_, err := m.Info(ctx, &c.p)
   138  		if !(err == nil && c.err == "" || err != nil && err.Error() == c.err) {
   139  			t.Errorf("case %d error mismatch. expected: %s, got: %s", i, c.err, err)
   140  			continue
   141  		}
   142  		// TODO - compare output, first add an Equal method to profile
   143  		// if got. {
   144  		// 	t.Errorf("case %d reference count mismatch. expected: %d, got: %d", i, c.refCount, len(got))
   145  		// 	continue
   146  		// }
   147  	}
   148  }
   149  
   150  func TestPeerConnectionsParamsPod(t *testing.T) {
   151  	if p := NewConnectParamsPod("peername"); p.Peername != "peername" {
   152  		t.Error("expected Peername to be set")
   153  	}
   154  
   155  	if p := NewConnectParamsPod("/ipfs/Foo"); p.NetworkID != "/ipfs/Foo" {
   156  		t.Error("expected NetworkID to be set")
   157  	}
   158  
   159  	ma := "/ip4/130.211.198.23/tcp/4001/p2p/QmNX9nSos8sRFvqGTwdEme6LQ8R1eJ8EuFgW32F9jjp2Pb"
   160  	if p := NewConnectParamsPod(ma); p.Multiaddr != ma {
   161  		t.Errorf("peer Multiaddr mismatch. expected: %q, got: %q", ma, p.Multiaddr)
   162  	}
   163  
   164  	if p := NewConnectParamsPod("QmNX9nSos8sRFvqGTwdEme6LQ8R1eJ8EuFgW32F9jjp2Pb"); p.ProfileID != "QmNX9nSos8sRFvqGTwdEme6LQ8R1eJ8EuFgW32F9jjp2Pb" {
   165  		t.Error("expected ProfileID to be set")
   166  	}
   167  
   168  	p := ConnectParamsPod{NetworkID: "/ipfs/QmNX9nSos8sRFvqGTwdEme6LQ8R1eJ8EuFgW32F9jjp2Pb"}
   169  	if _, err := p.Decode(); err != nil {
   170  		t.Error(err.Error())
   171  	}
   172  	p = ConnectParamsPod{NetworkID: "/ipfs/QmNX"}
   173  	if _, err := p.Decode(); err == nil {
   174  		t.Error("expected invalid decode to error")
   175  	}
   176  
   177  	p = ConnectParamsPod{ProfileID: "QmNX9nSos8sRFvqGTwdEme6LQ8R1eJ8EuFgW32F9jjp2Pb"}
   178  	if _, err := p.Decode(); err != nil {
   179  		t.Error(err.Error())
   180  	}
   181  	p = ConnectParamsPod{ProfileID: "21hub2dj23"}
   182  	if _, err := p.Decode(); err == nil {
   183  		t.Error("expected invalid decode to error")
   184  	}
   185  
   186  	p = ConnectParamsPod{Multiaddr: "/ip4/130.211.198.23/tcp/4001/ipfs/QmNX9nSos8sRFvqGTwdEme6LQ8R1eJ8EuFgW32F9jjp2Pb"}
   187  	if _, err := p.Decode(); err != nil {
   188  		t.Error(err.Error())
   189  	}
   190  	p = ConnectParamsPod{Multiaddr: "nhuh"}
   191  	if _, err := p.Decode(); err == nil {
   192  		t.Error("expected invalid decode to error")
   193  	}
   194  }
   195  
   196  func newTestQriNode(t *testing.T) *p2p.QriNode {
   197  	ctx, cancel := context.WithCancel(context.Background())
   198  	defer cancel()
   199  
   200  	r, err := repo.NewMemRepoWithProfile(ctx, testPeerProfile, newTestFS(ctx), event.NilBus)
   201  	if err != nil {
   202  		t.Fatal(err)
   203  	}
   204  	n, err := p2ptest.NewTestNodeFactory(p2p.NewTestableQriNode).New(r)
   205  	if err != nil {
   206  		t.Fatal(err)
   207  	}
   208  	node := n.(*p2p.QriNode)
   209  	return node
   210  }
   211  
   212  func newTestFS(ctx context.Context) *muxfs.Mux {
   213  	mux, err := muxfs.New(ctx, []qfs.Config{
   214  		{Type: "mem"},
   215  		{Type: "local"},
   216  		{Type: "http"},
   217  	})
   218  	if err != nil {
   219  		panic(err)
   220  	}
   221  
   222  	return mux
   223  }
   224  
   225  func newTestDisconnectedQriNode() (*p2p.QriNode, error) {
   226  	ctx := context.TODO()
   227  	pro := &profile.Profile{PrivKey: privKey}
   228  	r, err := repo.NewMemRepoWithProfile(ctx, pro, newTestFS(ctx), event.NilBus)
   229  	if err != nil {
   230  		return nil, err
   231  	}
   232  	p2pconf := config.DefaultP2P()
   233  	// This Node has P2P disabled.
   234  	p2pconf.Enabled = false
   235  	n, err := p2ptest.NewTestNodeFactory(p2p.NewTestableQriNode).NewWithConf(r, p2pconf)
   236  	if err != nil {
   237  		return nil, err
   238  	}
   239  	node := n.(*p2p.QriNode)
   240  	return node, err
   241  }