47 lines
2.4 KiB
Python
47 lines
2.4 KiB
Python
"""Checks for sandbox guest identity and network constraints."""
|
|
import importlib.util
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
spec = importlib.util.spec_from_file_location('sandbox_lifecycle', ROOT / 'scripts/network-sandbox-lifecycle-host.py')
|
|
lifecycle = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(lifecycle)
|
|
|
|
|
|
class SandboxLifecycleTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.router = ('name: jeannie-sandbox-openwrt\n'
|
|
'description: jeannie-network-sandbox-v1\n'
|
|
'net0: virtio=00:00:00:00:00:01,bridge=vmbr0,link_down=1\n'
|
|
'net1: virtio=00:00:00:00:00:02,bridge=vmbr9,trunks=10;20\n')
|
|
self.client10 = 'hostname: jeannie-sandbox-client-10\nunprivileged: 1\nnet0: name=eth0,bridge=vmbr9,tag=10\n'
|
|
self.client20 = 'hostname: jeannie-sandbox-client-20\nunprivileged: 1\nnet0: name=eth0,bridge=vmbr9,tag=20\n'
|
|
|
|
def test_expected_guests_are_accepted(self):
|
|
with patch.object(lifecycle, 'run', side_effect=[self.router, self.client10, self.client20]):
|
|
lifecycle.verify_guests()
|
|
|
|
def test_connected_wan_is_rejected(self):
|
|
with patch.object(lifecycle, 'run', return_value=self.router.replace(',link_down=1', '')):
|
|
with self.assertRaisesRegex(RuntimeError, 'WAN must remain disconnected'):
|
|
lifecycle.verify_guests()
|
|
|
|
def test_extra_client_interface_is_rejected(self):
|
|
with patch.object(lifecycle, 'run', side_effect=[self.router, self.client10 + 'net1: name=eth1,bridge=vmbr0\n']):
|
|
with self.assertRaisesRegex(RuntimeError, 'unexpected network interface'):
|
|
lifecycle.verify_guests()
|
|
|
|
def test_start_network_targets_only_the_selected_client(self):
|
|
with patch.object(lifecycle, 'run', side_effect=['', '', '', '', 'inet 172.29.210.101/24']) as command:
|
|
lifecycle.ensure_client_network(9110)
|
|
calls = [entry.args for entry in command.call_args_list]
|
|
self.assertTrue(all(args[:4] == ('pct', 'exec', '9110', '--') for args in calls))
|
|
self.assertIn(('pct', 'exec', '9110', '--', 'ip', 'link', 'set', 'dev', 'lo', 'up'), calls)
|
|
self.assertIn(('pct', 'exec', '9110', '--', 'busybox', 'udhcpc', '-n', '-t', '3', '-T', '2', '-i', 'eth0', '-s', '/usr/share/udhcpc/default.script'), calls)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|