Files
webup-cloud/node-agent/test_gateway.py
T

121 lines
6.7 KiB
Python

import base64
import importlib.util
import io
import json
from pathlib import Path
import subprocess
import sys
import tempfile
import types
import unittest
from unittest.mock import MagicMock, patch
import zipfile
spec = importlib.util.spec_from_file_location("gateway", Path(__file__).with_name("app.py"))
gateway = importlib.util.module_from_spec(spec)
spec.loader.exec_module(gateway)
class GatewayTests(unittest.TestCase):
def execute_file_tool(self, root, name, arguments):
payload = base64.b64encode(json.dumps({"name": name, "arguments": arguments}).encode()).decode()
script = gateway.SCRIPT.replace("pathlib.Path('/workspace')", "pathlib.Path(" + repr(str(root)) + ")")
result = subprocess.run([sys.executable, "-X", "utf8", "-c", "PAYLOAD=" + repr(payload) + "\n" + script], capture_output=True, timeout=10)
self.assertEqual(0, result.returncode, result.stderr.decode())
return json.loads(result.stdout)
def test_file_roundtrip_and_parent_traversal_rejection(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory) / "workspace"
root.mkdir()
self.assertIn("written", self.execute_file_tool(root, "write_file", {"path": "src/test.txt", "content": "真实内容"}))
self.assertEqual("真实内容", self.execute_file_tool(root, "read_file", {"path": "src/test.txt"})["content"])
self.assertIn("error", self.execute_file_tool(root, "write_file", {"path": "../outside.txt", "content": "denied"}))
self.assertFalse((Path(directory) / "outside.txt").exists())
def test_large_file_read_is_rejected(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
(root / "large.txt").write_bytes(b"x" * 64001)
self.assertIn("error", self.execute_file_tool(root, "read_file", {"path": "large.txt"}))
def test_workspace_inspection_requires_nonempty_source_and_excludes_dependencies(self):
with tempfile.TemporaryDirectory() as directory:
root=Path(directory)
(root/'node_modules').mkdir()
(root/'node_modules/fake.js').write_text('dependency code')
(root/'index.html').write_text('')
(root/'.env').write_text('SECRET=must-not-appear')
self.assertEqual(0,self.execute_file_tool(root,'workspace_inspect',{})['sourceFiles'])
(root/'index.html').write_text('<h1>Actual source</h1>')
result=self.execute_file_tool(root,'workspace_inspect',{})
self.assertEqual(1,result['sourceFiles'])
self.assertTrue(result['complete'])
self.assertEqual(['index.html'],[f['path'] for f in result['files']])
def test_archive_excludes_git_and_dependency_files(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
(root / "index.html").write_text("<h1>Test</h1>")
(root / "node_modules").mkdir()
(root / "node_modules/ignored.js").write_text("ignore")
result = self.execute_file_tool(root, "archive_source", {})
with zipfile.ZipFile(io.BytesIO(base64.b64decode(result["base64"]))) as archive:
self.assertEqual(["index.html"], archive.namelist())
def test_invalid_identity_and_missing_credentials_fail_closed(self):
with self.assertRaises(gateway.HTTPException):
gateway.workspace_name("../../host")
with patch.object(gateway, "TOKEN", "x" * 32):
with self.assertRaises(gateway.HTTPException):
gateway.authorize("Bearer wrong")
gateway.authorize("Bearer " + "x" * 32)
def test_created_workspace_has_isolation_and_persistent_data(self):
core, net = MagicMock(), MagicMock()
core.read_namespaced_pod.return_value = types.SimpleNamespace(status=types.SimpleNamespace(phase="Running", container_statuses=[types.SimpleNamespace(ready=True)]))
with patch.object(gateway.app.state, "core", core, create=True), patch.object(gateway.app.state, "net", net, create=True), patch.object(gateway, "IMAGE", "test/isolated-image:local"):
gateway.ensure_workspace("00000000-0000-0000-0000-000000000001")
pod = core.create_namespaced_pod.call_args.kwargs["body"]["spec"]
self.assertEqual("gvisor", pod["runtimeClassName"])
self.assertFalse(pod["automountServiceAccountToken"])
self.assertTrue(pod["containers"][0]["securityContext"]["readOnlyRootFilesystem"])
self.assertEqual(["ALL"], pod["containers"][0]["securityContext"]["capabilities"]["drop"])
self.assertIn("persistentVolumeClaim", pod["volumes"][0])
policy = net.create_namespaced_network_policy.call_args.kwargs["body"]["spec"]
self.assertEqual([], policy["ingress"])
self.assertIn("169.254.0.0/16", policy["egress"][1]["to"][0]["ipBlock"]["except"])
def test_transient_exec_handshake_recovers_before_tool_input(self):
project = "00000000-0000-0000-0000-000000000002"
request = gateway.Tool(name="write_file", arguments={"path": "index.html", "content": "test"})
connection = MagicMock()
with (patch.object(gateway, "TOKEN", "x" * 32),
patch.object(gateway, "ensure_workspace"),
patch.object(gateway, "stream", side_effect=[gateway.ApiException(status=503), connection]) as stream,
patch.object(gateway.time, "sleep"),
patch.object(gateway, "drain_exec_output", return_value='{"written":4}')):
self.assertEqual({"written": 4}, gateway.execute_tool(project, request, "Bearer " + "x" * 32, False))
self.assertEqual(2, stream.call_count)
connection.write_stdin.assert_called_once()
connection.close.assert_called_once()
def test_interrupted_tool_input_is_not_replayed(self):
project = "00000000-0000-0000-0000-000000000003"
request = gateway.Tool(name="run_command", arguments={"command": "npm install"})
connection = MagicMock()
connection.write_stdin.side_effect = gateway.WebSocketException("disconnected")
with (patch.object(gateway, "TOKEN", "x" * 32),
patch.object(gateway, "ensure_workspace"),
patch.object(gateway, "stream", return_value=connection) as stream):
with self.assertRaises(gateway.HTTPException) as error:
gateway.execute_tool(project, request, "Bearer " + "x" * 32, False)
self.assertEqual((502, "WORKSPACE_RESULT_UNKNOWN"), (error.exception.status_code, error.exception.detail))
stream.assert_called_once()
connection.write_stdin.assert_called_once()
connection.close.assert_called_once()
if __name__ == "__main__":
unittest.main()