diff --git a/test/cli/provider_test.go b/test/cli/provider_test.go new file mode 100644 index 00000000000..b2d98e8822e --- /dev/null +++ b/test/cli/provider_test.go @@ -0,0 +1,148 @@ +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 TestProvider(t *testing.T) { + t.Parallel() + + initNodes := func(t *testing.T, n int, fn func(n *harness.Node)) harness.Nodes { + nodes := harness.NewT(t).NewNodes(n).Init() + nodes.ForEachPar(fn) + return nodes.StartDaemons().Connect() + } + + expectNoProviders := func(t *testing.T, cid string, nodes ...*harness.Node) { + for _, node := range nodes { + res := node.IPFS("routing", "findprovs", "-n=1", cid) + require.Empty(t, res.Stdout.String()) + } + } + + expectProviders := func(t *testing.T, cid, expectedProvider string, nodes ...*harness.Node) { + for _, node := range nodes { + res := node.IPFS("routing", "findprovs", "-n=1", cid) + require.Equal(t, expectedProvider, res.Stdout.Trimmed()) + } + } + + t.Run("Basic Providing", func(t *testing.T) { + t.Parallel() + + nodes := initNodes(t, 2, func(n *harness.Node) { + n.SetIPFSConfig("Experimental.StrategicProviding", false) + }) + defer nodes.StopDaemons() + + cid := nodes[0].IPFSAddStr(time.Now().String()) + expectProviders(t, cid, nodes[0].PeerID().String(), nodes[1:]...) + }) + + t.Run("Basic Strategic Providing", func(t *testing.T) { + t.Parallel() + + nodes := initNodes(t, 2, func(n *harness.Node) { + n.SetIPFSConfig("Experimental.StrategicProviding", true) + }) + defer nodes.StopDaemons() + + cid := nodes[0].IPFSAddStr(time.Now().String()) + expectNoProviders(t, cid, nodes[1:]...) + }) + + t.Run("Reprovides with 'all' strategy", func(t *testing.T) { + t.Parallel() + + nodes := initNodes(t, 2, func(n *harness.Node) { + n.SetIPFSConfig("Reprovider.Strategy", "all") + }) + defer nodes.StopDaemons() + + cid := nodes[0].IPFSAddStr(time.Now().String(), "--local") + + expectNoProviders(t, cid, nodes[1:]...) + + nodes[0].IPFS("bitswap", "reprovide") + + expectProviders(t, cid, nodes[0].PeerID().String(), nodes[1:]...) + }) + + t.Run("Reprovides with 'pinned' strategy", func(t *testing.T) { + t.Parallel() + + foo := testutils.RandomBytes(1000) + bar := testutils.RandomBytes(1000) + + nodes := initNodes(t, 2, func(n *harness.Node) { + n.SetIPFSConfig("Reprovider.Strategy", "pinned") + }) + 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, cidFoo, nodes[1:]...) + expectNoProviders(t, cidBar, nodes[1:]...) + expectNoProviders(t, cidBarDir, nodes[1:]...) + + nodes[0].IPFS("bitswap", "reprovide") + + expectNoProviders(t, cidFoo, nodes[1:]...) + expectProviders(t, cidBar, nodes[0].PeerID().String(), nodes[1:]...) + expectProviders(t, cidBarDir, nodes[0].PeerID().String(), nodes[1:]...) + }) + + t.Run("Reprovides with 'roots' strategy", func(t *testing.T) { + t.Parallel() + + foo := testutils.RandomBytes(1000) + bar := testutils.RandomBytes(1000) + baz := testutils.RandomBytes(1000) + + nodes := initNodes(t, 2, func(n *harness.Node) { + n.SetIPFSConfig("Reprovider.Strategy", "roots") + }) + 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, cidFoo, nodes[1:]...) + expectNoProviders(t, cidBar, nodes[1:]...) + expectNoProviders(t, cidBarDir, nodes[1:]...) + + nodes[0].IPFS("bitswap", "reprovide") + + expectNoProviders(t, cidFoo, nodes[1:]...) + expectNoProviders(t, cidBar, nodes[1:]...) + expectProviders(t, cidBaz, nodes[0].PeerID().String(), nodes[1:]...) + expectProviders(t, cidBarDir, nodes[0].PeerID().String(), nodes[1:]...) + }) + + t.Run("Providing works without ticking", func(t *testing.T) { + t.Parallel() + + nodes := initNodes(t, 2, func(n *harness.Node) { + n.SetIPFSConfig("Reprovider.Interval", "0") + }) + defer nodes.StopDaemons() + + cid := nodes[0].IPFSAddStr(time.Now().String(), "--offline") + + expectNoProviders(t, cid, nodes[1:]...) + + nodes[0].IPFS("bitswap", "reprovide") + + expectProviders(t, cid, nodes[0].PeerID().String(), nodes[1:]...) + }) +} diff --git a/test/sharness/t0175-provider.sh b/test/sharness/t0175-provider.sh deleted file mode 100755 index cca110fe101..00000000000 --- a/test/sharness/t0175-provider.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env bash - -test_description="Test reprovider" - -. lib/test-lib.sh - -NUM_NODES=2 - -test_expect_success 'init iptb' ' - iptb testbed create -type localipfs -force -count $NUM_NODES -init -' - -test_expect_success 'peer ids' ' - PEERID_0=$(iptb attr get 0 id) && - PEERID_1=$(iptb attr get 1 id) -' - -test_expect_success 'use strategic providing' ' - iptb run -- ipfs config --json Experimental.StrategicProviding false -' - -startup_cluster ${NUM_NODES} - -test_expect_success 'add test object' ' - HASH_0=$(date +"%FT%T.%N%z" | ipfsi 0 add -q) -' - -findprovs_expect '$HASH_0' '$PEERID_0' - -test_expect_success 'stop node 1' ' - iptb stop -' - -test_done diff --git a/test/sharness/t0175-reprovider.sh b/test/sharness/t0175-reprovider.sh deleted file mode 100755 index 09535ecc4f5..00000000000 --- a/test/sharness/t0175-reprovider.sh +++ /dev/null @@ -1,140 +0,0 @@ -#!/usr/bin/env bash - -test_description="Test reprovider" - -. lib/test-lib.sh - -NUM_NODES=6 - -init_strategy() { - test_expect_success 'init iptb' ' - iptb testbed create -type localipfs -force -count $NUM_NODES -init - ' - - test_expect_success 'peer ids' ' - PEERID_0=$(iptb attr get 0 id) && - PEERID_1=$(iptb attr get 1 id) - ' - - test_expect_success 'use pinning strategy for reprovider' ' - ipfsi 0 config Reprovider.Strategy '$1' - ' - - startup_cluster ${NUM_NODES} -} - -reprovide() { - test_expect_success 'reprovide' ' - # TODO: this hangs, though only after reprovision was done - ipfsi 0 bitswap reprovide - ' -} - -# Test 'all' strategy -init_strategy 'all' - -test_expect_success 'add test object' ' - HASH_0=$(date +"%FT%T.%N%z" | ipfsi 0 add -q --local) -' - -findprovs_empty '$HASH_0' -reprovide -findprovs_expect '$HASH_0' '$PEERID_0' - -test_expect_success 'Stop iptb' ' - iptb stop -' - -# Test 'pinned' strategy -init_strategy 'pinned' - -test_expect_success 'prepare test files' ' - date +"%FT%T.%N%z" > f1 && - date +"%FT%T.%N%z" > f2 -' - -test_expect_success 'add test objects' ' - HASH_FOO=$(ipfsi 0 add -q --offline --pin=false f1) && - HASH_BAR=$(ipfsi 0 add -q --offline --pin=false f2) && - HASH_BAR_DIR=$(ipfsi 0 add -q --offline -w f2) -' - -findprovs_empty '$HASH_FOO' -findprovs_empty '$HASH_BAR' -findprovs_empty '$HASH_BAR_DIR' - -reprovide - -findprovs_empty '$HASH_FOO' -findprovs_expect '$HASH_BAR' '$PEERID_0' -findprovs_expect '$HASH_BAR_DIR' '$PEERID_0' - -test_expect_success 'Stop iptb' ' - iptb stop -' - -# Test 'roots' strategy -init_strategy 'roots' - -test_expect_success 'prepare test files' ' - date +"%FT%T.%N%z" > f1 && - date +"%FT%T.%N%z" > f2 && - date +"%FT%T.%N%z" > f3 -' - -test_expect_success 'add test objects' ' - HASH_FOO=$(ipfsi 0 add -q --offline --pin=false f1) && - HASH_BAR=$(ipfsi 0 add -q --offline --pin=false f2) && - HASH_BAZ=$(ipfsi 0 add -q --offline f3) && - HASH_BAR_DIR=$(ipfsi 0 add -Q --offline -w f2) -' - -findprovs_empty '$HASH_FOO' -findprovs_empty '$HASH_BAR' -findprovs_empty '$HASH_BAR_DIR' - -reprovide - -findprovs_empty '$HASH_FOO' -findprovs_empty '$HASH_BAR' -findprovs_expect '$HASH_BAZ' '$PEERID_0' -findprovs_expect '$HASH_BAR_DIR' '$PEERID_0' - -test_expect_success 'Stop iptb' ' - iptb stop -' - -# Test reprovider working with ticking disabled -test_expect_success 'init iptb' ' - iptb testbed create -type localipfs -force -count $NUM_NODES -init -' - -test_expect_success 'peer ids' ' - PEERID_0=$(iptb attr get 0 id) && - PEERID_1=$(iptb attr get 1 id) -' - -test_expect_success 'Disable reprovider ticking' ' - ipfsi 0 config Reprovider.Interval 0 -' - -startup_cluster ${NUM_NODES} - -test_expect_success 'add test object' ' - HASH_0=$(date +"%FT%T.%N%z" | ipfsi 0 add -q --offline) -' - -findprovs_empty '$HASH_0' -reprovide -findprovs_expect '$HASH_0' '$PEERID_0' - -test_expect_success 'resolve object $HASH_0' ' - HASH_WITH_PREFIX=$(ipfsi 1 resolve $HASH_0) -' -findprovs_expect '$HASH_WITH_PREFIX' '$PEERID_0' - -test_expect_success 'Stop iptb' ' - iptb stop -' - -test_done diff --git a/test/sharness/t0175-strategic-provider.sh b/test/sharness/t0175-strategic-provider.sh deleted file mode 100755 index fafd6e5388c..00000000000 --- a/test/sharness/t0175-strategic-provider.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env bash - -test_description="Test reprovider" - -. lib/test-lib.sh - -NUM_NODES=2 - -test_expect_success 'init iptb' ' - iptb testbed create -type localipfs -force -count $NUM_NODES -init -' - -test_expect_success 'peer ids' ' - PEERID_0=$(iptb attr get 0 id) && - PEERID_1=$(iptb attr get 1 id) -' - -test_expect_success 'use strategic providing' ' - iptb run -- ipfs config --json Experimental.StrategicProviding true -' - -startup_cluster ${NUM_NODES} - -test_expect_success 'add test object' ' - HASH_0=$(date +"%FT%T.%N%z" | ipfsi 0 add -q) -' - -findprovs_empty '$HASH_0' - -test_expect_success 'stop node 1' ' - iptb stop -' - -test_done