diff --git a/tests/unit/utils/cleanup/test_nginx_vhosts.py b/tests/unit/utils/cleanup/test_nginx_vhosts.py index e8811aef74..ed685a2a9d 100644 --- a/tests/unit/utils/cleanup/test_nginx_vhosts.py +++ b/tests/unit/utils/cleanup/test_nginx_vhosts.py @@ -137,6 +137,44 @@ class TestIterVhostFiles(NginxVhostsTestBase, unittest.TestCase): ) self.assertEqual(got, []) + def test_yields_www_redirect_variant(self) -> None: + self._mk_role( + "web-svc-cdn", + application_id="web-svc-cdn", + canonical=["cdn.infinito.example"], + ) + bare = self._touch_vhost("cdn.infinito.example", "https") + redirect = self._touch_vhost("www.cdn.infinito.example", "https") + + got = sorted( + iter_vhost_files_for_entity( + "cdn", + nginx_dir=self.nginx_dir, + domain_primary="infinito.example", + roles_dir=self.roles_dir, + ) + ) + self.assertEqual(got, sorted([bare, redirect])) + + def test_www_prefixed_domain_not_double_prefixed(self) -> None: + self._mk_role( + "web-opt-rdr-www", + application_id="web-opt-rdr-www", + canonical=["www.w3redirect.infinito.example"], + ) + existing = self._touch_vhost("www.w3redirect.infinito.example", "https") + self._touch_vhost("www.www.w3redirect.infinito.example", "https") + + got = list( + iter_vhost_files_for_entity( + "opt-rdr-www", + nginx_dir=self.nginx_dir, + domain_primary="infinito.example", + roles_dir=self.roles_dir, + ) + ) + self.assertEqual(got, [existing]) + class TestPurgeVhostFiles(NginxVhostsTestBase, unittest.TestCase): def test_removes_only_matching_vhosts(self) -> None: @@ -183,6 +221,30 @@ class TestPurgeVhostFiles(NginxVhostsTestBase, unittest.TestCase): ) self.assertEqual(removed, []) + def test_removes_www_redirect_variant(self) -> None: + self._mk_role( + "web-svc-cdn", + application_id="web-svc-cdn", + canonical=["cdn.infinito.example"], + ) + bare = self._touch_vhost("cdn.infinito.example", "https") + redirect = self._touch_vhost("www.cdn.infinito.example", "https") + unrelated = self._touch_vhost("www.unrelated.infinito.example", "https") + + removed = sorted( + purge_vhost_files_for_entities( + ["cdn"], + nginx_dir=self.nginx_dir, + domain_primary="infinito.example", + roles_dir=self.roles_dir, + ) + ) + + self.assertEqual(removed, sorted([bare, redirect])) + self.assertFalse(bare.exists()) + self.assertFalse(redirect.exists()) + self.assertTrue(unrelated.exists()) + def test_multiple_entities_in_one_call(self) -> None: self._mk_role( "web-app-matomo", diff --git a/utils/cleanup/nginx_vhosts.py b/utils/cleanup/nginx_vhosts.py index 39ceb7a790..47ff3b33ac 100644 --- a/utils/cleanup/nginx_vhosts.py +++ b/utils/cleanup/nginx_vhosts.py @@ -61,7 +61,8 @@ def iter_vhost_files_for_entity( roles_dir: Path = ROLES_DIR, ) -> Iterable[Path]: """Yield existing vhost files (http + https) for every app belonging - to *entity*. Only yields files that exist on disk. + to *entity*, plus the ``www..conf`` redirect variant + generated by ``web-opt-rdr-www``. Only yields files that exist. """ apps = apps_for_entity(entity, roles_dir=roles_dir) if not apps: @@ -75,10 +76,14 @@ def iter_vhost_files_for_entity( if not app_conf: continue for domain in iter_app_domains(app_conf, include_aliases=True): - for proto in _PROTOCOLS: - conf_file = servers_dir / proto / f"{domain}.conf" - if conf_file.is_file(): - yield conf_file + domain_variants = [domain] + if not domain.startswith("www."): + domain_variants.append(f"www.{domain}") + for variant in domain_variants: + for proto in _PROTOCOLS: + conf_file = servers_dir / proto / f"{variant}.conf" + if conf_file.is_file(): + yield conf_file def purge_vhost_files_for_entities(