Add integration tests for multi stressor support in stress-ng
PATCH 1/1 Add integration tests for multi stressor support in stress-ng
Add integrations test that checked the single-stressor and multi-stressor support as well as the cpuset logic in this context.
Assisted-by Claude Opus 4.8
Signed-off-by Sana Sharma <[email protected]>
--- tests/test_stressng.py | 310 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 301 insertions(+), 9 deletions(-)
tests/test_stressng.py +301 -9
@@ -3,18 +3,21 @@ # Copyright 2026 Sana Sharma <[email protected]> """ Unit tests for the new stressng input validation """ -import os import sys import shutil import tempfile import subprocess import unittest +from pathlib import Path +from types import SimpleNamespace from unittest.mock import patch, MagicMock -sys.path.insert(0,'.') +sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) from rteval.modules.loads import stressng from rteval.rtevalConfig import rtevalConfig from rteval.Log import Log +RTEVAL_CMD = str(Path(__file__).resolve().parent.parent / 'rteval-cmd') + _TEST_TMPDIRS = []
@@ -52,6 +55,7 @@ def make_stressng(**opts): cfg.AppendConfig('stressng', section) return stressng.Stressng(cfg.GetSection('stressng'), Log()) + class TestGetValidStressors(unittest.TestCase): """Test suite for the function get_valid_stressors"""
@@ -61,20 +65,21 @@ class TestGetValidStressors(unittest.TestCase): mock_run.return_value = MagicMock(stdout="cpu vm matrix\n") self.assertEqual(stressng.get_valid_stressors(), ['cpu', 'vm', 'matrix']) - @patch('rteval.modules.loads.stressng.subprocess.run', - side_effect=FileNotFoundError) - def test_not_installed_exits(self, _): + @patch('rteval.modules.loads.stressng.subprocess.run') + def test_not_installed_returns_empty(self, mock_run): """Test when stress-ng is not installed""" + mock_run.side_effect = FileNotFoundError('stress-ng') with self.assertRaises(SystemExit) as cm: stressng.get_valid_stressors() self.assertEqual(cm.exception.code, 1) - @patch('rteval.modules.loads.stressng.subprocess.run', - side_effect=subprocess.CalledProcessError(1, 'stress-ng')) - def test_query_failure_exits(self, _): + @patch('rteval.modules.loads.stressng.subprocess.run') + def test_query_failure_returns_empty(self, mock_run): """Test when the stress-ng query fails""" - with self.assertRaises(SystemExit): + mock_run.side_effect = subprocess.CalledProcessError(1, 'stress-ng') + with self.assertRaises(SystemExit) as cm: stressng.get_valid_stressors() + self.assertEqual(cm.exception.code, 1) class TestValidateStressor(unittest.TestCase): """Test suite for the function validate_stressor"""
@@ -93,6 +98,293 @@ class TestValidateStressor(unittest.TestCase): stressng.validate_stressors('bogus') self.assertEqual(cm.exception.code, 1) +""" +Tests for the stress-ng stressor functionality. +""" + +class TestSingleStressor(unittest.TestCase): + """Test suite for single stressor case""" + + def tearDown(self): + _cleanup_test_tmpdirs() + + def test_no_stressor_single(self): + """Test that the single stressor argument with no stressors throws the correct error""" + result = subprocess.run( + [RTEVAL_CMD, "--stressng-stressor"], + capture_output=True, + text=True + ) + assert result.returncode != 0 + assert "usage: rteval-cmd" in result.stderr + assert "rteval-cmd: error: argument --stressng-stressor: expected one argument" in result.stderr + + def test_default_worker_single(self): + """Test that for the single stressor the undefined workers defaults to 0""" + result = subprocess.run( + [RTEVAL_CMD, "--stressng-stressor", "cpu", "-d", "5"], + capture_output=True, + text=True + ) + assert result.returncode == 0 + assert "stressng: stress-ng --cpu 0 --taskset" in result.stdout + + def test_set_worker_single(self): + """Test that single stressor with a set number of workers passes the number through""" + result = subprocess.run( + [RTEVAL_CMD, "--stressng-stressor", "cpu", "--stressng-workers", "3", "-d", "5"], + capture_output=True, + text=True + ) + assert result.returncode == 0 + assert "stressng: stress-ng --cpu 3 --taskset" in result.stdout + + def test_single_with_multi(self): + """Tests that passing multiple stressors with the single stressor argument exits with an error""" + result = subprocess.run( + [RTEVAL_CMD, "--stressng-stressor", "cpu,vm", "--stressng-workers", "3"], + capture_output=True, + text=True + ) + assert result.returncode != 0 + assert "Invalid stress-ng stressor: 'cpu,vm'" in result.stdout + assert "Run 'stress-ng --stressors' to see valid options" in result.stdout + + def test_invalid_stressor_single(self): + """Tests that an invalid stressor name with the single stressor argument exits with an error""" + result = subprocess.run( + [RTEVAL_CMD, "--stressng-stressor", "bogus"], + capture_output=True, + text=True + ) + assert result.returncode != 0 + assert "Invalid stress-ng stressor: 'bogus'" in result.stdout + assert "Run 'stress-ng --stressors' to see valid options" in result.stdout + + def test_multiple_singles_with_invalid(self): + """Tests that an invalid stressor among repeated single stressor arguments exits with an error""" + result = subprocess.run( + [RTEVAL_CMD, "--stressng-stressor", "cpu", "--stressng-stressor", "bogus"], + capture_output=True, + text=True + ) + assert result.returncode != 0 + assert "Invalid stress-ng stressor: 'bogus'" in result.stdout + assert "Run 'stress-ng --stressors' to see valid options" in result.stdout + + def test_multiple_singles(self): + """Tests that when multiple single stressors are passed with single arguments the code runs all the stressors""" + result = subprocess.run( + [RTEVAL_CMD, "--stressng-stressor", "cpu", "--stressng-stressor", "vm", "--stressng-workers", "3", "-d", "5"], + capture_output=True, + text=True + ) + assert result.returncode == 0 + assert "stressng: stress-ng --cpu 3 --vm 3 --taskset" in result.stdout + +class TestMultiStressor(unittest.TestCase): + + def tearDown(self): + _cleanup_test_tmpdirs() + + def test_no_stressor_multi(self): + """Tests that multi stressors with no stressor name leads to an error""" + result = subprocess.run( + [RTEVAL_CMD, "--stressng-stressors"], + capture_output=True, + text=True + ) + assert result.returncode != 0 + assert "usage: rteval-cmd" in result.stderr + assert "rteval-cmd: error: argument --stressng-stressors: expected one argument" in result.stderr + + def test_one_stressor_multi(self): + """Tests that multiple stressors with one stressor argument works""" + result = subprocess.run( + [RTEVAL_CMD, "--stressng-stressors", "cpu", "-d", "5"], + capture_output=True, + text=True + ) + assert result.returncode == 0 + assert "stressng: stress-ng --cpu 0 --taskset" in result.stdout + + def test_multi_stressor_default_workers(self): + """Tests that the default workers is 0""" + result = subprocess.run( + [RTEVAL_CMD, "--stressng-stressors", "cpu,vm", "-d", "5"], + capture_output=True, + text=True + ) + assert result.returncode == 0 + assert "stressng: stress-ng --cpu 0 --vm 0 --taskset" in result.stdout + + def test_multi_stressor_set_workers(self): + """Tests that multiple stressors with a set number of workers passes correctly""" + result = subprocess.run( + [RTEVAL_CMD, "--stressng-stressors", "cpu,vm", "--stressng-workers", "2", "-d", "5"], + capture_output=True, + text=True + ) + assert result.returncode == 0 + assert "stressng: stress-ng --cpu 2 --vm 2 --taskset" in result.stdout + + def test_invalid_stressor_multi(self): + """Tests that an invalid stressor name with the multi stressor argument exits with an error""" + result = subprocess.run( + [RTEVAL_CMD, "--stressng-stressors", "bogus"], + capture_output=True, + text=True + ) + assert result.returncode != 0 + assert "Invalid stress-ng stressor: 'bogus'" in result.stdout + assert "Run 'stress-ng --stressors' to see valid options" in result.stdout + + def test_mixed_valid_invalid_multi(self): + """Tests that a mix of valid and invalid stressors exits with an error naming only the invalid one""" + result = subprocess.run( + [RTEVAL_CMD, "--stressng-stressors", "cpu,bogus"], + capture_output=True, + text=True + ) + assert result.returncode != 0 + assert "Invalid stress-ng stressor: 'bogus'" in result.stdout + assert "Invalid stress-ng stressor: 'cpu,bogus'" not in result.stdout + assert "Run 'stress-ng --stressors' to see valid options" in result.stdout + +class TestWorkloadPrepareCpuset(unittest.TestCase): + """ + Integration tests for the cpuset/taskset logic in _WorkloadPrepare. + + Each scenario is exercised for both ways of configuring stressors. + Stressng.__init__() normalizes both forms to a validated list stored in + cfg.stressors (see TestDonotrun), so that is what _WorkloadPrepare() + always sees: + * single stressor form -> ['cpu'] (from --stressng-stressor cpu) + * multi stressor form -> ['cpu', 'vm'] (from --stressng-stressors cpu,vm) + """ + + # stressor configs covered by every cpuset scenario: (label, cfg.stressors, + # expected --<flag> options that should land in args) + STRESSOR_FORMS = ( + ("single", ['cpu'], ['--cpu']), + ("multi", ['cpu', 'vm'], ['--cpu', '--vm']), + ) + + @staticmethod + def make_stressng(stressors, workers='0', timeout=None, cpulist=""): + """Build a Stressng instance without running the heavy __init__. + + Only the attributes read by _WorkloadPrepare() are populated. The + cfg.stressors attribute mirrors what __init__ would store: a + validated list of stressor names, for both the single- and + multi-stressor flags. + """ + inst = stressng.Stressng.__new__(stressng.Stressng) + inst.cfg = SimpleNamespace(stressors=stressors, workers=workers, + timeout=timeout) + inst.cpulist = cpulist + inst._logging = False + inst._log = MagicMock() + inst.args = None + return inst + + @staticmethod + def taskset_arg(args): + """Return the value following --taskset, or None if not present.""" + if '--taskset' not in args: + return None + return args[args.index('--taskset') + 1] + + @patch('rteval.modules.loads.stressng.get_valid_stressors', + return_value=['cpu', 'vm', 'matrix', 'io']) + @patch('rteval.modules.loads.stressng.os.open', return_value=42) + @patch('rteval.modules.loads.stressng.SysTopology') + def test_cpulist_restricts_taskset(self, mock_systop, _mock_open, _mock_valid): + """A cpulist keeps only its CPUs, intersected per node.""" + topo = mock_systop.return_value + topo.getnodes.return_value = [0, 1] + topo.getcpus.side_effect = lambda n: {0: [0, 1, 2, 3], + 1: [4, 5, 6, 7]}[n] + + for label, stressors, flags in self.STRESSOR_FORMS: + with self.subTest(form=label): + s = self.make_stressng(stressors=stressors, cpulist="0,1,4,5") + s._WorkloadPrepare() + + self.assertEqual(self.taskset_arg(s.args), "0,1,4,5") + for flag in flags: + self.assertIn(flag, s.args) + + @patch('rteval.modules.loads.stressng.get_valid_stressors', + return_value=['cpu', 'vm', 'matrix', 'io']) + @patch('rteval.modules.loads.stressng.os.open', return_value=42) + @patch('rteval.modules.loads.stressng.SysTopology') + def test_empty_node_is_dropped(self, mock_systop, _mock_open, _mock_valid): + """A node whose CPUs are all filtered out is removed from the taskset.""" + topo = mock_systop.return_value + topo.getnodes.return_value = [0, 1] + topo.getcpus.side_effect = lambda n: {0: [0, 1, 2, 3], + 1: [4, 5, 6, 7]}[n] + + # cpulist only matches CPUs on node 0, so node 1 has no usable CPUs + for label, stressors, flags in self.STRESSOR_FORMS: + with self.subTest(form=label): + s = self.make_stressng(stressors=stressors, cpulist="0,1") + s._WorkloadPrepare() + + self.assertEqual(self.taskset_arg(s.args), "0,1") + for flag in flags: + self.assertIn(flag, s.args) + + @patch('rteval.cpulist_utils.sysread', return_value="2-3") + @patch('rteval.cpulist_utils._isolated_file_exists', return_value=True) + @patch('rteval.modules.loads.stressng.get_valid_stressors', + return_value=['cpu', 'vm', 'matrix', 'io']) + @patch('rteval.modules.loads.stressng.os.open', return_value=42) + @patch('rteval.modules.loads.stressng.SysTopology') + def test_no_cpulist_excludes_isolated(self, mock_systop, _mock_open, + _mock_valid, _mock_isofile, _mock_sysread): + """Without a cpulist, isolated CPUs (2,3 here) are excluded.""" + topo = mock_systop.return_value + topo.getnodes.return_value = [0] + topo.getcpus.side_effect = lambda n: [0, 1, 2, 3] + + for label, stressors, flags in self.STRESSOR_FORMS: + with self.subTest(form=label): + s = self.make_stressng(stressors=stressors, cpulist="") + s._WorkloadPrepare() + + self.assertEqual(self.taskset_arg(s.args), "0,1") + for flag in flags: + self.assertIn(flag, s.args) + + @patch('rteval.modules.loads.stressng.get_valid_stressors', + return_value=['cpu', 'vm', 'matrix', 'io']) + @patch('rteval.modules.loads.stressng.os.open', return_value=42) + @patch('rteval.modules.loads.stressng.SysTopology') + def test_workers_and_timeout_appended(self, mock_systop, _mock_open, _mock_valid): + """Worker count and timeout are placed in args before --taskset.""" + topo = mock_systop.return_value + topo.getnodes.return_value = [0] + topo.getcpus.side_effect = lambda n: [0, 1] + + # single stressor: --cpu <workers> --timeout <t> ... + s = self.make_stressng(stressors=['cpu'], workers='3', timeout='30', + cpulist="0,1") + s._WorkloadPrepare() + self.assertEqual(s.args[:5], + ['stress-ng', '--cpu', '3', '--timeout', '30']) + self.assertEqual(self.taskset_arg(s.args), "0,1") + + # multi stressor: each stressor gets the worker count, then --timeout + s = self.make_stressng(stressors=['cpu', 'vm'], workers='2', timeout='30', + cpulist="0,1") + s._WorkloadPrepare() + self.assertEqual(s.args[:7], + ['stress-ng', '--cpu', '2', '--vm', '2', + '--timeout', '30']) + self.assertEqual(self.taskset_arg(s.args), "0,1") + class TestDonotrun(unittest.TestCase): """Whether the module opts in/out of running via _donotrun.
Signature · 1 line
2.56.1