| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/ipfs/kubo/config" |
| 9 | "github.com/ipfs/kubo/test/cli/harness" |
| 10 | "github.com/stretchr/testify/assert" |
| 11 | ) |
| 12 | |
| 13 | func TestBackupBootstrapPeers(t *testing.T) { |
| 14 | nodes := harness.NewT(t).NewNodes(3).Init() |
| 15 | nodes.ForEachPar(func(n *harness.Node) { |
| 16 | n.UpdateConfig(func(cfg *config.Config) { |
| 17 | cfg.Bootstrap = []string{} |
| 18 | cfg.Addresses.Swarm = []string{fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", harness.NewRandPort())} |
| 19 | cfg.Discovery.MDNS.Enabled = false |
| 20 | cfg.Internal.BackupBootstrapInterval = config.NewOptionalDuration(250 * time.Millisecond) |
| 21 | }) |
| 22 | }) |
| 23 | |
| 24 | // Start all nodes and ensure they all have no peers. |
| 25 | nodes.StartDaemons() |
| 26 | nodes.ForEachPar(func(n *harness.Node) { |
| 27 | assert.Len(t, n.Peers(), 0) |
| 28 | }) |
| 29 | |
| 30 | // Connect nodes 0 and 1, ensure they know each other. |
| 31 | nodes[0].Connect(nodes[1]) |
| 32 | assert.Len(t, nodes[0].Peers(), 1) |
| 33 | assert.Len(t, nodes[1].Peers(), 1) |
| 34 | assert.Len(t, nodes[2].Peers(), 0) |
| 35 | |
| 36 | // Wait a bit to ensure that 0 and 1 saved their temporary bootstrap backups. |
| 37 | time.Sleep(time.Millisecond * 500) |
| 38 | nodes.StopDaemons() |
| 39 | |
| 40 | // Start 1 and 2. 2 does not know anyone yet. |
| 41 | nodes[1].StartDaemon() |
| 42 | defer nodes[1].StopDaemon() |
| 43 | nodes[2].StartDaemon() |
| 44 | defer nodes[2].StopDaemon() |
| 45 | assert.Len(t, nodes[1].Peers(), 0) |
| 46 | assert.Len(t, nodes[2].Peers(), 0) |
| 47 | |
| 48 | // Connect 1 and 2, ensure they know each other. |
| 49 | nodes[1].Connect(nodes[2]) |
| 50 | assert.Len(t, nodes[1].Peers(), 1) |
| 51 | assert.Len(t, nodes[2].Peers(), 1) |
| 52 | |
| 53 | // Start 0, wait a bit. Should connect to 1, and then discover 2 via the |
| 54 | // backup bootstrap peers. |
| 55 | nodes[0].StartDaemon() |
| 56 | defer nodes[0].StopDaemon() |
| 57 | time.Sleep(time.Millisecond * 500) |
| 58 | |
| 59 | // Check if they're all connected. |
| 60 | assert.Len(t, nodes[0].Peers(), 2) |
| 61 | assert.Len(t, nodes[1].Peers(), 2) |
| 62 | assert.Len(t, nodes[2].Peers(), 2) |
| 63 | } |