diff --git a/cmdeploy/src/cmdeploy/tests/test_dns.py b/cmdeploy/src/cmdeploy/tests/test_dns.py index d6f756b7..3615e5f8 100644 --- a/cmdeploy/src/cmdeploy/tests/test_dns.py +++ b/cmdeploy/src/cmdeploy/tests/test_dns.py @@ -1,3 +1,5 @@ +from copy import deepcopy + import pytest from cmdeploy import remote @@ -8,38 +10,63 @@ from cmdeploy.dns import check_full_zone, check_initial_remote_data def mockdns_base(monkeypatch): qdict = {} - def query_dns(typ, domain): - try: - return qdict[typ][domain] - except KeyError: - return "" + def shell(command, fail_ok=False, print=print): + if command.startswith("dig"): + if command == "dig": + return "." + if "SOA" in command: + return ( + "delta.chat. 21600 IN SOA ns1.first-ns.de. dns.hetzner.com." + " 2025102800 14400 1800 604800 3600" + ) + command_chunks = command.split() + domain, typ = command_chunks[4], command_chunks[6] + try: + return qdict[typ][domain] + except KeyError: + return "" + return remote.rshell.shell(command=command, fail_ok=fail_ok, print=print) - monkeypatch.setattr(remote.rdns, query_dns.__name__, query_dns) + monkeypatch.setattr(remote.rdns, shell.__name__, shell) return qdict @pytest.fixture -def mockdns(mockdns_base): - mockdns_base.update( - { - "A": {"some.domain": "1.1.1.1"}, - "AAAA": {"some.domain": "fde5:cd7a:9e1c:3240:5a99:936f:cdac:53ae"}, - "CNAME": { - "mta-sts.some.domain": "some.domain.", - "www.some.domain": "some.domain.", - }, - } - ) +def mockdns_expected(): + return { + "A": {"some.domain": "1.1.1.1"}, + "AAAA": {"some.domain": "fde5:cd7a:9e1c:3240:5a99:936f:cdac:53ae"}, + "CNAME": { + "mta-sts.some.domain": "some.domain.", + "www.some.domain": "some.domain.", + }, + } + + +@pytest.fixture(params=["plain", "with-dns-comments"]) +def mockdns(request, mockdns_base, mockdns_expected): + mockdns_base.update(deepcopy(mockdns_expected)) + match request.param: + case "plain": + pass + case "with-dns-comments": + for typ, data in mockdns_base.items(): + for host, result in data.items(): + mockdns_base[typ][host] = ( + ";; some unsuccessful attempt result\n" + "; and another with a single semicolon\n" + f"{result}" + ) return mockdns_base class TestPerformInitialChecks: - def test_perform_initial_checks_ok1(self, mockdns): + def test_perform_initial_checks_ok1(self, mockdns, mockdns_expected): remote_data = remote.rdns.perform_initial_checks("some.domain") - assert remote_data["A"] == mockdns["A"]["some.domain"] - assert remote_data["AAAA"] == mockdns["AAAA"]["some.domain"] - assert remote_data["MTA_STS"] == mockdns["CNAME"]["mta-sts.some.domain"] - assert remote_data["WWW"] == mockdns["CNAME"]["www.some.domain"] + assert remote_data["A"] == mockdns_expected["A"]["some.domain"] + assert remote_data["AAAA"] == mockdns_expected["AAAA"]["some.domain"] + assert remote_data["MTA_STS"] == mockdns_expected["CNAME"]["mta-sts.some.domain"] + assert remote_data["WWW"] == mockdns_expected["CNAME"]["www.some.domain"] @pytest.mark.parametrize("drop", ["A", "AAAA"]) def test_perform_initial_checks_with_one_of_A_AAAA(self, mockdns, drop):