Commit 00fe334b authored by Romain Courteaud's avatar Romain Courteaud

Move default DNS on configuration level

parent 4dfd8e48
...@@ -39,24 +39,22 @@ class UrlCheckerBotTestCase(unittest.TestCase): ...@@ -39,24 +39,22 @@ class UrlCheckerBotTestCase(unittest.TestCase):
def test_emptyConfiguration(self): def test_emptyConfiguration(self):
resolver_ip = "192.168.0.254" resolver_ip = "192.168.0.254"
bot = WebBot(mapping={"SQLITE": ":memory:"})
bot.initDB()
resolver = urlchecker_dns.dns.resolver.Resolver(configure=False) resolver = urlchecker_dns.dns.resolver.Resolver(configure=False)
resolver.nameservers.append(resolver_ip) resolver.nameservers.append(resolver_ip)
with mock.patch( with mock.patch(
"urlchecker_dns.get_default_resolver" "urlchecker_configuration.get_default_resolver"
) as mock_get_default_resolver: ) as mock_get_default_resolver, mock.patch(
"urlchecker_dns.dns.resolver.Resolver.query"
) as mock_query:
mock_get_default_resolver.return_value = resolver mock_get_default_resolver.return_value = resolver
with mock.patch( mock_query.return_value = [MockAnswer("1.2.3.4")]
"urlchecker_dns.dns.resolver.Resolver.query" bot = WebBot(mapping={"SQLITE": ":memory:"})
) as mock_query: bot.initDB()
mock_query.return_value = [MockAnswer("1.2.3.4")] bot.iterateLoop()
bot.iterateLoop()
assert mock_query.call_count == 1 assert mock_query.call_count == 1
assert bot._db.Status.select().count() == 1 assert bot._db.Status.select().count() == 1
assert bot._db.Status.get().text == "loop" assert bot._db.Status.get().text == "loop"
...@@ -70,14 +68,11 @@ class UrlCheckerBotTestCase(unittest.TestCase): ...@@ -70,14 +68,11 @@ class UrlCheckerBotTestCase(unittest.TestCase):
def test_oneNameserverOneDomainOneIp(self): def test_oneNameserverOneDomainOneIp(self):
resolver_ip = "127.0.0.1" resolver_ip = "127.0.0.1"
bot = WebBot(mapping={"SQLITE": ":memory:", "DOMAIN": "example.org"})
bot.initDB()
resolver = urlchecker_dns.dns.resolver.Resolver(configure=False) resolver = urlchecker_dns.dns.resolver.Resolver(configure=False)
resolver.nameservers.append(resolver_ip) resolver.nameservers.append(resolver_ip)
with mock.patch( with mock.patch(
"urlchecker_dns.get_default_resolver" "urlchecker_configuration.get_default_resolver"
) as mock_get_default_resolver, mock.patch( ) as mock_get_default_resolver, mock.patch(
"urlchecker_dns.dns.resolver.Resolver.query" "urlchecker_dns.dns.resolver.Resolver.query"
) as mock_query, mock.patch( ) as mock_query, mock.patch(
...@@ -89,6 +84,11 @@ class UrlCheckerBotTestCase(unittest.TestCase): ...@@ -89,6 +84,11 @@ class UrlCheckerBotTestCase(unittest.TestCase):
mock_get_default_resolver.return_value = resolver mock_get_default_resolver.return_value = resolver
mock_query.return_value = [MockAnswer("1.2.3.4")] mock_query.return_value = [MockAnswer("1.2.3.4")]
bot = WebBot(
mapping={"SQLITE": ":memory:", "DOMAIN": "example.org"}
)
bot.initDB()
bot.iterateLoop() bot.iterateLoop()
assert mock_query.call_count == 2 assert mock_query.call_count == 2
......
...@@ -455,51 +455,6 @@ class UrlCheckerDNSTestCase(unittest.TestCase): ...@@ -455,51 +455,6 @@ class UrlCheckerDNSTestCase(unittest.TestCase):
assert self.db.NetworkChange.get().state == "closed" assert self.db.NetworkChange.get().state == "closed"
assert self.db.NetworkChange.get().status_id == status_id assert self.db.NetworkChange.get().status_id == status_id
def test_getReachableResolverList_noiplist(self):
resolver_ip = "127.0.0.1"
domain = "example.org"
rdtype = "A"
status_id = logStatus(self.db, "foo")
resolver = urlchecker_dns.dns.resolver.Resolver(configure=False)
resolver.nameservers.append(resolver_ip)
with mock.patch(
"urlchecker_dns.get_default_resolver"
) as mock_get_default_resolver:
mock_get_default_resolver.return_value = resolver
with mock.patch(
"urlchecker_dns.dns.resolver.Resolver.query"
) as mock_query:
mock_query.return_value = [
MockAnswer("4.3.2.1"),
MockAnswer("1.2.3.4"),
]
result = getReachableResolverList(self.db, status_id, [])
assert mock_get_default_resolver.call_count == 1
assert mock_query.call_count == 1
mock_query.assert_called_with(
domain, rdtype, raise_on_no_answer=False
)
assert self.db.DnsChange.select().count() == 1
assert self.db.DnsChange.get().resolver_ip == resolver_ip
assert self.db.DnsChange.get().domain == domain
assert self.db.DnsChange.get().rdtype == rdtype
assert self.db.DnsChange.get().response == "1.2.3.4, 4.3.2.1"
assert self.db.DnsChange.get().status_id == status_id
assert result == ["127.0.0.1"]
assert self.db.NetworkChange.select().count() == 1
assert self.db.NetworkChange.get().ip == resolver_ip
assert self.db.NetworkChange.get().port == 53
assert self.db.NetworkChange.get().transport == "UDP"
assert self.db.NetworkChange.get().state == "open"
assert self.db.NetworkChange.get().status_id == status_id
################################################ ################################################
# getDomainIpDict # getDomainIpDict
################################################ ################################################
......
...@@ -10,9 +10,7 @@ def runUrlChecker(): ...@@ -10,9 +10,7 @@ def runUrlChecker():
@runUrlChecker.command("bot", short_help="Runs url checker bot.") @runUrlChecker.command("bot", short_help="Runs url checker bot.")
@click.option( @click.option(
"--sqlite", "--sqlite", "-s", help="The path of the sqlite DB. (default: :memory:)"
"-s",
help="The path of the sqlite DB. (default: :memory:)"
) )
@click.option("--dns", "-d", help="The IP of the DNS server.") @click.option("--dns", "-d", help="The IP of the DNS server.")
@click.option("--url", "-u", help="The url to check.") @click.option("--url", "-u", help="The url to check.")
......
import configparser import configparser
import os import os
from dns.resolver import get_default_resolver
CONFIG_SECTION = "URLCHECKER" CONFIG_SECTION = "URLCHECKER"
...@@ -9,12 +11,7 @@ def createConfiguration( ...@@ -9,12 +11,7 @@ def createConfiguration(
): ):
config = configparser.ConfigParser(empty_lines_in_values=False) config = configparser.ConfigParser(empty_lines_in_values=False)
# Default values # Default values
config[CONFIG_SECTION] = { config[CONFIG_SECTION] = {"INTERVAL": -1, "DOMAIN": "", "URL": ""}
"INTERVAL": -1,
"DOMAIN": "",
"URL": "",
"DNS": "",
}
# User defined values # User defined values
if (envvar is not None) and (envvar in os.environ): if (envvar is not None) and (envvar in os.environ):
...@@ -26,7 +23,11 @@ def createConfiguration( ...@@ -26,7 +23,11 @@ def createConfiguration(
# Required values # Required values
if "SQLITE" not in config[CONFIG_SECTION]: if "SQLITE" not in config[CONFIG_SECTION]:
config[CONFIG_SECTION]["SQLITE"] = ":memory:" config[CONFIG_SECTION]["SQLITE"] = ":memory:"
if "DNS" not in config[CONFIG_SECTION]:
config[CONFIG_SECTION]["DNS"] = "\n".join(
get_default_resolver().nameservers
)
return config[CONFIG_SECTION] return config[CONFIG_SECTION]
......
from dns.resolver import get_default_resolver
import dns import dns
from urlchecker_network import logNetwork from urlchecker_network import logNetwork
...@@ -73,9 +72,6 @@ def queryDNS(db, status_id, resolver_ip, domain_text, rdtype): ...@@ -73,9 +72,6 @@ def queryDNS(db, status_id, resolver_ip, domain_text, rdtype):
def getReachableResolverList(db, status_id, resolver_ip_list): def getReachableResolverList(db, status_id, resolver_ip_list):
# Create a list of resolver object # Create a list of resolver object
if len(resolver_ip_list) == 0:
resolver_ip_list = get_default_resolver().nameservers
result_ip_list = [] result_ip_list = []
# Check the DNS server availability once # Check the DNS server availability once
# to prevent using it later if it is down # to prevent using it later if it is down
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment