Skip to content

Commit

Permalink
Make tests works without networking.
Browse files Browse the repository at this point in the history
  • Loading branch information
funilrys committed Sep 10, 2023
1 parent 675add7 commit 5ed8100
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 14 deletions.
2 changes: 1 addition & 1 deletion tests/pyfunceble_tests_base.py
Expand Up @@ -68,7 +68,7 @@

class PyFuncebleTestsBase(unittest.TestCase):
"""
Use when you want to avoid using the network to fetch crutial datasets.
Use when you want to avoid using the network to fetch crucial datasets.
"""

IANA_DATASET = pyf_test_dataset.IANA_DATASETS
Expand Down
22 changes: 18 additions & 4 deletions tests/query/dnss/test_nameserver.py
Expand Up @@ -211,13 +211,17 @@ def test_split_nameserver_from_port_ipv6_convention_port_not_digit(self) -> None

self.assertEqual(expected, actual)

@unittest.mock.patch.object(dns.resolver.Resolver, "read_resolv_conf")
@unittest.mock.patch.object(dns.resolver.Resolver, "resolve")
def test_get_ip_from_nameserver(self, resolver_patch) -> None:
def test_get_ip_from_nameserver(
self, resolver_patch, read_resolvconf_patch
) -> None:
"""
Tests the method which let us get the IP from a nameserver.
"""

resolver_patch.side_effect = self.fake_resolver
read_resolvconf_patch.return_value = None

given = "example.org"

Expand Down Expand Up @@ -260,13 +264,15 @@ def test_get_ip_from_nameserver_not_valid_domain(self) -> None:

self.assertEqual(expected, actual)

@unittest.mock.patch.object(dns.resolver.Resolver, "read_resolv_conf")
@unittest.mock.patch.object(dns.resolver.Resolver, "resolve")
def test_set_nameservers(self, resolver_patch) -> None:
def test_set_nameservers(self, resolver_patch, read_resolvconf_patch) -> None:
"""
Tests the method which let us set the nameserver to work with.
"""

resolver_patch.side_effect = self.fake_resolver
read_resolvconf_patch.return_value = None

given = ["example.org:53"]

Expand All @@ -291,14 +297,18 @@ def test_set_nameservers(self, resolver_patch) -> None:
self.assertEqual(expected_nameserver, actual)
self.assertEqual(expected_nameserver_port, actual_nameserver_port)

@unittest.mock.patch.object(dns.resolver.Resolver, "read_resolv_conf")
@unittest.mock.patch.object(dns.resolver.Resolver, "resolve")
def test_set_nameservers_through_init(self, resolver_patch) -> None:
def test_set_nameservers_through_init(
self, resolver_patch, read_resolvconf_patch
) -> None:
"""
Tests the method which let us set the nameserver from the contributor
method.
"""

resolver_patch.side_effect = self.fake_resolver
read_resolvconf_patch.return_value = None

given = ["example.org:53"]

Expand Down Expand Up @@ -427,13 +437,17 @@ def test_set_nameservers_empty_list(self) -> None:

self.assertRaises(ValueError, lambda: Nameservers().set_nameservers(given))

@unittest.mock.patch.object(dns.resolver.Resolver, "read_resolv_conf")
@unittest.mock.patch.object(dns.resolver.Resolver, "resolve")
def test_guess_and_set_nameservers(self, resolver_patch) -> None:
def test_guess_and_set_nameservers(
self, resolver_patch, read_resolvconf_patch
) -> None:
"""
Tests the method which let us guess the nameserver to use.
"""

resolver_patch.side_effect = self.fake_resolver
read_resolvconf_patch.return_value = None

given = ["example.org:53"]

Expand Down
33 changes: 32 additions & 1 deletion tests/query/dnss/test_query_tool.py
Expand Up @@ -62,12 +62,19 @@
import dns.query

from PyFunceble.config.loader import ConfigLoader
from PyFunceble.query.dns.nameserver import Nameservers
from PyFunceble.query.dns.query_tool import DNSQueryTool, DNSQueryToolRecord

try:
from pyfunceble_tests_base import PyFuncebleTestsBase
except ModuleNotFoundError: # pragma: no cover
from ...pyfunceble_tests_base import PyFuncebleTestsBase


# pylint: disable=protected-access, too-many-lines


class TestDNSQueryTool(unittest.TestCase):
class TestDNSQueryTool(PyFuncebleTestsBase):
"""
Tests our DNS query tool.
"""
Expand All @@ -77,6 +84,8 @@ def setUp(self) -> None:
Setups everything needed for the tests.
"""

super().setUp()

def fake_response(data: str) -> object:
return dataclasses.make_dataclass(
"FakeResponse", [("address", str, dataclasses.field(default=data))]
Expand All @@ -97,6 +106,20 @@ def fake_resolver(_: str, rqtype: str):
self.resolve_patch = unittest.mock.patch.object(
dns.resolver.Resolver, "resolve"
)

self.nameservers_ports_patch = unittest.mock.patch.object(
Nameservers, "get_nameserver_ports"
)
self.nameservers_ns_patch = unittest.mock.patch.object(
Nameservers, "get_nameservers"
)

self.mock_ns_ns = self.nameservers_ns_patch.start()
self.mock_ns_ns.return_value = ["192.168.1.1", "10.47.91.9"]

self.mock_ns_ports = self.nameservers_ports_patch.start()
self.mock_ns_ports.return_value = {"192.168.1.1": 53, "10.47.91.9": 53}

self.udp_query_patch = unittest.mock.patch.object(dns.query, "udp")
self.tcp_query_patch = unittest.mock.patch.object(dns.query, "tcp")
self.https_query_patch = unittest.mock.patch.object(dns.query, "https")
Expand Down Expand Up @@ -125,17 +148,25 @@ def tearDown(self) -> None:
self.https_query_patch.stop()
self.tls_query_patch.stop()

self.nameservers_ports_patch.stop()
self.nameservers_ns_patch.stop()

del self.resolve_patch
del self.udp_query_patch
del self.tcp_query_patch
del self.https_query_patch
del self.tls_query_patch
del self.nameservers_ports_patch

del self.mock_resolve
del self.mock_udp_query
del self.mock_tcp_query
del self.mock_https_query
del self.mock_tls_query
del self.mock_ns_ports
del self.mock_ns_ns

super().tearDown()

@staticmethod
def timout_response(*args, **kwargs) -> None:
Expand Down
28 changes: 20 additions & 8 deletions tests/query/dnss/test_resolver.py
Expand Up @@ -59,8 +59,13 @@
from PyFunceble.config.loader import ConfigLoader
from PyFunceble.query.dns.resolver import Resolver

try:
from pyfunceble_tests_base import PyFuncebleTestsBase
except ModuleNotFoundError: # pragma: no cover
from ...pyfunceble_tests_base import PyFuncebleTestsBase

class TestResolver(unittest.TestCase):

class TestResolver(PyFuncebleTestsBase):
"""
Provides the tests of our resolver configurator.
"""
Expand All @@ -70,6 +75,8 @@ def setUp(self) -> None:
Setups everything needed for the tests.
"""

super().setUp()

def fake_response(data: str) -> object:
return dataclasses.make_dataclass(
"FakeResponse", [("address", str, dataclasses.field(default=data))]
Expand Down Expand Up @@ -108,13 +115,17 @@ def tearDown(self) -> None:

del self.resolver_provider

def test_set_nameservers(self) -> None:
super().tearDown()

@unittest.mock.patch.object(dns.resolver.Resolver, "read_resolv_conf")
def test_set_nameservers(self, read_resolvconf_patch) -> None:
"""
Tests the method which let us set the nameservers to work with.
"""

given = ["example.org"]

read_resolvconf_patch.return_value = None
self.resolver_provider.set_nameservers(given)

expected = [
Expand All @@ -137,7 +148,8 @@ def test_set_nameservers(self) -> None:

self.assertEqual(expected, actual)

def test_set_nameservers_through_init(self) -> None:
@unittest.mock.patch.object(dns.resolver.Resolver, "read_resolv_conf")
def test_set_nameservers_through_init(self, read_resolvconf_patch) -> None:
"""
Tests the method which let us set the nameservers to work with.
Expand All @@ -147,6 +159,7 @@ def test_set_nameservers_through_init(self) -> None:

given = ["example.org"]

read_resolvconf_patch.return_value = None
resolver_provider = Resolver(nameservers=given)

expected = [
Expand Down Expand Up @@ -225,11 +238,14 @@ def test_guess_and_set_timeout(self) -> None:

self.assertEqual(expected, actual)

def test_get_resolver(self) -> None:
@unittest.mock.patch.object(dns.resolver.Resolver, "read_resolv_conf")
def test_get_resolver(self, read_resolv_conf) -> None:
"""
Tests the method which let us get the configured resolver.
"""

read_resolv_conf.return_value = None

self.resolver_provider.set_nameservers(["example.org"])
self.resolver_provider.set_timeout(5.0)

Expand Down Expand Up @@ -260,7 +276,3 @@ def test_get_resolver(self) -> None:
the_second_resolver = self.resolver_provider.get_resolver()

self.assertEqual(id(the_resolver), id(the_second_resolver))


if __name__ == "__main__":
unittest.main()

0 comments on commit 5ed8100

Please sign in to comment.