Files
cti/installer/tests/test_online_installer.py
2026-10-17 09:45:00 +03:30

379 lines
18 KiB
Python

"""Exercise actual installer sections without changing the host OS or services.
Run: python3 -m unittest discover -s installer/tests -v
Real Debian/systemd integration still needs a disposable VM.
"""
import importlib.machinery
import importlib.util
import io
import os
from pathlib import Path
import shlex
import subprocess
import tarfile
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
SOURCE = (ROOT / "installer/install.sh").read_text()
loader = importlib.machinery.SourceFileLoader("check_nginx_ports", str(ROOT / "installer/debian/check-nginx-ports"))
spec = importlib.util.spec_from_loader(loader.name, loader)
PORTS = importlib.util.module_from_spec(spec)
loader.exec_module(PORTS)
def section(start, end):
offset = SOURCE.index(start)
return SOURCE[offset:SOURCE.index(end, offset)]
PRELUDE = """set -Eeuo pipefail
log() { :; }
log_event() { :; }
stage_begin() { :; }
stage_done() { :; }
stage_skip() { STAGE_STATES[$1]=READY; }
render_ui() { :; }
fail() { echo "$*" >&2; exit 1; }
quiet_command() { "$@"; }
"""
def bash(script, env=None, check=True):
postgres = section("postgres_command() {", "quiet_command() {")
return subprocess.run(["bash", "-c", PRELUDE + postgres + script],
env=dict(os.environ, **(env or {})),
text=True, capture_output=True, check=check, timeout=30)
class OnlineInstallerTests(unittest.TestCase):
def test_shell_syntax(self):
for path in ("installer/install.sh", "installer/online-installer/install.sh", "installer/build.sh"):
subprocess.run(["bash", "-n", str(ROOT / path)], check=True)
for path in ("installer/install.sh", "installer/online-installer/install.sh"):
installer = (ROOT / path).read_text()
self.assertIn("allowmultiplelogin = yes", installer)
self.assertIn("pline.local-asterisk-api-token", installer)
stage_names = installer.split("STAGE_NAMES=(", 1)[1].split(")", 1)[0]
stage_states = installer.split("STAGE_STATES=(", 1)[1].split(")", 1)[0]
self.assertEqual(stage_names.count('"') // 2, stage_states.count('"') // 2)
def test_installer_respects_server_configured_apt_repositories(self):
commands = section("apt_cmd() {", "apt_policy() {")
self.assertNotIn("Dir::Etc::sourcelist", commands)
self.assertNotIn("Dir::Etc::sourceparts", commands)
self.assertNotIn("Dir::State::lists", commands)
self.assertIn("Acquire::ForceIPv4=true", commands)
self.assertIn("Acquire::Languages=none", commands)
def test_local_package_install_does_not_force_apt_refresh(self):
commands = section("apt_cmd() {", "apt_policy() {")
update = section("apt_update() {", "package_installed()")
result = bash('''
UI_TTY=0
APT_LOCK_TIMEOUT=600
APT_SOURCES_FILE=fixture
APT_LISTS_DIR=fixture
APT_METADATA_READY=0
CODENAME=bookworm
ARCH=amd64
apt-get() { echo "$*"; }
''' + commands + update + '\napt_cmd install --reinstall fixture.deb\n')
self.assertEqual(len(result.stdout.splitlines()), 1)
self.assertTrue(result.stdout.splitlines()[0].endswith(" install --reinstall fixture.deb"))
def test_dependencies_on_fresh_and_existing_hosts(self):
block = section('stage_begin 1 "Checking required system packages"', "JAVA_RUNTIME=")
for fresh in (True, False):
for compose in ("v1", "v2"):
with self.subTest(fresh=fresh, compose=compose):
script = '''
STAGE_STATES=(WAIT RUN)
APT_METADATA_READY=0
APT_OPTIONS=(-y --no-install-recommends)
tools_installed=$((1-FRESH))
apt_update() { APT_METADATA_READY=1; }
package_installed() { (( ! FRESH )); }
apt_policy() { echo 'Candidate: 1.0'; }
apt_cmd() { echo "$*"; tools_installed=1; }
systemctl() { ((tools_installed)); }
command() {
if [[ ${1:-} == -v && ${2:-} == docker ]]; then ((tools_installed));
elif [[ ${1:-} == -v && ${2:-} == docker-compose ]]; then [[ $COMPOSE == v1 ]] && ((tools_installed));
else builtin command "$@"; fi
}
docker() { [[ $COMPOSE == v2 ]] && ((tools_installed)); }
''' + block
result = bash(script, {"FRESH": str(int(fresh)), "COMPOSE": compose,
"APT_MIRROR_SCHEME": "https"})
if fresh:
for package in ("dmidecode", "procps", "util-linux", "openssh-server", "docker.io", "docker-compose"):
self.assertIn(package, result.stdout)
else:
self.assertEqual(result.stdout, "")
def test_backup_preserves_license_and_existing_configuration(self):
block = section('stage_begin 3 "Backing up existing CTI application files',
'stage_begin 4 "Force reinstalling')
for existing in (False, True):
with self.subTest(existing=existing), tempfile.TemporaryDirectory() as directory:
root = Path(directory)
app = root / "opt/cti"
app.mkdir(parents=True)
if existing:
(app / "license.lic").write_text("customer-license")
(app / "application.properties").write_text(
"spring.datasource.url=jdbc:postgresql://127.0.0.1:5433/customer\n")
(app / "base/etc/asterisk").mkdir(parents=True)
(app / "base/etc/asterisk/manager.conf").write_text("customer-settings")
rewritten = block
for path in ("/opt/cti", "/etc/nginx", "/etc/ssl/raymand-cti", "/var/www/cti"):
rewritten = rewritten.replace(path, str(root / path.lstrip("/")))
fixture = root / "package.deb"
fixture.write_text("package")
script = '''
STOPPED_CTI_SERVICES=()
systemctl() { [[ $EXISTING == 1 && $* == 'is-active --quiet raymnad-cti.service' ]] || [[ $1 == stop ]]; }
runuser() {
[[ $* != *psql* || $* == *PGPORT=5433* ]] || exit 99
if [[ $* == *pg_database_size* ]]; then echo 10;
elif [[ $* == *psql* ]]; then echo 1;
elif [[ $* == *pg_dump* ]]; then echo archive;
elif [[ $* == *pg_restore* ]]; then cat >/dev/null; fi
}
dpkg-deb() { echo 3.0.0; }
append_captured_output() { :; }
''' + rewritten
bash(script, {"EXISTING": str(int(existing)), "PACKAGE_DEB": str(fixture),
"UI_LOG_FILE": str(root / "log"),
"RAYMAND_CTI_BACKUP_DIR": str(root / "backups")})
backup = next((root / "backups").glob("installer-*"))
subprocess.run(["sha256sum", "-c", "SHA256SUMS"], cwd=backup,
capture_output=True, check=True)
if existing:
self.assertEqual((app / "license.lic").read_text(), "customer-license")
with tarfile.open(backup / "application-files.tar.gz") as archive:
name = str(app / "license.lic").lstrip("/")
self.assertEqual(archive.extractfile(name).read(), b"customer-license")
self.assertTrue((backup / "database.dump").exists())
else:
self.assertFalse((backup / "database.dump").exists())
def test_java25_selection_on_fresh_and_upgrade_hosts(self):
block = section("JAVA_RUNTIME=openjdk-25-jre-headless", 'stage_begin 3 "Preparing PostgreSQL')
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
binary = root / "jvm/java-25/bin/java"
binary.parent.mkdir(parents=True)
binary.write_text("#!/bin/sh\necho ' java.specification.version = 25' >&2\n")
binary.chmod(0o755)
for active in ("", "25", "26"):
for candidate in ("25", "(none)"):
with self.subTest(active=active, candidate=candidate):
script = '''
APT_METADATA_READY=1
APT_OPTIONS=(-y --no-install-recommends)
APT_ADOPTIUM_SOURCE="$TEST_ADOPTIUM_SOURCE"
java() { echo " java.specification.version = $ACTIVE_JAVA" >&2; }
apt_update() { APT_METADATA_READY=1; }
apt_policy() { echo "Candidate: $CANDIDATE"; }
apt_cmd() { echo "apt $*"; }
curl() { echo key; }
gpg() { cat >/dev/null; touch "$APT_SOURCES_DIR/adoptium.gpg"; }
update-alternatives() {
[[ $1 == --set && $2 == java && $3 == */java-25/bin/java ]]
ACTIVE_JAVA=25
echo 'selected Java 25'
}
''' + block.replace("/usr/lib/jvm", str(root / "jvm"))
result = bash(script, {"ACTIVE_JAVA": active, "CANDIDATE": candidate,
"APT_SOURCES_DIR": str(root),
"TEST_ADOPTIUM_SOURCE": str(root / "adoptium.list"),
"CODENAME": "bookworm"})
if active == "25":
self.assertEqual(result.stdout, "")
else:
self.assertIn("selected Java 25", result.stdout)
self.assertIn("temurin-25-jdk" if candidate == "(none)"
else "openjdk-25-jre-headless", result.stdout)
def test_reinstall_always_runs_and_rejects_stale_jar(self):
block = section('log "Force reinstalling Raymand CTI package version',
"install -d -m 0755 /opt/cti")
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
jar = root / "installed.jar"
jar.write_bytes(b"new app")
with tarfile.open(root / "package.tar", "w") as archive:
entry = tarfile.TarInfo("./opt/cti/raymandcti.jar")
entry.size = 7
archive.addfile(entry, io.BytesIO(b"new app"))
script = '''
APT_OPTIONS=(-y --no-install-recommends)
PACKAGE_VERSION=3.0.0
STAGED_PACKAGE=fixture.deb
apt_cmd() { echo "$*"; }
dpkg-deb() { cat "$TEST_ARCHIVE"; }
''' + block.replace("sha256sum /opt/cti/raymandcti.jar", "sha256sum " + shlex.quote(str(jar)))
env = {"TEST_ARCHIVE": str(root / "package.tar")}
for _ in range(2):
result = bash(script, env)
self.assertIn("--force-overwrite", result.stdout)
self.assertIn("--reinstall", result.stdout)
self.assertIn("--force-confold", result.stdout)
jar.write_bytes(b"old app")
result = bash(script, env, check=False)
self.assertNotEqual(result.returncode, 0)
self.assertIn("replacement was not verified", result.stderr)
def test_failed_backup_restarts_only_previously_running_cti_services(self):
cleanup = section("cleanup() {", "trap cleanup EXIT")
for mutation in (0, 1):
result = bash('''
UI_TTY=0
JAVA_PRESTART_PID=''
STOPPED_CTI_SERVICES=(pline.service)
SEED_CONTAINER=''
APT_SOURCES_DIR=''
APT_ADOPTIUM_SOURCE=''
UI_LOG_FILE=''
systemctl() { echo "$*"; }
''' + cleanup + '\nif false; then :; else cleanup; fi\n',
{"PACKAGE_INSTALL_STARTED": str(mutation)})
self.assertEqual(result.stdout, "start pline.service\n" if mutation == 0 else "")
def test_ami_bootstrap_preserves_upgrade_settings(self):
block = section('AMI_BOOTSTRAP_DIR="$ASTERISK_BASE', 'systemctl is-active --quiet docker.service')
for fresh in (True, False):
with self.subTest(fresh=fresh), tempfile.TemporaryDirectory() as directory:
root = Path(directory)
config = root / "base/etc/asterisk"
(config / "manager.d").mkdir(parents=True)
if not fresh:
(config / "manager.conf").write_text("[general]\nenabled=yes\nport=2342\n; customer setting\n")
(config / "manager.d/pline.conf").write_text("[pline]\nsecret=existing\n")
# Exercise file writes as the current user; strip only ownership arguments.
script = block.replace("-o pline -g pline ", "").replace(
'chown pline:pline "$AMI_MANAGER_CONFIG" "$AMI_BOOTSTRAP_CONFIG"', ":")
bash(script, {"ASTERISK_BASE": str(root), "NEED_SEED_CONFIG": str(int(fresh))})
text = (config / "manager.conf").read_text()
self.assertIn('#include "manager.d/*.conf"', text)
if fresh:
self.assertIn("allowmultiplelogin = yes", (config / "manager.d/pline.conf").read_text())
if not fresh:
self.assertIn("; customer setting", text)
self.assertEqual((config / "manager.d/pline.conf").read_text(), "[pline]\nsecret=existing\n")
def test_database_commands_use_configured_port(self):
database = section('stage_begin 5 "Checking PostgreSQL role', "unset DB_PASSWORD")
self.assertNotIn("runuser -u postgres -- psql", database)
self.assertEqual(database.count('postgres_command env PGPORT="$DB_PORT" psql'), 3)
def test_postgres_runs_in_accessible_directory_without_changing_parent(self):
result = bash('''
parent=$PWD
runuser() { [[ $PWD == /tmp ]]; printf '%s\\n' "$*"; }
postgres_command env PGPORT=5433 psql -d customer
[[ $PWD == "$parent" ]]
''')
self.assertEqual(result.stdout.strip(), '-u postgres -- env PGPORT=5433 psql -d customer')
def test_journal_since_uses_epoch_timestamp(self):
timestamp = section('JAVA_SERVICE_START_TIME=', 'BACKEND_LISTENERS=')
result = bash(timestamp + '\nprintf "%s" "$JAVA_SERVICE_START_TIME"\n')
self.assertRegex(result.stdout, r'^@\d+$')
def test_default_hold_music_is_created_without_overwriting_existing_audio(self):
block = section('DEFAULT_HOLD_MUSIC=', 'if [[ "${STAGE_STATES[6]}"')
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
target = root / 'base/var/lib/asterisk/moh/pline-hold.wav'
target.parent.mkdir(parents=True)
source = root / 'default.wav'
source.write_bytes(b'default audio')
block = block.replace('/opt/cti/resource/pline-hold.wav', str(source))
bash(block, {"ASTERISK_BASE": str(root)})
self.assertEqual(target.read_bytes(), b'default audio')
target.write_bytes(b'customer audio')
bash(block, {"ASTERISK_BASE": str(root)})
self.assertEqual(target.read_bytes(), b'customer audio')
def test_nginx_waits_for_https_and_docker_is_not_globally_restarted(self):
self.assertNotIn("systemctl restart docker", SOURCE)
self.assertNotIn('for service in postgresql docker nginx', SOURCE)
self.assertLess(SOURCE.index('quiet_command /usr/lib/raymand-cti/setup-https'),
SOURCE.index('quiet_command /usr/lib/raymand-cti/check-nginx-ports'))
self.assertLess(SOURCE.index('quiet_command /usr/lib/raymand-cti/check-nginx-ports'),
SOURCE.index('quiet_command systemctl restart nginx.service'))
def test_conflicting_web_servers_are_disabled_before_nginx(self):
block = section('disable_conflicting_web_servers() {', 'quiet_command /usr/sbin/nginx -t')
script = '''
systemctl() {
case "$*" in
"cat apache2.service"|"cat caddy.service") return 0 ;;
"is-active --quiet apache2.service") return 0 ;;
"is-enabled --quiet caddy.service") return 0 ;;
"disable --now apache2.service"|"disable --now caddy.service") echo "$*"; return 0 ;;
*) return 1 ;;
esac
}
''' + block
result = bash(script)
self.assertEqual(result.stdout.splitlines(), ["disable --now apache2.service", "disable --now caddy.service"])
self.assertNotIn("nginx.service", result.stdout)
def test_nginx_include_migration_keeps_include_inside_https_block(self):
helper = (ROOT / "installer/debian/ensure-nginx-proxy-include").read_text()
for already_included in (False, True):
with self.subTest(included=already_included), tempfile.TemporaryDirectory() as directory:
config = Path(directory) / "site.conf"
include = ' include /etc/nginx/raymand-cti/webrtc.conf;\n'
config.write_text('server {\n listen 80;\n}\nserver {\n listen 443 ssl;\n'
' # customer settings\n'
' include /etc/nginx/raymand-cti/custom-proxies.conf;\n'
+ (include if already_included else '') + '}\n')
script = helper.replace('Path("/etc/nginx/sites-available/raymand-cti")',
'Path(' + repr(str(config)) + ')')
for _ in range(2):
subprocess.run(["python3", "-c", script], check=True)
result = config.read_text()
self.assertEqual(result.count(include), 1)
self.assertNotIn("custom-proxies.conf", result)
self.assertTrue(result.endswith(include + '}\n'))
self.assertIn("# customer settings", result)
class NginxPortTests(unittest.TestCase):
CONFIG = "server {\n listen 80;\n listen [::]:443 ssl;\n listen 8443 ssl;\n}"
def listeners(self, process="apache2", pid="123", port=80):
return f'LISTEN 0 511 0.0.0.0:{port} 0.0.0.0:* users:(("{process}",pid={pid},fd=6))'
def test_fresh_host_has_no_conflicts(self):
self.assertEqual(PORTS.conflicts(self.CONFIG, "", "", lambda _: ""), [])
def test_managed_nginx_is_allowed_on_upgrade(self):
self.assertEqual(PORTS.conflicts(self.CONFIG, self.listeners("nginx"),
"/system.slice/nginx.service",
lambda _: "0::/system.slice/nginx.service\n"), [])
def test_apache_and_unmanaged_nginx_are_reported(self):
for process in ("apache2", "nginx", "docker-proxy"):
line = self.listeners(process)
self.assertEqual(PORTS.conflicts(self.CONFIG, line, "/system.slice/nginx.service",
lambda _: "0::/system.slice/other.service\n"), [line])
def test_custom_ports_are_checked_but_unrelated_ports_are_ignored(self):
custom = self.listeners(port=8443)
self.assertEqual(PORTS.conflicts(self.CONFIG, custom, "", lambda _: ""), [custom])
self.assertEqual(PORTS.conflicts(self.CONFIG, self.listeners(port=9000), "", lambda _: ""), [])
def test_missing_pid_information_does_not_hide_conflict(self):
line = "LISTEN 0 511 [::]:443 [::]:*"
self.assertEqual(PORTS.conflicts(self.CONFIG, line, "", lambda _: ""), [line])
if __name__ == "__main__":
unittest.main()