Olivier Gayot has proposed merging ~ogayot/curtin:fix-curthooks-no-kernel into 
curtin:master.

Commit message:
curthooks: fix exception when passing 'kernel: null'

When we don't want curthooks to install a kernel, we can pass:

  kernel: null

However, currently, it makes the code raise an exception because we run
.get() on the kernel object even if it is None.

 config.merge_config(mapping, kernel_cfg.get('mapping', {}))
 AttributeError: 'NoneType' object has no attribute 'get'
 'NoneType' object has no attribute 'get'
    
Fixed by returning gracefully without installing any kernel upon
encountering this configuration.
    
    Signed-off-by: Olivier Gayot <olivier.ga...@canonical.com>


Requested reviews:
  curtin developers (curtin-dev)
Related bugs:
  Bug #2026225 in curtin: "failed kernel mapping specification"
  https://bugs.launchpad.net/curtin/+bug/2026225

For more details, see:
https://code.launchpad.net/~ogayot/curtin/+git/curtin/+merge/446197

When we specify 'kernel: null' in the curthooks configuration, curtin raises an 
exception:

builtin_curthooks
     install_kernel(cfg, target)
   File 
"/snap/ubuntu-desktop-installer/x1/lib/python3.10/site-packages/curtin/commands/curthooks.py",
 line 375, in install_kernel
     config.merge_config(mapping, kernel_cfg.get('mapping', {}))
 AttributeError: 'NoneType' object has no attribute 'get'
 'NoneType' object has no attribute 'get'
 curtin: Installation failed with exception: Unexpected error while running 
command.
-- 
Your team curtin developers is requested to review the proposed merge of 
~ogayot/curtin:fix-curthooks-no-kernel into curtin:master.
diff --git a/curtin/commands/curthooks.py b/curtin/commands/curthooks.py
index c52d5ab..b0844d5 100644
--- a/curtin/commands/curthooks.py
+++ b/curtin/commands/curthooks.py
@@ -365,12 +365,13 @@ def install_kernel(cfg, target):
     kernel_cfg = cfg.get('kernel', {'package': None,
                                     'fallback-package': "linux-generic",
                                     'mapping': {}})
-    if kernel_cfg is not None:
-        kernel_package = kernel_cfg.get('package')
-        kernel_fallback = kernel_cfg.get('fallback-package')
-    else:
-        kernel_package = None
-        kernel_fallback = None
+
+    if kernel_cfg is None:
+        LOG.debug("Not installing any kernel since kernel: null was specified")
+        return
+
+    kernel_package = kernel_cfg.get('package')
+    kernel_fallback = kernel_cfg.get('fallback-package')
 
     mapping = copy.deepcopy(KERNEL_MAPPING)
     config.merge_config(mapping, kernel_cfg.get('mapping', {}))
diff --git a/tests/unittests/test_curthooks.py b/tests/unittests/test_curthooks.py
index d6b5445..5866c43 100644
--- a/tests/unittests/test_curthooks.py
+++ b/tests/unittests/test_curthooks.py
@@ -94,6 +94,17 @@ class TestCurthooksInstallKernel(CiTestCase):
             self.mock_instpkg.assert_called_with(
                 [kernel_package], target=self.target, env=env)
 
+    def test__installs_kernel_null(self):
+        kernel_cfg = {'kernel': None}
+        self.mock_get_flash_kernel_pkgs.return_value = None
+
+        with patch.dict(os.environ, clear=True):
+            curthooks.install_kernel(kernel_cfg, self.target)
+
+            env = {'FK_FORCE': 'yes', 'FK_FORCE_CONTAINER': 'yes'}
+
+            self.mock_instpkg.assert_not_called()
+
 
 class TestEnableDisableUpdateInitramfs(CiTestCase):
 
-- 
Mailing list: https://launchpad.net/~curtin-dev
Post to     : curtin-dev@lists.launchpad.net
Unsubscribe : https://launchpad.net/~curtin-dev
More help   : https://help.launchpad.net/ListHelp

Reply via email to