From 51b919cc11c46af082068c63d7639a93baafd775 Mon Sep 17 00:00:00 2001 From: James Valleroy Date: Sun, 1 Sep 2024 07:27:15 -0400 Subject: [PATCH] storage: Handle grub-pc package not available On systems where the grub-pc package is not available (e.g. ARM), dpkg-query will have an exit status of 1. Handle the error that is raised in this case. Tests: - Added unit tests for storage._diagnose_grub_configured. - Tested on Raspberry Pi 4. Closes: #2441 Signed-off-by: James Valleroy Reviewed-by: Sunil Mohan Adapa --- plinth/modules/storage/__init__.py | 14 +++++-- plinth/modules/storage/tests/test_storage.py | 41 ++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/plinth/modules/storage/__init__.py b/plinth/modules/storage/__init__.py index 5fe21eca8..7c9529b3d 100644 --- a/plinth/modules/storage/__init__.py +++ b/plinth/modules/storage/__init__.py @@ -417,9 +417,17 @@ def _diagnose_grub_configured() -> DiagnosticCheck | None: install device is not selected. """ result = None - status = subprocess.check_output([ - 'dpkg-query', '--show', '--showformat=${db:Status-Abbrev}', 'grub-pc' - ]).decode().strip() + try: + status = subprocess.check_output([ + 'dpkg-query', '--show', '--showformat=${db:Status-Abbrev}', + 'grub-pc' + ]).decode().strip() + except subprocess.CalledProcessError as err: + if err.returncode == 1: + return None + + raise err + if status[0] != 'i': logger.info('grub-pc is not installed') return None diff --git a/plinth/modules/storage/tests/test_storage.py b/plinth/modules/storage/tests/test_storage.py index 232c2bdf1..a203aea5a 100644 --- a/plinth/modules/storage/tests/test_storage.py +++ b/plinth/modules/storage/tests/test_storage.py @@ -4,6 +4,7 @@ Test module for storage module operations. """ import contextlib +import dataclasses import re import subprocess import tempfile @@ -11,7 +12,9 @@ from unittest.mock import patch import psutil import pytest +from django.utils.translation import gettext_noop +from plinth.diagnostic_check import DiagnosticCheck, Result from plinth.modules import storage from plinth.modules.storage import privileged @@ -360,3 +363,41 @@ def test_is_partition_read_only(disk_partitions): assert not storage.is_partition_read_only('/') assert not storage.is_partition_read_only('/foo') assert storage.is_partition_read_only('/bar') + + +@patch('subprocess.check_output') +def test_diagnose_grub_configured(check_output): + """Test whether checking grub-pc package configuration failed works.""" + diagnose = storage._diagnose_grub_configured \ + # pylint: disable=protected-access + + passed = DiagnosticCheck('storage-grub-configured', + gettext_noop('grub package is configured'), + Result.PASSED) + failed = dataclasses.replace(passed, result=Result.FAILED) + warning = dataclasses.replace(passed, result=Result.WARNING) + + # installed and configured + check_output.return_value = b'ii ' + assert diagnose() == passed + + # failed configuration + check_output.return_value = b'iF ' + assert diagnose() == failed + + # should be installed, but somehow is not + check_output.return_value = b'in ' + assert diagnose() == warning + + # not installed + check_output.return_value = b'un ' + assert diagnose() is None + + # grub-pc package is not available (e.g. ARM devices) + check_output.side_effect = subprocess.CalledProcessError( + cmd=[ + 'dpkg-query', '--show', '--showformat=${db:Status-Abbrev}', + 'grub-pc' + ], returncode=1, + stderr=b'dpkg-query: no packages found matching grub-pc\n') + assert diagnose() is None