Skip to content

Commit

Permalink
Fixed wrong string comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
fernando79513 committed Sep 16, 2024
1 parent 3d21c9d commit e5da674
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion checkbox-support/checkbox_support/scripts/run_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 34 additions & 4 deletions checkbox-support/checkbox_support/tests/test_run_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
)
Expand All @@ -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",
Expand All @@ -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):
Expand Down

0 comments on commit e5da674

Please sign in to comment.