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
@@ -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()
|
||||
Reference in new issue
Block a user