def test_signal_handler(self):
        daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER)
        daemon_pcied.stop_event.set = mock.MagicMock()
        daemon_pcied.log_info = mock.MagicMock()
        daemon_pcied.log_warning = mock.MagicMock()

        # Test SIGHUP
        daemon_pcied.signal_handler(pcied.signal.SIGHUP, None)
        assert daemon_pcied.log_info.call_count == 1
        daemon_pcied.log_info.assert_called_with(
            "Caught signal 'SIGHUP' - ignoring...")
        assert daemon_pcied.log_warning.call_count == 0
        assert daemon_pcied.stop_event.set.call_count == 0
        assert pcied.exit_code == 0

        # Reset
        daemon_pcied.log_info.reset_mock()
        daemon_pcied.log_warning.reset_mock()
        daemon_pcied.stop_event.set.reset_mock()

        # Test SIGINT
        test_signal = pcied.signal.SIGINT
        daemon_pcied.signal_handler(test_signal, None)
        assert daemon_pcied.log_info.call_count == 1
        daemon_pcied.log_info.assert_called_with(
            "Caught signal 'SIGINT' - exiting...")
        assert daemon_pcied.log_warning.call_count == 0
        assert daemon_pcied.stop_event.set.call_count == 1
        assert pcied.exit_code == (128 + test_signal)

        # Reset
        daemon_pcied.log_info.reset_mock()
        daemon_pcied.log_warning.reset_mock()
        daemon_pcied.stop_event.set.reset_mock()

        # Test SIGTERM
        test_signal = pcied.signal.SIGTERM
        daemon_pcied.signal_handler(test_signal, None)
        assert daemon_pcied.log_info.call_count == 1
        daemon_pcied.log_info.assert_called_with(
            "Caught signal 'SIGTERM' - exiting...")
        assert daemon_pcied.log_warning.call_count == 0
        assert daemon_pcied.stop_event.set.call_count == 1
        assert pcied.exit_code == (128 + test_signal)

        # Reset
        daemon_pcied.log_info.reset_mock()
        daemon_pcied.log_warning.reset_mock()
        daemon_pcied.stop_event.set.reset_mock()
        pcied.exit_code = 0

        # Test an unhandled signal
        daemon_pcied.signal_handler(pcied.signal.SIGUSR1, None)
        assert daemon_pcied.log_warning.call_count == 1
        daemon_pcied.log_warning.assert_called_with(
            "Caught unhandled signal 'SIGUSR1' - ignoring...")
        assert daemon_pcied.log_info.call_count == 0
        assert daemon_pcied.stop_event.set.call_count == 0
        assert pcied.exit_code == 0
    def test_check_pcie_devices(self):
        daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER)
        daemon_pcied.update_pcie_devices_status_db = mock.MagicMock()
        daemon_pcied.check_n_update_pcie_aer_stats = mock.MagicMock()
        pcied.platform_pcieutil.get_pcie_check = mock.MagicMock()

        daemon_pcied.check_pcie_devices()
        assert daemon_pcied.update_pcie_devices_status_db.call_count == 1
        assert daemon_pcied.check_n_update_pcie_aer_stats.call_count == 0
    def test_check_n_update_pcie_aer_stats(self, mock_read):
        daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER)
        daemon_pcied.device_table = mock.MagicMock()
        daemon_pcied.update_aer_to_statedb = mock.MagicMock()
        pcied.platform_pcieutil.get_pcie_aer_stats = mock.MagicMock()

        mock_read.return_value = None
        daemon_pcied.check_n_update_pcie_aer_stats(0, 1, 0)
        assert daemon_pcied.update_aer_to_statedb.call_count == 0
        assert daemon_pcied.device_table.set.call_count == 0
        assert pcied.platform_pcieutil.get_pcie_aer_stats.call_count == 0

        mock_read.return_value = '1714'
        daemon_pcied.check_n_update_pcie_aer_stats(0, 1, 0)
        assert daemon_pcied.update_aer_to_statedb.call_count == 1
        assert daemon_pcied.device_table.set.call_count == 1
        assert pcied.platform_pcieutil.get_pcie_aer_stats.call_count == 1
    def test_update_pcie_devices_status_db(self):
        daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER)
        daemon_pcied.status_table = mock.MagicMock()
        daemon_pcied.log_info = mock.MagicMock()
        daemon_pcied.log_error = mock.MagicMock()

        # test for pass resultInfo
        daemon_pcied.update_pcie_devices_status_db(0)
        assert daemon_pcied.status_table.set.call_count == 1
        assert daemon_pcied.log_info.call_count == 1
        assert daemon_pcied.log_error.call_count == 0

        daemon_pcied.status_table.set.reset_mock()
        daemon_pcied.log_info.reset_mock()

        # test for resultInfo with 1 device failed to detect
        daemon_pcied.update_pcie_devices_status_db(1)
        assert daemon_pcied.status_table.set.call_count == 1
        assert daemon_pcied.log_info.call_count == 0
        assert daemon_pcied.log_error.call_count == 1
    def test_update_aer_to_statedb(self):
        daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER)
        daemon_pcied.log_debug = mock.MagicMock()
        daemon_pcied.device_table = mock.MagicMock()
        daemon_pcied.device_name = mock.MagicMock()
        daemon_pcied.aer_stats = mock.MagicMock()

        mocked_expected_fvp = pcied.swsscommon.FieldValuePairs([
            ("correctable|field1", '0'),
            ("correctable|field2", '0'),
            ("fatal|field3", '0'),
            ("fatal|field4", '0'),
            ("non_fatal|field5", '0'),
            ("non_fatal|field6", '0'),
        ])

        daemon_pcied.update_aer_to_statedb()
        assert daemon_pcied.log_debug.call_count == 1
        assert daemon_pcied.device_table.set.call_count == 0

        daemon_pcied.device_table.set.reset_mock()
    def test_run(self):
        daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER)
        daemon_pcied.check_pcie_devices = mock.MagicMock()

        daemon_pcied.run()
        assert daemon_pcied.check_pcie_devices.call_count == 1