@cryptotaxi247 / kubo / commits / 9fb09dd39

test: port routing DHT tests to Go (#9709)

Gus Eggert committed Mar 29, 2023 at 19:28 UTC 9fb09dd398c21b36a33a743433a550ecf6442c54
4 files changed +126 -130
test/cli/dht_legacy_test.go
+1
@@ -13,6 +13,7 @@ import (
13 )
14
15 func TestLegacyDHT(t *testing.T) {
16 + t.Parallel()
17 nodes := harness.NewT(t).NewNodes(5).Init()
18 nodes.ForEachPar(func(node *harness.Node) {
19 node.IPFS("config", "Routing.Type", "dht")
test/cli/harness/nodes.go
+2 -2
@@ -57,8 +57,8 @@ func (n Nodes) Connect() Nodes {
57 return n
58 }
59
60 -func (n Nodes) StartDaemons() Nodes {
61 - ForEachPar(n, func(node *Node) { node.StartDaemon() })
60 +func (n Nodes) StartDaemons(args ...string) Nodes {
61 + ForEachPar(n, func(node *Node) { node.StartDaemon(args...) })
62 return n
63 }
64
test/cli/routing_dht_test.go new
+123
@@ -0,0 +1,123 @@
1 +package cli
2 +
3 +import (
4 + "fmt"
5 + "testing"
6 +
7 + "github.com/ipfs/kubo/test/cli/harness"
8 + "github.com/ipfs/kubo/test/cli/testutils"
9 + "github.com/stretchr/testify/assert"
10 + "github.com/stretchr/testify/require"
11 +)
12 +
13 +func testRoutingDHT(t *testing.T, enablePubsub bool) {
14 + t.Run(fmt.Sprintf("enablePubSub=%v", enablePubsub), func(t *testing.T) {
15 + t.Parallel()
16 + nodes := harness.NewT(t).NewNodes(5).Init()
17 + nodes.ForEachPar(func(node *harness.Node) {
18 + node.IPFS("config", "Routing.Type", "dht")
19 + })
20 +
21 + var daemonArgs []string
22 + if enablePubsub {
23 + daemonArgs = []string{
24 + "--enable-pubsub-experiment",
25 + "--enable-namesys-pubsub",
26 + }
27 + }
28 +
29 + nodes.StartDaemons(daemonArgs...).Connect()
30 +
31 + t.Run("ipfs routing findpeer", func(t *testing.T) {
32 + t.Parallel()
33 + res := nodes[1].RunIPFS("routing", "findpeer", nodes[0].PeerID().String())
34 + assert.Equal(t, 0, res.ExitCode())
35 +
36 + swarmAddr := nodes[0].SwarmAddrsWithoutPeerIDs()[0]
37 + require.Equal(t, swarmAddr.String(), res.Stdout.Trimmed())
38 + })
39 +
40 + t.Run("ipfs routing get <key>", func(t *testing.T) {
41 + t.Parallel()
42 + hash := nodes[2].IPFSAddStr("hello world")
43 + nodes[2].IPFS("name", "publish", "/ipfs/"+hash)
44 +
45 + res := nodes[1].IPFS("routing", "get", "/ipns/"+nodes[2].PeerID().String())
46 + assert.Contains(t, res.Stdout.String(), "/ipfs/"+hash)
47 +
48 + t.Run("put round trips (#3124)", func(t *testing.T) {
49 + t.Parallel()
50 + nodes[0].WriteBytes("get_result", res.Stdout.Bytes())
51 + res := nodes[0].IPFS("routing", "put", "/ipns/"+nodes[2].PeerID().String(), "get_result")
52 + assert.Greater(t, len(res.Stdout.Lines()), 0, "should put to at least one node")
53 + })
54 +
55 + t.Run("put with bad keys fails (issue #5113, #4611)", func(t *testing.T) {
56 + t.Parallel()
57 + keys := []string{"foo", "/pk/foo", "/ipns/foo"}
58 + for _, key := range keys {
59 + key := key
60 + t.Run(key, func(t *testing.T) {
61 + t.Parallel()
62 + res := nodes[0].RunIPFS("routing", "put", key)
63 + assert.Equal(t, 1, res.ExitCode())
64 + assert.Contains(t, res.Stderr.String(), "invalid")
65 + assert.Empty(t, res.Stdout.String())
66 + })
67 + }
68 + })
69 +
70 + t.Run("get with bad keys (issue #4611)", func(t *testing.T) {
71 + for _, key := range []string{"foo", "/pk/foo"} {
72 + key := key
73 + t.Run(key, func(t *testing.T) {
74 + t.Parallel()
75 + res := nodes[0].RunIPFS("routing", "get", key)
76 + assert.Equal(t, 1, res.ExitCode())
77 + assert.Contains(t, res.Stderr.String(), "invalid")
78 + assert.Empty(t, res.Stdout.String())
79 + })
80 + }
81 + })
82 + })
83 +
84 + t.Run("ipfs routing findprovs", func(t *testing.T) {
85 + t.Parallel()
86 + hash := nodes[3].IPFSAddStr("some stuff")
87 + res := nodes[4].IPFS("routing", "findprovs", hash)
88 + assert.Equal(t, nodes[3].PeerID().String(), res.Stdout.Trimmed())
89 + })
90 +
91 + t.Run("routing commands fail when offline", func(t *testing.T) {
92 + t.Parallel()
93 + node := harness.NewT(t).NewNode().Init()
94 +
95 + // these cannot be run in parallel due to repo locking
96 + // this seems like a bug, we should be able to run these without locking the repo
97 +
98 + t.Run("routing findprovs", func(t *testing.T) {
99 + res := node.RunIPFS("routing", "findprovs", testutils.CIDEmptyDir)
100 + assert.Equal(t, 1, res.ExitCode())
101 + assert.Contains(t, res.Stderr.String(), "this command must be run in online mode")
102 + })
103 +
104 + t.Run("routing findpeer", func(t *testing.T) {
105 + res := node.RunIPFS("routing", "findpeer", testutils.CIDEmptyDir)
106 + assert.Equal(t, 1, res.ExitCode())
107 + assert.Contains(t, res.Stderr.String(), "this command must be run in online mode")
108 + })
109 +
110 + t.Run("routing put", func(t *testing.T) {
111 + node.WriteBytes("foo", []byte("foo"))
112 + res := node.RunIPFS("routing", "put", "/ipns/"+node.PeerID().String(), "foo")
113 + assert.Equal(t, 1, res.ExitCode())
114 + assert.Contains(t, res.Stderr.String(), "this action must be run in online mode")
115 + })
116 + })
117 + })
118 +}
119 +
120 +func TestRoutingDHT(t *testing.T) {
121 + testRoutingDHT(t, false)
122 + testRoutingDHT(t, true)
123 +}
test/sharness/t0170-routing-dht.sh deleted
-128
@@ -1,128 +0,0 @@
1 -#!/usr/bin/env bash
2 -
3 -# This file does the same tests as t0170-dht.sh but uses 'routing' commands instead
4 -# (only exception is query, which lives only under dht)
5 -test_description="Test routing command for DHT queries"
6 -
7 -. lib/test-lib.sh
8 -
9 -test_dht() {
10 - NUM_NODES=5
11 -
12 - test_expect_success 'init iptb' '
13 - rm -rf .iptb/ &&
14 - iptb testbed create -type localipfs -count $NUM_NODES -init
15 - '
16 -
17 - test_expect_success 'DHT-only routing' '
18 - iptb run -- ipfs config Routing.Type dht
19 - '
20 -
21 - startup_cluster $NUM_NODES $@
22 -
23 - test_expect_success 'peer ids' '
24 - PEERID_0=$(iptb attr get 0 id) &&
25 - PEERID_2=$(iptb attr get 2 id)
26 - '
27 -
28 - # ipfs routing findpeer <peerID>
29 - test_expect_success 'findpeer' '
30 - ipfsi 1 routing findpeer $PEERID_0 | sort >actual &&
31 - ipfsi 0 id -f "<addrs>" | cut -d / -f 1-5 | sort >expected &&
32 - test_cmp actual expected
33 - '
34 -
35 - # ipfs routing get <key>
36 - test_expect_success 'get with good keys works' '
37 - HASH="$(echo "hello world" | ipfsi 2 add -q)" &&
38 - ipfsi 2 name publish "/ipfs/$HASH" &&
39 - ipfsi 1 routing get "/ipns/$PEERID_2" >get_result
40 - '
41 -
42 - test_expect_success 'get with good keys contains the right value' '
43 - cat get_result | grep -aq "/ipfs/$HASH"
44 - '
45 -
46 - test_expect_success 'put round trips (#3124)' '
47 - ipfsi 0 routing put "/ipns/$PEERID_2" get_result | sort >putted &&
48 - [ -s putted ] ||
49 - test_fsh cat putted
50 - '
51 -
52 - test_expect_success 'put with bad keys fails (issue #5113)' '
53 - ipfsi 0 routing put "foo" <<<bar >putted
54 - ipfsi 0 routing put "/pk/foo" <<<bar >>putted
55 - ipfsi 0 routing put "/ipns/foo" <<<bar >>putted
56 - [ ! -s putted ] ||
57 - test_fsh cat putted
58 - '
59 -
60 - test_expect_success 'put with bad keys returns error (issue #4611)' '
61 - test_must_fail ipfsi 0 routing put "foo" <<<bar &&
62 - test_must_fail ipfsi 0 routing put "/pk/foo" <<<bar &&
63 - test_must_fail ipfsi 0 routing put "/ipns/foo" <<<bar
64 - '
65 -
66 - test_expect_success 'get with bad keys (issue #4611)' '
67 - test_must_fail ipfsi 0 routing get "foo" &&
68 - test_must_fail ipfsi 0 routing get "/pk/foo"
69 - '
70 -
71 - test_expect_success "add a ref so we can find providers for it" '
72 - echo "some stuff" > afile &&
73 - HASH=$(ipfsi 3 add -q afile)
74 - '
75 -
76 - # ipfs routing findprovs <key>
77 - test_expect_success 'findprovs' '
78 - ipfsi 4 routing findprovs $HASH > provs &&
79 - iptb attr get 3 id > expected &&
80 - test_cmp provs expected
81 - '
82 -
83 - # ipfs routing get --enc=json has correct properties
84 - test_expect_success 'routing get --enc=json has correct properties' '
85 - HASH="$(echo "hello world" | ipfsi 2 add -q)" &&
86 - ipfsi 2 name publish "/ipfs/$HASH" &&
87 - ipfsi 1 routing get --enc=json "/ipns/$PEERID_2" | jq -e "has(\"Extra\") and has(\"Type\")"
88 - '
89 -
90 - # ipfs dht query <peerID>
91 - #
92 - # We test all nodes. 4 nodes should see the same peer ID, one node (the
93 - # closest) should see a different one.
94 -
95 - for i in $(test_seq 0 4); do
96 - test_expect_success "dht query from $i" '
97 - ipfsi "$i" dht query "$HASH" | head -1 >closest-$i
98 - '
99 - done
100 -
101 - test_expect_success "collecting results" '
102 - cat closest-* | sort | uniq -c | sed -e "s/ *\([0-9]\+\) .*/\1/g" | sort -g > actual &&
103 - echo 1 > expected &&
104 - echo 4 >> expected
105 - '
106 -
107 - test_expect_success "checking results" '
108 - test_cmp actual expected
109 - '
110 -
111 - test_expect_success 'stop iptb' '
112 - iptb stop
113 - '
114 -
115 - test_expect_success "dht commands fail when offline" '
116 - test_must_fail ipfsi 0 routing findprovs "$HASH" 2>err_findprovs &&
117 - test_must_fail ipfsi 0 routing findpeer "$HASH" 2>err_findpeer &&
118 - test_must_fail ipfsi 0 routing put "/ipns/$PEERID_2" "get_result" 2>err_put &&
119 - test_should_contain "this command must be run in online mode" err_findprovs &&
120 - test_should_contain "this command must be run in online mode" err_findpeer &&
121 - test_should_contain "this action must be run in online mode" err_put
122 - '
123 -}
124 -
125 -test_dht
126 -test_dht --enable-pubsub-experiment --enable-namesys-pubsub
127 -
128 -test_done