Files
computer-playbook/tests/unit/python/roles/web-app-nextcloud/test_server_id.py
T
kevinveenbirkenbachandClaude Opus 5.5 b0c622ab44 fix(web-app-nextcloud): give each PHP server its own NC_serverid
The admin overview reported that no server identifier is configured. Nextcloud 34 accepts 1 to 511, treats values below 1 as unset and asks for one NC_serverid per server when config.php is shared, which it is between nextcloud, cron, the task processing worker and the swarm replicas. A single serverid in config.php would let them mint identical Snowflake IDs in the same millisecond. The nextcloud service now carries NC_serverid 1 in compose and its task slot in swarm, so the check passes and replicas never share an id; the other services keep the hostname fallback.

A unit test renders the service environment in both modes and fails when the compose id leaves the accepted range.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-29 17:24:17 +01:00

41 lines
1.3 KiB
Python

from __future__ import annotations
import unittest
from jinja2 import Environment, select_autoescape
from utils.cache.files import PROJECT_ROOT, read_text
from utils.cache.yaml import load_yaml_str
from utils.roles.mapping import ROLE_FILE_TEMPL_COMPOSE
TEMPLATE = read_text(
str(PROJECT_ROOT / "roles/web-app-nextcloud" / ROLE_FILE_TEMPL_COMPOSE)
)
def _environment(deployment_mode: str) -> dict:
start = TEMPLATE.index(" environment:\n")
end = TEMPLATE.index(" sysctls:", start)
rendered = (
Environment(autoescape=select_autoescape(default_for_string=False))
.from_string(TEMPLATE[start:end])
.render(DEPLOYMENT_MODE=deployment_mode)
)
return load_yaml_str(rendered)["environment"]
class TestServerId(unittest.TestCase):
def test_compose_pins_a_server_id_nextcloud_accepts(self):
server_id = int(_environment("compose")["NC_serverid"])
self.assertGreaterEqual(server_id, 1)
self.assertLessEqual(server_id, 511)
def test_swarm_gives_every_replica_its_task_slot(self):
environment = _environment("swarm")
self.assertEqual(environment["NC_serverid"], "{{.Task.Slot}}")
self.assertEqual(environment["TASK_SLOT"], "{{.Task.Slot}}")
if __name__ == "__main__":
unittest.main()