Cole Walker 04bbd6f0ff Add CguHandler, DmesgWatcher, GnssMonitor classes
The CguHandler class reads a given ts2phc config file and uses this to
derive the nmea_serialport, pci address and cgu path. These values can
be short-circuited if they are known in advance.
From there, the cgu is read and parsed into a dict in order to
easily derive the status of the various pins on the NIC.

DmesgWatcher and GnssMonitor use an observer pattern. DmesgWatcher is
the subject and follows entries in a dmesg log for patterns that
GnssMonitor observers care about. It then updates the GnssMonitor
observers with the matched entry allowing GnssMonitor to parse and
handle the update as required. The DmesgWatcher can be extended to
support other observer types in the future.

GnssMonitor attaches to DmesgWatcher and performs the handling for
changes in GNSS status. This includes sending the status change to a
publisher which will be implemented in a future review.

Unit tests are included for CguHandler and DmesgWatcher. GnssMonitor is
covered by the tests as it is built off the functionality of
CguHandler.

Tox has been updated to automatically run the unit tests.

Testing:

Pass: Unit tests pass

Story: 2010056
Task: 45500

Signed-off-by: Cole Walker <cole.walker@windriver.com>
Change-Id: I4be477aa0fce8baa418a3ff450c6b998683ec10b
2022-06-23 17:08:37 -04:00

57 lines
2.4 KiB
Python

#
# Copyright (c) 2022 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
import unittest
import os
from unittest.mock import MagicMock
from trackingfunctionsdk.common.helpers.dmesg_watcher import DmesgWatcher
from trackingfunctionsdk.common.helpers.gnss_monitor import GnssMonitor
testpath = os.environ.get("TESTPATH", "")
class DmesgWatcherTests(unittest.TestCase):
testDmesgWatcher = DmesgWatcher()
observer_a = GnssMonitor(testpath + "./test_input_files/ts2phc_valid.conf",
"tty_GNSS_1800_0", "0000:18:00.0",
testpath + "./test_input_files/mock_cgu_output")
observer_b = GnssMonitor(testpath + "./test_input_files/ts2phc_valid.conf",
"tty_GNSS_1a00_0", "0000:1a:00.0",
testpath + "./test_input_files/mock_cgu_output")
def test_parse_dmesg_event(self):
self.testDmesgWatcher.attach(self.observer_a)
self.testDmesgWatcher.notify = MagicMock()
with open(testpath + "./test_input_files/mock_kern.log", 'r') as dmesg:
for line in dmesg:
self.testDmesgWatcher.parse_dmesg_event(line)
assert self.testDmesgWatcher.notify.called
# Test that notify is not called when there is no match
self.testDmesgWatcher.notify.reset_mock()
self.testDmesgWatcher.attach(self.observer_b)
with open(testpath + "./test_input_files/mock_kern.log", 'r') as dmesg:
for line in dmesg:
self.testDmesgWatcher.parse_dmesg_event(line)
assert self.testDmesgWatcher.notify.assert_not_called
def test_attach_detach(self):
self.testDmesgWatcher.attach(self.observer_a)
self.testDmesgWatcher.attach(self.observer_b)
self.assertEqual(len(self.testDmesgWatcher._observers), 2)
self.testDmesgWatcher.detach(self.observer_a)
self.testDmesgWatcher.detach(self.observer_b)
self.assertEqual(len(self.testDmesgWatcher._observers), 0)
def test_notify(self):
self.observer_a.update = MagicMock
self.testDmesgWatcher.notify(observer=self.observer_a,
matched_line="2022-06-03T19:50:05.959 controller-0 kernel: warning [ "
"4.635511] ice 0000:18:00.0: <DPLL1> state changed to: "
"locked_ho_ack, pin GNSS-1PPS")
assert self.observer_a.update.called