diff --git a/broker/helpers.py b/broker/helpers.py index c86be564..1b0fe0a6 100644 --- a/broker/helpers.py +++ b/broker/helpers.py @@ -454,8 +454,9 @@ def simple_retry(cmd, cmd_args=None, cmd_kwargs=None, max_timeout=60, _cur_timeo class FileLock: - """Basic file locking class that acquires and releases locks - recommended usage is the context manager which will handle everything for you + """Basic file locking class that acquires and releases locks. + + Recommended usage is the context manager which will handle everything for you with FileLock("basic_file.txt"): Path("basic_file.txt").write_text("some text") @@ -468,6 +469,7 @@ def __init__(self, file_name, timeout=10): self.timeout = timeout def wait_file(self): + """Wait for the lock file to be released, then acquire it.""" timeout_after = time.time() + self.timeout while self.lock.exists(): if time.time() <= timeout_after: @@ -479,12 +481,13 @@ def wait_file(self): self.lock.touch() def return_file(self): + """Release the lock file.""" self.lock.unlink() - def __enter__(self): + def __enter__(self): # noqa: D105 self.wait_file() - def __exit__(self, *tb_info): + def __exit__(self, *tb_info): # noqa: D105 self.return_file() diff --git a/tests/test_broker.py b/tests/test_broker.py index cbcab8de..a15427c5 100644 --- a/tests/test_broker.py +++ b/tests/test_broker.py @@ -1,8 +1,17 @@ -from broker import broker, Broker, helpers +from broker import broker, Broker, helpers, settings from broker.providers import test_provider import pytest +@pytest.fixture(scope="module") +def temp_inventory(): + """Temporarily move the local inventory, then move it back when done""" + backup_path = settings.inventory_path.rename(f"{settings.inventory_path.absolute()}.bak") + yield + settings.inventory_path.unlink() + backup_path.rename(settings.inventory_path) + + def test_empty_init(): """Broker should be able to init without any arguments""" broker_inst = Broker() @@ -50,7 +59,7 @@ def test_broker_empty_checkin(): broker_inst.checkin() -def test_broker_checkin_n_sync_empty_hostname(): +def test_broker_checkin_n_sync_empty_hostname(temp_inventory): """Test that broker can checkin and sync inventory with a host that has empty hostname""" broker_inst = broker.Broker(nick="test_nick") broker_inst.checkout()