dmidecode: add wrapper for `dmidecode -s`

This commit is contained in:
Dan Bungert 2023-07-26 13:25:57 -06:00
parent 037ec648ff
commit c8c87de19a
2 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,27 @@
# Copyright 2023 Canonical, Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from subiquitycore.utils import run_command
def dmidecode_get(key: str) -> str:
"""Retrieve a value from `dmidecode -s`
:param key: dmidecode(8) KEYWORD
:returns: stripped string output, or empty string on failure
"""
sp = run_command(["dmidecode", "-s", key], check=False, text=True)
if sp.returncode == 0:
return sp.stdout.strip()
return ""

View File

@ -0,0 +1,33 @@
# Copyright 2023 Canonical, Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import unittest
from subprocess import CompletedProcess
from unittest import mock
from subiquity.common.dmidecode import dmidecode_get
class TestDmidecode(unittest.TestCase):
@mock.patch("subiquity.common.dmidecode.run_command")
def test_fail(self, run_cmd):
run_cmd.return_value = CompletedProcess([], 1)
self.assertEqual("", dmidecode_get("invalid-key"))
@mock.patch("subiquity.common.dmidecode.run_command")
def test_poweredge(self, run_cmd):
expected = "PowerEdge R6525"
run_cmd.return_value = CompletedProcess([], 0, stdout=expected)
self.assertEqual(expected, dmidecode_get("system-product-name"))