From 6e48aae1ec8eb2bd6da68b64f1510830fdc35dab Mon Sep 17 00:00:00 2001
From: Alok Ranjan <alok.ranjan@linaro.org>
Date: Mon, 25 Nov 2024 18:56:20 +0530
Subject: [PATCH 1/2] jobs: refactor lava_job_priority

Move the validation of lava_job_priority to jobs.
Just one check in jobs instead of doing it in all the different devices.

Signed-off-by: Alok Ranjan <alok.ranjan@linaro.org>
---
 tuxlava/devices/avh.py      |  8 --------
 tuxlava/devices/fastboot.py | 16 ----------------
 tuxlava/devices/fvp.py      | 17 -----------------
 tuxlava/devices/nfs.py      |  9 ---------
 tuxlava/devices/qemu.py     |  9 ---------
 tuxlava/devices/ssh.py      |  9 ---------
 tuxlava/jobs.py             | 12 ++++++++++++
 7 files changed, 12 insertions(+), 68 deletions(-)

diff --git a/tuxlava/devices/avh.py b/tuxlava/devices/avh.py
index 6037d64662f9..4434f62d3a53 100644
--- a/tuxlava/devices/avh.py
+++ b/tuxlava/devices/avh.py
@@ -106,14 +106,6 @@ class AvhDevice(Device):
 
         kwargs["redirect_to_kmsg"] = self.redirect_to_kmsg
 
-        tmp_ljp = kwargs.get("parameters").get("LAVA_JOB_PRIORITY") or 50
-        if "LAVA_JOB_PRIORITY" in kwargs.get("parameters").keys():
-            if int(tmp_ljp) > 100 or int(tmp_ljp) <= 0:
-                raise InvalidArgument(
-                    "argument --parameters LAVA_JOB_PRIORITY must be a value between 1-100"
-                )
-        kwargs["LAVA_JOB_PRIORITY"] = tmp_ljp
-
         # render the template
         tests = [
             t.render(
diff --git a/tuxlava/devices/fastboot.py b/tuxlava/devices/fastboot.py
index ffa74b4ade3b..44817ff57222 100644
--- a/tuxlava/devices/fastboot.py
+++ b/tuxlava/devices/fastboot.py
@@ -122,14 +122,6 @@ class FastbootDevice(Device):
         kwargs["command_name"] = slugify(
             kwargs.get("parameters").get("command-name", "command")
         )
-
-        tmp_ljp = kwargs.get("parameters").get("LAVA_JOB_PRIORITY") or 50
-        if "LAVA_JOB_PRIORITY" in kwargs.get("parameters").keys():
-            if int(tmp_ljp) > 100 or int(tmp_ljp) <= 0:
-                raise InvalidArgument(
-                    "argument --parameters LAVA_JOB_PRIORITY must be a value between 1-100"
-                )
-        kwargs["LAVA_JOB_PRIORITY"] = tmp_ljp
         kwargs["tags"] = self.tags
 
         # populate all other parameters supplied
@@ -294,14 +286,6 @@ class FastbootAOSPDevice(Device):
             kwargs.get("parameters").get("command-name", "command")
         )
 
-        tmp_ljp = kwargs.get("parameters").get("LAVA_JOB_PRIORITY") or 50
-        if "LAVA_JOB_PRIORITY" in kwargs.get("parameters").keys():
-            if int(tmp_ljp) > 100 or int(tmp_ljp) <= 0:
-                raise InvalidArgument(
-                    "argument --parameters LAVA_JOB_PRIORITY must be a value between 1-100"
-                )
-        kwargs["LAVA_JOB_PRIORITY"] = tmp_ljp
-
         if "TUXSUITE_BAKE_VENDOR_DOWNLOAD_URL" not in kwargs.get("parameters").keys():
             raise InvalidArgument(
                 "argument --parameters TUXSUITE_BAKE_VENDOR_DOWNLOAD_URL must be provided"
diff --git a/tuxlava/devices/fvp.py b/tuxlava/devices/fvp.py
index 306f9d9a78a7..a18eb7012e03 100644
--- a/tuxlava/devices/fvp.py
+++ b/tuxlava/devices/fvp.py
@@ -96,15 +96,6 @@ class AEMvAFVPDevice(FVPDevice):
         )
 
         kwargs["redirect_to_kmsg"] = self.redirect_to_kmsg
-
-        tmp_ljp = kwargs.get("parameters").get("LAVA_JOB_PRIORITY") or 50
-        if "LAVA_JOB_PRIORITY" in kwargs.get("parameters").keys():
-            if int(tmp_ljp) > 100 or int(tmp_ljp) <= 0:
-                raise InvalidArgument(
-                    "argument --parameters LAVA_JOB_PRIORITY must be a value between 1-100"
-                )
-        kwargs["LAVA_JOB_PRIORITY"] = tmp_ljp
-
         # render the template
         tests = [
             t.render(
@@ -206,14 +197,6 @@ class MorelloFVPDevice(FVPDevice):
         kwargs["support_tests"] = self.support_tests
         kwargs["boot_timeout"] = self.boot_timeout
 
-        tmp_ljp = kwargs.get("parameters").get("LAVA_JOB_PRIORITY") or 50
-        if "LAVA_JOB_PRIORITY" in kwargs.get("parameters").keys():
-            if int(tmp_ljp) > 100 or int(tmp_ljp) <= 0:
-                raise InvalidArgument(
-                    "argument --parameters LAVA_JOB_PRIORITY must be a value between 1-100"
-                )
-        kwargs["LAVA_JOB_PRIORITY"] = tmp_ljp
-
         # render the template
         tests = [
             t.render(
diff --git a/tuxlava/devices/nfs.py b/tuxlava/devices/nfs.py
index 154fea107238..5acc3713f1ac 100644
--- a/tuxlava/devices/nfs.py
+++ b/tuxlava/devices/nfs.py
@@ -110,17 +110,8 @@ class NfsDevice(Device):
         kwargs["command_name"] = slugify(
             kwargs.get("parameters").get("command-name", "command")
         )
-
         kwargs["redirect_to_kmsg"] = self.redirect_to_kmsg
 
-        tmp_ljp = kwargs.get("parameters").get("LAVA_JOB_PRIORITY") or 50
-        if "LAVA_JOB_PRIORITY" in kwargs.get("parameters").keys():
-            if int(tmp_ljp) > 100 or int(tmp_ljp) <= 0:
-                raise InvalidArgument(
-                    "argument --parameters LAVA_JOB_PRIORITY must be a value between 1-100"
-                )
-        kwargs["LAVA_JOB_PRIORITY"] = tmp_ljp
-
         for key in kwargs.get("parameters").keys():
             kwargs[key] = kwargs.get("parameters").get(key)
 
diff --git a/tuxlava/devices/qemu.py b/tuxlava/devices/qemu.py
index f27e040623a4..a3ab827b3524 100644
--- a/tuxlava/devices/qemu.py
+++ b/tuxlava/devices/qemu.py
@@ -132,17 +132,8 @@ class QemuDevice(Device):
         kwargs["command_name"] = slugify(
             kwargs.get("parameters").get("command-name", "command")
         )
-
         kwargs["redirect_to_kmsg"] = self.redirect_to_kmsg
 
-        tmp_ljp = kwargs.get("parameters").get("LAVA_JOB_PRIORITY") or 50
-        if "LAVA_JOB_PRIORITY" in kwargs.get("parameters").keys():
-            if int(tmp_ljp) > 100 or int(tmp_ljp) <= 0:
-                raise InvalidArgument(
-                    "argument --parameters LAVA_JOB_PRIORITY must be a value between 1-100"
-                )
-        kwargs["LAVA_JOB_PRIORITY"] = tmp_ljp
-
         # render the template
         tests = [
             t.render(
diff --git a/tuxlava/devices/ssh.py b/tuxlava/devices/ssh.py
index e409bf0f3e97..e6390670704d 100644
--- a/tuxlava/devices/ssh.py
+++ b/tuxlava/devices/ssh.py
@@ -54,15 +54,6 @@ class SSHDevice(Device):
         )
 
         kwargs["redirect_to_kmsg"] = self.redirect_to_kmsg
-
-        tmp_ljp = kwargs.get("parameters").get("LAVA_JOB_PRIORITY") or 50
-        if "LAVA_JOB_PRIORITY" in kwargs.get("parameters").keys():
-            if int(tmp_ljp) > 100 or int(tmp_ljp) <= 0:
-                raise InvalidArgument(
-                    "argument --parameters LAVA_JOB_PRIORITY must be a value between 1-100"
-                )
-        kwargs["LAVA_JOB_PRIORITY"] = tmp_ljp
-
         # render the template
         tests = [
             t.render(
diff --git a/tuxlava/jobs.py b/tuxlava/jobs.py
index fdcff5b2b510..c5a0313340c1 100644
--- a/tuxlava/jobs.py
+++ b/tuxlava/jobs.py
@@ -131,6 +131,17 @@ class Job:
         tests = "_".join(self.tests) if self.tests else "boot"
         return f"Job {self.device}/{tests}"
 
+    @property
+    def lava_job_priority(self):
+        priority = (
+            self.parameters.get("LAVA_JOB_PRIORITY", 50) if self.parameters else 50
+        )
+        if int(priority) > 100 or int(priority) <= 0:
+            raise InvalidArgument(
+                "argument --parameters LAVA_JOB_PRIORITY must be a value between 1-100"
+            )
+        return priority
+
     def render(self) -> str:
         # Render the job definition
         overlays = []
@@ -251,6 +262,7 @@ class Job:
             "boot_args": self.boot_args,
             "secrets": self.secrets,
             "deploy_os": self.deploy_os,
+            "LAVA_JOB_PRIORITY": self.lava_job_priority,
         }
         definition = self.device.definition(**def_arguments)
         return definition
-- 
2.45.2

