Skip to contentSkip to conversation

rteval: Added integration tests multi stressor support in stress-ng

PATCH Superseded Sana Sharma 2 participants 2 messages Oct 30 – Nov 3

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rteval: Added integration tests multi stressor support in stress-ng

Added 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
---
 tests/test_stressng.py | 239 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 238 insertions(+), 1 deletion(-)
tests/test_stressng.py +238 -1
index 29291e6..d684ff6 100644
@@ -6,8 +6,11 @@
 import sys
 import subprocess
 import unittest
+from types import SimpleNamespace
 from unittest.mock import patch, MagicMock
-sys.path.insert(0,'.')
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
 from rteval.modules.loads import stressng
 
 class TestGetValidStressors(unittest.TestCase):
@@ -50,6 +53,240 @@ class TestValidateStressor(unittest.TestCase):
         with self.assertRaises(ValueError):    
             stressng.validate_stressors('bogus')
 
+
+"""Integration Tests for stress_ng stressor functionality"""
+
+class TestSingleStressor(unittest.TestCase):
+    """Test suite for single stressor case"""
+
+    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"],
+            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"],
+            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 raises a Value Error """
+        result = subprocess.run(
+            ["./rteval-cmd", "--stressng-stressor", "cpu,vm", "--stressng-workers", "3"],
+            capture_output=True,
+            text=True
+        )
+        assert result.returncode != 0
+        assert "ValueError: Invalid stress-ng stressors: cpu,vm. Run 'stress-ng --stressors' to see valid options." in result.stderr
+
+    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"],
+            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 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"],
+            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"],
+            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"],
+            capture_output=True,
+            text=True
+        )
+        assert result.returncode == 0
+        assert "stressng: stress-ng --cpu 2 --vm 2 --taskset" 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:
+      * single stressor form  -> cfg.stressors is a list, e.g. ['cpu']
+        (this is what __init__ produces from --stressng-stressor)
+      * multi stressor form   -> cfg.stressors is a comma-separated string,
+        e.g. 'cpu,vm' (this comes from --stressng-stressors)
+    """
+
+    # 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 list for
+        the single-stressor flag, or a comma-separated string for the
+        multi-stressor flag.
+        """
+        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")
+
+
 def main():
     """Run the test suite"""
     unittest.main(verbosity=2)
Signature · 5 lines
2.54.0


-- 
Sana Sharma <[email protected]>
#1 lore raw
Quoted 1 line · Sana Sharma

[PATCH v1] rteval: Added integration tests multi stressor support in stress-ng

Thanks for the patch Sana. Two fixes for a v2, and then a request for a follow-up patch.

Fixes for this patch:

1. Don't rely on the 60s default duration. These tests only check the stress-ng command line that rteval builds, which does not depend on run length, so pass a short explicit -d. I verified -d 5 runs cleanly (exit 0, command logged); even -d 1 worked, but 5 leaves a little margin. As written, each run-based test takes the full 60 seconds.

2. Don't depend on the default measurement module. With no --measurement-module the tests default to timerlat/rtla; on a machine that only has cyclictest the run exits non-zero and the "returncode == 0" asserts fail. Pass --measurement-module explicitly (cyclictest is a safe choice for a test), or otherwise guarantee a measurement tool is present.

Follow-up patch (separate from the v2 above):

The happy paths are well covered, but I'd like a follow-up patch adding negative / corner-case coverage. These are cheap to write as rootless unit tests - like the existing TestWorkloadPrepareCpuset and the validate_stressors tests - rather than as more full runs:

- partial-invalid list, e.g. cpu,bogus -> ValueError naming only 'bogus'
- multiple invalid, e.g. bogus,fake -> both listed
- empty / trailing comma / bare comma ("cpu,", "", ",") - today these raise with an ugly message ("Invalid stress-ng stressors: ."); filter empties, give a clear message, and test it
- case sensitivity (CPU) - confirm it is intentionally rejected
- both --stressng-stressor and --stressng-stressors together - test that the multi form (--stressng-stressors) wins
- neither flag -> module is skipped (_donotrun)
- duplicates, e.g. cpu,cpu -> today this builds "--cpu 0 --cpu 0"; decide whether to de-duplicate or warn, then test it
- non-numeric / negative --stressng-workers -> either validate it, or document that it is passed straight through to stress-ng

Separately (and this one predates your series, so definitely its own patch): get_valid_stressors() currently sys.exit(1)s when stress-ng is missing or the query fails, and TestGetValidStressors locks that in. That is heavy-handed for a load module and is why these tests can't run where stress-ng is absent. It would be nicer to return [] and have the validators pass names through (let stress-ng report bad ones), updating the two existing tests to match - but that is a cleanup of existing code, not something to pile onto this series.

No need to fold all of that into the v2 - a separate patch on top is fine.

John