summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorChristian Breunig <christian@breunig.cc>2026-08-29 12:32:57 +0000
committerChristian Breunig <christian@breunig.cc>2026-08-29 12:32:57 +0000
commitcf018cdd38eb30830c55d54591238b85516e5c1a (patch)
tree82eb9c57524ad00f7d21d6a2e025f074417e3aed /python
parent90b7de7e084f86450d5ac42b005901972f362999 (diff)
downloadvyos-1x-cf018cdd38eb30830c55d54591238b85516e5c1a.tar.gz
vyos-1x-cf018cdd38eb30830c55d54591238b85516e5c1a.zip
smoketest: T9014: add CPU architecture decorator to guard tests
VyOS supports both amd64 and arm64 CPU architectures, but not every smoketest is applicable to both of them. Add a cpu_arch() decorator to vyos.utils.cpu which takes a list of supported CPU architectures - if the test is run on any other architecture it is skipped with a message naming both the supported and the current architecture. Tests without the decorator run everywhere. Use it to replace the ad-hoc IS_ARM64 checks in test_kernel_options.py and to guard the x86 only intel and intel_qat module sets in test_module_load.py, which so far failed on arm64.
Diffstat (limited to 'python')
-rw-r--r--python/vyos/utils/cpu.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/python/vyos/utils/cpu.py b/python/vyos/utils/cpu.py
index a4c0f2d54..f31d4a932 100644
--- a/python/vyos/utils/cpu.py
+++ b/python/vyos/utils/cpu.py
@@ -120,3 +120,55 @@ def get_available_cpus():
def get_half_cpus():
""" return 1/2 of the numbers of available CPUs """
return max(1, os.cpu_count() // 2)
+
+
+# Aliases for the architecture names reported by platform.machine()
+_arch_alias = {
+ 'x86_64': 'amd64',
+ 'amd64': 'amd64',
+ 'aarch64': 'arm64',
+ 'arm64': 'arm64',
+}
+
+def _normalize_arch(arch):
+ return _arch_alias.get(arch.lower(), arch.lower())
+
+def get_cpu_arch():
+ """ Returns the normalized CPU architecture of the running system,
+ e.g. "amd64" or "arm64". Architectures we do not know about are
+ returned as reported by platform.machine()
+ """
+ import platform
+
+ return _normalize_arch(platform.machine())
+
+def cpu_arch(*architectures):
+ """ Decorator restricting a unittest TestCase or test method to a list
+ of CPU architectures. If the current architecture is not supported,
+ the test is skipped instead of failing.
+
+ Both the native names (x86_64, aarch64) and the Debian names
+ (amd64, arm64) are accepted.
+
+ Usage:
+ @cpu_arch('amd64')
+ def test_intel_modules(self):
+ ...
+
+ @cpu_arch('amd64', 'arm64')
+ class TestFoo(unittest.TestCase):
+ ...
+ """
+ import unittest
+
+ # Allow both cpu_arch('amd64', 'arm64') and cpu_arch(['amd64', 'arm64'])
+ if len(architectures) == 1 and isinstance(architectures[0], (list, tuple, set)):
+ architectures = tuple(architectures[0])
+
+ supported = {_normalize_arch(arch) for arch in architectures}
+ current = get_cpu_arch()
+
+ reason = (f'Only supported on CPU architecture: {", ".join(sorted(supported))} '
+ f'(running on {current})')
+
+ return unittest.skipUnless(current in supported, reason)