Skip to content

Commit

Permalink
refactor: reprovider sharness into Go tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Mar 22, 2024
1 parent 9047fed commit 926744f
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 140 deletions.
115 changes: 115 additions & 0 deletions test/cli/reprovider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package cli

import (
"bytes"
"testing"
"time"

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

func TestReprovider(t *testing.T) {
t.Parallel()

initNodes := func(t *testing.T, strategy string, noInterval bool) harness.Nodes {
nodes := harness.NewT(t).NewNodes(2).Init()
nodes.ForEachPar(func(n *harness.Node) {
n.SetIPFSConfig("Reprovider.Strategy", strategy)

if noInterval {
n.SetIPFSConfig("Reprovider.Interval", "0")
}
})
return nodes.StartDaemons().Connect()
}

expectNoProviders := func(t *testing.T, node *harness.Node, cid string) {
res := node.IPFS("routing", "findprovs", "-n=1", cid)
require.Empty(t, res.Stdout.String())
}

expectProviders := func(t *testing.T, node *harness.Node, cid, expected string) {
res := node.IPFS("routing", "findprovs", "-n=1", cid)
require.Equal(t, expected+"\n", res.Stdout.String())
}

t.Run("Test 'all' strategy", func(t *testing.T) {
t.Parallel()

nodes := initNodes(t, "all", false)
defer nodes.StopDaemons()

cid := nodes[0].IPFSAddStr(time.Now().String(), "--local")

expectNoProviders(t, nodes[1], cid)
nodes[0].IPFS("bitswap", "reprovide")

expectProviders(t, nodes[1], cid, nodes[0].PeerID().String())
})

t.Run("Test 'pinned' strategy", func(t *testing.T) {
t.Parallel()

foo := testutils.RandomBytes(1000)
bar := testutils.RandomBytes(1000)

nodes := initNodes(t, "pinned", false)
defer nodes.StopDaemons()

cidFoo := nodes[0].IPFSAdd(bytes.NewReader(foo), "--offline", "--pin=false")
cidBar := nodes[0].IPFSAdd(bytes.NewReader(bar), "--offline", "--pin=false")
cidBarDir := nodes[0].IPFSAdd(bytes.NewReader(bar), "-Q", "--offline", "-w")

expectNoProviders(t, nodes[1], cidFoo)
expectNoProviders(t, nodes[1], cidBar)
expectNoProviders(t, nodes[1], cidBarDir)

nodes[0].IPFS("bitswap", "reprovide")

expectNoProviders(t, nodes[1], cidFoo)
expectProviders(t, nodes[1], cidBar, nodes[0].PeerID().String())
expectProviders(t, nodes[1], cidBarDir, nodes[0].PeerID().String())
})

t.Run("Test 'roots' strategy", func(t *testing.T) {
t.Parallel()

foo := testutils.RandomBytes(1000)
bar := testutils.RandomBytes(1000)
baz := testutils.RandomBytes(1000)

nodes := initNodes(t, "roots", false)
defer nodes.StopDaemons()

cidFoo := nodes[0].IPFSAdd(bytes.NewReader(foo), "--offline", "--pin=false")
cidBar := nodes[0].IPFSAdd(bytes.NewReader(bar), "--offline", "--pin=false")
cidBaz := nodes[0].IPFSAdd(bytes.NewReader(baz), "--offline")
cidBarDir := nodes[0].IPFSAdd(bytes.NewReader(bar), "-Q", "--offline", "-w")

expectNoProviders(t, nodes[1], cidFoo)
expectNoProviders(t, nodes[1], cidBar)
expectNoProviders(t, nodes[1], cidBarDir)

nodes[0].IPFS("bitswap", "reprovide")

expectNoProviders(t, nodes[1], cidFoo)
expectNoProviders(t, nodes[1], cidBar)
expectProviders(t, nodes[1], cidBaz, nodes[0].PeerID().String())
expectProviders(t, nodes[1], cidBarDir, nodes[0].PeerID().String())
})

t.Run("Test reprovider working with ticking disabled", func(t *testing.T) {
t.Parallel()

nodes := initNodes(t, "all", true)
defer nodes.StopDaemons()

cid := nodes[0].IPFSAddStr(time.Now().String(), "--offline")

expectNoProviders(t, nodes[1], cid)
nodes[0].IPFS("bitswap", "reprovide")
expectProviders(t, nodes[1], cid, nodes[0].PeerID().String())
})
}
140 changes: 0 additions & 140 deletions test/sharness/t0175-reprovider.sh

This file was deleted.

0 comments on commit 926744f

Please sign in to comment.