Created
June 26, 2024 13:48
-
-
Save bdmorin/91f9c74db70617f9d5e8c1291b14ab5f to your computer and use it in GitHub Desktop.
Very simple unit test for DNS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
import yaml | |
import dns.resolver | |
# Load the YAML file | |
with open('dns_tests.yaml', 'r') as file: | |
test_cases = yaml.safe_load(file) | |
class TestDNSLookup(unittest.TestCase): | |
def dns_lookup(self, host, record_type): | |
try: | |
answers = dns.resolver.resolve(host, record_type) | |
return [str(rdata) for rdata in answers] | |
except Exception as e: | |
return str(e) | |
def test_dns_lookups(self): | |
domain = test_cases['test']['domain'] | |
for test in test_cases['test']['records']: | |
with self.subTest(test=test): | |
host = f"{test['host']}.{domain}" | |
record_type = test['type'] | |
expected_value = test['value'] | |
result = self.dns_lookup(host, record_type) | |
if isinstance(result, list): | |
contains_expected = any(expected_value in r for r in result) | |
print(f"Hostname: {host}, Type: {record_type}, Expected: {expected_value}, Result: {result}") | |
self.assertTrue(contains_expected, f"Failed lookup for {host} {record_type}") | |
else: | |
print(f"Hostname: {host}, Type: {record_type}, Expected: {expected_value}, Result: {result}") | |
self.fail(f"DNS lookup failed for {host} {record_type} with error: {result}") | |
if __name__ == '__main__': | |
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
test: | |
domain: domain.com | |
records: | |
- host: go | |
type: CNAME | |
value: sites.domain.cloud | |
- host: k1._domainkey.e | |
type: TXT | |
value: k=rsa; p=xxxxxxxxxxxxxxxxxxxxx | |
- host: e | |
type: TXT | |
value: v=spf1 include:mailgun.org ~all | |
- host: email.e | |
type: CNAME | |
value: mailgun.org | |
- host: e | |
type: MX | |
value: mxa.mailgun.org | |
- host: e | |
type: MX | |
value: mxb.mailgun.org |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
❯ python dns_test.py | |
Hostname: go.domain.com, Type: CNAME, Expected: sites.domain.cloud, Result: ['sites.domain.cloud.'] | |
Hostname: k1._domainkey.e.domain.com, Type: TXT, Expected: k=rsa; p=xxxxxxxxxxxxxxxxxxxxx"'] | |
Hostname: e.domain.com, Type: TXT, Expected: v=spf1 include:mailgun.org ~all, Result: ['"v=spf1 include:mailgun.org ~all"'] | |
Hostname: email.e.domain.com, Type: CNAME, Expected: mailgun.org, Result: ['mailgun.org.'] | |
Hostname: e.domain.com, Type: MX, Expected: mxa.mailgun.org, Result: ['10 mxa.mailgun.org.', '10 mxb.mailgun.org.'] | |
Hostname: e.domain.com, Type: MX, Expected: mxb.mailgun.org, Result: ['10 mxa.mailgun.org.', '10 mxb.mailgun.org.'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment