2013-09-28 13:47:49 +02:00
|
|
|
"""
|
|
|
|
Tests for dnstools module.
|
|
|
|
"""
|
|
|
|
|
2013-12-14 00:35:29 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2013-09-28 13:47:49 +02:00
|
|
|
import pytest
|
2013-10-27 07:20:43 +01:00
|
|
|
|
2013-10-18 15:30:17 -07:00
|
|
|
pytestmark = pytest.mark.django_db
|
2013-09-28 13:47:49 +02:00
|
|
|
|
2013-11-09 23:19:05 +01:00
|
|
|
from dns.resolver import NXDOMAIN, NoAnswer
|
2013-10-03 19:26:39 +02:00
|
|
|
|
2014-08-30 18:27:21 +02:00
|
|
|
from ..dnstools import (add, delete, update, query_ns, rev_lookup, update_ns,
|
2014-08-30 16:08:10 +02:00
|
|
|
SameIpError, DnsUpdateError, FQDN)
|
2013-10-03 19:26:39 +02:00
|
|
|
|
2013-11-10 05:52:41 +01:00
|
|
|
# see also conftest.py
|
|
|
|
BASEDOMAIN = 'nsupdate.info'
|
2014-08-30 18:27:21 +02:00
|
|
|
INVALID_HOST = FQDN('test999', BASEDOMAIN) # this can't get updated
|
2013-11-10 05:52:41 +01:00
|
|
|
|
2013-09-28 13:47:49 +02:00
|
|
|
|
2014-08-30 16:08:10 +02:00
|
|
|
class TestFQDN(object):
|
|
|
|
def test_create(self):
|
|
|
|
fqdn = FQDN('test', 'example.org')
|
|
|
|
assert fqdn.host == 'test'
|
|
|
|
assert fqdn.domain == 'example.org'
|
|
|
|
assert str(fqdn) == 'test.example.org'
|
|
|
|
|
|
|
|
|
2013-11-09 23:19:05 +01:00
|
|
|
def remove_records(host, records=('A', 'AAAA', )):
|
|
|
|
# make sure the records are not there
|
|
|
|
for record in records:
|
2013-09-28 17:02:43 +02:00
|
|
|
try:
|
2013-11-09 23:19:05 +01:00
|
|
|
update_ns(host, record, action='del')
|
|
|
|
except (NXDOMAIN, NoAnswer):
|
2013-09-28 17:02:43 +02:00
|
|
|
# it is ok if it was never there
|
|
|
|
pass
|
2013-11-09 23:19:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestIntelligentUpdater(object):
|
2013-12-25 03:38:08 +01:00
|
|
|
def test_double_update(self, ddns_fqdn):
|
|
|
|
host, ip = ddns_fqdn, '1.2.3.4'
|
2013-11-09 23:19:05 +01:00
|
|
|
remove_records(host)
|
2013-09-28 17:02:43 +02:00
|
|
|
# first update with this IP, should work without issue:
|
|
|
|
update(host, ip)
|
2013-09-29 17:21:05 +02:00
|
|
|
assert query_ns(host, 'A') == ip
|
2013-09-28 17:02:43 +02:00
|
|
|
with pytest.raises(SameIpError):
|
|
|
|
# trying to update again with same IP should raise
|
|
|
|
update(host, ip)
|
|
|
|
|
|
|
|
|
2013-09-29 17:21:05 +02:00
|
|
|
class TestIntelligentAdder(object):
|
2013-12-25 03:38:08 +01:00
|
|
|
def test_double_add_same(self, ddns_fqdn):
|
|
|
|
host, ip = ddns_fqdn, '1.2.3.4'
|
2013-11-09 23:19:05 +01:00
|
|
|
remove_records(host)
|
2013-09-29 17:21:05 +02:00
|
|
|
# first add with this IP, should work without issue:
|
|
|
|
add(host, ip)
|
|
|
|
assert query_ns(host, 'A') == ip
|
|
|
|
with pytest.raises(SameIpError):
|
|
|
|
# trying to add again with same IP should raise
|
|
|
|
add(host, ip)
|
|
|
|
|
2013-12-25 03:38:08 +01:00
|
|
|
def test_double_add_different(self, ddns_fqdn):
|
|
|
|
host, ip = ddns_fqdn, '1.2.3.4'
|
2013-11-09 23:19:05 +01:00
|
|
|
remove_records(host)
|
2013-09-29 17:21:05 +02:00
|
|
|
# first add with this IP, should work without issue:
|
|
|
|
add(host, ip)
|
|
|
|
assert query_ns(host, 'A') == ip
|
|
|
|
different_ip = '4.3.2.1'
|
|
|
|
# trying to add again with same IP should raise
|
|
|
|
add(host, different_ip) # internally triggers an update
|
|
|
|
assert query_ns(host, 'A') == different_ip
|
|
|
|
|
|
|
|
|
|
|
|
class TestIntelligentDeleter(object):
|
2013-12-25 03:38:08 +01:00
|
|
|
def test_delete(self, ddns_fqdn):
|
|
|
|
host, ip = ddns_fqdn, '1.2.3.4'
|
2013-09-29 17:21:05 +02:00
|
|
|
# make sure the host is there
|
|
|
|
update_ns(host, 'A', ip, action='add')
|
|
|
|
delete(host)
|
|
|
|
# make sure it is gone
|
|
|
|
with pytest.raises(NXDOMAIN):
|
|
|
|
query_ns(host, 'A')
|
|
|
|
|
2013-12-25 03:38:08 +01:00
|
|
|
def test_double_delete(self, ddns_fqdn):
|
|
|
|
host = ddns_fqdn
|
2013-11-09 23:19:05 +01:00
|
|
|
remove_records(host)
|
2013-09-29 17:21:05 +02:00
|
|
|
delete(host) # hmm, this doesn't raise NXDOMAIN!?
|
|
|
|
|
2013-12-25 03:38:08 +01:00
|
|
|
def test_delete_typed(self, ddns_fqdn):
|
|
|
|
host, ip4, ip6 = ddns_fqdn, '1.2.3.4', '::42'
|
2013-11-21 04:02:16 +01:00
|
|
|
# make sure the host is there
|
|
|
|
update_ns(host, 'A', ip4, action='add')
|
|
|
|
update_ns(host, 'AAAA', ip6, action='add')
|
|
|
|
delete(host, 'A')
|
|
|
|
# make sure it is gone
|
|
|
|
with pytest.raises(NoAnswer):
|
|
|
|
query_ns(host, 'A')
|
|
|
|
delete(host, 'AAAA')
|
|
|
|
# make sure it is gone
|
|
|
|
with pytest.raises(NXDOMAIN):
|
|
|
|
query_ns(host, 'AAAA')
|
|
|
|
|
2013-09-29 17:21:05 +02:00
|
|
|
|
2013-09-28 15:29:16 +02:00
|
|
|
class TestQuery(object):
|
2013-12-25 03:38:08 +01:00
|
|
|
def test_queries_ok(self, ddns_fqdn):
|
|
|
|
host, ipv4, ipv6 = ddns_fqdn, '42.42.42.42', '::23'
|
2013-11-10 05:52:41 +01:00
|
|
|
remove_records(host)
|
2013-09-28 13:47:49 +02:00
|
|
|
with pytest.raises(NXDOMAIN):
|
2013-12-25 03:38:08 +01:00
|
|
|
query_ns(ddns_fqdn, 'A')
|
2013-09-28 13:47:49 +02:00
|
|
|
with pytest.raises(NXDOMAIN):
|
2013-12-25 03:38:08 +01:00
|
|
|
query_ns(ddns_fqdn, 'AAAA')
|
2013-11-10 05:52:41 +01:00
|
|
|
add(host, ipv4)
|
2013-12-25 03:38:08 +01:00
|
|
|
assert query_ns(ddns_fqdn, 'A') == ipv4
|
2013-11-10 05:52:41 +01:00
|
|
|
with pytest.raises(NoAnswer):
|
2013-12-25 03:38:08 +01:00
|
|
|
query_ns(ddns_fqdn, 'AAAA')
|
2013-11-10 05:52:41 +01:00
|
|
|
add(host, ipv6)
|
2013-12-25 03:38:08 +01:00
|
|
|
assert query_ns(ddns_fqdn, 'AAAA') == ipv6
|
2013-09-28 15:29:16 +02:00
|
|
|
|
|
|
|
|
2013-11-27 07:14:39 +01:00
|
|
|
class TestReverseLookup(object):
|
|
|
|
def test_rev_lookup_v4(self):
|
|
|
|
name, ip = 'google-public-dns-a.google.com', '8.8.8.8'
|
|
|
|
assert rev_lookup(ip) == name
|
|
|
|
|
|
|
|
def test_rev_lookup_v6(self):
|
|
|
|
name, ip = 'google-public-dns-a.google.com', '2001:4860:4860::8888'
|
|
|
|
assert rev_lookup(ip) == name
|
|
|
|
|
|
|
|
|
2013-09-28 15:29:16 +02:00
|
|
|
class TestUpdate(object):
|
2013-12-25 03:38:08 +01:00
|
|
|
def test_add_del_v4(self, ddns_fqdn):
|
|
|
|
host, ip = ddns_fqdn, '1.1.1.1'
|
2013-11-09 23:19:05 +01:00
|
|
|
remove_records(host)
|
2013-09-28 15:29:16 +02:00
|
|
|
response = update_ns(host, 'A', ip, action='add', ttl=60)
|
2013-12-14 00:35:29 +01:00
|
|
|
print(response)
|
2013-09-28 15:29:16 +02:00
|
|
|
assert query_ns(host, 'A') == ip
|
|
|
|
response = update_ns(host, 'A', action='del')
|
2013-12-14 00:35:29 +01:00
|
|
|
print(response)
|
2013-09-28 15:29:16 +02:00
|
|
|
with pytest.raises(NXDOMAIN):
|
|
|
|
query_ns(host, 'A') == ip
|
|
|
|
|
2013-12-25 03:38:08 +01:00
|
|
|
def test_update_v4(self, ddns_fqdn):
|
|
|
|
host, ip = ddns_fqdn, '2.2.2.2'
|
2013-09-28 15:29:16 +02:00
|
|
|
response = update_ns(host, 'A', ip, action='upd', ttl=60)
|
2013-12-14 00:35:29 +01:00
|
|
|
print(response)
|
2013-09-28 15:29:16 +02:00
|
|
|
assert query_ns(host, 'A') == ip
|
|
|
|
|
2013-12-25 03:38:08 +01:00
|
|
|
host, ip = ddns_fqdn, '3.3.3.3'
|
2013-09-28 15:29:16 +02:00
|
|
|
response = update_ns(host, 'A', ip, action='upd', ttl=60)
|
2013-12-14 00:35:29 +01:00
|
|
|
print(response)
|
2013-09-28 15:29:16 +02:00
|
|
|
assert query_ns(host, 'A') == ip
|
|
|
|
|
2013-12-25 03:38:08 +01:00
|
|
|
def test_add_del_v6(self, ddns_fqdn):
|
|
|
|
host, ip = ddns_fqdn, '::1'
|
2013-11-09 23:19:05 +01:00
|
|
|
remove_records(host)
|
2013-09-28 15:29:16 +02:00
|
|
|
response = update_ns(host, 'AAAA', ip, action='add', ttl=60)
|
2013-12-14 00:35:29 +01:00
|
|
|
print(response)
|
2013-09-28 15:29:16 +02:00
|
|
|
assert query_ns(host, 'AAAA') == ip
|
|
|
|
response = update_ns(host, 'AAAA', action='del')
|
2013-12-14 00:35:29 +01:00
|
|
|
print(response)
|
2013-09-28 15:29:16 +02:00
|
|
|
with pytest.raises(NXDOMAIN):
|
|
|
|
query_ns(host, 'AAAA') == ip
|
|
|
|
|
2013-12-25 03:38:08 +01:00
|
|
|
def test_update_v6(self, ddns_fqdn):
|
|
|
|
host, ip = ddns_fqdn, '::2'
|
2013-09-28 15:29:16 +02:00
|
|
|
response = update_ns(host, 'AAAA', ip, action='upd', ttl=60)
|
2013-12-14 00:35:29 +01:00
|
|
|
print(response)
|
2013-09-28 15:29:16 +02:00
|
|
|
assert query_ns(host, 'AAAA') == ip
|
|
|
|
|
2013-12-25 03:38:08 +01:00
|
|
|
host, ip = ddns_fqdn, '::3'
|
2013-09-28 15:29:16 +02:00
|
|
|
response = update_ns(host, 'AAAA', ip, action='upd', ttl=60)
|
2013-12-14 00:35:29 +01:00
|
|
|
print(response)
|
2013-09-28 15:29:16 +02:00
|
|
|
assert query_ns(host, 'AAAA') == ip
|
|
|
|
|
2013-12-25 03:38:08 +01:00
|
|
|
def test_update_mixed(self, ddns_fqdn):
|
|
|
|
host4, ip4 = ddns_fqdn, '4.4.4.4'
|
2013-09-28 15:29:16 +02:00
|
|
|
response = update_ns(host4, 'A', ip4, action='upd', ttl=60)
|
2013-12-14 00:35:29 +01:00
|
|
|
print(response)
|
2013-09-28 15:29:16 +02:00
|
|
|
assert query_ns(host4, 'A') == ip4
|
|
|
|
|
2013-12-25 03:38:08 +01:00
|
|
|
host6, ip6 = ddns_fqdn, '::4'
|
2013-09-28 15:29:16 +02:00
|
|
|
response = update_ns(host6, 'AAAA', ip6, action='upd', ttl=60)
|
2013-12-14 00:35:29 +01:00
|
|
|
print(response)
|
2013-09-28 15:29:16 +02:00
|
|
|
assert query_ns(host6, 'AAAA') == ip6
|
|
|
|
|
|
|
|
# make sure the v4 is unchanged
|
|
|
|
assert query_ns(host4, 'A') == ip4
|
|
|
|
|
2013-12-25 03:38:08 +01:00
|
|
|
host4, ip4 = ddns_fqdn, '5.5.5.5'
|
2013-09-28 15:29:16 +02:00
|
|
|
response = update_ns(host4, 'A', ip4, action='upd', ttl=60)
|
2013-12-14 00:35:29 +01:00
|
|
|
print(response)
|
2013-09-28 15:29:16 +02:00
|
|
|
assert query_ns(host4, 'A') == ip4
|
|
|
|
|
|
|
|
# make sure the v6 is unchanged
|
|
|
|
assert query_ns(host6, 'AAAA') == ip6
|
2013-11-10 05:52:41 +01:00
|
|
|
|
|
|
|
def test_bad_update(self):
|
2013-12-25 03:38:08 +01:00
|
|
|
# test whether we ONLY can update the TESTDOMAIN
|
2013-11-10 07:04:46 +01:00
|
|
|
with pytest.raises(DnsUpdateError):
|
|
|
|
response = update_ns(INVALID_HOST, 'A', '6.6.6.6', action='upd', ttl=60)
|
2013-12-14 00:35:29 +01:00
|
|
|
print(response)
|