forked from potpie-ai/potpie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_gvisor.py
More file actions
218 lines (186 loc) · 6.59 KB
/
test_gvisor.py
File metadata and controls
218 lines (186 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python3
"""
Test script for gVisor functionality.
Tests that gVisor detection and fallback work correctly on Mac/Windows.
"""
import sys
import platform
from pathlib import Path
# Add project root to path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
from app.modules.utils.gvisor_runner import (
is_gvisor_available,
run_command_isolated,
run_shell_command_isolated,
get_runsc_binary,
_is_running_in_container,
_check_docker_available,
)
def test_platform_detection():
"""Test that platform detection works correctly."""
print("=" * 60)
print("Platform Detection Test")
print("=" * 60)
print(f"Platform: {platform.system()}")
print(f"Architecture: {platform.machine()}")
print(f"Running in container: {_is_running_in_container()}")
print()
def test_gvisor_availability():
"""Test gVisor availability detection."""
print("=" * 60)
print("gVisor Availability Test")
print("=" * 60)
available = is_gvisor_available()
runsc_path = get_runsc_binary()
print(f"gVisor available: {available}")
print(f"runsc binary path: {runsc_path}")
if platform.system().lower() != "linux":
docker_available = _check_docker_available()
print(f"Docker with runsc runtime available: {docker_available}")
# On Mac/Windows, gVisor can be available via Docker Desktop
# Only assert False if Docker is not available (which would make gVisor unavailable)
if not docker_available:
print(
f"✓ Expected: gVisor not available on {platform.system()} (Docker not available)"
)
assert (
not available
), "gVisor should not be available on non-Linux platforms without Docker"
else:
print(
f"✓ gVisor may be available on {platform.system()} via Docker Desktop"
)
else:
print("Platform is Linux - gVisor may be available if installed")
print()
def test_command_execution():
"""Test that command execution works with fallback."""
print("=" * 60)
print("Command Execution Test")
print("=" * 60)
# Test 1: Simple command
print("Test 1: Simple echo command")
result = run_command_isolated(
command=["echo", "Hello from gVisor test"],
use_gvisor=True, # Try to use gVisor (will fall back on Mac)
)
print(f" Return code: {result.returncode}")
print(f" Success: {result.success}")
print(f" Stdout: {result.stdout.strip()}")
if result.stderr:
print(f" Stderr: {result.stderr.strip()}")
assert result.success, "Command should succeed"
assert "Hello from gVisor test" in result.stdout
print(" ✓ Passed")
print()
# Test 2: Shell command
print("Test 2: Shell command")
result = run_shell_command_isolated(
shell_command="echo 'Shell test' && echo 'Multiple lines'",
use_gvisor=True,
)
print(f" Return code: {result.returncode}")
print(f" Success: {result.success}")
print(f" Stdout: {result.stdout.strip()}")
assert result.success, "Shell command should succeed"
print(" ✓ Passed")
print()
# Test 3: Command with working directory
print("Test 3: Command with working directory")
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
test_file = Path(tmpdir) / "test.txt"
test_file.write_text("test content")
result = run_command_isolated(
command=["cat", "test.txt"],
working_dir=str(tmpdir),
use_gvisor=True,
)
print(f" Return code: {result.returncode}")
print(f" Success: {result.success}")
print(f" Stdout: {result.stdout.strip()}")
assert result.success, "Command with working dir should succeed"
assert "test content" in result.stdout
print(" ✓ Passed")
print()
# Test 4: Force no gVisor
print("Test 4: Force no gVisor (explicit fallback)")
result = run_command_isolated(
command=["echo", "No gVisor"],
use_gvisor=False, # Explicitly disable gVisor
)
print(f" Return code: {result.returncode}")
print(f" Success: {result.success}")
print(f" Stdout: {result.stdout.strip()}")
assert result.success, "Command without gVisor should succeed"
assert "No gVisor" in result.stdout
print(" ✓ Passed")
print()
def test_error_handling():
"""Test error handling."""
print("=" * 60)
print("Error Handling Test")
print("=" * 60)
# Test: Non-existent command
print("Test: Non-existent command")
result = run_command_isolated(
command=["nonexistent_command_xyz123"],
use_gvisor=True,
)
print(f" Return code: {result.returncode}")
print(f" Success: {result.success}")
assert not result.success, "Non-existent command should fail"
print(" ✓ Passed")
print()
# Test: Non-existent working directory
print("Test: Non-existent working directory")
result = run_command_isolated(
command=["ls"],
working_dir="/nonexistent/directory/xyz123",
use_gvisor=True,
)
print(f" Return code: {result.returncode}")
print(f" Success: {result.success}")
assert not result.success, "Non-existent directory should fail"
print(" ✓ Passed")
print()
def main():
"""Run all tests."""
print("\n" + "=" * 60)
print("gVisor Test Suite - Mac/Windows Fallback Test")
print("=" * 60)
print()
try:
test_platform_detection()
test_gvisor_availability()
test_command_execution()
test_error_handling()
print("=" * 60)
print("All Tests Passed! ✓")
print("=" * 60)
print()
print("Summary:")
print(f" - Platform: {platform.system()}")
print(f" - gVisor available: {is_gvisor_available()}")
print(" - Fallback working: ✓")
print(" - Commands execute correctly: ✓")
print()
if platform.system().lower() != "linux":
if is_gvisor_available():
print("On Mac/Windows, gVisor is available via Docker Desktop.")
else:
print("On Mac/Windows, gVisor is not available, but the system")
print("correctly falls back to regular subprocess execution.")
print()
return 0
except AssertionError as e:
print(f"\n❌ Test failed: {e}")
return 1
except Exception as e:
print(f"\n❌ Unexpected error: {e}")
import traceback
traceback.print_exc()
return 1
if __name__ == "__main__":
sys.exit(main())