feat: Verwaltung und geführte Referenzanbindung ausbauen

This commit is contained in:
2026-11-16 13:07:45 +01:00
parent 057f28b4ec
commit e0836b1f22
31 changed files with 1835 additions and 279 deletions

View File

@ -0,0 +1,94 @@
"""Zweck: Referenzinstaller darf fremde Units nicht verändern; Version: 1.
Selbsttest: python3 -m unittest discover -s betrieb -p test_referenz_installer.py.
Betriebssystemabfragen werden kontrolliert beantwortet; kein root benötigt.
"""
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
class ReferenzInstallerTest(unittest.TestCase):
def test_fremde_unit_wird_vor_aenderungen_abgelehnt(self):
wurzel = Path(__file__).resolve().parents[1]
with tempfile.TemporaryDirectory() as ordner:
basis = Path(ordner)
protokoll = basis / "aufrufe"
systemctl = basis / "systemctl"
systemctl.write_text(
'#!/bin/sh\n'
'echo "$*" >> "$KEYFORGE_TEST_PROTOKOLL"\n'
'if [ "$1" = show ]; then echo loaded; exit 0; fi\n'
'exit 0\n')
systemctl.chmod(0o700)
env = dict(os.environ, PATH=str(basis) + ':' + os.environ['PATH'],
KEYFORGE_TEST_PROTOKOLL=str(protokoll))
r = subprocess.run(
['bash', str(wurzel / 'scripts/referenz-installieren-srv04.sh'),
'--pruefen'], env=env, capture_output=True, text=True)
self.assertNotEqual(r.returncode, 0)
self.assertIn('Fremde Referenz-Unit', r.stdout)
aufrufe = protokoll.read_text()
self.assertNotIn('stop ', aufrufe)
self.assertNotIn('enable ', aufrufe)
def test_pythonumgebung_ist_fuer_dienstbenutzer_lesbar(self):
wurzel = Path(__file__).resolve().parents[1]
with tempfile.TemporaryDirectory() as ordner:
basis = Path(ordner)
requirements = basis / 'requirements.txt'
requirements.write_text('')
ziel = basis / 'venv'
r = subprocess.run(
['bash', str(wurzel / 'scripts/python-umgebung.sh'),
str(ziel), str(requirements)],
env=dict(os.environ, PIP_NO_INDEX='1',
PIP_DISABLE_PIP_VERSION_CHECK='1'),
capture_output=True, text=True)
self.assertEqual(r.returncode, 0, r.stderr)
for pfad in [ziel, ziel / 'bin', ziel / 'lib']:
self.assertEqual(pfad.stat().st_mode & 0o005, 0o005)
for pfad in ziel.rglob('site-packages'):
self.assertEqual(pfad.stat().st_mode & 0o005, 0o005)
subprocess.run([str(ziel / 'bin/python'), '-c', 'import pip'],
check=True, capture_output=True)
def test_rollback_stoppt_neuen_prozess_vor_programmwechsel(self):
wurzel = Path(__file__).resolve().parents[1]
with tempfile.TemporaryDirectory() as ordner:
basis = Path(ordner)
alt, neu = basis / 'alt', basis / 'neu'
alt.mkdir()
neu.mkdir()
aktuell = basis / 'current'
aktuell.symlink_to(neu)
aktiv = basis / 'aktiv'
laufend = basis / 'laufend'
aktiv.write_text('ja')
laufend.write_text(str(neu))
systemctl = basis / 'systemctl'
systemctl.write_text(
'#!/bin/sh\n'
'case "$1" in\n'
'stop) rm -f "$PROBE_AKTIV" ;;\n'
'start) if [ ! -e "$PROBE_AKTIV" ]; then '
'readlink -f "$PROBE_LINK" > "$PROBE_LAUFEND"; '
'touch "$PROBE_AKTIV"; fi ;;\n'
'esac\n')
systemctl.chmod(0o700)
env = dict(os.environ, PATH=str(basis) + ':' + os.environ['PATH'],
PROBE_AKTIV=str(aktiv), PROBE_LAUFEND=str(laufend),
PROBE_LINK=str(aktuell))
r = subprocess.run(
['bash', str(wurzel / 'scripts/programmrueckkehr.sh'),
str(alt), str(aktuell), 'isolierter-test.service'],
env=env, capture_output=True, text=True)
self.assertEqual(r.returncode, 0, r.stderr)
self.assertEqual(aktuell.resolve(), alt)
self.assertEqual(laufend.read_text().strip(), str(alt))
if __name__ == '__main__':
unittest.main()