From 747568aae32dfb727aeb1efcf9c6124a7191a092 Mon Sep 17 00:00:00 2001 From: Stanislav Fomichev Date: Wed, 18 Nov 2026 15:20:37 -0800 Subject: [PATCH] contest: hw: write net.config into every installed target hw_worker writes net.config only into drivers/net and drivers/net/hw. The kselftest Python lib (drivers/net/lib/py/env.py) reads net.config from the directory of the test being run, so tests in any other target, e.g. a new drivers/net/perf, get none of the NETIF, address and remote settings. Take the targets from the installed test list instead, the same list run_tests() executes. 'make install' generates it from the [ksft] target the orchestrator was configured with, so no extra plumbing via nic-test.env is needed, and --prep-only keeps working on any installed tree. If the list can't be read, warn rather than abort the interface setup. Rename _list_tests() to list_tests() now that it is used outside of lib/runner.py. Signed-off-by: Stanislav Fomichev --- contest/hw/hw_worker.py | 54 +++++++++++++------ contest/hw/lib/runner.py | 4 +- contest/hw/tests/test_hw_worker.py | 86 ++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 18 deletions(-) diff --git a/contest/hw/hw_worker.py b/contest/hw/hw_worker.py index 55bb5784..60417ae5 100644 --- a/contest/hw/hw_worker.py +++ b/contest/hw/hw_worker.py @@ -11,7 +11,8 @@ import sys import time -from lib.runner import DEFAULT_TEST_TIMEOUT, find_newest_test, run_tests +from lib.runner import (DEFAULT_TEST_TIMEOUT, find_newest_test, list_tests, + run_tests) TESTS_DIR = '/srv/hw-worker/tests' @@ -406,6 +407,39 @@ def _setup_irq_affinity(netif): print(f"WARN: could not restore combined channels on {netif}") +def _write_net_config(test_dir, env): + """Write net.config into every installed kselftest target. + + The kselftest Python lib (drivers/net/lib/py/env.py) reads net.config + from the directory of the test being run, so each target needs its + own copy. Take the targets from the installed test list instead of + hard-coding them, so whatever [ksft] target the orchestrator built + (drivers/net, drivers/net/hw, drivers/net/perf, ...) gets configured. + """ + config_lines = [f'{key}={env[key]}' for key in _NET_CONFIG_KEYS + if env.get(key)] + if not config_lines: + return + config_content = '\n'.join(config_lines) + '\n' + + try: + targets = sorted({target for target, _prog in list_tests(test_dir)}) + except (OSError, subprocess.TimeoutExpired) as e: + print(f"WARN: cannot list installed tests in {test_dir}: {e}") + targets = [] + if not targets: + print(f"WARN: no installed tests in {test_dir}, net.config not written") + + for target in targets: + config_dir = os.path.join(test_dir, target) + if not os.path.isdir(config_dir): + continue + path = os.path.join(config_dir, 'net.config') + with open(path, 'w', encoding='utf-8') as fp: + fp.write(config_content) + print(f"Wrote {path}") + + def setup_test_interfaces(test_dir): """Configure test NICs and write net.config from nic-test.env. @@ -413,7 +447,8 @@ def setup_test_interfaces(test_dir): IP addresses, and remote connectivity info. This function: 1. Brings up the DUT and peer interfaces 2. Adds IP addresses if not already configured - 3. Writes drivers/net/net.config for the kselftest framework + 3. Writes net.config into each installed target for the kselftest + framework """ env = _parse_env_file(os.path.join(test_dir, 'nic-test.env')) if not env: @@ -480,20 +515,7 @@ def setup_test_interfaces(test_dir): _wait_peer_reachable(netif, env['REMOTE_V6']) # Write net.config for the kselftest framework - config_lines = [] - for key in _NET_CONFIG_KEYS: - if env.get(key): - config_lines.append(f'{key}={env[key]}') - - if config_lines: - config_content = '\n'.join(config_lines) + '\n' - for subdir in ['drivers/net', 'drivers/net/hw']: - config_dir = os.path.join(test_dir, subdir) - if os.path.isdir(config_dir): - path = os.path.join(config_dir, 'net.config') - with open(path, 'w', encoding='utf-8') as fp: - fp.write(config_content) - print(f"Wrote {path}") + _write_net_config(test_dir, env) # Export env-only variables (not net.config) for the test framework for key in _ENV_ONLY_KEYS: diff --git a/contest/hw/lib/runner.py b/contest/hw/lib/runner.py index 8c748f3f..e705e119 100644 --- a/contest/hw/lib/runner.py +++ b/contest/hw/lib/runner.py @@ -97,7 +97,7 @@ def mark_attempted(test_dir, test_name): os.rename(tmp_path, path) -def _list_tests(test_dir): +def list_tests(test_dir): """List available tests from the installed kselftest layout. Reads kselftest-list.txt if present, otherwise scans for @@ -359,7 +359,7 @@ def run_tests(test_dir, results_dir, timeout=DEFAULT_TEST_TIMEOUT): a test died with its own subprocess.TimeoutExpired (leaving the machine in an unknown state, so the harness must reboot it). """ - tests = _list_tests(test_dir) + tests = list_tests(test_dir) if not tests: print("No tests found") return RunOutcome() diff --git a/contest/hw/tests/test_hw_worker.py b/contest/hw/tests/test_hw_worker.py index 54fa36cb..58a62823 100644 --- a/contest/hw/tests/test_hw_worker.py +++ b/contest/hw/tests/test_hw_worker.py @@ -756,6 +756,92 @@ def test_garbage_falls_back(self): DEFAULT_TEST_TIMEOUT) +class TestWriteNetConfig(unittest.TestCase): + """net.config goes into every installed target, not a fixed list.""" + + ENV = {'NETIF': 'eth0', 'LOCAL_V4': '192.0.2.1', 'DISRUPTIVE': '0', + 'NIPA_TEST_TIMEOUT': '60'} + CONTENT = 'NETIF=eth0\nLOCAL_V4=192.0.2.1\n' + + def _make_tree(self, test_dir, listed, dirs): + for subdir in dirs: + os.makedirs(os.path.join(test_dir, subdir)) + with open(os.path.join(test_dir, 'kselftest-list.txt'), 'w') as fp: + fp.write(''.join(f'{entry}\n' for entry in listed)) + + def _net_config(self, test_dir, subdir): + path = os.path.join(test_dir, subdir, 'net.config') + if not os.path.exists(path): + return None + with open(path, encoding='utf-8') as fp: + return fp.read() + + def test_all_listed_targets(self): + from hw_worker import _write_net_config + + with tempfile.TemporaryDirectory() as test_dir: + self._make_tree(test_dir, + ['drivers/net:ping.py', 'drivers/net:stats.py', + 'drivers/net/hw:rss_ctx.py', + 'drivers/net/perf:tput.py'], + ['drivers/net/hw', 'drivers/net/perf']) + + _write_net_config(test_dir, self.ENV) + + # Only net.config keys, not env-only vars or runner knobs + for subdir in ('drivers/net', 'drivers/net/hw', + 'drivers/net/perf'): + self.assertEqual(self._net_config(test_dir, subdir), + self.CONTENT) + + def test_unlisted_dir_skipped(self): + """drivers/net exists for the shared libs, but has no tests here.""" + from hw_worker import _write_net_config + + with tempfile.TemporaryDirectory() as test_dir: + self._make_tree(test_dir, ['drivers/net/perf:tput.py'], + ['drivers/net/lib/py', 'drivers/net/perf']) + + _write_net_config(test_dir, self.ENV) + + self.assertEqual(self._net_config(test_dir, 'drivers/net/perf'), + self.CONTENT) + self.assertIsNone(self._net_config(test_dir, 'drivers/net')) + + def test_missing_target_dir(self): + from hw_worker import _write_net_config + + with tempfile.TemporaryDirectory() as test_dir: + self._make_tree(test_dir, ['drivers/net/perf:tput.py'], []) + + _write_net_config(test_dir, self.ENV) + + self.assertFalse(os.path.exists( + os.path.join(test_dir, 'drivers/net/perf'))) + + def test_no_test_list(self): + """No kselftest-list.txt and no run_kselftest.sh: warn, don't crash.""" + from hw_worker import _write_net_config + + with tempfile.TemporaryDirectory() as test_dir: + os.makedirs(os.path.join(test_dir, 'drivers/net')) + + _write_net_config(test_dir, self.ENV) + + self.assertIsNone(self._net_config(test_dir, 'drivers/net')) + + def test_no_net_config_keys(self): + from hw_worker import _write_net_config + + with tempfile.TemporaryDirectory() as test_dir: + self._make_tree(test_dir, ['drivers/net:ping.py'], + ['drivers/net']) + + _write_net_config(test_dir, {'DISRUPTIVE': '0'}) + + self.assertIsNone(self._net_config(test_dir, 'drivers/net')) + + class TestTimeoutHandling(unittest.TestCase): @mock.patch('lib.runner.os.killpg') @mock.patch('subprocess.Popen')