| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/ipfs/kubo/test/cli/harness" |
| 7 | "github.com/stretchr/testify/assert" |
| 8 | "github.com/stretchr/testify/require" |
| 9 | ) |
| 10 | |
| 11 | func TestBootstrapCommandsWithAutoPlaceholder(t *testing.T) { |
| 12 | t.Parallel() |
| 13 | |
| 14 | t.Run("bootstrap add default", func(t *testing.T) { |
| 15 | t.Parallel() |
| 16 | // Test that 'ipfs bootstrap add default' works correctly |
| 17 | node := harness.NewT(t).NewNode().Init("--profile=test") |
| 18 | node.SetIPFSConfig("AutoConf.Enabled", true) |
| 19 | node.SetIPFSConfig("Bootstrap", []string{}) // Start with empty bootstrap |
| 20 | |
| 21 | // Add default bootstrap peers via "auto" placeholder |
| 22 | result := node.RunIPFS("bootstrap", "add", "default") |
| 23 | require.Equal(t, 0, result.ExitCode(), "bootstrap add default should succeed") |
| 24 | |
| 25 | output := result.Stdout.String() |
| 26 | t.Logf("Bootstrap add default output: %s", output) |
| 27 | assert.Contains(t, output, "added auto", "bootstrap add default should report adding 'auto'") |
| 28 | |
| 29 | // Verify bootstrap list shows "auto" |
| 30 | listResult := node.RunIPFS("bootstrap", "list") |
| 31 | require.Equal(t, 0, listResult.ExitCode(), "bootstrap list should succeed") |
| 32 | |
| 33 | listOutput := listResult.Stdout.String() |
| 34 | t.Logf("Bootstrap list after add default: %s", listOutput) |
| 35 | assert.Contains(t, listOutput, "auto", "bootstrap list should show 'auto' placeholder") |
| 36 | }) |
| 37 | |
| 38 | t.Run("bootstrap add auto explicitly", func(t *testing.T) { |
| 39 | t.Parallel() |
| 40 | // Test that 'ipfs bootstrap add auto' works correctly |
| 41 | node := harness.NewT(t).NewNode().Init("--profile=test") |
| 42 | node.SetIPFSConfig("AutoConf.Enabled", true) |
| 43 | node.SetIPFSConfig("Bootstrap", []string{}) // Start with empty bootstrap |
| 44 | |
| 45 | // Add "auto" placeholder explicitly |
| 46 | result := node.RunIPFS("bootstrap", "add", "auto") |
| 47 | require.Equal(t, 0, result.ExitCode(), "bootstrap add auto should succeed") |
| 48 | |
| 49 | output := result.Stdout.String() |
| 50 | t.Logf("Bootstrap add auto output: %s", output) |
| 51 | assert.Contains(t, output, "added auto", "bootstrap add auto should report adding 'auto'") |
| 52 | |
| 53 | // Verify bootstrap list shows "auto" |
| 54 | listResult := node.RunIPFS("bootstrap", "list") |
| 55 | require.Equal(t, 0, listResult.ExitCode(), "bootstrap list should succeed") |
| 56 | |
| 57 | listOutput := listResult.Stdout.String() |
| 58 | t.Logf("Bootstrap list after add auto: %s", listOutput) |
| 59 | assert.Contains(t, listOutput, "auto", "bootstrap list should show 'auto' placeholder") |
| 60 | }) |
| 61 | |
| 62 | t.Run("bootstrap add default converts to auto", func(t *testing.T) { |
| 63 | t.Parallel() |
| 64 | // Test that 'ipfs bootstrap add default' adds "auto" to the bootstrap list |
| 65 | node := harness.NewT(t).NewNode().Init("--profile=test") |
| 66 | node.SetIPFSConfig("Bootstrap", []string{}) // Start with empty bootstrap |
| 67 | node.SetIPFSConfig("AutoConf.Enabled", true) // Enable AutoConf to allow adding "auto" |
| 68 | |
| 69 | // Add default bootstrap peers |
| 70 | result := node.RunIPFS("bootstrap", "add", "default") |
| 71 | require.Equal(t, 0, result.ExitCode(), "bootstrap add default should succeed") |
| 72 | assert.Contains(t, result.Stdout.String(), "added auto", "should report adding 'auto'") |
| 73 | |
| 74 | // Verify bootstrap list shows "auto" |
| 75 | var bootstrap []string |
| 76 | node.GetIPFSConfig("Bootstrap", &bootstrap) |
| 77 | require.Equal(t, []string{"auto"}, bootstrap, "Bootstrap should contain ['auto']") |
| 78 | }) |
| 79 | |
| 80 | t.Run("bootstrap add default fails when AutoConf disabled", func(t *testing.T) { |
| 81 | t.Parallel() |
| 82 | // Test that adding default/auto fails when AutoConf is disabled |
| 83 | node := harness.NewT(t).NewNode().Init("--profile=test") |
| 84 | node.SetIPFSConfig("Bootstrap", []string{}) // Start with empty bootstrap |
| 85 | node.SetIPFSConfig("AutoConf.Enabled", false) // Disable AutoConf |
| 86 | |
| 87 | // Try to add default - should fail |
| 88 | result := node.RunIPFS("bootstrap", "add", "default") |
| 89 | require.NotEqual(t, 0, result.ExitCode(), "bootstrap add default should fail when AutoConf disabled") |
| 90 | assert.Contains(t, result.Stderr.String(), "AutoConf is disabled", "should mention AutoConf is disabled") |
| 91 | |
| 92 | // Try to add auto - should also fail |
| 93 | result = node.RunIPFS("bootstrap", "add", "auto") |
| 94 | require.NotEqual(t, 0, result.ExitCode(), "bootstrap add auto should fail when AutoConf disabled") |
| 95 | assert.Contains(t, result.Stderr.String(), "AutoConf is disabled", "should mention AutoConf is disabled") |
| 96 | }) |
| 97 | |
| 98 | t.Run("bootstrap rm with auto placeholder", func(t *testing.T) { |
| 99 | t.Parallel() |
| 100 | // Test that selective removal fails properly when "auto" is present |
| 101 | node := harness.NewT(t).NewNode().Init("--profile=test") |
| 102 | node.SetIPFSConfig("AutoConf.Enabled", true) |
| 103 | node.SetIPFSConfig("Bootstrap", []string{"auto"}) // Start with auto |
| 104 | |
| 105 | // Try to remove a specific peer - should fail with helpful error |
| 106 | result := node.RunIPFS("bootstrap", "rm", "/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN") |
| 107 | require.NotEqual(t, 0, result.ExitCode(), "bootstrap rm of specific peer should fail when 'auto' is present") |
| 108 | |
| 109 | output := result.Stderr.String() |
| 110 | t.Logf("Bootstrap rm error output: %s", output) |
| 111 | assert.Contains(t, output, "cannot remove individual bootstrap peers when using 'auto' placeholder", |
| 112 | "should provide helpful error message about auto placeholder") |
| 113 | assert.Contains(t, output, "disable AutoConf", |
| 114 | "should suggest disabling AutoConf as solution") |
| 115 | assert.Contains(t, output, "ipfs bootstrap rm --all", |
| 116 | "should suggest using rm --all as alternative") |
| 117 | }) |
| 118 | |
| 119 | t.Run("bootstrap rm --all with auto placeholder", func(t *testing.T) { |
| 120 | t.Parallel() |
| 121 | // Test that 'ipfs bootstrap rm --all' works with "auto" placeholder |
| 122 | node := harness.NewT(t).NewNode().Init("--profile=test") |
| 123 | node.SetIPFSConfig("AutoConf.Enabled", true) |
| 124 | node.SetIPFSConfig("Bootstrap", []string{"auto"}) // Start with auto |
| 125 | |
| 126 | // Remove all bootstrap peers |
| 127 | result := node.RunIPFS("bootstrap", "rm", "--all") |
| 128 | require.Equal(t, 0, result.ExitCode(), "bootstrap rm --all should succeed with auto placeholder") |
| 129 | |
| 130 | output := result.Stdout.String() |
| 131 | t.Logf("Bootstrap rm --all output: %s", output) |
| 132 | assert.Contains(t, output, "removed auto", "bootstrap rm --all should report removing 'auto'") |
| 133 | |
| 134 | // Verify bootstrap list is now empty |
| 135 | listResult := node.RunIPFS("bootstrap", "list") |
| 136 | require.Equal(t, 0, listResult.ExitCode(), "bootstrap list should succeed") |
| 137 | |
| 138 | listOutput := listResult.Stdout.String() |
| 139 | t.Logf("Bootstrap list after rm --all: %s", listOutput) |
| 140 | assert.Empty(t, listOutput, "bootstrap list should be empty after rm --all") |
| 141 | |
| 142 | // Test the rm all subcommand too |
| 143 | node.SetIPFSConfig("Bootstrap", []string{"auto"}) // Reset to auto |
| 144 | |
| 145 | result = node.RunIPFS("bootstrap", "rm", "all") |
| 146 | require.Equal(t, 0, result.ExitCode(), "bootstrap rm all should succeed with auto placeholder") |
| 147 | |
| 148 | output = result.Stdout.String() |
| 149 | t.Logf("Bootstrap rm all output: %s", output) |
| 150 | assert.Contains(t, output, "removed auto", "bootstrap rm all should report removing 'auto'") |
| 151 | }) |
| 152 | |
| 153 | t.Run("bootstrap mixed auto and specific peers", func(t *testing.T) { |
| 154 | t.Parallel() |
| 155 | // Test that bootstrap commands work when mixing "auto" with specific peers |
| 156 | node := harness.NewT(t).NewNode().Init("--profile=test") |
| 157 | node.SetIPFSConfig("AutoConf.Enabled", true) |
| 158 | node.SetIPFSConfig("Bootstrap", []string{}) // Start with empty bootstrap |
| 159 | |
| 160 | // Add a specific peer first |
| 161 | specificPeer := "/ip4/127.0.0.1/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" |
| 162 | result := node.RunIPFS("bootstrap", "add", specificPeer) |
| 163 | require.Equal(t, 0, result.ExitCode(), "bootstrap add specific peer should succeed") |
| 164 | |
| 165 | // Add auto placeholder |
| 166 | result = node.RunIPFS("bootstrap", "add", "auto") |
| 167 | require.Equal(t, 0, result.ExitCode(), "bootstrap add auto should succeed") |
| 168 | |
| 169 | // Verify bootstrap list shows both |
| 170 | listResult := node.RunIPFS("bootstrap", "list") |
| 171 | require.Equal(t, 0, listResult.ExitCode(), "bootstrap list should succeed") |
| 172 | |
| 173 | listOutput := listResult.Stdout.String() |
| 174 | t.Logf("Bootstrap list with mixed peers: %s", listOutput) |
| 175 | assert.Contains(t, listOutput, "auto", "bootstrap list should contain 'auto' placeholder") |
| 176 | assert.Contains(t, listOutput, specificPeer, "bootstrap list should contain specific peer") |
| 177 | |
| 178 | // Try to remove the specific peer - should fail because auto is present |
| 179 | result = node.RunIPFS("bootstrap", "rm", specificPeer) |
| 180 | require.NotEqual(t, 0, result.ExitCode(), "bootstrap rm of specific peer should fail when 'auto' is present") |
| 181 | |
| 182 | output := result.Stderr.String() |
| 183 | assert.Contains(t, output, "cannot remove individual bootstrap peers when using 'auto' placeholder", |
| 184 | "should provide helpful error message about auto placeholder") |
| 185 | |
| 186 | // Remove all should work and remove both auto and specific peer |
| 187 | result = node.RunIPFS("bootstrap", "rm", "--all") |
| 188 | require.Equal(t, 0, result.ExitCode(), "bootstrap rm --all should succeed") |
| 189 | |
| 190 | output = result.Stdout.String() |
| 191 | t.Logf("Bootstrap rm --all output with mixed peers: %s", output) |
| 192 | // Should report removing both the specific peer and auto |
| 193 | assert.Contains(t, output, "removed", "should report removing peers") |
| 194 | |
| 195 | // Verify bootstrap list is now empty |
| 196 | listResult = node.RunIPFS("bootstrap", "list") |
| 197 | require.Equal(t, 0, listResult.ExitCode(), "bootstrap list should succeed") |
| 198 | |
| 199 | listOutput = listResult.Stdout.String() |
| 200 | assert.Empty(t, listOutput, "bootstrap list should be empty after rm --all") |
| 201 | }) |
| 202 | } |