diff --git a/scripts/network-sandbox-lifecycle-host.py b/scripts/network-sandbox-lifecycle-host.py index c90d75f..4a21b7a 100644 --- a/scripts/network-sandbox-lifecycle-host.py +++ b/scripts/network-sandbox-lifecycle-host.py @@ -76,23 +76,19 @@ def status(command, guest): def ensure_client_network(guest): - """Start DHCP if this minimal Alpine image did not start networking.""" - command = r'''set -eu -ip link set dev eth0 up -if command -v rc-update >/dev/null 2>&1 && [ -f /etc/init.d/networking ]; then - rc-update add networking boot >/dev/null -fi -if ip -4 addr show dev eth0 | grep -q 'inet '; then - echo 'IPv4 address already present' -elif busybox pidof udhcpc >/dev/null 2>&1; then - echo 'DHCP client already running' -else - test -x /usr/share/udhcpc/default.script || { echo 'Alpine DHCP script is missing' >&2; exit 1; } - mkdir -p /run - busybox udhcpc -b -i eth0 -p /run/jeannie-sandbox-udhcpc.pid -s /usr/share/udhcpc/default.script - echo 'DHCP client started; a lease may take time' -fi''' - print(f'Container {guest}: {run("pct", "exec", str(guest), "--", "sh", "-c", command)}', flush=True) + """Obtain a DHCP lease with direct pct exec arguments, without a shell.""" + prefix = ('pct', 'exec', str(guest), '--') + run(*prefix, 'ip', 'link', 'set', 'dev', 'eth0', 'up') + address = run(*prefix, 'ip', '-4', 'addr', 'show', 'dev', 'eth0') + if 'inet ' in address: + print(f'Container {guest}: IPv4 address already present', flush=True) + return + run(*prefix, 'busybox', 'udhcpc', '-n', '-t', '3', '-T', '2', + '-i', 'eth0', '-s', '/usr/share/udhcpc/default.script') + address = run(*prefix, 'ip', '-4', 'addr', 'show', 'dev', 'eth0') + if 'inet ' not in address: + raise RuntimeError(f'Container {guest} did not obtain an IPv4 address') + print(f'Container {guest}: DHCP lease acquired', flush=True) def owned_existing_guests(): diff --git a/scripts/network-sandbox-verify-host.py b/scripts/network-sandbox-verify-host.py index 3f0a125..c4cadf5 100644 --- a/scripts/network-sandbox-verify-host.py +++ b/scripts/network-sandbox-verify-host.py @@ -60,15 +60,14 @@ def client_address(guest, network): print(f'Client {guest} address check: {exc}', file=sys.stderr) print('Interface state:', file=sys.stderr) print(output[:2000], file=sys.stderr) - diagnostic = subprocess.run( - ['pct', 'exec', str(guest), '--', 'sh', '-c', - "printf '%s\\n' '-- network configuration'; " - "if [ -f /etc/network/interfaces ]; then cat /etc/network/interfaces; else echo 'No /etc/network/interfaces'; fi; " - "printf '%s\\n' '-- DHCP processes'; ps | grep '[u]dhcpc' || true"], - capture_output=True, text=True, timeout=20) - print(diagnostic.stdout[:3000], file=sys.stderr) - if diagnostic.returncode: - print(f'Client diagnostics exit: {diagnostic.returncode}', file=sys.stderr) + for label, command in [('network configuration', ('cat', '/etc/network/interfaces')), + ('processes', ('ps',))]: + diagnostic = subprocess.run(['pct', 'exec', str(guest), '--', *command], + capture_output=True, text=True, timeout=20) + print(f'-- {label}', file=sys.stderr) + print(diagnostic.stdout[:3000], file=sys.stderr) + if diagnostic.returncode: + print(f'Client diagnostics exit: {diagnostic.returncode}', file=sys.stderr) raise diff --git a/tests/jeannie/test_network_sandbox_lifecycle.py b/tests/jeannie/test_network_sandbox_lifecycle.py index 9e09714..cb7d09f 100644 --- a/tests/jeannie/test_network_sandbox_lifecycle.py +++ b/tests/jeannie/test_network_sandbox_lifecycle.py @@ -34,11 +34,11 @@ class SandboxLifecycleTests(unittest.TestCase): lifecycle.verify_guests() def test_start_network_targets_only_the_selected_client(self): - with patch.object(lifecycle, 'run', return_value='DHCP client started') as command: + with patch.object(lifecycle, 'run', side_effect=['', '', '', 'inet 172.29.210.101/24']) as command: lifecycle.ensure_client_network(9110) - args = command.call_args.args - self.assertEqual(args[:5], ('pct', 'exec', '9110', '--', 'sh')) - self.assertIn('busybox udhcpc -b -i eth0', args[-1]) + 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', '--', 'busybox', 'udhcpc', '-n', '-t', '3', '-T', '2', '-i', 'eth0', '-s', '/usr/share/udhcpc/default.script'), calls) if __name__ == '__main__':