Repair monitor cluster upgrade without blocking K1 package configuration

This commit is contained in:
DCCONSTRUCTIONS
2026-09-08 10:44:27 +03:00
parent 6492ebcdee
commit 14f8375713
11 changed files with 160 additions and 21 deletions
+5 -2
View File
@@ -9,9 +9,10 @@ import pytest
PACKAGING = Path(__file__).resolve().parents[1] / "apps/node-agent/packaging"
@pytest.mark.parametrize("monitor_fails", [False, True])
@pytest.mark.parametrize("active,configured", [(True, True), (False, True), (True, False)])
def test_node_update_restores_only_previously_running_configured_plugin(
tmp_path, active, configured,
tmp_path, active, configured, monitor_fails,
):
binary = tmp_path / "bin"
binary.mkdir()
@@ -34,7 +35,7 @@ case "$1" in
esac
''')
(binary / "getent").write_text("#!/bin/sh\nexit 0\n")
(binary / "setup-monitor").write_text("#!/bin/sh\nprintf '%s\\n' monitor-setup >> \"$TEST_EVENTS\"\n")
(binary / "setup-monitor").write_text("#!/bin/sh\nprintf '%s\\n' monitor-setup >> \"$TEST_EVENTS\"\nexit " + ("1" if monitor_fails else "0") + "\n")
(binary / "dpkg-query").write_text(
"#!/bin/sh\necho 'install ok " + ("installed" if configured else "unpacked") + "'\n"
)
@@ -56,3 +57,5 @@ esac
assert ("start mission-core-k1.service" in calls) is (active and configured)
assert state.exists() is (active and configured)
assert not (run / "mission-core-node-k1-upgrade-active").exists()
assert (run / "mission-core-monitor-setup-failed").exists() is monitor_fails
+74
View File
@@ -0,0 +1,74 @@
"""Exercise the monitor bootstrap against PostgreSQL's systemd path contract."""
import os
import subprocess
from pathlib import Path
import pytest
SETUP = Path(__file__).resolve().parents[1] / "apps/node-agent/packaging/setup-monitor"
@pytest.mark.parametrize("legacy,foreign", [(False, False), (True, False), (True, True)])
def test_cluster_path_and_r18_migration_are_repeatable(tmp_path, legacy, foreign):
binary = tmp_path / "bin"
binary.mkdir()
conf = tmp_path / "etc/postgresql/16"
conf.mkdir(parents=True)
if legacy:
(conf / "ndc-monitor").mkdir()
(conf / "ndc-monitor/postgresql.conf").write_text("preserved R18 configuration")
driver = binary / "driver"
driver.write_text("""#!/usr/bin/env python3
import os,sys,pathlib
root=pathlib.Path(os.environ['TEST_ROOT']);name=pathlib.Path(sys.argv[0]).name;args=sys.argv[1:]
with (root/'events').open('a') as log:log.write(name+' '+ ' '.join(args)+'\\n')
conf=root/'etc/postgresql/16'
if name=='id':print(0)
elif name=='install':pathlib.Path(args[-1]).mkdir(parents=True,exist_ok=True)
elif name=='pg_conftool':
print(os.environ.get('TEST_DATA_ROOT',str(root/'var/lib/mission-core-monitor-db')) if args[-1]=='data_directory' else '5433')
elif name=='pg_createcluster':
(conf/args[1]).mkdir();(conf/args[1]/'postgresql.conf').write_text('new database')
elif name=='pg_renamecluster':
(conf/args[1]).rename(conf/args[2])
elif name=='systemctl' and args[0]=='restart' and args[1].startswith('postgresql@'):
# Exact installed Debian template: AssertPathExists=/etc/postgresql/%I/postgresql.conf.
instance=args[1].split('@')[1].removesuffix('.service')
assert (root/'etc/postgresql'/instance.replace('-','/')/'postgresql.conf').is_file()
""")
driver.chmod(0o700)
for name in [
"id",
"getent",
"install",
"pg_conftool",
"pg_createcluster",
"pg_renamecluster",
"systemctl",
"runuser",
]:
(binary / name).symlink_to(driver)
script = tmp_path / "setup"
text = SETUP.read_text()
for path in ["/etc/", "/var/lib/", "/run/"]:
text = text.replace(path, str(tmp_path / path.lstrip("/")) + "/")
script.write_text(text)
env = dict(
os.environ, PATH=str(binary) + os.pathsep + os.environ["PATH"], TEST_ROOT=str(tmp_path)
)
if foreign:
env["TEST_DATA_ROOT"] = "/unrelated/database"
result = subprocess.run(["/bin/sh", str(script)], env=env, capture_output=True)
assert result.returncode != 0
assert (conf / "ndc-monitor/postgresql.conf").is_file()
assert "pg_renamecluster" not in (tmp_path / "events").read_text()
return
for _ in range(2):
subprocess.run(["/bin/sh", str(script)], env=env, check=True, capture_output=True)
calls = (tmp_path / "events").read_text()
assert calls.count("pg_renamecluster 16 ndc-monitor ndcmonitor") == int(legacy)
assert calls.count("pg_createcluster 16 ndcmonitor") == int(not legacy)
assert not (conf / "ndc-monitor").exists()
if legacy:
assert (conf / "ndcmonitor/postgresql.conf").read_text() == "preserved R18 configuration"