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):
def test_emptyConfiguration(self):
resolver_ip = "192.168.0.254"
bot = WebBot(mapping={"SQLITE": ":memory:"})
bot.initDB()
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:
"urlchecker_configuration.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
with mock.patch(
"urlchecker_dns.dns.resolver.Resolver.query"
) as mock_query:
mock_query.return_value = [MockAnswer("1.2.3.4")]
bot.iterateLoop()
mock_query.return_value = [MockAnswer("1.2.3.4")]
bot = WebBot(mapping={"SQLITE": ":memory:"})
bot.initDB()
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.get().text == "loop"
......@@ -70,14 +68,11 @@ class UrlCheckerBotTestCase(unittest.TestCase):
def test_oneNameserverOneDomainOneIp(self):
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.nameservers.append(resolver_ip)
with mock.patch(
"urlchecker_dns.get_default_resolver"
"urlchecker_configuration.get_default_resolver"
) as mock_get_default_resolver, mock.patch(
"urlchecker_dns.dns.resolver.Resolver.query"
) as mock_query, mock.patch(
......@@ -89,6 +84,11 @@ class UrlCheckerBotTestCase(unittest.TestCase):
mock_get_default_resolver.return_value = resolver
mock_query.return_value = [MockAnswer("1.2.3.4")]
bot = WebBot(
mapping={"SQLITE": ":memory:", "DOMAIN": "example.org"}
)
bot.initDB()
bot.iterateLoop()
assert mock_query.call_count == 2
......
......@@ -455,51 +455,6 @@ class UrlCheckerDNSTestCase(unittest.TestCase):
assert self.db.NetworkChange.get().state == "closed"
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
################################################
......
......@@ -10,9 +10,7 @@ def runUrlChecker():
@runUrlChecker.command("bot", short_help="Runs url checker bot.")
@click.option(
"--sqlite",
"-s",
help="The path of the sqlite DB. (default: :memory:)"
"--sqlite", "-s", help="The path of the sqlite DB. (default: :memory:)"
)
@click.option("--dns", "-d", help="The IP of the DNS server.")
@click.option("--url", "-u", help="The url to check.")
......
import configparser
import os
from dns.resolver import get_default_resolver
CONFIG_SECTION = "URLCHECKER"
......@@ -9,12 +11,7 @@ def createConfiguration(
):
config = configparser.ConfigParser(empty_lines_in_values=False)
# Default values
config[CONFIG_SECTION] = {
"INTERVAL": -1,
"DOMAIN": "",
"URL": "",
"DNS": "",
}
config[CONFIG_SECTION] = {"INTERVAL": -1, "DOMAIN": "", "URL": ""}
# User defined values
if (envvar is not None) and (envvar in os.environ):
......@@ -26,7 +23,11 @@ def createConfiguration(
# Required values
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]
......
from dns.resolver import get_default_resolver
import dns
from urlchecker_network import logNetwork
......@@ -73,9 +72,6 @@ def queryDNS(db, status_id, resolver_ip, domain_text, rdtype):
def getReachableResolverList(db, status_id, resolver_ip_list):
# Create a list of resolver object
if len(resolver_ip_list) == 0:
resolver_ip_list = get_default_resolver().nameservers
result_ip_list = []
# Check the DNS server availability once
# 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