From 8612e772cf21d3e270f492c7fc65b9de0bd0981a Mon Sep 17 00:00:00 2001 From: Athanasius Date: Fri, 19 Mar 2021 11:58:02 +0000 Subject: [PATCH] tests/journal_lock.py: Initial framework. No actual test per se yet, but this runs with `pytest -k 'journal_lock'` --- tests/journal_lock.py/test_journal_lock.py | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/journal_lock.py/test_journal_lock.py diff --git a/tests/journal_lock.py/test_journal_lock.py b/tests/journal_lock.py/test_journal_lock.py new file mode 100644 index 00000000..6f5d7d38 --- /dev/null +++ b/tests/journal_lock.py/test_journal_lock.py @@ -0,0 +1,57 @@ +"""Tests for journal_lock.py code.""" +# Tests: +# - Need logging set up, at TRACE level. +# +# - Will need to mock config for the retrieval of journaldir. +# - Can ask pytest to create a unique tmp dir: +# +# +# +# - Is file actually locked after obtain_lock(). Problem: We opened the +# file in a manner which means nothing else can open it. Also I assume +# that the same process will either be allowed to lock it 'again' or +# overwrite the lock. +# +# Expected failures if: +# +# 1. Lock already held (elsewhere). +# 2. Can't open lock file 'w+'. +# 3. Path to lock file doesn't exist. +# 4. journaldir is None (default on Linux). +# +# - Does release_lock() work? Easier to test, if it's worked.... +# 1. return True if not locked. +# 2. return True if locked, but successful unlock. +# 3. return False otherwise. +# +# - JournalLock.set_path_from_journaldir +# 1. When journaldir is None. +# 2. Succeeds otherwise? +# +# - Can any string to pathlib.Path result in an invalid path for other +# operations? +# +# - Not sure about testing JournalAlreadyLocked class. + +from pytest._pytest import monkeypatch, tmpdir + +from config import config +from journal_lock import JournalLock + + +def test_journal_lock_init(monkeypatch: monkeypatch, tmpdir: tmpdir): # type: ignore + """Test JournalLock instantiation.""" + + def get_str(key: str, *, default: str = None) -> str: + """Mock config.*Config get_str to provide fake journaldir.""" + if key == 'journaldir': + print('journaldir: using tmpdir') + return tmpdir + + print('Other key, calling up ...') + return config.get_str(key) # Call the non-mocked + + with monkeypatch.context() as m: + m.setattr(config, "get_str", get_str) + print(f'{tmpdir=}') + jlock = JournalLock()