Skip to content

Commit

Permalink
Fixing test that assumed local inventory was blank
Browse files Browse the repository at this point in the history
This simply adds the temp_inventory fixture to the file for this test.
There are a few tests in different locations that also use this fixture
so in the future, these could likely be consolidated.
  • Loading branch information
JacobCallahan committed Aug 30, 2023
1 parent 25dd38a commit 96d258e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
11 changes: 7 additions & 4 deletions broker/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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:
Expand All @@ -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()


Expand Down
13 changes: 11 additions & 2 deletions tests/test_broker.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 96d258e

Please sign in to comment.