Document staging publication and test Apache rollback
This commit is contained in:
1 parent
0f9f0adc5c
commit
528fa73286
2 files changed
+113
No files matched your search
@@ -1,5 +1,6 @@
|
||||
from pathlib import Path
|
||||
from subprocess import CompletedProcess
|
||||
from tempfile import TemporaryDirectory
|
||||
from unittest.mock import Mock, patch
|
||||
from uuid import uuid4
|
||||
|
||||
@@ -7,6 +8,7 @@ from django.test import SimpleTestCase
|
||||
|
||||
from coulobre.database_node import DatabaseSpec
|
||||
from coulobre.database_node_backup import check_wordpress_schema
|
||||
from coulobre.php_runtime import PhpRuntime
|
||||
from coulobre.web_node import WebSite
|
||||
from coulobre.wordpress_staging import WordPressStagingRevision
|
||||
from coulobre.wordpress_staging_node import (
|
||||
@@ -14,6 +16,12 @@ from coulobre.wordpress_staging_node import (
|
||||
read_source_config,
|
||||
verify_stage,
|
||||
)
|
||||
from coulobre.wordpress_staging_publication import (
|
||||
apply_vhost,
|
||||
render_pool,
|
||||
render_runtime,
|
||||
render_vhost,
|
||||
)
|
||||
|
||||
|
||||
class StagingRevisionTests(SimpleTestCase):
|
||||
@@ -119,3 +127,68 @@ class WordPressSchemaTests(SimpleTestCase):
|
||||
check_wordpress_schema(cursor, "test_db", "wp_")
|
||||
else:
|
||||
check_wordpress_schema(cursor, "test_db", "wp_")
|
||||
|
||||
|
||||
class StagingPublicationTests(SimpleTestCase):
|
||||
def setUp(self):
|
||||
self.revision = WordPressStagingRevision(
|
||||
WebSite(7, "wordpress.example.test"),
|
||||
DatabaseSpec(7, "wordpress"),
|
||||
"preview.example.test",
|
||||
uuid4(),
|
||||
)
|
||||
|
||||
def test_https_requires_access_for_static_files_and_php(self):
|
||||
directory = Path("/srv/coulobre/wordpress-staging") / self.revision.key
|
||||
config = render_vhost(
|
||||
self.revision,
|
||||
directory,
|
||||
Path("/etc/wordpress.htpasswd"),
|
||||
(Path("/etc/fullchain.pem"), Path("/etc/privkey.pem")),
|
||||
)
|
||||
self.assertIn("ServerName preview.example.test", config)
|
||||
self.assertIn('RedirectMatch permanent "', config)
|
||||
self.assertIn("https://preview.example.test/$1", config)
|
||||
self.assertIn('Alias "/.well-known/acme-challenge/"', config)
|
||||
self.assertIn('<Location "/">', config)
|
||||
self.assertIn("Require valid-user", config)
|
||||
self.assertIn("RequestHeader unset Authorization", config)
|
||||
self.assertIn(
|
||||
'Header always set X-Robots-Tag "noindex, nofollow"', config
|
||||
)
|
||||
self.assertIn(
|
||||
'Header always set Cache-Control "private, no-store"', config
|
||||
)
|
||||
self.assertNotIn("wordpress.example.test", config.partition("\n")[2])
|
||||
|
||||
def test_the_web_runtime_uses_the_staging_identity_and_blocks_egress(self):
|
||||
directory = Path("/srv/coulobre/wordpress-staging") / self.revision.key
|
||||
unit = render_runtime(self.revision, directory, PhpRuntime("8.5"))
|
||||
pool = render_pool(self.revision, directory)
|
||||
self.assertIn("PrivateNetwork=yes", unit)
|
||||
self.assertIn("RestrictAddressFamilies=AF_UNIX", unit)
|
||||
self.assertIn("TemporaryFileSystem=/run:ro", unit)
|
||||
self.assertIn("BindReadOnlyPaths=/run/mysqld", unit)
|
||||
self.assertIn(f"user = {self.revision.key}", pool)
|
||||
self.assertIn("php_admin_value[sendmail_path] = /usr/bin/false", pool)
|
||||
|
||||
@patch("coulobre.wordpress_staging_publication.read_root_file")
|
||||
@patch("coulobre.wordpress_staging_publication.web.run")
|
||||
def test_failed_reload_restores_the_previous_route(self, run, read):
|
||||
read.side_effect = Path.read_text
|
||||
run.side_effect = (None, ValueError("Apache reload failed"), None)
|
||||
with TemporaryDirectory() as work:
|
||||
root = Path(work)
|
||||
available = root / "sites-available"
|
||||
enabled = root / "sites-enabled"
|
||||
available.mkdir()
|
||||
enabled.mkdir()
|
||||
config = available / "staging.conf"
|
||||
link = enabled / config.name
|
||||
link.symlink_to(Path("../sites-available") / config.name)
|
||||
config.write_text("previous verified revision")
|
||||
with self.assertRaisesRegex(ValueError, "Apache reload failed"):
|
||||
apply_vhost(config, link, "new verified revision")
|
||||
self.assertEqual(config.read_text(), "previous verified revision")
|
||||
self.assertTrue(link.exists())
|
||||
self.assertEqual(run.call_count, 3)
|
||||
Reference in new issue
Block a user