Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: port routing DHT tests to Go #9709

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/cli/dht_legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

func TestLegacyDHT(t *testing.T) {
t.Parallel()
nodes := harness.NewT(t).NewNodes(5).Init()
nodes.ForEachPar(func(node *harness.Node) {
node.IPFS("config", "Routing.Type", "dht")
Expand Down
4 changes: 2 additions & 2 deletions test/cli/harness/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (n Nodes) Connect() Nodes {
return n
}

func (n Nodes) StartDaemons() Nodes {
ForEachPar(n, func(node *Node) { node.StartDaemon() })
func (n Nodes) StartDaemons(args ...string) Nodes {
ForEachPar(n, func(node *Node) { node.StartDaemon(args...) })
return n
}

Expand Down
123 changes: 123 additions & 0 deletions test/cli/routing_dht_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package cli

import (
"fmt"
"testing"

"github.com/ipfs/kubo/test/cli/harness"
"github.com/ipfs/kubo/test/cli/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func testRoutingDHT(t *testing.T, enablePubsub bool) {
t.Run(fmt.Sprintf("enablePubSub=%v", enablePubsub), func(t *testing.T) {
t.Parallel()
nodes := harness.NewT(t).NewNodes(5).Init()
nodes.ForEachPar(func(node *harness.Node) {
node.IPFS("config", "Routing.Type", "dht")
})

var daemonArgs []string
if enablePubsub {
daemonArgs = []string{
"--enable-pubsub-experiment",
"--enable-namesys-pubsub",
}
}

nodes.StartDaemons(daemonArgs...).Connect()

t.Run("ipfs routing findpeer", func(t *testing.T) {
t.Parallel()
res := nodes[1].RunIPFS("routing", "findpeer", nodes[0].PeerID().String())
assert.Equal(t, 0, res.ExitCode())

swarmAddr := nodes[0].SwarmAddrsWithoutPeerIDs()[0]
require.Equal(t, swarmAddr.String(), res.Stdout.Trimmed())
})

t.Run("ipfs routing get <key>", func(t *testing.T) {
t.Parallel()
hash := nodes[2].IPFSAddStr("hello world")
nodes[2].IPFS("name", "publish", "/ipfs/"+hash)

res := nodes[1].IPFS("routing", "get", "/ipns/"+nodes[2].PeerID().String())
assert.Contains(t, res.Stdout.String(), "/ipfs/"+hash)

t.Run("put round trips (#3124)", func(t *testing.T) {
t.Parallel()
nodes[0].WriteBytes("get_result", res.Stdout.Bytes())
res := nodes[0].IPFS("routing", "put", "/ipns/"+nodes[2].PeerID().String(), "get_result")
assert.Greater(t, len(res.Stdout.Lines()), 0, "should put to at least one node")
})

t.Run("put with bad keys fails (issue #5113, #4611)", func(t *testing.T) {
t.Parallel()
keys := []string{"foo", "/pk/foo", "/ipns/foo"}
for _, key := range keys {
key := key
t.Run(key, func(t *testing.T) {
t.Parallel()
res := nodes[0].RunIPFS("routing", "put", key)
assert.Equal(t, 1, res.ExitCode())
assert.Contains(t, res.Stderr.String(), "invalid")
assert.Empty(t, res.Stdout.String())
})
}
})

t.Run("get with bad keys (issue #4611)", func(t *testing.T) {
for _, key := range []string{"foo", "/pk/foo"} {
key := key
t.Run(key, func(t *testing.T) {
t.Parallel()
res := nodes[0].RunIPFS("routing", "get", key)
assert.Equal(t, 1, res.ExitCode())
assert.Contains(t, res.Stderr.String(), "invalid")
assert.Empty(t, res.Stdout.String())
})
}
})
})

t.Run("ipfs routing findprovs", func(t *testing.T) {
t.Parallel()
hash := nodes[3].IPFSAddStr("some stuff")
res := nodes[4].IPFS("routing", "findprovs", hash)
assert.Equal(t, nodes[3].PeerID().String(), res.Stdout.Trimmed())
})

t.Run("routing commands fail when offline", func(t *testing.T) {
t.Parallel()
node := harness.NewT(t).NewNode().Init()

// these cannot be run in parallel due to repo locking
// this seems like a bug, we should be able to run these without locking the repo

t.Run("routing findprovs", func(t *testing.T) {
res := node.RunIPFS("routing", "findprovs", testutils.CIDEmptyDir)
assert.Equal(t, 1, res.ExitCode())
assert.Contains(t, res.Stderr.String(), "this command must be run in online mode")
})

t.Run("routing findpeer", func(t *testing.T) {
res := node.RunIPFS("routing", "findpeer", testutils.CIDEmptyDir)
assert.Equal(t, 1, res.ExitCode())
assert.Contains(t, res.Stderr.String(), "this command must be run in online mode")
})

t.Run("routing put", func(t *testing.T) {
node.WriteBytes("foo", []byte("foo"))
res := node.RunIPFS("routing", "put", "/ipns/"+node.PeerID().String(), "foo")
assert.Equal(t, 1, res.ExitCode())
assert.Contains(t, res.Stderr.String(), "this action must be run in online mode")
})
})
})
}

func TestRoutingDHT(t *testing.T) {
testRoutingDHT(t, false)
testRoutingDHT(t, true)
}
128 changes: 0 additions & 128 deletions test/sharness/t0170-routing-dht.sh

This file was deleted.