forked from potpie-ai/potpie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_gvisor_docker.py
More file actions
189 lines (160 loc) · 5.31 KB
/
test_gvisor_docker.py
File metadata and controls
189 lines (160 loc) · 5.31 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
#!/usr/bin/env python3
"""
Test gVisor through Docker on Mac
"""
import sys
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,
_check_docker_available,
_is_docker_desktop,
)
def test_gvisor_setup():
"""Test gVisor setup and availability."""
print("=" * 60)
print("gVisor Docker Setup Test")
print("=" * 60)
print()
print("1. Checking Docker availability...")
docker_available = _check_docker_available()
print(f" Docker with runsc runtime: {docker_available}")
print()
print("2. Checking if Docker Desktop...")
is_desktop = _is_docker_desktop()
print(f" Docker Desktop detected: {is_desktop}")
print()
print("3. Checking gVisor availability...")
gvisor_available = is_gvisor_available()
print(f" gVisor available: {gvisor_available}")
print()
return gvisor_available
def test_commands():
"""Test running commands through gVisor."""
print("=" * 60)
print("Testing Commands Through gVisor")
print("=" * 60)
print()
# Test 1: Simple echo command
print("Test 1: Simple echo command")
result = run_command_isolated(
command=["echo", "Hello from gVisor test"],
use_gvisor=True,
)
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 with multiple commands
print("Test 2: Shell command (date + echo)")
result = run_shell_command_isolated(
shell_command="date && echo 'Command executed'",
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"
assert "Command executed" in result.stdout
print(" ✓ Passed")
print()
# Test 3: Command with working directory
import tempfile
import os
print("Test 3: Command with working directory")
with tempfile.TemporaryDirectory() as tmpdir:
test_file = os.path.join(tmpdir, "test.txt")
with open(test_file, "w") as f:
f.write("test content")
result = run_command_isolated(
command=["cat", "test.txt"],
working_dir=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: List files
print("Test 4: List files (ls)")
result = run_command_isolated(
command=["ls", "-la", "/"],
use_gvisor=True,
)
print(f" Return code: {result.returncode}")
print(f" Success: {result.success}")
print(f" Stdout (first 200 chars): {result.stdout[:200]}...")
assert result.success, "ls command should succeed"
print(" ✓ Passed")
print()
# Test 5: Environment variables
print("Test 5: Environment variables")
result = run_command_isolated(
command=["sh", "-c", "echo $TEST_VAR"],
env={"TEST_VAR": "test_value"},
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 env var should succeed"
assert "test_value" in result.stdout
print(" ✓ Passed")
print()
def main():
"""Run all tests."""
print("\n" + "=" * 60)
print("gVisor Docker Test Suite")
print("=" * 60)
print()
try:
# Test setup
gvisor_available = test_gvisor_setup()
if not gvisor_available:
print("⚠️ gVisor is not available.")
print()
print("To enable gVisor on Mac:")
print("1. Open Docker Desktop Settings > Docker Engine")
print("2. Add this to the JSON:")
print(" {")
print(' "runtimes": {')
print(' "runsc": {')
print(' "path": "/usr/local/bin/runsc"')
print(" }")
print(" }")
print(" }")
print("3. Click 'Apply & Restart'")
print("4. Run this test again")
print()
return 1
# Test commands
test_commands()
print("=" * 60)
print("All Tests Passed! ✓")
print("=" * 60)
print()
print("gVisor is working correctly through Docker on Mac!")
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())