feat: helper script to create new records if they don't exist

This commit is contained in:
mxmehl committed 2026-10-22 14:27:54 +02:00
1 parent af8fe5e4c0
commit 835f1642d9
5 files changed
+198 -12

No files matched your search

+55
View File
@@ -80,6 +80,14 @@ class MainTest(unittest.TestCase):
with open(self.path, "w") as f:
f.write(CONFIG)
os.chmod(self.path, 0o600)
# Never talk to the real INWX from tests
patcher = mock.patch.object(
sh,
"ensure_record",
return_value=(True, {"content": "192.0.2.1", "ttl": 300}),
)
self.ensure = patcher.start()
self.addCleanup(patcher.stop)
def run_main(self, *args):
out = io.StringIO()
@@ -126,6 +134,53 @@ class MainTest(unittest.TestCase):
with open(self.path, "rb") as f:
return tomllib.load(f)["hosts"][host]["token_sha256"]
def test_creates_missing_record_with_default_ttl(self):
output = self.run_main("fresh.example.com")
args = self.ensure.call_args.args
self.assertEqual(args[1:], ("example.com", "fresh.example.com", "A", 300))
self.assertIn("DNS: created A record fresh.example.com", output)
def test_types_ttl_and_zone_are_passed_on(self):
self.run_main(
"fresh.example.co.uk", "-z", "example.co.uk", "-t", "a,aaaa", "--ttl", "60"
)
calls = [c.args[1:] for c in self.ensure.call_args_list]
self.assertEqual(
calls,
[
("example.co.uk", "fresh.example.co.uk", "A", 60),
("example.co.uk", "fresh.example.co.uk", "AAAA", 60),
],
)
def test_existing_record_is_reported_not_changed(self):
self.ensure.return_value = (False, {"content": "1.2.3.4", "ttl": 3600})
output = self.run_main("fresh.example.com")
self.assertIn("A record fresh.example.com exists (1.2.3.4, TTL 3600)", output)
def test_zone_of_existing_host_comes_from_config(self):
self.run_main("b.example.com", "--yes")
self.assertEqual(self.ensure.call_args.args[1], "example.com")
def test_no_dns_skips_inwx(self):
self.run_main("fresh.example.com", "--no-dns")
self.ensure.assert_not_called()
def test_inwx_failure_changes_nothing(self):
self.ensure.side_effect = sh.InwxError(
"account.login: 2200 Authentication error"
)
with self.assertRaises(SystemExit) as ctx:
self.run_main("fresh.example.com")
self.assertIn("Nothing was changed", str(ctx.exception))
with open(self.path) as f:
self.assertEqual(f.read(), CONFIG)
def test_invalid_record_type(self):
with self.assertRaises(SystemExit):
self.run_main("fresh.example.com", "-t", "MX")
self.ensure.assert_not_called()
def test_invalid_hostname_leaves_config_untouched(self):
for bad in ('x"] \n[evil', "no_dots", "a..b", "-a.example.com"):
with self.assertRaises(SystemExit):