|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Unit tests for certman's renewal-window handling. |
| 3 | +
|
| 4 | +Run: python3 scripts/tests/test_certman.py |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +import shutil |
| 9 | +import sys |
| 10 | +import tempfile |
| 11 | +import unittest |
| 12 | + |
| 13 | +sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")) |
| 14 | + |
| 15 | +import certman # noqa: E402 |
| 16 | + |
| 17 | +# A lineage config as certbot writes it: the key ships commented out, and |
| 18 | +# everything after the first section header is out of bounds for a top-level key. |
| 19 | +CERTBOT_CONF = """\ |
| 20 | +version = 5.7.0 |
| 21 | +archive_dir = /etc/letsencrypt/archive/example.com |
| 22 | +cert = /etc/letsencrypt/live/example.com/cert.pem |
| 23 | +# renew_before_expiry = 30 days |
| 24 | +
|
| 25 | +[renewalparams] |
| 26 | +authenticator = dns-cloudflare |
| 27 | +""" |
| 28 | + |
| 29 | + |
| 30 | +def _manager(path: str) -> certman.CertManager: |
| 31 | + """A CertManager that reads and writes one temp file. |
| 32 | +
|
| 33 | + __init__ builds a provider from the environment; none of that is involved |
| 34 | + in rewriting a config file, so bypass it. |
| 35 | + """ |
| 36 | + mgr = object.__new__(certman.CertManager) |
| 37 | + mgr._renewal_conf_path = lambda domain: path # noqa: SLF001 |
| 38 | + return mgr |
| 39 | + |
| 40 | + |
| 41 | +class RenewalWindowTest(unittest.TestCase): |
| 42 | + def setUp(self): |
| 43 | + self._saved = os.environ.get("RENEW_DAYS_BEFORE") |
| 44 | + # Its own directory, so a test may make the directory unwritable |
| 45 | + # without touching anything it does not own. |
| 46 | + self.dir = tempfile.mkdtemp() |
| 47 | + self.path = os.path.join(self.dir, "example.com.conf") |
| 48 | + self.write(CERTBOT_CONF) |
| 49 | + |
| 50 | + def tearDown(self): |
| 51 | + os.chmod(self.dir, 0o700) |
| 52 | + shutil.rmtree(self.dir, ignore_errors=True) |
| 53 | + if self._saved is None: |
| 54 | + os.environ.pop("RENEW_DAYS_BEFORE", None) |
| 55 | + else: |
| 56 | + os.environ["RENEW_DAYS_BEFORE"] = self._saved |
| 57 | + |
| 58 | + def write(self, text): |
| 59 | + with open(self.path, "w", encoding="utf-8") as fh: |
| 60 | + fh.write(text) |
| 61 | + |
| 62 | + def read(self): |
| 63 | + with open(self.path, encoding="utf-8") as fh: |
| 64 | + return fh.read() |
| 65 | + |
| 66 | + def apply(self, value): |
| 67 | + if value is None: |
| 68 | + os.environ.pop("RENEW_DAYS_BEFORE", None) |
| 69 | + else: |
| 70 | + os.environ["RENEW_DAYS_BEFORE"] = value |
| 71 | + _manager(self.path).apply_renewal_window("example.com") |
| 72 | + |
| 73 | + def test_setting_is_written_above_the_first_section(self): |
| 74 | + self.apply("365") |
| 75 | + body = self.read() |
| 76 | + self.assertIn("renew_before_expiry = 365 days", body) |
| 77 | + self.assertLess( |
| 78 | + body.index("renew_before_expiry"), body.index("[renewalparams]") |
| 79 | + ) |
| 80 | + |
| 81 | + def test_setting_replaces_a_previous_value_without_duplicating(self): |
| 82 | + self.apply("365") |
| 83 | + self.apply("45") |
| 84 | + body = self.read() |
| 85 | + self.assertIn("renew_before_expiry = 45 days", body) |
| 86 | + self.assertNotIn("365", body) |
| 87 | + self.assertEqual(body.count("renew_before_expiry"), 1) |
| 88 | + |
| 89 | + def test_unsetting_removes_a_previously_written_value(self): |
| 90 | + """The regression: the lineage config outlives the container. |
| 91 | +
|
| 92 | + Leaving the setting behind kept the certificate permanently due, and |
| 93 | + the documented way out -- unset the variable -- did nothing. |
| 94 | + """ |
| 95 | + self.apply("365") |
| 96 | + self.assertIn("renew_before_expiry = 365 days", self.read()) |
| 97 | + self.apply(None) |
| 98 | + self.assertNotIn("renew_before_expiry = 365 days", self.read()) |
| 99 | + |
| 100 | + def test_unsetting_leaves_certbots_commented_template_alone(self): |
| 101 | + self.apply(None) |
| 102 | + self.assertIn("# renew_before_expiry = 30 days", self.read()) |
| 103 | + |
| 104 | + def test_unsetting_on_an_untouched_config_changes_nothing(self): |
| 105 | + self.apply(None) |
| 106 | + self.assertEqual(self.read(), CERTBOT_CONF) |
| 107 | + |
| 108 | + def test_invalid_value_leaves_the_config_alone(self): |
| 109 | + self.apply("365") |
| 110 | + before = self.read() |
| 111 | + for bad in ("0", "-1", "later", "30 days"): |
| 112 | + self.apply(bad) |
| 113 | + self.assertEqual(self.read(), before, f"{bad!r} should be ignored") |
| 114 | + |
| 115 | + def test_missing_lineage_is_not_an_error(self): |
| 116 | + os.unlink(self.path) |
| 117 | + self.apply("365") |
| 118 | + self.assertFalse(os.path.exists(self.path)) |
| 119 | + |
| 120 | + def test_write_is_atomic_and_leaves_no_scratch_file(self): |
| 121 | + """The live config is renamed into place, never truncated in place. |
| 122 | +
|
| 123 | + Opening it with "w" would empty it before the replacement was written, |
| 124 | + and a lineage config certbot cannot parse is one it cannot renew. |
| 125 | + """ |
| 126 | + self.apply("365") |
| 127 | + self.assertFalse( |
| 128 | + os.path.exists(self.path + ".dstack-tmp"), |
| 129 | + "temporary file left behind", |
| 130 | + ) |
| 131 | + self.assertTrue(self.read().endswith("\n")) |
| 132 | + self.assertIn("[renewalparams]", self.read()) |
| 133 | + |
| 134 | + def test_a_failed_write_leaves_the_original_intact(self): |
| 135 | + original = self.read() |
| 136 | + os.chmod(self.dir, 0o500) # cannot create the temp file |
| 137 | + try: |
| 138 | + self.apply("365") |
| 139 | + finally: |
| 140 | + os.chmod(self.dir, 0o700) |
| 141 | + self.assertEqual(self.read(), original) |
| 142 | + |
| 143 | + def test_wildcard_uses_the_bare_lineage_name(self): |
| 144 | + self.assertEqual( |
| 145 | + certman.CertManager._lineage_name("*.example.com"), "example.com" |
| 146 | + ) |
| 147 | + self.assertEqual( |
| 148 | + certman.CertManager._lineage_name("example.com"), "example.com" |
| 149 | + ) |
| 150 | + |
| 151 | + |
| 152 | +class NoDuplicateDefinitionsTest(unittest.TestCase): |
| 153 | + """Python shadows a repeated class or method silently; unittest counts the |
| 154 | + later one and the total still goes up, so a duplicated block looks like |
| 155 | + passing tests. Same guard as test_dnsguide.py.""" |
| 156 | + |
| 157 | + def test_no_duplicate_definitions(self): |
| 158 | + import ast, collections, pathlib |
| 159 | + |
| 160 | + tree = ast.parse(pathlib.Path(__file__).read_text()) |
| 161 | + names = [n.name for n in tree.body if isinstance(n, ast.ClassDef)] |
| 162 | + for cls in (n for n in tree.body if isinstance(n, ast.ClassDef)): |
| 163 | + names += [f"{cls.name}.{m.name}" for m in cls.body |
| 164 | + if isinstance(m, (ast.FunctionDef, ast.AsyncFunctionDef))] |
| 165 | + dupes = [n for n, c in collections.Counter(names).items() if c > 1] |
| 166 | + self.assertEqual(dupes, [], f"defined more than once: {dupes}") |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == "__main__": |
| 170 | + unittest.main(verbosity=2) |
0 commit comments