| 1 | package autoconf |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | |
| 7 | "github.com/ipfs/kubo/test/cli/harness" |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | "github.com/stretchr/testify/require" |
| 10 | ) |
| 11 | |
| 12 | // TestSwarmConnectWithAutoConf tests that ipfs swarm connect works properly |
| 13 | // when AutoConf is enabled and a daemon is running. |
| 14 | // |
| 15 | // This is a regression test for the issue where: |
| 16 | // - AutoConf disabled: ipfs swarm connect works |
| 17 | // - AutoConf enabled: ipfs swarm connect fails with "Error: connect" |
| 18 | // |
| 19 | // The issue affects CLI command fallback behavior when the HTTP API connection fails. |
| 20 | func TestSwarmConnectWithAutoConf(t *testing.T) { |
| 21 | t.Parallel() |
| 22 | |
| 23 | t.Run("AutoConf disabled - should work", func(t *testing.T) { |
| 24 | testSwarmConnectWithAutoConfSetting(t, false, true) // expect success |
| 25 | }) |
| 26 | |
| 27 | t.Run("AutoConf enabled - should work", func(t *testing.T) { |
| 28 | testSwarmConnectWithAutoConfSetting(t, true, true) // expect success (fix the bug!) |
| 29 | }) |
| 30 | } |
| 31 | |
| 32 | func testSwarmConnectWithAutoConfSetting(t *testing.T, autoConfEnabled bool, expectSuccess bool) { |
| 33 | // Create IPFS node with test profile |
| 34 | node := harness.NewT(t).NewNode().Init("--profile=test") |
| 35 | |
| 36 | // Configure AutoConf |
| 37 | node.SetIPFSConfig("AutoConf.Enabled", autoConfEnabled) |
| 38 | |
| 39 | // Set up bootstrap peers so the node has something to connect to |
| 40 | // Use the same bootstrap peers from boxo/autoconf fallbacks |
| 41 | node.SetIPFSConfig("Bootstrap", []string{ |
| 42 | "/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", |
| 43 | "/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa", |
| 44 | "/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb", |
| 45 | }) |
| 46 | |
| 47 | // CRITICAL: Start the daemon first - this is the key requirement |
| 48 | // The daemon must be running and working properly |
| 49 | node.StartDaemon() |
| 50 | defer node.StopDaemon() |
| 51 | |
| 52 | // Give daemon time to start up completely |
| 53 | time.Sleep(3 * time.Second) |
| 54 | |
| 55 | // Verify daemon is responsive |
| 56 | result := node.RunIPFS("id") |
| 57 | require.Equal(t, 0, result.ExitCode(), "Daemon should be responsive before testing swarm connect") |
| 58 | t.Logf("Daemon is running and responsive. AutoConf enabled: %v", autoConfEnabled) |
| 59 | |
| 60 | // Now test swarm connect to a bootstrap peer |
| 61 | // This should work because: |
| 62 | // 1. The daemon is running |
| 63 | // 2. The CLI should connect to the daemon via API |
| 64 | // 3. The daemon should handle the swarm connect request |
| 65 | result = node.RunIPFS("swarm", "connect", "/dnsaddr/bootstrap.libp2p.io") |
| 66 | |
| 67 | // swarm connect should work regardless of AutoConf setting |
| 68 | assert.Equal(t, 0, result.ExitCode(), |
| 69 | "swarm connect should succeed with AutoConf=%v. stderr: %s", |
| 70 | autoConfEnabled, result.Stderr.String()) |
| 71 | |
| 72 | // Should contain success message |
| 73 | output := result.Stdout.String() |
| 74 | assert.Contains(t, output, "success", |
| 75 | "swarm connect output should contain 'success' with AutoConf=%v. output: %s", |
| 76 | autoConfEnabled, output) |
| 77 | |
| 78 | // Additional diagnostic: Check if ipfs id shows addresses |
| 79 | // Both AutoConf enabled and disabled should show proper addresses |
| 80 | result = node.RunIPFS("id") |
| 81 | require.Equal(t, 0, result.ExitCode(), "ipfs id should work with AutoConf=%v", autoConfEnabled) |
| 82 | |
| 83 | idOutput := result.Stdout.String() |
| 84 | t.Logf("ipfs id output with AutoConf=%v: %s", autoConfEnabled, idOutput) |
| 85 | |
| 86 | // Addresses should not be null regardless of AutoConf setting |
| 87 | assert.Contains(t, idOutput, `"Addresses"`, "ipfs id should show Addresses field") |
| 88 | assert.NotContains(t, idOutput, `"Addresses": null`, |
| 89 | "ipfs id should not show null addresses with AutoConf=%v", autoConfEnabled) |
| 90 | } |