Files
EscapistsReintroduced/tests/test_dedicated_native.py
T

68 lines
3.3 KiB
Python

#!/usr/bin/env python3
import contextlib
import importlib.util
import io
from pathlib import Path
import sys
import tempfile
import unittest
from unittest.mock import patch
spec = importlib.util.spec_from_file_location('dedicated_native', Path(__file__).parents[1] / 'tools/dedicated_native.py')
runner = importlib.util.module_from_spec(spec)
spec.loader.exec_module(runner)
class DedicatedNativeTests(unittest.TestCase):
def probe(self, namespace='isolated', interfaces=None, routes=None, ipv6=''):
interfaces = interfaces if interfaces is not None else [(1, 'lo')]
routes = routes if routes is not None else 'Iface Destination Gateway\n'
def content(path, *args):
return io.StringIO(ipv6 if path.endswith('ipv6_route') else routes)
with patch('os.readlink', return_value=namespace), patch('socket.if_nameindex', return_value=interfaces), patch('builtins.open', side_effect=content), patch.object(sys, 'argv', ['probe', 'host', '/server']), patch('os.execv') as launch, contextlib.redirect_stdout(io.StringIO()):
try:
exec(runner.PROBE, {})
except AssertionError:
self.assertFalse(launch.called)
return False
launch.assert_called_once_with('/server', ['/server'])
return True
def test_isolation_gate_executes_only_private_loopback(self):
self.assertTrue(self.probe())
self.assertFalse(self.probe(namespace='host'))
self.assertFalse(self.probe(interfaces=[(1, 'lo'), (2, 'eth0')]))
self.assertFalse(self.probe(routes='Iface Destination\neth0 00000000\n'))
self.assertFalse(self.probe(ipv6='0000 eth0\n'))
self.assertTrue(self.probe(ipv6='0000 lo\n'))
def test_vanilla_control_has_no_successor_or_custom_systems(self):
args=runner.engine_args('vanilla-everon')
self.assertNotIn('-addons',args)
self.assertNotIn('-addonsDir',args)
self.assertNotIn('-worldSystemsConfig',args)
self.assertEqual(args[-2:],['-server','Missions/21_GM_Eden.conf'])
self.assertEqual(args[0],'/tmp/runtime/ArmaReforgerServer')
def test_manifest_detects_mutation_and_refuses_links(self):
with tempfile.TemporaryDirectory() as directory:
root=Path(directory); target=root/'a.c'; target.write_text('first')
old=runner.manifest(root); target.write_text('second')
self.assertNotEqual(old,runner.manifest(root))
(root/'linked').symlink_to(target)
with self.assertRaises(ValueError): runner.manifest(root)
def test_routes_do_not_mix_public_config_or_client_flags(self):
for route in ['header','diag-world','vanilla-everon']:
args=runner.engine_args(route)
self.assertNotIn('-config',args)
self.assertNotIn('-client',args)
self.assertIn('-noBackend',args)
self.assertNotIn('-rpl-validation-rdb-disable',args)
normal=runner.engine_args('header')
self.assertEqual(normal[0],'/tmp/runtime/ArmaReforgerServer')
self.assertTrue(normal[normal.index('-server')+1].endswith('ERR_Everon_Base.conf'))
self.assertNotIn('-worldSystemsConfig',normal)
self.assertIn('-worldSystemsConfig',runner.engine_args('diag-world'))
if __name__ == '__main__': unittest.main()