Skip to content

Commit

Permalink
Add: unit tests for removable_storage_watcher::main
Browse files Browse the repository at this point in the history
  • Loading branch information
p-gentili committed Nov 22, 2024
1 parent 6a1a45e commit 013c572
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
4 changes: 2 additions & 2 deletions providers/base/bin/removable_storage_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ def _summarize_changes(self, delta_records):
)


def main():
def main(argv=sys.argv[1:]):
description = "Wait for the specified device to be inserted or removed."
parser = argparse.ArgumentParser(description=description)
parser.add_argument("action", choices=["insert", "remove"])
Expand Down Expand Up @@ -1046,7 +1046,7 @@ def main():
help="Don't require drive being automounted",
)
parser.set_defaults(logging_level=logging.WARNING)
args = parser.parse_args()
args = parser.parse_args(argv)

# Configure logging as requested
# XXX: This may be incorrect as logging.basicConfig() fails after any other
Expand Down
67 changes: 67 additions & 0 deletions providers/base/tests/test_removable_storage_watcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import unittest
import sys
from unittest.mock import patch, MagicMock

sys.modules["dbus"] = MagicMock()
sys.modules["dbus.exceptions"] = MagicMock()
sys.modules["dbus.mainloop.glib"] = MagicMock()
sys.modules["gi"] = MagicMock()
sys.modules["gi.repository"] = MagicMock()

import removable_storage_watcher


class RemovableStorageWatcherTests(unittest.TestCase):

@patch("removable_storage_watcher.connect_to_system_bus")
@patch("removable_storage_watcher.is_udisks2_supported")
@patch("removable_storage_watcher.UDisks1StorageDeviceListener")
def test_main_udisk1(self, mock_udisk, mock_check, mock_connect):
"""
Test the main function connects to the system bus,
opens a UDisks1 listener and check the action is performed
until timeout.
"""

mock_connect.return_value = ("bus", "loop")
mock_check.return_value = False

argv = ["insert", "usb"]
removable_storage_watcher.main(argv)

mock_connect.assert_called_once_with()
mock_udisk.assert_called_once_with(
"bus",
"loop",
"insert",
["usb"],
0,
False,
)

mock_udisk.return_value.check.assert_called_once_with(20)

@patch("removable_storage_watcher.connect_to_system_bus")
@patch("removable_storage_watcher.is_udisks2_supported")
@patch("removable_storage_watcher.UDisks2StorageDeviceListener")
def test_main_udisk2(self, mock_udisk, mock_check, mock_connect):
"""
Test the main function uses a UDisks2 listener when
supported.
"""

mock_connect.return_value = ("bus", "loop")
mock_check.return_value = True

argv = ["insert", "usb"]
removable_storage_watcher.main(argv)

mock_udisk.assert_called_once_with(
"bus",
"loop",
"insert",
["usb"],
0,
False,
False,
)

0 comments on commit 013c572

Please sign in to comment.