Skip to content

Commit

Permalink
Merge pull request #630 from NebraLtd/shawaj/serial
Browse files Browse the repository at this point in the history
serial: change serial number check
  • Loading branch information
shawaj authored Jun 29, 2023
2 parents a1ce39d + c4aa8af commit 8ca36df
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 14 deletions.
72 changes: 61 additions & 11 deletions hw_diag/tests/test_get_serial_number.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,91 @@
import unittest
import sys
import pytest

from unittest.mock import mock_open, patch
sys.path.append("..")
from hw_diag.utilities.hardware import get_serial_number # noqa
from hw_diag.utilities.hardware import get_serial_number, load_serial_number, \
load_cpu_info

TEST_SERIAL = "00000000a3e7kg80"

class TestGetSerialNumber(unittest.TestCase):
TEST_CPU_INFO = """
processor : 0
BogoMIPS : 48.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x0
CPU part : 0xd04
CPU revision : 2
Serial : 912558f1a3ae877d
"""

TEST_DATA = "00000000a3e7kg80"
TEST_SERIAL_NUMBER_RESULT = {'serial': '00000000a3e7kg80'}
TEST_CPU_INFO_RESULT = {'serial': '912558f1a3ae877d'}
FAILED_CPU_INFO_RESULT = {}
FAILED_SERIAL_NUMBER_RESULT = {}

class TestGetSerialNumber(unittest.TestCase):

right_value = {'serial_number': '00000000a3e7kg80'}
diag = {}

@pytest.fixture(autouse=True)
def _pass_fixtures(self, caplog):
self.caplog = caplog

def test_get_serialnumber(self):
m = mock_open(read_data=self.TEST_DATA)
with patch('builtins.open', m):
m = mock_open(read_data=TEST_SERIAL)
with patch("builtins.open", m):
get_serial_number(self.diag)
self.assertEqual(self.diag["serial_number"],
self.right_value["serial_number"])

def test_strip_serialnumber(self):
m = mock_open(read_data="%s\x00" % self.TEST_DATA)
with patch('builtins.open', m):
m = mock_open(read_data="%s\x00" % TEST_SERIAL)
with patch("builtins.open", m):
get_serial_number(self.diag)
self.assertEqual(self.diag["serial_number"],
self.right_value["serial_number"])

def test_available_file(self):
with patch("builtins.open", mock_open(read_data=self.TEST_DATA)) as mf:
with patch("builtins.open", mock_open(read_data=TEST_SERIAL)) as mf:
fh_mock = mf.return_value.__enter__.return_value
fh_mock.write.side_effect = FileNotFoundError
get_serial_number(self.diag)
self.assertRaises(FileNotFoundError)

def test_permissions_error(self):
with patch("builtins.open", mock_open(read_data=self.TEST_DATA)) as mf:
with patch("builtins.open", mock_open(read_data=TEST_SERIAL)) as mf:
fh_mock = mf.return_value.__enter__.return_value
fh_mock.write.side_effect = PermissionError
get_serial_number(self.diag)
self.assertRaises(PermissionError)

def test_load_serial_number(self):
with patch("builtins.open", mock_open(read_data=TEST_SERIAL)):
serial = load_serial_number()
self.assertEqual(serial["serial"], TEST_SERIAL_NUMBER_RESULT["serial"])

def test_load_serial_number_fail(self):
with patch("builtins.open", mock_open()) as mf:
mf.side_effect = FileNotFoundError()

serial = load_serial_number()
captured = self.caplog
self.assertTrue('failed to load /proc/device-tree/serial-number' in str(captured.text))
self.assertEqual(serial, FAILED_SERIAL_NUMBER_RESULT)

def test_load_cpuinfo(self):
with patch("builtins.open", mock_open(read_data=TEST_CPU_INFO)):
cpuinfo = load_cpu_info()
self.assertEqual(cpuinfo["serial"], TEST_CPU_INFO_RESULT["serial"])

def test_load_cpuinfo_fail(self):
with patch("builtins.open", mock_open()) as mf:
mf.side_effect = FileNotFoundError()

cpuinfo = load_cpu_info()
captured = self.caplog
self.assertTrue('failed to load /proc/cpuinfo' in str(captured.text))
self.assertEqual(cpuinfo, FAILED_CPU_INFO_RESULT)
22 changes: 19 additions & 3 deletions hw_diag/utilities/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,14 @@ def get_serial_number(diagnostics):
"""
try:
cpuinfo = load_cpu_info()
serial = load_serial_number()
serial_number = ""
if has_valid_serial(cpuinfo):
if has_valid_serial(serial):
serial_number = serial[CPUINFO_SERIAL_KEY]
elif has_valid_serial(cpuinfo):
serial_number = cpuinfo[CPUINFO_SERIAL_KEY]
else:
serial_number = open("/proc/device-tree/serial-number").readline() \
.rstrip('\x00')
serial_number = "Serial number not found"
except FileNotFoundError as e:
raise e
except PermissionError as e:
Expand Down Expand Up @@ -296,6 +298,20 @@ def load_cpu_info() -> dict:
return cpuinfo


def load_serial_number() -> dict:
'''
returns /proc/device-tree/serial-number as dict, keys are case-insensitive
'''
serial = {}
try:
serial_number = open("/proc/device-tree/serial-number").readline() \
.rstrip('\x00')
serial[CPUINFO_SERIAL_KEY] = serial_number
except Exception as e:
logging.warning(f"failed to load /proc/device-tree/serial-number: {e}")
return serial


@retry(Exception, tries=5, delay=5, max_delay=15, backoff=2, logger=logging)
def lora_module_test():
"""
Expand Down

0 comments on commit 8ca36df

Please sign in to comment.