mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-06 19:01:54 +00:00
Every script a role ships now lives under `files/<language>/`, and the unit
suite is split the same way: `tests/unit/python/` beside new sibling trees for
the other languages. `tests/lint/filesystem/test_role_files_layout.py` keeps it
that way; directories whose shape is dictated from outside carry a `.nocheck`
naming the constraint (Docker build contexts, WordPress mu-plugins, the Joomla
extension, Odoo addons, Playwright suites, the `files/test/test.sh` harness
that `test-e2e-cli` resolves generically).
This cannot be applied in halves. Moving a file and repointing what names it
are the same change: 14 references resolve only at deploy time, so the tree is
green either before or after, never between.
Those 14 were found by static resolution over every reference class, not by the
test suite -- a full `make quality-high` was green while all of them were
broken:
* 7 `src:` values built from a role variable holding a bare filename
(svc-bkp-remote-2-local, decidim, espocrm, friendica, joomla, pixelfed x2).
The variable doubles as the destination name inside the container, so the
language segment belongs at the call site, not in the variable.
* `sys-service/tasks/04_base.yml` resolved every consumer's systemd script
through `first_found(..., errors='strict')` over `files/script.{sh,py}`.
18 roles ship exactly that and have no template fallback: the play would
have died on each.
* `sys-front-inj-javascript` fell back to `<role>/files/javascript.js`; the
interface now reads `files/javascript/javascript.js`.
* `sys-front-inj-logout/templates/head_sub.j2` built the cache-busting mtime
path through `path_absolute(... 'files', ...)`.
* 4 `ansible.builtin.script:` invocations naming a bare command
(pull.py, prune.sh, set_postgres_superuser_password.sh, ganesha-serve.sh).
* `dev-python/files/shell/install.sh` pointed at a sibling role's script.
Depth-dependent paths inside the moved trees were bumped accordingly: 59
`parents[N]` computations, 12 relative climbs, and the per-role recover CLI,
whose path `cli/administration/recover/recoverers.py` still assembled from the
pre-move location.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
187 lines
6.0 KiB
Python
187 lines
6.0 KiB
Python
"""Unit tests for `utils.cleanup.databases_csv` (databases.csv wipe helper)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import csv
|
|
import io
|
|
import unittest
|
|
from contextlib import redirect_stderr, redirect_stdout
|
|
from pathlib import Path
|
|
from tempfile import TemporaryDirectory
|
|
|
|
from utils.cleanup.databases_csv import main, wipe_database_entries
|
|
|
|
_HEADER = "instance;database;username;password\n"
|
|
|
|
|
|
class DatabasesCsvTestBase(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self._tmp = TemporaryDirectory()
|
|
self.addCleanup(self._tmp.cleanup)
|
|
self.csv_file = Path(self._tmp.name) / "databases.csv"
|
|
|
|
def _write(self, content: str) -> None:
|
|
self.csv_file.write_text(content, encoding="utf-8")
|
|
|
|
def _read(self) -> list[list[str]]:
|
|
with self.csv_file.open(newline="", encoding="utf-8") as fh:
|
|
return list(csv.reader(fh, delimiter=";"))
|
|
|
|
|
|
class TestWipeDatabaseEntries(DatabasesCsvTestBase, unittest.TestCase):
|
|
def test_removes_matching_row_by_name(self):
|
|
self._write(
|
|
_HEADER
|
|
+ "mariadb;matomo;matomo;pw1\n"
|
|
+ "postgres;keycloak;keycloak;pw2\n"
|
|
+ "mariadb;nextcloud;nextcloud;pw3\n"
|
|
)
|
|
|
|
removed = wipe_database_entries(["web-app-matomo"], csv_file=self.csv_file)
|
|
|
|
self.assertEqual(removed, ["matomo:matomo"])
|
|
self.assertEqual(
|
|
self._read(),
|
|
[
|
|
["instance", "database", "username", "password"],
|
|
["postgres", "keycloak", "keycloak", "pw2"],
|
|
["mariadb", "nextcloud", "nextcloud", "pw3"],
|
|
],
|
|
)
|
|
|
|
def test_no_match_does_not_rewrite_file(self):
|
|
self._write(_HEADER + "postgres;keycloak;keycloak;pw2\n")
|
|
before_mtime = self.csv_file.stat().st_mtime_ns
|
|
|
|
removed = wipe_database_entries(["web-app-matomo"], csv_file=self.csv_file)
|
|
|
|
self.assertEqual(removed, [])
|
|
self.assertEqual(self.csv_file.stat().st_mtime_ns, before_mtime)
|
|
|
|
def test_missing_file_returns_empty(self):
|
|
removed = wipe_database_entries(["web-app-matomo"], csv_file=self.csv_file)
|
|
|
|
self.assertEqual(removed, [])
|
|
self.assertFalse(self.csv_file.exists())
|
|
|
|
def test_multiple_app_ids_in_one_call(self):
|
|
self._write(
|
|
_HEADER
|
|
+ "mariadb;matomo;matomo;pw1\n"
|
|
+ "postgres;keycloak;keycloak;pw2\n"
|
|
+ "mariadb;nextcloud;nextcloud;pw3\n"
|
|
)
|
|
|
|
removed = wipe_database_entries(
|
|
["web-app-matomo", "web-app-keycloak"],
|
|
csv_file=self.csv_file,
|
|
)
|
|
|
|
self.assertEqual(sorted(removed), ["keycloak:keycloak", "matomo:matomo"])
|
|
self.assertEqual(
|
|
self._read(),
|
|
[
|
|
["instance", "database", "username", "password"],
|
|
["mariadb", "nextcloud", "nextcloud", "pw3"],
|
|
],
|
|
)
|
|
|
|
def test_username_match_when_database_is_wildcard(self):
|
|
self._write(_HEADER + "mariadb;*;matomo;pw1\npostgres;*;keycloak;pw2\n")
|
|
|
|
removed = wipe_database_entries(["web-app-matomo"], csv_file=self.csv_file)
|
|
|
|
self.assertEqual(removed, ["*:matomo"])
|
|
self.assertEqual(
|
|
self._read(),
|
|
[
|
|
["instance", "database", "username", "password"],
|
|
["postgres", "*", "keycloak", "pw2"],
|
|
],
|
|
)
|
|
|
|
def test_unknown_app_id_no_op(self):
|
|
self._write(_HEADER + "mariadb;matomo;matomo;pw1\n")
|
|
|
|
removed = wipe_database_entries(
|
|
["web-app-does-not-exist"], csv_file=self.csv_file
|
|
)
|
|
|
|
self.assertEqual(removed, [])
|
|
|
|
def test_blank_lines_are_preserved(self):
|
|
self._write(
|
|
_HEADER
|
|
+ "mariadb;matomo;matomo;pw1\n"
|
|
+ "\n"
|
|
+ "postgres;keycloak;keycloak;pw2\n"
|
|
)
|
|
|
|
removed = wipe_database_entries(["web-app-matomo"], csv_file=self.csv_file)
|
|
|
|
self.assertEqual(removed, ["matomo:matomo"])
|
|
self.assertEqual(
|
|
self._read(),
|
|
[
|
|
["instance", "database", "username", "password"],
|
|
[],
|
|
["postgres", "keycloak", "keycloak", "pw2"],
|
|
],
|
|
)
|
|
|
|
def test_header_is_kept_even_when_all_data_rows_removed(self):
|
|
self._write(_HEADER + "mariadb;matomo;matomo;pw1\n")
|
|
|
|
removed = wipe_database_entries(["web-app-matomo"], csv_file=self.csv_file)
|
|
|
|
self.assertEqual(removed, ["matomo:matomo"])
|
|
self.assertEqual(
|
|
self._read(),
|
|
[["instance", "database", "username", "password"]],
|
|
)
|
|
|
|
def test_short_row_is_kept(self):
|
|
self._write(_HEADER + "garbage\nmariadb;matomo;matomo;pw1\n")
|
|
|
|
removed = wipe_database_entries(["web-app-matomo"], csv_file=self.csv_file)
|
|
|
|
self.assertEqual(removed, ["matomo:matomo"])
|
|
self.assertEqual(
|
|
self._read(),
|
|
[
|
|
["instance", "database", "username", "password"],
|
|
["garbage"],
|
|
],
|
|
)
|
|
|
|
def test_csv_without_header_is_still_processed(self):
|
|
self._write("mariadb;matomo;matomo;pw1\npostgres;keycloak;keycloak;pw2\n")
|
|
|
|
removed = wipe_database_entries(["web-app-matomo"], csv_file=self.csv_file)
|
|
|
|
self.assertEqual(removed, ["matomo:matomo"])
|
|
self.assertEqual(
|
|
self._read(),
|
|
[["postgres", "keycloak", "keycloak", "pw2"]],
|
|
)
|
|
|
|
|
|
class TestMainShim(DatabasesCsvTestBase, unittest.TestCase):
|
|
def test_no_argv_prints_usage_and_returns_2(self):
|
|
stderr = io.StringIO()
|
|
with redirect_stderr(stderr):
|
|
rc = main([])
|
|
self.assertEqual(rc, 2)
|
|
self.assertIn("usage:", stderr.getvalue())
|
|
|
|
def test_main_uses_default_path_when_no_override(self):
|
|
stdout = io.StringIO()
|
|
with redirect_stdout(stdout):
|
|
rc = main(["web-app-nonexistent"])
|
|
self.assertEqual(rc, 0)
|
|
self.assertIn("No databases.csv entries to wipe", stdout.getvalue())
|
|
|
|
|
|
if __name__ == "__main__": # pragma: no cover
|
|
unittest.main()
|