Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 38 additions & 16 deletions contest/hw/hw_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -406,14 +407,48 @@ 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.

The hwksft orchestrator deploys nic-test.env with interface names,
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:
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions contest/hw/lib/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
86 changes: 86 additions & 0 deletions contest/hw/tests/test_hw_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down