From e3cf03338aee2094186a249a7ad5a486b24370f9 Mon Sep 17 00:00:00 2001 From: aaron Date: Mon, 30 Aug 2021 03:08:26 +0200 Subject: [PATCH] add unittests --- run_tests.sh | 3 +++ test_dnsverify.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100755 run_tests.sh create mode 100644 test_dnsverify.py diff --git a/run_tests.sh b/run_tests.sh new file mode 100755 index 0000000..1fd30a2 --- /dev/null +++ b/run_tests.sh @@ -0,0 +1,3 @@ +#!/bin/env bash + +pipenv run python -m unittest -bv test_dnsverify diff --git a/test_dnsverify.py b/test_dnsverify.py new file mode 100644 index 0000000..dfb0c16 --- /dev/null +++ b/test_dnsverify.py @@ -0,0 +1,50 @@ +#!/bin/env python + +''' + Unit tests for dnsverify +''' + +import unittest + +from dnsverify import do_check +from dnsverify import get_authoritative_ns +from dnsverify import get_domains_from_yaml +from dnsverify import verify_authoritative_ns +from pylint.lint import Run +from pylint.reporters import CollectingReporter + +class TestDnsVerify(unittest.TestCase): + def test_do_check(self): + ''' + ... Test if checks acutally fail when an error occures. + ''' + self.assertEqual(do_check(['puzzle.ch'], + ['ns1.google.com'], + verbose=False), + False) + def test_get_authoritative_ns(self): + ''' + ... Test if garbage queries trigger a stack trace. + ''' + self.assertEqual(get_authoritative_ns('dinimer.lolwas'), []) + def test_get_domains_from_yaml(self): + ''' + ... Test wheter missing files generate an empty list. + ''' + self.assertEqual(get_domains_from_yaml(["notexist.yaml"]), []) + def test_verify_authoritative_ns(self): + ''' + ... Test wheter false is returned when the ns does not match the list. And test if type errors are handeled correctly. + ''' + self.assertEqual(verify_authoritative_ns('ns1.google.com', ['ns2.google.com']), False) + self.assertRaises(TypeError, verify_authoritative_ns, 123, ['123']) + self.assertRaises(TypeError, verify_authoritative_ns, "123", "123") + + def test_pylint(self): + ''' + ... Test wheter the coding style is acceptable. + ''' + rep = CollectingReporter() + results = Run(['dnsverify.py', '-sn'], reporter=rep, exit=False) + score = results.linter.stats['global_note'] + self.assertGreaterEqual(score, 8, "Pylint score to low. Fix your code.")