← Back to team overview

sts-sponsors team mailing list archive

[Merge] ~alexsander-souza/maas:lp2022833_fix_machine_hints into maas:master

 

Alexsander de Souza has proposed merging ~alexsander-souza/maas:lp2022833_fix_machine_hints into maas:master.

Commit message:
improve Machine Hints matching
    
on Power machines, 'motherboard' is None
    
fixes LP#2022833


Requested reviews:
  MAAS Maintainers (maas-maintainers)
Related bugs:
  Bug #2022833 in MAAS: "machine-config-hints fails on Power machines"
  https://bugs.launchpad.net/maas/+bug/2022833

For more details, see:
https://code.launchpad.net/~alexsander-souza/maas/+git/maas/+merge/444074
-- 
Your team MAAS Committers is subscribed to branch maas:master.
diff --git a/src/metadataserver/builtin_scripts/commissioning_scripts/40-maas-01-machine-config-hints b/src/metadataserver/builtin_scripts/commissioning_scripts/40-maas-01-machine-config-hints
index 889f604..1a9ff56 100644
--- a/src/metadataserver/builtin_scripts/commissioning_scripts/40-maas-01-machine-config-hints
+++ b/src/metadataserver/builtin_scripts/commissioning_scripts/40-maas-01-machine-config-hints
@@ -27,11 +27,15 @@
 # script_type: commissioning
 # timeout: 60
 # --- End MAAS 1.0 script metadata ---
+from functools import reduce
 import json
 import os
 import sys
 
 
+def get_by_path(dataDict, mapList):
+    return reduce(lambda a,b : a.get(b,{}), mapList, dataDict)
+
 def read_json_file(path):
     try:
         with open(path) as fd:
@@ -44,15 +48,11 @@ def read_json_file(path):
 
 def detect_nvidia_dgx(machine_resources):
     """Returns whether machine-resources output suggests a DGX system"""
-    if "resources" not in machine_resources:
-        return False
-    if "system" not in machine_resources["resources"]:
-        return False
-    if "motherboard" not in machine_resources["resources"]["system"]:
+    motherboard = get_by_path(machine_resources, ["resources", "system", "motherboard"])
+    if not motherboard:
         return False
-    motherboard = machine_resources["resources"]["system"]["motherboard"]
-    vendor = motherboard.get("vendor", None)
-    product = motherboard.get("product", None)
+    vendor = motherboard.get("vendor", "")
+    product = motherboard.get("product", "")
     return (
         vendor.lower() == "nvidia"
         and product.lower().removeprefix("nvidia ") == "dgx"
diff --git a/src/metadataserver/builtin_scripts/commissioning_scripts/tests/test_machine_config_hints.py b/src/metadataserver/builtin_scripts/commissioning_scripts/tests/test_machine_config_hints.py
new file mode 100644
index 0000000..6f385e7
--- /dev/null
+++ b/src/metadataserver/builtin_scripts/commissioning_scripts/tests/test_machine_config_hints.py
@@ -0,0 +1,63 @@
+# Copyright 2023 Canonical Ltd.  This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+from importlib.machinery import SourceFileLoader
+from importlib.util import module_from_spec, spec_from_loader
+
+from maastesting.factory import factory
+from maastesting.testcase import MAASTestCase
+
+spec = spec_from_loader(
+    "machine_hint",
+    SourceFileLoader(
+        "machine_hint",
+        "src/metadataserver/builtin_scripts/commissioning_scripts/40-maas-01-machine-config-hints",
+    ),
+)
+machine_hint = module_from_spec(spec)
+spec.loader.exec_module(machine_hint)
+
+
+class TestMachineHints(MAASTestCase):
+    def test_provide_hints_generic(self):
+        self.assertEqual(
+            {"platform": "generic", "tags": []},
+            machine_hint.provide_hints({}),
+        )
+        self.assertEqual(
+            {"platform": "generic", "tags": []},
+            machine_hint.provide_hints(
+                {"resources": {"system": {"motherboard": None}}}
+            ),
+        )
+        self.assertEqual(
+            {"platform": "generic", "tags": []},
+            machine_hint.provide_hints(
+                {
+                    "resources": {
+                        "system": {
+                            "motherboard": {
+                                "vendor": factory.make_string(),
+                                "product": factory.make_string(),
+                            }
+                        }
+                    }
+                }
+            ),
+        )
+
+    def test_provide_hints_nvidia(self):
+        dgx_res = {
+            "resources": {
+                "system": {
+                    "motherboard": {
+                        "vendor": "nvidia",
+                        "product": "nvidia dgx",
+                    }
+                }
+            }
+        }
+        self.assertEqual(
+            {"platform": "nvidia-dgx", "tags": []},
+            machine_hint.provide_hints(dgx_res),
+        )

Follow ups