40 lines
1.7 KiB
Python
40 lines
1.7 KiB
Python
import fcntl
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from pageswiki_cli_test import pageswiki_cli
|
|
|
|
|
|
class DRGuardTest(unittest.TestCase):
|
|
def test_postgresql_rejected_before_database_open(self):
|
|
with patch.dict(os.environ, {"PAGESWIKI_DB_BACKEND": "postgresql"}):
|
|
with self.assertRaisesRegex(pageswiki_cli.CLIError, "HTTP pages-cli"):
|
|
pageswiki_cli.PagesWikiCLI()
|
|
|
|
def test_standby_rejected_before_database_open(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
(root / "pages.db").write_bytes(b"not a database; must not be opened")
|
|
(root / "dr-node.json").write_text(json.dumps({"version": 1, "role": "standby"}))
|
|
with patch.dict(os.environ, {"PAGESWIKI_DATA_DIR": directory, "PAGESWIKI_DB": str(root / "pages.db"), "PAGESWIKI_DOCUMENTS_DIR": str(root / "documents")}):
|
|
with self.assertRaisesRegex(pageswiki_cli.CLIError, "standby"):
|
|
pageswiki_cli.PagesWikiCLI()
|
|
|
|
def test_offline_maintenance_lock_blocks_cli(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
(root / "pages.db").write_bytes(b"not a database")
|
|
with (root / ".pageswiki-instance.lock").open("wb") as lock:
|
|
fcntl.flock(lock.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
with patch.dict(os.environ, {"PAGESWIKI_DATA_DIR": directory, "PAGESWIKI_DB": str(root / "pages.db")}):
|
|
with self.assertRaisesRegex(pageswiki_cli.CLIError, "maintenance guard"):
|
|
pageswiki_cli.PagesWikiCLI()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|