diff --git a/kernelci/runtime/lava.py b/kernelci/runtime/lava.py index c7a231727d..e5b865e542 100644 --- a/kernelci/runtime/lava.py +++ b/kernelci/runtime/lava.py @@ -339,39 +339,38 @@ def __init__(self, configs, kcictx=None, **kwargs): self._context = kcictx self._server = self._connect() + def _resolve_priority(self, value, default): + """Resolve a priority value (string, number, or None) to an integer.""" + if value is None: + return default + if isinstance(value, str): + priority_map = { + 'high': self.PRIORITY_HIGH, + 'medium': self.PRIORITY_MEDIUM, + 'low': self.PRIORITY_LOW, + } + return priority_map.get(value.lower(), default) + return max(0, min(100, int(value))) + def _get_priority(self, job): node = job.node submitter = node.get('submitter') - priority_map = { - 'high': self.PRIORITY_HIGH, - 'medium': self.PRIORITY_MEDIUM, - 'low': self.PRIORITY_LOW, - } - if submitter and submitter != self.SERVICE_PIPELINE: - # Human submission - check for user-specified priority user_priority = node.get('data', {}).get('priority') - if user_priority: - if isinstance(user_priority, str): - priority = priority_map.get(user_priority.lower(), self.PRIORITY_HIGHEST) - elif isinstance(user_priority, (int, float)): - priority = max(0, min(100, user_priority)) - else: - priority = self.PRIORITY_HIGHEST - else: - priority = self.PRIORITY_HIGHEST + priority = self._resolve_priority( + user_priority, self.PRIORITY_HIGHEST + ) else: - # Pipeline submission - use tree priority tree_priority = node.get('data', {}).get('tree_priority') - - if tree_priority and isinstance(tree_priority, str): - priority = priority_map.get(tree_priority.lower(), self.PRIORITY_LOW) - elif tree_priority and isinstance(tree_priority, (int, float)): - priority = max(0, min(100, tree_priority)) + if tree_priority is not None: + priority = self._resolve_priority( + tree_priority, self.PRIORITY_LOW + ) else: - job_priority = job.config.priority - priority = job_priority if job_priority is not None else self.PRIORITY_LOW + priority = self._resolve_priority( + job.config.priority, self.PRIORITY_LOW + ) if self.config.priority: priority = int(priority * self.config.priority / 100) diff --git a/tests/test_runtime.py b/tests/test_runtime.py index efc8842f6a..52d2615adf 100644 --- a/tests/test_runtime.py +++ b/tests/test_runtime.py @@ -129,6 +129,30 @@ def test_lava_priority_hierarchy(): assert lab._get_priority(job_human_set_numeric) == expected_priority +def test_lava_priority_job_config_fallback(): + """Test priority fallback to job.config.priority with string values""" + config = kernelci.config.load('tests/configs/lava-runtimes.yaml') + runtimes = config['runtimes'] + runtime_config = runtimes['lab-min-12-max-40-new-runtime'] + lab = kernelci.runtime.get_runtime(runtime_config) + + job_config_medium = type('JobConfig', (), {'priority': 'medium'})() + job_fallback_str = type('Job', (), { + 'node': {'data': {}, 'submitter': 'service:pipeline'}, + 'config': job_config_medium + })() + expected_priority = int(12 + (40 - 12) * 40 / 100) + assert lab._get_priority(job_fallback_str) == expected_priority + + job_config_high = type('JobConfig', (), {'priority': 'high'})() + job_fallback_high = type('Job', (), { + 'node': {'data': {}, 'submitter': 'service:pipeline'}, + 'config': job_config_high + })() + expected_priority = int(12 + (40 - 12) * 60 / 100) + assert lab._get_priority(job_fallback_high) == expected_priority + + def test_lava_priority_scale(): """Test the logic for determining the priority of LAVA jobs""" config = kernelci.config.load('tests/configs/lava-runtimes.yaml')