import importlib.util from pathlib import Path import subprocess import tempfile import unittest from unittest.mock import patch path = Path(__file__).resolve().parents[2] / 'scripts/network-sandbox-capture-host.py' spec = importlib.util.spec_from_file_location('sandbox_capture_host', path) capture = importlib.util.module_from_spec(spec) spec.loader.exec_module(capture) class SandboxCaptureTest(unittest.TestCase): def test_only_dedicated_interfaces_are_allowed(self): self.assertEqual(capture.PORTS, {'vlan10': ('veth9110i0', 'vmbr9'), 'vlan20': ('veth9111i0', 'vmbr9'), 'trunk': ('tap9100i1', 'vmbr9'), 'wan': ('tap9100i0', 'vmbr0')}) def test_report_marks_failed_sections(self): success = subprocess.CompletedProcess([], 0, stdout='packets\n', stderr='') failure = subprocess.CompletedProcess([], 1, stdout='', stderr='filter unavailable') with tempfile.TemporaryDirectory() as directory, \ patch.object(capture.subprocess, 'run', side_effect=[success, failure, success]): path = Path(directory) / 'report.txt' warnings = capture.report('tshark', Path(directory) / 'traffic.pcapng', path, 'vlan10', 'stopped') self.assertEqual(warnings, ['DNS queries and responses']) self.assertIn('filter unavailable', path.read_text()) def test_unexpected_filesystem_is_rejected(self): result = subprocess.CompletedProcess([], 0, stdout='wrong-uuid rw,relatime\n', stderr='') with patch.object(capture.subprocess, 'run', return_value=result): with self.assertRaises(RuntimeError): capture.storage_root()