master
go 170 lines 5.22 KB
Raw
1 package cli
2
3 import (
4 "fmt"
5 "strconv"
6 "strings"
7 "testing"
8 "time"
9
10 "github.com/ipfs/kubo/test/cli/harness"
11 "github.com/ipfs/kubo/test/cli/testutils"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14 )
15
16 func waitUntilProvidesComplete(t *testing.T, n *harness.Node) {
17 getCidsCount := func(line string) int {
18 trimmed := strings.TrimSpace(line)
19 countStr := strings.SplitN(trimmed, " ", 2)[0]
20 count, err := strconv.Atoi(countStr)
21 require.NoError(t, err)
22 return count
23 }
24
25 queuedProvides, ongoingProvides := true, true
26 for queuedProvides || ongoingProvides {
27 res := n.IPFS("provide", "stat", "-a")
28 require.NoError(t, res.Err)
29 for _, line := range res.Stdout.Lines() {
30 if trimmed, ok := strings.CutPrefix(line, " Provide queue:"); ok {
31 provideQueueSize := getCidsCount(trimmed)
32 queuedProvides = provideQueueSize > 0
33 }
34 if trimmed, ok := strings.CutPrefix(line, " Ongoing provides:"); ok {
35 ongoingProvideCount := getCidsCount(trimmed)
36 ongoingProvides = ongoingProvideCount > 0
37 }
38 }
39 time.Sleep(10 * time.Millisecond)
40 }
41 }
42
43 func testRoutingDHT(t *testing.T, enablePubsub bool) {
44 t.Run(fmt.Sprintf("enablePubSub=%v", enablePubsub), func(t *testing.T) {
45 t.Parallel()
46 nodes := harness.NewT(t).NewNodes(5).Init()
47 nodes.ForEachPar(func(node *harness.Node) {
48 node.IPFS("config", "Routing.Type", "dht")
49 })
50
51 var daemonArgs []string
52 if enablePubsub {
53 daemonArgs = []string{
54 "--enable-pubsub-experiment",
55 "--enable-namesys-pubsub",
56 }
57 }
58
59 nodes.StartDaemons(daemonArgs...).Connect()
60 t.Cleanup(func() { nodes.StopDaemons() })
61
62 t.Run("ipfs routing findpeer", func(t *testing.T) {
63 t.Parallel()
64 res := nodes[1].RunIPFS("routing", "findpeer", nodes[0].PeerID().String())
65 assert.Equal(t, 0, res.ExitCode())
66
67 swarmAddr := nodes[0].SwarmAddrsWithoutPeerIDs()[0]
68 require.Equal(t, swarmAddr.String(), res.Stdout.Trimmed())
69 })
70
71 t.Run("ipfs routing get <key>", func(t *testing.T) {
72 t.Parallel()
73 hash := nodes[2].IPFSAddStr("hello world")
74 nodes[2].IPFS("name", "publish", "/ipfs/"+hash)
75
76 res := nodes[1].IPFS("routing", "get", "/ipns/"+nodes[2].PeerID().String())
77 assert.Contains(t, res.Stdout.String(), "/ipfs/"+hash)
78
79 t.Run("put round trips (#3124)", func(t *testing.T) {
80 t.Parallel()
81 nodes[0].WriteBytes("get_result", res.Stdout.Bytes())
82 res := nodes[0].IPFS("routing", "put", "/ipns/"+nodes[2].PeerID().String(), "get_result")
83 assert.Greater(t, len(res.Stdout.Lines()), 0, "should put to at least one node")
84 })
85
86 t.Run("put with bad keys fails (issue #5113, #4611)", func(t *testing.T) {
87 t.Parallel()
88 keys := []string{"foo", "/pk/foo", "/ipns/foo"}
89 for _, key := range keys {
90 t.Run(key, func(t *testing.T) {
91 t.Parallel()
92 res := nodes[0].RunIPFS("routing", "put", key)
93 assert.Equal(t, 1, res.ExitCode())
94 assert.Contains(t, res.Stderr.String(), "invalid")
95 assert.Empty(t, res.Stdout.String())
96 })
97 }
98 })
99
100 t.Run("get with bad keys (issue #4611)", func(t *testing.T) {
101 for _, key := range []string{"foo", "/pk/foo"} {
102 t.Run(key, func(t *testing.T) {
103 t.Parallel()
104 res := nodes[0].RunIPFS("routing", "get", key)
105 assert.Equal(t, 1, res.ExitCode())
106 assert.Contains(t, res.Stderr.String(), "invalid")
107 assert.Empty(t, res.Stdout.String())
108 })
109 }
110 })
111 })
112
113 t.Run("ipfs routing findprovs", func(t *testing.T) {
114 t.Parallel()
115 hash := nodes[3].IPFSAddStr("some stuff")
116 waitUntilProvidesComplete(t, nodes[3])
117 res := nodes[4].IPFS("routing", "findprovs", hash)
118 assert.Equal(t, nodes[3].PeerID().String(), res.Stdout.Trimmed())
119 })
120
121 t.Run("routing commands fail when offline", func(t *testing.T) {
122 t.Parallel()
123 node := harness.NewT(t).NewNode().Init()
124
125 // these cannot be run in parallel due to repo locking
126 // this seems like a bug, we should be able to run these without locking the repo
127
128 t.Run("routing findprovs", func(t *testing.T) {
129 res := node.RunIPFS("routing", "findprovs", testutils.CIDEmptyDir)
130 assert.Equal(t, 1, res.ExitCode())
131 assert.Contains(t, res.Stderr.String(), "this command must be run in online mode")
132 })
133
134 t.Run("routing findpeer", func(t *testing.T) {
135 res := node.RunIPFS("routing", "findpeer", testutils.CIDEmptyDir)
136 assert.Equal(t, 1, res.ExitCode())
137 assert.Contains(t, res.Stderr.String(), "this command must be run in online mode")
138 })
139
140 t.Run("routing put", func(t *testing.T) {
141 node.WriteBytes("foo", []byte("foo"))
142 res := node.RunIPFS("routing", "put", "/ipns/"+node.PeerID().String(), "foo")
143 assert.Equal(t, 1, res.ExitCode())
144 assert.Contains(t, res.Stderr.String(), "can't put while offline: pass `--allow-offline` to override")
145 })
146 })
147 })
148 }
149
150 func testSelfFindDHT(t *testing.T) {
151 t.Run("ipfs routing findpeer fails for self", func(t *testing.T) {
152 t.Parallel()
153 nodes := harness.NewT(t).NewNodes(1).Init()
154 nodes.ForEachPar(func(node *harness.Node) {
155 node.IPFS("config", "Routing.Type", "dht")
156 })
157
158 nodes.StartDaemons()
159 defer nodes.StopDaemons()
160
161 res := nodes[0].RunIPFS("dht", "findpeer", nodes[0].PeerID().String())
162 assert.Equal(t, 1, res.ExitCode())
163 })
164 }
165
166 func TestRoutingDHT(t *testing.T) {
167 testRoutingDHT(t, false)
168 testRoutingDHT(t, true)
169 testSelfFindDHT(t)
170 }