package execenv import ( "context" "os" "os/exec" "path/filepath" "strings" "testing" "time" ) func TestSandboxImportPipAndOffline(t *testing.T) { if _, err := exec.LookPath("bwrap"); err != nil { t.Skip("bwrap not available") } if _, err := exec.LookPath("python3"); err != nil { t.Skip("python3 not available") } root, err := Open(t.TempDir()) if err != nil { t.Fatal(err) } site := t.TempDir() if err := os.WriteFile(filepath.Join(site, "hello.py"), []byte("value = 1\n"), 0o644); err != nil { t.Fatal(err) } ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() if err := Doctor(ctx, "/usr/bin/python3", []string{site}, []string{"hello"}, true, []string{site}); err != nil { t.Fatal(err) } shim := filepath.Join(root.BinDir(), "pip") out, err := exec.Command("bwrap", "--unshare-all", "--die-with-parent", "--ro-bind", "/", "/", "--tmpfs", "/tmp", "--ro-bind", root.BinDir(), root.BinDir(), "--", shim).CombinedOutput() if err == nil || !strings.Contains(string(out), "python_env") { t.Fatalf("pip shim err=%v out=%s", err, out) } netOut, err := exec.Command("bwrap", "--unshare-net", "--die-with-parent", "--ro-bind", "/", "/", "--", "/usr/bin/python3", "-c", "import socket; socket.create_connection(('1.1.1.1', 443), 2)").CombinedOutput() if err == nil { t.Fatalf("sandbox network should be unreachable, out=%s", netOut) } }