From e5da67460715f3a5d94a4d597d1d0ef567f64495 Mon Sep 17 00:00:00 2001 From: ferbraher Date: Mon, 16 Sep 2024 17:31:41 +0200 Subject: [PATCH] Fixed wrong string comparison --- .../checkbox_support/scripts/run_watcher.py | 2 +- .../tests/test_run_watcher.py | 38 +++++++++++++++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/checkbox-support/checkbox_support/scripts/run_watcher.py b/checkbox-support/checkbox_support/scripts/run_watcher.py index 92592335f6..ede02a8ee7 100644 --- a/checkbox-support/checkbox_support/scripts/run_watcher.py +++ b/checkbox-support/checkbox_support/scripts/run_watcher.py @@ -238,7 +238,7 @@ def _parse_journal_line(self, line_str): ).group(1) # Look for insertion action - if "USB Mass Storage device detected" or "uas" in line_str: + if "USB Mass Storage device detected" in line_str or "uas" in line_str: self.action = "insertion" # Look for removal action diff --git a/checkbox-support/checkbox_support/tests/test_run_watcher.py b/checkbox-support/checkbox_support/tests/test_run_watcher.py index 608ea43ae5..7ee9a53dc0 100644 --- a/checkbox-support/checkbox_support/tests/test_run_watcher.py +++ b/checkbox-support/checkbox_support/tests/test_run_watcher.py @@ -303,50 +303,65 @@ def test_usb_storage_no_removal(self): USBStorage._validate_removal(mock_usb_storage) def test_usb_storage_parse_journal_line(self): - mock_usb_storage = MagicMock() line_str = "new high-speed USB device" + mock_usb_storage = MagicMock() USBStorage._parse_journal_line(mock_usb_storage, line_str) self.assertEqual(mock_usb_storage.device, "high_speed_usb") line_str = "new SuperSpeed USB device" + mock_usb_storage = MagicMock() USBStorage._parse_journal_line(mock_usb_storage, line_str) self.assertEqual(mock_usb_storage.device, "super_speed_usb") line_str = "new SuperSpeed Gen 1 USB device" + mock_usb_storage = MagicMock() USBStorage._parse_journal_line(mock_usb_storage, line_str) self.assertEqual(mock_usb_storage.device, "super_speed_gen1_usb") line_str = "new SuperSpeed Plus Gen 2x1 USB device" + mock_usb_storage = MagicMock() USBStorage._parse_journal_line(mock_usb_storage, line_str) self.assertEqual( mock_usb_storage.device, "super_speed_plus_gen2x1_usb" ) line_str = "new high-speed USB device number 1 using ehci_hcd" + mock_usb_storage = MagicMock() USBStorage._parse_journal_line(mock_usb_storage, line_str) self.assertEqual(mock_usb_storage.driver, "ehci_hcd") line_str = "new high-speed USB device number 4 using xhci_hcd" + mock_usb_storage = MagicMock() USBStorage._parse_journal_line(mock_usb_storage, line_str) self.assertEqual(mock_usb_storage.driver, "xhci_hcd") line_str = "USB Mass Storage device detected" + mock_usb_storage = MagicMock() USBStorage._parse_journal_line(mock_usb_storage, line_str) self.assertEqual(mock_usb_storage.action, "insertion") line_str = "kernel: scsi host0: uas" + mock_usb_storage = MagicMock() USBStorage._parse_journal_line(mock_usb_storage, line_str) self.assertEqual(mock_usb_storage.action, "insertion") line_str = "USB disconnect, device" + mock_usb_storage = MagicMock() USBStorage._parse_journal_line(mock_usb_storage, line_str) self.assertEqual(mock_usb_storage.action, "removal") line_str = "sdb: sdb1" + mock_usb_storage = MagicMock() USBStorage._parse_journal_line(mock_usb_storage, line_str) self.assertEqual(mock_usb_storage.mounted_partition, "sdb1") + line_str = "Invalid line" + mock_usb_storage = MagicMock() + mock_usb_storage.action = None + USBStorage._parse_journal_line(mock_usb_storage, line_str) + self.assertEqual(mock_usb_storage.action, None) + def test_mediacard_storage_init(self): mediacard_storage = MediacardStorage("mediacard", "zapper_addr") self.assertEqual(mediacard_storage.storage_type, "mediacard") @@ -382,22 +397,30 @@ def test_mediacard_storage_no_removal(self): MediacardStorage._validate_removal(mock_mediacard_storage) def test_mediacard_storage_parse_journal_line(self): - mock_mediacard_storage = MagicMock() line_str = "mmcblk0: p1" + mock_mediacard_storage = MagicMock() MediacardStorage._parse_journal_line(mock_mediacard_storage, line_str) self.assertEqual(mock_mediacard_storage.mounted_partition, "mmcblk0p1") line_str = "new SD card at address 123456" + mock_mediacard_storage = MagicMock() MediacardStorage._parse_journal_line(mock_mediacard_storage, line_str) self.assertEqual(mock_mediacard_storage.action, "insertion") self.assertEqual(mock_mediacard_storage.device, "SD") self.assertEqual(mock_mediacard_storage.address, "123456") line_str = "card 123456 removed" + mock_mediacard_storage = MagicMock() MediacardStorage._parse_journal_line(mock_mediacard_storage, line_str) self.assertEqual(mock_mediacard_storage.action, "removal") + line_str = "Invalid line" + mock_usb_storage = MagicMock() + mock_usb_storage.action = None + USBStorage._parse_journal_line(mock_usb_storage, line_str) + self.assertEqual(mock_usb_storage.action, None) + def test_thunderbolt_storage_init(self): thunderbolt_storage = ThunderboltStorage("thunderbolt", "zapper_addr") self.assertEqual(thunderbolt_storage.storage_type, "thunderbolt") @@ -432,9 +455,9 @@ def test_thunderbolt_storage_no_removal(self): ThunderboltStorage._validate_removal(mock_thunderbolt_storage) def test_thunderbolt_storage_parse_journal_line(self): - mock_thunderbolt_storage = MagicMock() line_str = "nvme0n1: p1" + mock_thunderbolt_storage = MagicMock() ThunderboltStorage._parse_journal_line( mock_thunderbolt_storage, line_str ) @@ -443,17 +466,25 @@ def test_thunderbolt_storage_parse_journal_line(self): ) line_str = "thunderbolt 1-1: new device found" + mock_thunderbolt_storage = MagicMock() ThunderboltStorage._parse_journal_line( mock_thunderbolt_storage, line_str ) self.assertEqual(mock_thunderbolt_storage.action, "insertion") line_str = "thunderbolt 1-1: device disconnected" + mock_thunderbolt_storage = MagicMock() ThunderboltStorage._parse_journal_line( mock_thunderbolt_storage, line_str ) self.assertEqual(mock_thunderbolt_storage.action, "removal") + line_str = "Invalid line" + mock_usb_storage = MagicMock() + mock_usb_storage.action = None + USBStorage._parse_journal_line(mock_usb_storage, line_str) + self.assertEqual(mock_usb_storage.action, None) + def test_parse_args(self): with patch( "sys.argv", @@ -479,7 +510,6 @@ def test_main_usb_insertion(self, mock_parse_args, mock_usb): # check that the watcher is an USBStorage object self.assertIsInstance(watcher, USBStorage) - @patch("checkbox_support.scripts.run_watcher.input", MagicMock()) @patch("checkbox_support.scripts.run_watcher.USBStorage", spec=USBStorage) @patch("checkbox_support.scripts.run_watcher.parse_args") def test_main_usb_storage(self, mock_parse_args, mock_usb):