|
48 | 48 | WorkspaceTmpDir bool |
49 | 49 | OnlyOutTestToml bool |
50 | 50 | Subset bool |
| 51 | + DebugSandbox bool |
51 | 52 | ) |
52 | 53 |
|
53 | 54 | // In order to debug CLI running under acceptance test, search for TestInprocessMode and update |
@@ -80,6 +81,7 @@ func init() { |
80 | 81 | flag.BoolVar(&WorkspaceTmpDir, "workspace-tmp-dir", false, "Run tests on the workspace file system (For DBR testing).") |
81 | 82 | flag.BoolVar(&OnlyOutTestToml, "only-out-test-toml", false, "Only regenerate out.test.toml files without running tests") |
82 | 83 | flag.BoolVar(&Subset, "subset", false, "Select a subset of EnvMatrix variants that cover all output files. Auto-enabled on -update (unless -run specifies a variant with '=').") |
| 84 | + flag.BoolVar(&DebugSandbox, "debugsandbox", false, "Use a per-test blocking proxy instead of a shared one; shows exactly which test caused unexpected internet access (slower)") |
83 | 85 | } |
84 | 86 |
|
85 | 87 | const ( |
@@ -328,6 +330,12 @@ func testAccept(t *testing.T, inprocessMode bool, singleTest string) int { |
328 | 330 | } |
329 | 331 | } |
330 | 332 |
|
| 333 | + const sharedProxyHint = "; re-run with -debugsandbox to see which test caused this" |
| 334 | + sandboxProxyURL := "" |
| 335 | + if cloudEnv == "" && !DebugSandbox { |
| 336 | + sandboxProxyURL = internal.StartRejectingProxy(t, sharedProxyHint) |
| 337 | + } |
| 338 | + |
331 | 339 | setReplsForTestEnvVars(t, &repls) |
332 | 340 |
|
333 | 341 | if cloudEnv != "" && UseVersion == "" { |
@@ -455,15 +463,15 @@ func testAccept(t *testing.T, inprocessMode bool, singleTest string) int { |
455 | 463 | // If the matrix expands to a single empty envset, run the test directly |
456 | 464 | // without creating a subtest (avoids the "#00" dummy subtest name). |
457 | 465 | if len(expanded) == 1 && len(expanded[0]) == 0 { |
458 | | - runTest(t, dir, 0, coverDir, repls.Clone(), config, nil, envFilters) |
| 466 | + runTest(t, dir, 0, coverDir, repls.Clone(), config, nil, envFilters, sandboxProxyURL) |
459 | 467 | } else { |
460 | 468 | for ind, envset := range expanded { |
461 | 469 | envname := strings.Join(envset, "/") |
462 | 470 | t.Run(envname, func(t *testing.T) { |
463 | 471 | if runParallel { |
464 | 472 | t.Parallel() |
465 | 473 | } |
466 | | - runTest(t, dir, ind, coverDir, repls.Clone(), config, envset, envFilters) |
| 474 | + runTest(t, dir, ind, coverDir, repls.Clone(), config, envset, envFilters, sandboxProxyURL) |
467 | 475 | }) |
468 | 476 | } |
469 | 477 | } |
@@ -609,6 +617,7 @@ func runTest(t *testing.T, |
609 | 617 | config internal.TestConfig, |
610 | 618 | customEnv []string, |
611 | 619 | envFilters []string, |
| 620 | + sharedProxyURL string, |
612 | 621 | ) { |
613 | 622 | // Check env filters early, before creating any resources like directories on the file system. |
614 | 623 | // Creating / deleting too many directories causes this error on the workspace FUSE mount: |
@@ -763,6 +772,26 @@ func runTest(t *testing.T, |
763 | 772 | cmd.Env = append(cmd.Env, "TESTDIR="+absDir) |
764 | 773 | cmd.Env = append(cmd.Env, "CLOUD_ENV="+cloudEnv) |
765 | 774 | cmd.Env = append(cmd.Env, "CURRENT_USER_NAME="+user.UserName) |
| 775 | + if !isRunningOnCloud { |
| 776 | + proxyURL := sharedProxyURL |
| 777 | + if DebugSandbox { |
| 778 | + // Per-test proxy: errors are attributed to this subtest's t, making |
| 779 | + // it immediately clear which test caused the internet access. |
| 780 | + proxyURL = internal.StartRejectingProxy(t, "") |
| 781 | + } |
| 782 | + // Only block HTTPS: the local test server is plain HTTP (http://127.0.0.1:PORT) |
| 783 | + // so HTTP_PROXY would intercept its traffic. All real external calls use HTTPS. |
| 784 | + cmd.Env = append(cmd.Env, "HTTPS_PROXY="+proxyURL) |
| 785 | + // Python's urllib does not automatically bypass the proxy for loopback |
| 786 | + // addresses the way Go does, so the test-server helper scripts |
| 787 | + // (kill_after.py, callserver.py, …) would route their requests through |
| 788 | + // the blocking proxy. NO_PROXY exempts them. |
| 789 | + cmd.Env = append(cmd.Env, "NO_PROXY=127.0.0.1,localhost") |
| 790 | + // Terraform phones home to checkpoint-api.hashicorp.com on every run to |
| 791 | + // check for updates. Disable it so these CONNECT requests don't reach the |
| 792 | + // blocking proxy and fail every terraform-engine test. |
| 793 | + cmd.Env = append(cmd.Env, "CHECKPOINT_DISABLE=1") |
| 794 | + } |
766 | 795 | cmd.Dir = tmpDir |
767 | 796 |
|
768 | 797 | outputPath := filepath.Join(tmpDir, "output.txt") |
|
0 commit comments