<commit_before># tests for dns_parser.py

import pytest
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + '/../')

from dnstest_checks import DNStestChecks
from dnstest_config import DnstestConfig


# dict of known (mocked) reverse DNS values for test and prod servers
known_rev_dns = {'test_server_stub': {}, 'prod_server_stub': {}}
known_rev_dns['prod_server_stub']['192.168.3.11'] = 'prodonlywithrev.example.com'
known_rev_dns['prod_server_stub']['172.16.31.10'] = 'prodwithtestrev.example.com'
known_rev_dns['prod_server_stub']['172.16.17.32'] = 'addedname3.example.com'
known_rev_dns['prod_server_stub']['192.168.127.12'] = 'addedwithrev.example.com'
known_rev_dns['prod_server_stub']['172.16.31.10'] = 'groijg.example.com'
known_rev_dns['prod_server_stub']['172.16.17.32'] = 'renametest5.example.com'
known_rev_dns['prod_server_stub']['192.168.3.11'] = 'renametest6.example.com'
known_rev_dns['prod_server_stub']['192.168.3.11'] = 'SERVFAIL'

known_rev_dns['test_server_stub']['10.188.12.10'] = 'foo.example.com'
known_rev_dns['test_server_stub']['172.16.31.10'] = 'prodwithtestrev.example.com'
known_rev_dns['test_server_stub']['172.16.31.10'] = 'newwithreverse.example.com'
known_rev_dns['test_server_stub']['172.16.58.3'] = 'newBADreverse.example.com'
known_rev_dns['test_server_stub']['172.16.17.32'] = 'addedname3.example.com'
known_rev_dns['test_server_stub']['192.168.127.12'] = 'addedwithrev.example.com'
known_rev_dns['test_server_stub']['172.16.31.10'] = 'addedbadprodrev.example.com'
known_rev_dns['test_server_stub']['192.168.3.11'] = 'prodrevservfail.example.com'
known_rev_dns['test_server_stub']['172.16.31.10'] = 'renametest2.example.com'
known_rev_dns['test_server_stub']['172.16.17.32'] = 'renametest3b.example.com'
known_rev_dns['test_server_stub']['172.16.17.32'] = 'renametest5.example.com'
known_rev_dns['test_server_stub']['192.168.3.11'] = 'renametest6b.example.com'

# dict of known (mocked) forward DNS values for test and prod servers
# each known_dns[server][name] is [value, record_type]
known_dns = {'test_server_stub': {}, 'prod_server_stub': {}}
known_dns['prod_server_stub']['existinghostname.example.com'] = ['172.16.17.32', 'A']
known_dns['prod_server_stub']['addedhostname.example.com'] = ['172.16.58.3', 'A']
known_dns['prod_server_stub']['prodonlyhostname.example.com'] = ['172.16.17.32', 'A']
known_dns['prod_server_stub']['prodonlywithrev.example.com'] = ['192.168.3.11', 'A']
known_dns['prod_server_stub']['prodwithtestrev.example.com'] = ['172.16.31.10', 'A']
known_dns['prod_server_stub']['servfail-test.example.com'] = ['192.168.127.12', 'A']
known_dns['prod_server_stub']['addedcname.example.com'] = ['barbaz', 'CNAME']
known_dns['prod_server_stub']['renamedname.example.com'] = ['172.16.17.32', 'A']
known_dns['prod_server_stub']['addedname2.example.com'] = ['192.168.3.11', 'A']
known_dns['prod_server_stub']['addedname3.example.com'] = ['192.168.127.12', 'A']
known_dns['prod_server_stub']['addedwithrev.example.com'] = ['192.168.127.12', 'A']
known_dns['prod_server_stub']['addedbadprodrev.example.com'] = ['172.16.31.10', 'A']
known_dns['prod_server_stub']['addedprodrevservfail.example.com'] = ['192.168.3.11', 'A']
known_dns['prod_server_stub']['renametest1.example.com'] = ['172.16.17.32', 'A']
known_dns['prod_server_stub']['renametest2.example.com'] = ['172.16.31.10', 'A']
known_dns['prod_server_stub']['renametest3.example.com'] = ['172.16.17.32', 'A']
known_dns['prod_server_stub']['renametest4.example.com'] = ['172.16.58.3', 'A']
known_dns['prod_server_stub']['renametest5.example.com'] = ['172.16.17.32', 'A']
known_dns['prod_server_stub']['renametest6b.example.com'] = ['192.168.3.11', 'A']
known_dns['prod_server_stub']['servfail-prod'] = ['STATUS', 'SERVFAIL']
known_dns['prod_server_stub']['servfail-prod.example.com'] = ['STATUS', 'SERVFAIL']
known_dns['prod_server_stub']['servall-test'] = ['STATUS', 'SERVFAIL']
known_dns['prod_server_stub']['servfail-all.example.com'] = ['STATUS', 'SERVFAIL']

known_dns['test_server_stub']['newhostname.example.com'] = ['192.168.3.11', 'A']
known_dns['test_server_stub']['addedhostname.example.com'] = ['172.16.58.3', 'A']
known_dns['test_server_stub']['host-no-reverse.example.com'] = ['172.16.17.32', 'A']
known_dns['test_server_stub']['servfail-prod.example.com'] = ['192.168.127.12', 'A']
known_dns['test_server_stub']['cnametestonly.example.com'] = ['foobar', 'CNAME']
known_dns['test_server_stub']['newwithreverse.example.com'] = ['172.16.31.10', 'A']
known_dns['test_server_stub']['newwrongreverse.example.com'] = ['172.16.58.3', 'A']
known_dns['test_server_stub']['addedcname.example.com'] = ['barbaz', 'CNAME']
known_dns['test_server_stub']['addedname2.example.com'] = ['172.16.17.32', 'A']
known_dns['test_server_stub']['addedname3.example.com'] = ['172.16.17.32', 'A']
known_dns['test_server_stub']['addedwithrev.example.com'] = ['192.168.127.12', 'A']
known_dns['test_server_stub']['addedbadprodrev.example.com'] = ['172.16.31.10', 'A']
known_dns['test_server_stub']['addedprodrevservfail.example.com'] = ['192.168.3.11', 'A']
known_dns['test_server_stub']['renametest1b.example.com'] = ['172.16.17.32', 'A']
known_dns['test_server_stub']['renametest2b.example.com'] = ['172.16.31.10', 'A']
known_dns['test_server_stub']['renametest3b.example.com'] = ['172.16.17.32', 'A']
known_dns['test_server_stub']['renametest4b.example.com'] = ['172.16.58.3', 'A']
known_dns['test_server_stub']['renametest5b.example.com'] = ['172.16.17.32', 'A']
known_dns['test_server_stub']['renametest6b.example.com'] = ['192.168.3.11', 'A']
known_dns['test_server_stub']['servfail-test'] = ['STATUS', 'SERVFAIL']
known_dns['test_server_stub']['servfail-test.example.com'] = ['STATUS', 'SERVFAIL']
known_dns['test_server_stub']['servfail-test2'] = ['STATUS', 'SERVFAIL']
known_dns['test_server_stub']['servfail-test2.example.com'] = ['STATUS', 'SERVFAIL']
known_dns['test_server_stub']['servall-test'] = ['STATUS', 'SERVFAIL']
known_dns['test_server_stub']['servfail-all.example.com'] = ['STATUS', 'SERVFAIL']
known_dns['test_server_stub']['removetest1.example.com'] = ['172.16.31.10', 'A']

class TestDNSCheckRename:
    """
    Test DNS checks, using stubbed name resolution methods that return static values.

    The code in this class checks the logic of dnstest.py's test_*_name methods, which take
    input describing the change, and query nameservers to check current prod and staging status.
    """

    @pytest.fixture(scope="module")
    def setup_checks(self):
        config = DnstestConfig()
        config.server_test = "test_server_stub"
        config.server_prod = "prod_server_stub"
        config.default_domain = ".example.com"
        config.have_reverse_dns = True

        chk = DNStestChecks(config)
        # stub
        chk.DNS.resolve_name = self.stub_resolve_name
        # stub
        chk.DNS.lookup_reverse = self.stub_lookup_reverse
        return chk

    def stub_resolve_name(self, query, to_server, to_port=53):
        """
        stub method

        return a dict that looks like the return value from dnstest.resolve_name
        but either returns one of a hard-coded group of dicts, or an error.
        """

        if query in known_dns[to_server] and known_dns[to_server][query][0] == "STATUS":
            return {'status': known_dns[to_server][query][1]}
        elif query in known_dns[to_server]:
            return {'answer': {'name': query, 'data': known_dns[to_server][query][0], 'typename': known_dns[to_server][query][1], 'classstr': 'IN', 'ttl': 360, 'type': 5, 'class': 1, 'rdlength': 14}}
        else:
            return {'status': 'NXDOMAIN'}

    def stub_lookup_reverse(self, name, to_server, to_port=53):
        """
        stub method

        return a dict that looks like the return value from dnstest.lookup_reverse
        but either returns one of a hard-coded group of dicts, or an error.
        """

        if name in known_rev_dns[to_server] and known_rev_dns[to_server][name] == "SERVFAIL":
            return {'status': 'SERVFAIL'}
        elif name in known_rev_dns[to_server]:
            return {'answer': {'name': name, 'data': known_rev_dns[to_server][name], 'typename': 'PTR', 'classstr': 'IN', 'ttl': 360, 'type': 12, 'class': 1, 'rdlength': 33}}
        else:
            return {'status': 'NXDOMAIN'}

    ###########################################
    # Done with setup, start the actual tests #
    ###########################################

    @pytest.mark.parametrize(("oldname", "newname", "value", "result"), [
        ("renametest1", "renametest1b", "172.16.17.32", {'message': 'rename renametest1 => renametest1b (TEST)', 'result': True, 'secondary': [], 'warnings': ['no reverse DNS appears to be set for 172.16.17.320 (TEST)']}),
        ("renametest2", "renametest2b", "172.16.31.10", {'message': 'rename renametest2 => renametest2b (TEST)', 'result': True, 'secondary': [], 'warnings': ['renametest2 appears to still have reverse DNS set to renametest2.example.com (TEST)']}),
        ("renametest3", "renametest3b", "172.16.17.32", {'message': 'rename renametest3 => renametest3b (TEST)', 'result': True, 'secondary': ['reverse DNS is set correctly for 172.16.17.32 (TEST)'], 'warnings': []}),
        # this next one should fail, it's actually an addition and a deletion, but values differ
        ("renametest4", "renametest4b", "192.168.127.12", {'message': 'renametest4 => renametest4b rename is bad, resolves to 172.16.58.3 in TEST (expected value was 192.168.127.12) (TEST)', 'result': False, 'secondary': [], 'warnings': []}),
        ("renametest5", "renametest5b", "172.16.17.32", {'message': 'rename renametest5 => renametest5b (TEST)', 'result': True, 'secondary': [], 'warnings': ['renametest5 appears to still have reverse DNS set to renametest5.example.com (TEST)']}),
    ])
    def test_dns_rename(self, setup_checks, oldname, newname, value, result):
        """
        Test checks for renaming a record in DNS (new name, same value)
        """
        foo = setup_checks.check_renamed_name(oldname, newname, value)
        assert foo == result

    @pytest.mark.parametrize(("oldname", "newname", "value", "result"), [
        ("addedname2", "renamedname", "172.16.17.32", {'message': 'addedname2 got answer from PROD (192.168.3.11), old name is still active (PROD)', 'result': False, 'secondary': [], 'warnings': []}),
        ("renametest5", "renametest5b", "172.16.17.32", {'message': 'renametest5b got status NXDOMAIN (PROD)', 'result': False, 'secondary': [], 'warnings': []}),
    ])
    def test_dns_verify_rename(self, setup_checks, oldname, newname, value, result):
        """
        Test checks for verifying a renamed record in DNS (new name, same value)
        """
        foo = setup_checks.verify_renamed_name(oldname, newname, value)
        assert foo == result<commit_msg>completed first refactor of mocked DNS test methods and configuration. Now just need to clean up this file/class, and then replicate to the others.<commit_after># tests for dns_parser.py

import pytest
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + '/../')

from dnstest_checks import DNStestChecks
from dnstest_config import DnstestConfig

"""
This dict stores the DNS results that our DNS-mocking functions will return.
Format of the 'known_dns' dict:
[chk|ver] - whether this is pre-change (_check methods) or post-change (_verify methods)
    [prod|test] - whether this is for the prod or test DNS server
        [fwd|rev] - whether this is forward or reverse DNS
            [recordname] - the name of this record, as sent to the DNS query methods
                = value - a string or list of the record value, see below
value can be:
for 'rev' dns:
  - a string with the value of the PTR record
  - the string "SERVFAIL", which returns a SERVFAIL result
for 'fwd' dns:
  - a list whose first item is "STATUS", and whose second item is the 'status' attribute of the DNS result
  - a list whose first item is the data/value of the record, and whose second item is the typename of the record (i.e. "A" or "CNAME")
"""
known_dns = {'chk': {'test': {'fwd': {}, 'rev': {}}, 'prod': {'fwd': {}, 'rev': {}}}, 'ver': {'test': {'fwd': {}, 'rev': {}}, 'prod': {'fwd': {}, 'rev': {}}}}

# test 0
known_dns['chk']['prod']['fwd']['renametest1.example.com'] = ['172.16.17.32', 'A']
known_dns['chk']['test']['fwd']['renametest1b.example.com'] = ['172.16.17.32', 'A']
known_dns['ver']['test']['fwd']['renametest1b.example.com'] = ['172.16.17.32', 'A']
# test 1
known_dns['chk']['prod']['fwd']['renametest2.example.com'] = ['172.16.31.10', 'A']
known_dns['chk']['test']['fwd']['renametest2b.example.com'] = ['172.16.31.10', 'A']
known_dns['chk']['test']['rev']['172.16.31.10'] = 'renametest2.example.com'
known_dns['ver']['test']['fwd']['renametest2b.example.com'] = ['172.16.31.10', 'A']
# test 2
known_dns['chk']['prod']['fwd']['renametest3.example.com'] = ['172.16.17.32', 'A']
known_dns['chk']['test']['fwd']['renametest3b.example.com'] = ['172.16.17.32', 'A']
known_dns['chk']['test']['rev']['172.16.17.32'] = 'renametest3b.example.com'
known_dns['ver']['test']['fwd']['renametest3b.example.com'] = ['172.16.17.32', 'A']
# test 3
known_dns['chk']['prod']['fwd']['renametest4.example.com'] = ['172.16.58.3', 'A']
known_dns['chk']['test']['fwd']['renametest4b.example.com'] = ['172.16.58.3', 'A']
# test 4
known_dns['chk']['prod']['fwd']['renametest5.example.com'] = ['172.16.17.32', 'A']
known_dns['chk']['prod']['rev']['172.16.17.32'] = 'renametest5.example.com'
known_dns['chk']['test']['fwd']['renametest5b.example.com'] = ['172.16.17.32', 'A']
known_dns['chk']['test']['rev']['172.16.17.32'] = 'renametest5.example.com'
# test 5
known_dns['chk']['prod']['fwd']['renamedname.example.com'] = ['172.16.17.32', 'A']
known_dns['chk']['prod']['fwd']['addedname2.example.com'] = ['192.168.3.11', 'A']
known_dns['chk']['test']['fwd']['addedname2.example.com'] = ['172.16.17.32', 'A']
known_dns['ver']['prod']['fwd']['renamedname.example.com'] = ['172.16.17.32', 'A']
known_dns['ver']['prod']['fwd']['addedname2.example.com'] = ['192.168.3.11', 'A']

class TestDNSCheckRename:
    """
    Test DNS checks, using stubbed name resolution methods that return static values.

    The code in this class checks the logic of dnstest.py's test_*_name methods, which take
    input describing the change, and query nameservers to check current prod and staging status.
    """

    @pytest.fixture(scope="module")
    def setup_checks(self):
        """
        Sets up test environment for tests of check methods,
        including redefining resolve_name and lookup_reverse
        to the appropriate methods in this class
        """
        config = DnstestConfig()
        config.server_test = "test"
        config.server_prod = "prod"
        config.default_domain = ".example.com"
        config.have_reverse_dns = True

        chk = DNStestChecks(config)
        # stub
        chk.DNS.resolve_name = self.stub_resolve_name
        # stub
        chk.DNS.lookup_reverse = self.stub_lookup_reverse
        return chk

    @pytest.fixture(scope="module")
    def setup_verifies(self):
        """
        Sets up test environment for tests of verify methods,
        including redefining resolve_name and lookup_reverse
        to the appropriate methods in this class
        """
        config = DnstestConfig()
        config.server_test = "test"
        config.server_prod = "prod"
        config.default_domain = ".example.com"
        config.have_reverse_dns = True

        chk = DNStestChecks(config)
        # stub
        chk.DNS.resolve_name = self.stub_resolve_name_verify
        # stub
        chk.DNS.lookup_reverse = self.stub_lookup_reverse_verify
        return chk

    def stub_resolve_name(self, query, to_server, to_port=53):
        """
        stub method

        return a dict that looks like the return value from dnstest.resolve_name
        but either returns one of a hard-coded group of dicts, or an error.
        """

        if query in known_dns['chk'][to_server]['fwd'] and known_dns['chk'][to_server]['fwd'][query][0] == "STATUS":
            return {'status': known_dns['chk'][to_server]['fwd'][query][1]}
        elif query in known_dns['chk'][to_server]['fwd']:
            return {'answer': {'name': query, 'data': known_dns['chk'][to_server]['fwd'][query][0], 'typename': known_dns['chk'][to_server]['fwd'][query][1], 'classstr': 'IN', 'ttl': 360, 'type': 5, 'class': 1, 'rdlength': 14}}
        else:
            return {'status': 'NXDOMAIN'}

    def stub_lookup_reverse(self, name, to_server, to_port=53):
        """
        stub method

        return a dict that looks like the return value from dnstest.lookup_reverse
        but either returns one of a hard-coded group of dicts, or an error.
        """

        if name in known_dns['chk'][to_server]['rev'] and known_dns['chk'][to_server]['rev'][name] == "SERVFAIL":
            return {'status': 'SERVFAIL'}
        elif name in known_dns['chk'][to_server]['rev']:
            return {'answer': {'name': name, 'data': known_dns['chk'][to_server]['rev'][name], 'typename': 'PTR', 'classstr': 'IN', 'ttl': 360, 'type': 12, 'class': 1, 'rdlength': 33}}
        else:
            return {'status': 'NXDOMAIN'}

    def stub_resolve_name_verify(self, query, to_server, to_port=53):
        """
        stub method

        return a dict that looks like the return value from dnstest.resolve_name
        but either returns one of a hard-coded group of dicts, or an error.
        """

        if query in known_dns['ver'][to_server]['fwd'] and known_dns['ver'][to_server]['fwd'][query][0] == "STATUS":
            return {'status': known_dns['ver'][to_server]['fwd'][query][1]}
        elif query in known_dns['ver'][to_server]['fwd']:
            return {'answer': {'name': query, 'data': known_dns['ver'][to_server]['fwd'][query][0], 'typename': known_dns['ver'][to_server]['fwd'][query][1], 'classstr': 'IN', 'ttl': 360, 'type': 5, 'class': 1, 'rdlength': 14}}
        else:
            return {'status': 'NXDOMAIN'}

    def stub_lookup_reverse_verify(self, name, to_server, to_port=53):
        """
        stub method

        return a dict that looks like the return value from dnstest.lookup_reverse
        but either returns one of a hard-coded group of dicts, or an error.
        """

        if name in known_dns['ver'][to_server]['rev'] and known_dns['ver'][to_server]['rev'][name] == "SERVFAIL":
            return {'status': 'SERVFAIL'}
        elif name in known_dns['ver'][to_server]['rev']:
            return {'answer': {'name': name, 'data': known_dns['ver'][to_server]['rev'][name], 'typename': 'PTR', 'classstr': 'IN', 'ttl': 360, 'type': 12, 'class': 1, 'rdlength': 33}}
        else:
            return {'status': 'NXDOMAIN'}


    ###########################################
    # Done with setup, start the actual tests #
    ###########################################

    TESTS = [
        ("renametest1", "renametest1b", "172.16.17.32"), # 0
        ("renametest2", "renametest2b", "172.16.31.10"), # 1
        ("renametest3", "renametest3b", "172.16.17.32"), # 2
        # this next one should fail, it's actually an addition and a deletion, but values differ
        ("renametest4", "renametest4b", "192.168.127.12"), # 3
        ("renametest5", "renametest5b", "172.16.17.32"), # 4
        ]

    @pytest.mark.parametrize(("oldname", "newname", "value", "result"), [
        TESTS[0] + ({'message': 'rename renametest1 => renametest1b (TEST)', 'result': True, 'secondary': [], 'warnings': ['no reverse DNS appears to be set for 172.16.17.32 (TEST)']}, ),
        TESTS[1] + ({'message': 'rename renametest2 => renametest2b (TEST)', 'result': True, 'secondary': [], 'warnings': ['renametest2 appears to still have reverse DNS set to renametest2.example.com (TEST)']}, ),
        TESTS[2] + ({'message': 'rename renametest3 => renametest3b (TEST)', 'result': True, 'secondary': ['reverse DNS is set correctly for 172.16.17.32 (TEST)'], 'warnings': []},),
        # this next one should fail, it's actually an addition and a deletion, but values differ
        TESTS[3] + ({'message': 'renametest4 => renametest4b rename is bad, resolves to 172.16.58.3 in TEST (expected value was 192.168.127.12) (TEST)', 'result': False, 'secondary': [], 'warnings': []}, ),
        TESTS[4] + ({'message': 'rename renametest5 => renametest5b (TEST)', 'result': True, 'secondary': [], 'warnings': ['renametest5 appears to still have reverse DNS set to renametest5.example.com (TEST)']}, ),
    ])
    def test_dns_rename(self, setup_checks, oldname, newname, value, result):
        """
        Test checks for renaming a record in DNS (new name, same value)
        """
        foo = setup_checks.check_renamed_name(oldname, newname, value)
        assert foo == result

    @pytest.mark.parametrize(("oldname", "newname", "value", "result"), [
        ("addedname2", "renamedname", "172.16.17.32", {'message': 'addedname2 got answer from PROD (192.168.3.11), old name is still active (PROD)', 'result': False, 'secondary': [], 'warnings': []}),
        TESTS[4] + ({'message': 'renametest5b got status NXDOMAIN (PROD)', 'result': False, 'secondary': [], 'warnings': []}, ),
    ])
    def test_dns_verify_rename(self, setup_verifies, oldname, newname, value, result):
        """
        Test checks for verifying a renamed record in DNS (new name, same value)
        """
        foo = setup_verifies.verify_renamed_name(oldname, newname, value)
        assert foo == result
{'fraction_non_alphanumeric': 0.19291575872780378, 'fraction_numerical': 0.051601835968287824, 'mean_word_length': 4.3683424589347934, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 101, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2629251', 'n_tokens_mistral': 8155, 'n_tokens_neox': 7621, 'n_words': 1589}