Add an isolated PHP smoke test for hosting plans
This commit is contained in:
1 parent
64ee6ddcba
commit
374408f8c7
14 files changed
+253
-6
No files matched your search
@@ -48,6 +48,29 @@ to RabbitMQ. After a broker outage, queued requests resume when the worker and
|
||||
scheduler reconnect. Plans that remain incomplete five minutes after dispatch
|
||||
are eligible for delivery again.
|
||||
|
||||
## Run an isolated PHP test site
|
||||
|
||||
Open a completed preview for the account's current domain and select **Download
|
||||
test bundle**. The archive contains the exact Apache preview, a matching PHP-FPM
|
||||
pool, site metadata, and a small PHP test page. It is intended for the isolated
|
||||
Ubuntu check below.
|
||||
|
||||
Run the test with Docker from the repository root. Set the bundle path to the
|
||||
downloaded tar archive.
|
||||
|
||||
```sh
|
||||
docker build -t coulobre-web-smoke tests/web-node
|
||||
bundle="$PWD/coulobre-web-plan-1.tar"
|
||||
docker run --rm --network none \
|
||||
--mount "type=bind,src=$bundle,dst=/test/site.tar,readonly" \
|
||||
coulobre-web-smoke
|
||||
```
|
||||
|
||||
The Ubuntu container installs a system user for the site, checks the Apache and
|
||||
PHP-FPM configurations, and makes an HTTP request with the account's primary
|
||||
domain. A passing check confirms PHP execution through the generated socket.
|
||||
The container uses its own filesystem and loopback interface.
|
||||
|
||||
## MariaDB
|
||||
|
||||
Create a dedicated MariaDB database and application user. Install the MariaDB or
|
||||
|
||||
@@ -25,6 +25,7 @@ mariadb = ["mysqlclient>=2.2.1,<3"]
|
||||
"templates/*.html",
|
||||
"templates/hosting/*.conf",
|
||||
"templates/hosting/*.html",
|
||||
"templates/hosting/*.php",
|
||||
]
|
||||
|
||||
[tool.ruff]
|
||||
|
||||
@@ -28,5 +28,13 @@ class WebProvisioningPlan(models.Model):
|
||||
def for_account(cls, account):
|
||||
return cls(account=account, primary_domain=account.primary_domain)
|
||||
|
||||
@property
|
||||
def can_export(self):
|
||||
return (
|
||||
self.completed_at is not None
|
||||
and bool(self.apache_config)
|
||||
and self.primary_domain == self.account.primary_domain
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.primary_domain)
|
||||
@@ -6,6 +6,7 @@ from django.template.loader import render_to_string
|
||||
from django.utils import timezone
|
||||
|
||||
from .models import WebProvisioningPlan
|
||||
from .web_node import web_site_configuration
|
||||
|
||||
REDISPATCH_AFTER = timedelta(minutes=5)
|
||||
|
||||
@@ -37,7 +38,6 @@ def render_web_plan(plan_id):
|
||||
return
|
||||
|
||||
apache_config = render_to_string(
|
||||
"hosting/apache-vhost.conf",
|
||||
{"account_id": str(plan.account_id), "domain": plan.primary_domain},
|
||||
"hosting/apache-vhost.conf", web_site_configuration(plan)
|
||||
)
|
||||
pending.update(apache_config=apache_config, completed_at=timezone.now())
|
||||
@@ -1,14 +1,14 @@
|
||||
<VirtualHost *:80>
|
||||
ServerName {{ domain }}
|
||||
DocumentRoot "/srv/coulobre/web/{{ account_id }}/public"
|
||||
DocumentRoot "{{ document_root }}"
|
||||
|
||||
<Directory "/srv/coulobre/web/{{ account_id }}/public">
|
||||
<Directory "{{ document_root }}">
|
||||
AllowOverride None
|
||||
Options None
|
||||
Require all granted
|
||||
</Directory>
|
||||
|
||||
<FilesMatch "\.php$">
|
||||
SetHandler "proxy:unix:/run/php/coulobre-{{ account_id }}.sock|fcgi://localhost/"
|
||||
SetHandler "proxy:unix:{{ socket_path }}|fcgi://localhost/"
|
||||
</FilesMatch>
|
||||
</VirtualHost>
|
||||
@@ -0,0 +1,11 @@
|
||||
[{{ service_user }}]
|
||||
user = {{ service_user }}
|
||||
group = {{ service_user }}
|
||||
listen = {{ socket_path }}
|
||||
listen.owner = www-data
|
||||
listen.group = www-data
|
||||
listen.mode = 0660
|
||||
pm = ondemand
|
||||
pm.max_children = 2
|
||||
clear_env = yes
|
||||
security.limit_extensions = .php
|
||||
@@ -21,7 +21,12 @@
|
||||
{% endif %}
|
||||
{% if plan.completed_at %}
|
||||
<section class="surface" aria-labelledby="configuration-heading">
|
||||
<header class="section-heading"><h2 id="configuration-heading">Virtual host configuration</h2></header>
|
||||
<header class="section-heading">
|
||||
<h2 id="configuration-heading">Virtual host configuration</h2>
|
||||
{% if plan.can_export %}
|
||||
<a class="button secondary" href="{% url 'panel:download_smoke_bundle' plan.pk %}">Download test bundle</a>
|
||||
{% endif %}
|
||||
</header>
|
||||
<pre class="configuration"><code>{{ plan.apache_config }}</code></pre>
|
||||
</section>
|
||||
{% else %}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
header('Content-Type: text/plain; charset=UTF-8');
|
||||
echo "Coulobre PHP-FPM smoke check passed.\n";
|
||||
@@ -0,0 +1,73 @@
|
||||
import json
|
||||
import tarfile
|
||||
from io import BytesIO
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import TestCase
|
||||
from django.urls import reverse
|
||||
|
||||
from .models import HostingAccount, WebProvisioningPlan
|
||||
from .tasks import render_web_plan
|
||||
|
||||
|
||||
class WebSmokeBundleTests(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
users = get_user_model().objects
|
||||
cls.operator = users.create_superuser("operator")
|
||||
cls.viewer = users.create_user("viewer")
|
||||
cls.account = HostingAccount.objects.create(
|
||||
name="Example hosting", primary_domain="example.org"
|
||||
)
|
||||
cls.plan = WebProvisioningPlan.for_account(cls.account)
|
||||
cls.plan.save()
|
||||
|
||||
def setUp(self):
|
||||
self.client.force_login(self.operator)
|
||||
self.download = reverse("panel:download_smoke_bundle", args=[self.plan.pk])
|
||||
|
||||
def test_export_uses_the_reviewed_preview_and_matching_php_fpm_settings(self):
|
||||
render_web_plan.run(self.plan.pk)
|
||||
|
||||
detail = self.client.get(reverse("panel:plan", args=[self.plan.pk]))
|
||||
self.assertContains(detail, "Download test bundle")
|
||||
response = self.client.get(self.download)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response["Content-Type"], "application/x-tar")
|
||||
self.assertIn("attachment", response["Content-Disposition"])
|
||||
|
||||
self.plan.refresh_from_db()
|
||||
with tarfile.open(fileobj=BytesIO(response.content)) as bundle:
|
||||
self.assertEqual(
|
||||
set(bundle.getnames()),
|
||||
{"site.json", "apache.conf", "php-fpm.conf", "public/index.php"},
|
||||
)
|
||||
config = json.load(bundle.extractfile("site.json"))
|
||||
self.assertEqual(config["domain"], "example.org")
|
||||
self.assertEqual(config["account_id"], self.account.pk)
|
||||
|
||||
apache = bundle.extractfile("apache.conf").read().decode()
|
||||
fpm = bundle.extractfile("php-fpm.conf").read().decode()
|
||||
self.assertEqual(apache, self.plan.apache_config)
|
||||
self.assertIn(config["document_root"], apache)
|
||||
self.assertIn(config["socket_path"], apache)
|
||||
self.assertIn(f"listen = {config['socket_path']}", fpm)
|
||||
self.assertIn(f"user = {config['service_user']}", fpm)
|
||||
self.assertIn(b"<?php", bundle.extractfile("public/index.php").read())
|
||||
|
||||
def test_pending_and_stale_previews_cannot_be_exported(self):
|
||||
self.assertEqual(self.client.get(self.download).status_code, 409)
|
||||
render_web_plan.run(self.plan.pk)
|
||||
HostingAccount.objects.filter(pk=self.account.pk).update(
|
||||
primary_domain="updated.example"
|
||||
)
|
||||
|
||||
self.assertEqual(self.client.get(self.download).status_code, 409)
|
||||
detail = self.client.get(reverse("panel:plan", args=[self.plan.pk]))
|
||||
self.assertNotContains(detail, "Download test bundle")
|
||||
|
||||
def test_download_requires_plan_view_permission(self):
|
||||
render_web_plan.run(self.plan.pk)
|
||||
self.client.force_login(self.viewer)
|
||||
|
||||
self.assertEqual(self.client.get(self.download).status_code, 403)
|
||||
@@ -24,4 +24,9 @@ urlpatterns = [
|
||||
),
|
||||
path("plans/", views.PlanList.as_view(), name="plans"),
|
||||
path("plans/<int:pk>/", views.PlanDetail.as_view(), name="plan"),
|
||||
path(
|
||||
"plans/<int:pk>/smoke-bundle/",
|
||||
views.DownloadWebSmokeBundle.as_view(),
|
||||
name="download_smoke_bundle",
|
||||
),
|
||||
]
|
||||
@@ -1,11 +1,13 @@
|
||||
from django.contrib.auth.mixins import PermissionRequiredMixin
|
||||
from django.db.models import Q
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import redirect
|
||||
from django.views import View
|
||||
from django.views.generic import CreateView, DetailView, ListView, UpdateView
|
||||
from django.views.generic.detail import SingleObjectMixin
|
||||
|
||||
from .models import HostingAccount, WebProvisioningPlan
|
||||
from .web_node import build_web_smoke_bundle
|
||||
|
||||
VIEW_ACCOUNTS = "hosting.view_hostingaccount"
|
||||
CHANGE_ACCOUNTS = "hosting.change_hostingaccount"
|
||||
@@ -93,3 +95,23 @@ class PlanDetail(PermissionRequiredMixin, DetailView):
|
||||
permission_required = VIEW_PLANS
|
||||
template_name = "hosting/plan_detail.html"
|
||||
context_object_name = "plan"
|
||||
|
||||
|
||||
class DownloadWebSmokeBundle(PlanDetail):
|
||||
def render_to_response(self, context, **response_kwargs):
|
||||
plan = self.object
|
||||
if not plan.can_export:
|
||||
return HttpResponse(
|
||||
"Request a completed Apache preview for the current account domain.",
|
||||
status=409,
|
||||
content_type="text/plain",
|
||||
)
|
||||
|
||||
return HttpResponse(
|
||||
build_web_smoke_bundle(plan),
|
||||
content_type="application/x-tar",
|
||||
headers={
|
||||
"Cache-Control": "private, no-store",
|
||||
"Content-Disposition": f'attachment; filename="coulobre-web-plan-{plan.pk}.tar"',
|
||||
},
|
||||
)
|
||||
@@ -0,0 +1,35 @@
|
||||
import json
|
||||
import tarfile
|
||||
from io import BytesIO
|
||||
|
||||
from django.template.loader import render_to_string
|
||||
|
||||
|
||||
def web_site_configuration(plan):
|
||||
service_user = f"coulobre-{plan.account_id}"
|
||||
return {
|
||||
"account_id": plan.account_id,
|
||||
"domain": plan.primary_domain,
|
||||
"service_user": service_user,
|
||||
"document_root": f"/srv/coulobre/web/{plan.account_id}/public",
|
||||
"socket_path": f"/run/php/{service_user}.sock",
|
||||
}
|
||||
|
||||
|
||||
def build_web_smoke_bundle(plan):
|
||||
configuration = web_site_configuration(plan)
|
||||
files = {
|
||||
"site.json": json.dumps(configuration, indent=2) + "\n",
|
||||
"apache.conf": plan.apache_config,
|
||||
"php-fpm.conf": render_to_string("hosting/php-fpm-pool.conf", configuration),
|
||||
"public/index.php": render_to_string("hosting/smoke-index.php"),
|
||||
}
|
||||
|
||||
archive = BytesIO()
|
||||
with tarfile.open(fileobj=archive, mode="w") as bundle:
|
||||
for name, text in files.items():
|
||||
data = text.encode("utf-8")
|
||||
member = tarfile.TarInfo(name)
|
||||
member.size = len(data)
|
||||
bundle.addfile(member, BytesIO(data))
|
||||
return archive.getvalue()
|
||||
@@ -0,0 +1,11 @@
|
||||
FROM ubuntu:26.04
|
||||
|
||||
RUN apt-get update \
|
||||
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
|
||||
apache2 curl jq php8.5-fpm \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY smoke.sh /usr/local/bin/coulobre-web-smoke
|
||||
|
||||
ENTRYPOINT ["/bin/sh", "/usr/local/bin/coulobre-web-smoke"]
|
||||
CMD ["/test/site.tar"]
|
||||
@@ -0,0 +1,48 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
scratch=$(mktemp -d)
|
||||
tar --extract --file "$1" --directory "$scratch" --no-same-owner --no-same-permissions
|
||||
account_id=$(jq -er '.account_id' "$scratch/site.json")
|
||||
domain=$(jq -er '.domain' "$scratch/site.json")
|
||||
|
||||
case "$account_id" in
|
||||
''|*[!0-9]*) echo 'Invalid hosting account ID.' >&2; exit 1 ;;
|
||||
esac
|
||||
case "$domain" in
|
||||
''|*[!a-z0-9.-]*) echo 'Invalid primary domain.' >&2; exit 1 ;;
|
||||
esac
|
||||
|
||||
account_dir="/srv/coulobre/web/$account_id"
|
||||
site_user="coulobre-$account_id"
|
||||
site_conf="$site_user.conf"
|
||||
|
||||
useradd --system --user-group --no-create-home --home-dir "$account_dir" \
|
||||
--shell /usr/sbin/nologin "$site_user"
|
||||
install -d -m 0755 "$account_dir" /run/php
|
||||
install -d -m 0750 -o "$site_user" -g www-data "$account_dir/public"
|
||||
install -m 0640 -o "$site_user" -g www-data \
|
||||
"$scratch/public/index.php" "$account_dir/public/index.php"
|
||||
|
||||
install -m 0644 "$scratch/apache.conf" "/etc/apache2/sites-available/$site_conf"
|
||||
a2enmod proxy_fcgi >/dev/null
|
||||
a2dissite 000-default >/dev/null
|
||||
a2ensite "$site_conf" >/dev/null
|
||||
printf 'ServerName localhost\n' > /etc/apache2/conf-enabled/coulobre-smoke.conf
|
||||
|
||||
printf '[global]\nerror_log = /proc/self/fd/2\ninclude = %s\n' \
|
||||
"$scratch/php-fpm.conf" > "$scratch/fpm.conf"
|
||||
php-fpm8.5 --test --fpm-config "$scratch/fpm.conf"
|
||||
apache2ctl configtest
|
||||
|
||||
php-fpm8.5 --daemonize --fpm-config "$scratch/fpm.conf"
|
||||
apache2ctl start
|
||||
|
||||
body=$(curl --fail --silent --show-error --noproxy '*' --max-time 5 \
|
||||
--retry 5 --retry-connrefused --header "Host: $domain" http://127.0.0.1/index.php)
|
||||
if [ "$body" != 'Coulobre PHP-FPM smoke check passed.' ]; then
|
||||
echo 'The test site did not return the expected PHP response.' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
printf '%s via %s\n' "$body" "$domain"
|
||||
Reference in new issue
Block a user