Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

test(real-binaries): run cron and nginx service code against real parsers

Juan Denisa0a111c0719a
diff --git a/.github/workflows/backend-ci.yml b/.github/workflows/backend-ci.yml
index 83b79e60..e5a1f746 100644
--- a/.github/workflows/backend-ci.yml
+++ b/.github/workflows/backend-ci.yml
@@ -94,3 +94,36 @@ jobs:
# to debug a red shard locally.
working-directory: backend
run: python -m pytest tests -v --splits 4 --group ${{ matrix.group }}
+
+ # The @real_binaries leg (plan 82 §E): the same service code the shards run
+ # against fake_subprocess meets the REAL parsers here — the panel's
+ # generated crontab text goes through `crontab -`/`crontab -l`, and every
+ # rendered vhost flavor through `nginx -t`. This is the class of bug the
+ # #117 cron fix taught us write-only assertions cannot see. The tests skip
+ # themselves everywhere unless SERVERKIT_REAL_BINARIES=1, so the shards
+ # above never mutate a runner's crontab by surprise.
+ real-binaries:
+ name: real binaries
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v4
+ - uses: actions/setup-python@v5
+ with:
+ python-version: '3.11'
+ cache: pip
+ cache-dependency-path: backend/requirements.txt
+ - name: Install nginx and cron
+ run: |
+ sudo apt-get update -qq
+ sudo apt-get install -y -qq nginx-core cron
+ - name: Install dependencies
+ working-directory: backend
+ run: |
+ python -m pip install --upgrade pip
+ pip install -r requirements.txt
+ pip install pytest
+ - name: Run real-binaries tests
+ working-directory: backend
+ env:
+ SERVERKIT_REAL_BINARIES: '1'
+ run: python -m pytest tests -m real_binaries -v
diff --git a/.github/workflows/scripts-ci.yml b/.github/workflows/scripts-ci.yml
index 0d70670e..ecf9aeb8 100644
--- a/.github/workflows/scripts-ci.yml
+++ b/.github/workflows/scripts-ci.yml
@@ -198,6 +198,20 @@ jobs:
bash scripts/test/test_release_workflow.sh
'
+ # The probe-honesty integration legs (plan 75 §D) — real ufw/nginx/firewalld
+ # in 4 Docker shapes (sbinless PATH, non-root+sudo, rocky/firewalld,
+ # cap-dropped LXC shape). The harness existed since plan 75 but was wired
+ # into NO workflow, so the one pytest suite that runs firewall probes
+ # against real binaries never ran in CI (plan 82 §E).
+ probe-matrix:
+ name: probe matrix (real firewall probes)
+ runs-on: ubuntu-latest
+ timeout-minutes: 30
+ steps:
+ - uses: actions/checkout@v4
+ - name: Run probe matrix
+ run: bash scripts/test/probe-matrix.sh
+
python-provisioning:
name: python provisioning (${{ matrix.image }})
runs-on: ubuntu-latest
diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py
index f52da7cd..5ab46065 100644
--- a/backend/tests/conftest.py
+++ b/backend/tests/conftest.py
@@ -109,6 +109,17 @@ def pytest_configure(config):
'STRUCTURE — Flask cannot unregister a blueprint or a url rule, so such '
'a mutation would leak into every later test on a shared app.',
)
+ config.addinivalue_line(
+ 'markers',
+ 'real_binaries: run service code against the REAL system binaries '
+ '(crontab, nginx -t, ...) instead of the fake_subprocess stub — the '
+ 'leg that catches what write-only assertions cannot (plan 82 §E). '
+ 'These tests mutate real system state (e.g. the invoking user\'s '
+ 'crontab, with backup/restore), so they additionally skip unless '
+ 'SERVERKIT_REAL_BINARIES=1: select with '
+ '`SERVERKIT_REAL_BINARIES=1 pytest tests -m real_binaries` on Linux '
+ '(WSL works) or in CI\'s real-binaries job.',
+ )
@pytest.fixture(autouse=True)
diff --git a/backend/tests/test_real_crontab_roundtrip.py b/backend/tests/test_real_crontab_roundtrip.py
new file mode 100644
index 00000000..1bc40326
--- /dev/null
+++ b/backend/tests/test_real_crontab_roundtrip.py
@@ -0,0 +1,123 @@
+"""Cron round trip against the REAL crontab binary (plan 82 §E).
+
+test_cron_crontab_join.py proves the #117 join against an in-memory crontab
+store; this file is the same cycle with the stubs removed — the panel's
+generated crontab text goes through `crontab -` and comes back through
+`crontab -l`, so a syntax the real parser rejects, or a normalization it
+applies that breaks the marker join, fails HERE instead of on a user's box.
+
+State safety: mutates the invoking user's crontab, with the pre-test crontab
+captured and restored by the fixture — which is why these tests need the
+explicit SERVERKIT_REAL_BINARIES=1 opt-in on top of the marker. Run with:
+
+ SERVERKIT_REAL_BINARIES=1 pytest tests -m real_binaries
+"""
+import os
+import platform
+import shutil
+import subprocess
+
+import pytest
+
+from app.services.cron_service import CronService
+
+
+pytestmark = [
+ pytest.mark.real_binaries,
+ pytest.mark.skipif(platform.system() != 'Linux',
+ reason='real crontab needs Linux'),
+ pytest.mark.skipif(shutil.which('crontab') is None,
+ reason='crontab binary not installed'),
+ pytest.mark.skipif(os.environ.get('SERVERKIT_REAL_BINARIES') != '1',
+ reason='mutates the real user crontab; opt in with '
+ 'SERVERKIT_REAL_BINARIES=1'),
+]
+
+
+def real_crontab(tmp_path, monkeypatch):
+ """Metadata redirected to tmp; the crontab is REAL, saved and restored."""
+ import app.services.cron_service as mod
+ monkeypatch.setattr(mod, 'JOBS_FILE', str(tmp_path / 'cron_jobs.json'))
+
+ saved = subprocess.run(['crontab', '-l'], capture_output=True, text=True)
+ original = saved.stdout if saved.returncode == 0 else None
+ yield
+ if original is None:
+ subprocess.run(['crontab', '-r'], capture_output=True)
+ else:
+ subprocess.run(['crontab', '-'], input=original,
+ capture_output=True, text=True)
+
+
+CMD = '/usr/local/bin/real-backup.sh --full'
+
+
+def _job_named(name):
+ jobs = CronService.list_jobs()['jobs']
+ matches = [j for j in jobs if j.get('name') == name]
+ assert matches, f'{name!r} not in {[j.get("name") for j in jobs]}'
+ return matches[0]
+
+
+def test_add_list_toggle_remove_against_real_crontab(real_crontab):
+ added = CronService.add_job('*/5 * * * *', CMD, name='Real Backup')
+ assert added['success'], added
+
+ # The real crontab now holds the marker + line the panel wrote.
+ live = subprocess.run(['crontab', '-l'], capture_output=True, text=True)
+ assert '# ServerKit Job:' in live.stdout
+ assert CMD in live.stdout
+
+ # list joins the re-parsed line back to its metadata (#117's cycle).
+ job = _job_named('Real Backup')
+ job_id = job['id']
+ assert not job_id.startswith('cron_'), 'synthetic id — metadata join lost'
+
+ # Mutations must find the job by that id, through the real binary.
+ toggled = CronService.toggle_job(job_id, enabled=False)
+ assert toggled['success'], toggled
+ assert _job_named('Real Backup').get('enabled') is False
+
+ toggled = CronService.toggle_job(job_id, enabled=True)
+ assert toggled['success'], toggled
+ live = subprocess.run(['crontab', '-l'], capture_output=True, text=True)
+ active = [l for l in live.stdout.splitlines()
+ if CMD in l and not l.lstrip().startswith('#')]
+ assert active, 're-enabled job line is not active in the real crontab'
+
+ removed = CronService.remove_job(job_id)
+ assert removed['success'], removed
+ live = subprocess.run(['crontab', '-l'], capture_output=True, text=True)
+ assert CMD not in (live.stdout or '')
+ assert all(j.get('name') != 'Real Backup'
+ for j in CronService.list_jobs()['jobs'])
+
+
+def test_panel_jobs_coexist_with_operator_lines(real_crontab):
+ # An operator-authored line the panel must list but never destroy.
+ operator_line = '17 3 * * * /opt/operator/rotate-logs.sh'
+ subprocess.run(['crontab', '-'], input=operator_line + '\n',
+ capture_output=True, text=True)
+
+ added = CronService.add_job('0 * * * *', CMD, name='Hourly')
+ assert added['success'], added
+ removed = CronService.remove_job(_job_named('Hourly')['id'])
+ assert removed['success'], removed
+
+ live = subprocess.run(['crontab', '-l'], capture_output=True, text=True)
+ assert operator_line in live.stdout
+ assert CMD not in live.stdout
+
+
+def test_name_with_whitespace_cannot_break_the_real_crontab(real_crontab):
+ added = CronService.add_job('*/10 * * * *', CMD,
+ name='evil\n* * * * * /tmp/injected.sh')
+ assert added['success'], added
+
+ live = subprocess.run(['crontab', '-l'], capture_output=True, text=True)
+ assert '/tmp/injected.sh' not in [
+ l.split(None, 5)[-1] if len(l.split(None, 5)) == 6 else ''
+ for l in live.stdout.splitlines()
+ if l.strip() and not l.strip().startswith('#')
+ ], 'newline in a job name injected a live crontab line'
diff --git a/backend/tests/test_real_nginx_vhost_syntax.py b/backend/tests/test_real_nginx_vhost_syntax.py
new file mode 100644
index 00000000..446b062c
--- /dev/null
+++ b/backend/tests/test_real_nginx_vhost_syntax.py
@@ -0,0 +1,87 @@
+"""Generated vhosts through the REAL nginx parser (plan 82 §E).
+
+scripts/test/test_nginx_conf.sh runs `nginx -t` on the SHIPPED static
+vhosts; nothing ever fed NginxService.render_site_config's GENERATED output
+back through the real parser, so a template edit nginx rejects only failed
+on a user's box at reload time. Each rendered flavor is wrapped in a minimal
+nginx.conf and parsed with `nginx -t`.
+
+Read-only for the system (everything under tmp_path); still gated with the
+other real-binaries tests for one consistent selection story. Run with:
+
+ SERVERKIT_REAL_BINARIES=1 pytest tests -m real_binaries
+"""
+import os
+import platform
+import shutil
+import subprocess
+
+import pytest
+
+from app.services.nginx_service import NginxService
+
+
+NGINX = (shutil.which('nginx')
+ or next((p for p in ('/usr/sbin/nginx', '/usr/local/sbin/nginx')
+ if os.path.exists(p)), None))
+
+pytestmark = [
+ pytest.mark.real_binaries,
+ pytest.mark.skipif(platform.system() != 'Linux',
+ reason='real nginx needs Linux'),
+ pytest.mark.skipif(NGINX is None, reason='nginx binary not installed'),
+ pytest.mark.skipif(os.environ.get('SERVERKIT_REAL_BINARIES') != '1',
+ reason='real-binaries leg; opt in with '
+ 'SERVERKIT_REAL_BINARIES=1'),
+]
+
+
+def _nginx_t(tmp_path, vhost_config):
+ """`nginx -t` over a minimal wrapper conf that includes *vhost_config*."""
+ vhost = tmp_path / 'vhost.conf'
+ vhost.write_text(vhost_config)
+ wrapper = tmp_path / 'nginx.conf'
+ wrapper.write_text(
+ f'pid {tmp_path}/nginx.pid;\n'
+ f'error_log {tmp_path}/error.log;\n'
+ 'events {}\n'
+ 'http {\n'
+ f' access_log {tmp_path}/access.log;\n'
+ f' client_body_temp_path {tmp_path}/body;\n'
+ f' proxy_temp_path {tmp_path}/proxy;\n'
+ f' fastcgi_temp_path {tmp_path}/fastcgi;\n'
+ f' uwsgi_temp_path {tmp_path}/uwsgi;\n'
+ f' scgi_temp_path {tmp_path}/scgi;\n'
+ f' include {vhost};\n'
+ '}\n')
+ return subprocess.run([NGINX, '-t', '-c', str(wrapper)],
+ capture_output=True, text=True)
+
+
+def _render(**kwargs):
+ rendered = NginxService.render_site_config(**kwargs)
+ assert rendered.get('success'), rendered
+ return rendered['config']
+
+
[email protected]('flavor,kwargs', [
+ ('docker-proxy', dict(name='shop', app_type='docker',
+ domains=['shop.example.com'], port=8003)),
+ ('static', dict(name='blog', app_type='static',
+ domains=['blog.example.com'], root_path='/var/www/blog')),
+ ('php', dict(name='wp', app_type='php',
+ domains=['wp.example.com'], root_path='/var/www/wp')),
+ ('docker-micro-cache', dict(name='fast', app_type='docker',
+ domains=['fast.example.com'], port=8004,
+ micro_cache=True)),
+])
+def test_generated_vhost_parses_with_real_nginx(tmp_path, flavor, kwargs):
+ proc = _nginx_t(tmp_path, _render(**kwargs))
+ assert proc.returncode == 0, (
+ f'{flavor}: real nginx rejected the generated vhost:\n{proc.stderr}')
+
+
+def test_the_harness_itself_rejects_garbage(tmp_path):
+ """A wrapper that passed everything would make the suite vacuous."""
+ proc = _nginx_t(tmp_path, 'server { this is not nginx syntax }')
+ assert proc.returncode != 0