make the tests use a test zone
for the dnstools tests, tests were already changed to use a random test host in the test zone, so parallel tests runs should not use the same hostnames. test_api tests still use same host names, though
This commit is contained in:
parent
7de76a142a
commit
852e541778
37
conftest.py
37
conftest.py
@ -8,13 +8,14 @@ from django.conf import settings
|
|||||||
|
|
||||||
# this is to create a Domain entries in the database, so they can be used for unit tests:
|
# this is to create a Domain entries in the database, so they can be used for unit tests:
|
||||||
BASEDOMAIN = "nsupdate.info"
|
BASEDOMAIN = "nsupdate.info"
|
||||||
TEST_HOST = 'test.' + BASEDOMAIN # unit tests can update this host ONLY
|
TESTDOMAIN = "tests." + BASEDOMAIN
|
||||||
|
TEST_HOST = 'test.' + TESTDOMAIN # unit tests can update this host ONLY
|
||||||
TEST_SECRET = "secret"
|
TEST_SECRET = "secret"
|
||||||
TEST_HOST2 = 'test2.' + BASEDOMAIN
|
TEST_HOST2 = 'test2.' + TESTDOMAIN
|
||||||
TEST_SECRET2 = "somethingelse"
|
TEST_SECRET2 = "somethingelse"
|
||||||
NAMESERVER_IP = "85.10.192.104"
|
NAMESERVER_IP = "85.10.192.104"
|
||||||
NAMESERVER_UPDATE_ALGORITHM = "HMAC_SHA512"
|
NAMESERVER_UPDATE_ALGORITHM = "HMAC_SHA512"
|
||||||
# no problem, you can ONLY update the TEST_HOST with this secret, nothing else:
|
# no problem, you can ONLY update the TESTDOMAIN with this secret, nothing else:
|
||||||
NAMESERVER_UPDATE_SECRET = "YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYQ=="
|
NAMESERVER_UPDATE_SECRET = "YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYQ=="
|
||||||
NAMESERVER_PUBLIC = True
|
NAMESERVER_PUBLIC = True
|
||||||
|
|
||||||
@ -29,6 +30,26 @@ SECURE = False # SSL/SNI support on python 2.x sucks :(
|
|||||||
|
|
||||||
from django.utils.translation import activate
|
from django.utils.translation import activate
|
||||||
|
|
||||||
|
from random import randint
|
||||||
|
from nsupdate.main.dnstools import update_ns
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.yield_fixture(scope="function")
|
||||||
|
def ddns_hostname():
|
||||||
|
"""
|
||||||
|
get a random hostname for tests and make sure it is removed from dns
|
||||||
|
after the test
|
||||||
|
"""
|
||||||
|
hostname = "test%d" % randint(1000000000, 2000000000)
|
||||||
|
yield hostname
|
||||||
|
update_ns(hostname + '.' + TESTDOMAIN, 'A', action='del')
|
||||||
|
update_ns(hostname + '.' + TESTDOMAIN, 'AAAA', action='del')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.yield_fixture(scope="function")
|
||||||
|
def ddns_fqdn(ddns_hostname):
|
||||||
|
yield ddns_hostname + '.' + TESTDOMAIN
|
||||||
|
|
||||||
|
|
||||||
# Note: fixture must be "function" scope (default), see https://github.com/pelme/pytest_django/issues/33
|
# Note: fixture must be "function" scope (default), see https://github.com/pelme/pytest_django/issues/33
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
@ -44,9 +65,9 @@ def db_init(db): # note: db is a predefined fixture and required here to have t
|
|||||||
u.save()
|
u.save()
|
||||||
u2 = user_model.objects.create_user(USERNAME2, 'test@example.org', PASSWORD)
|
u2 = user_model.objects.create_user(USERNAME2, 'test@example.org', PASSWORD)
|
||||||
u2.save()
|
u2.save()
|
||||||
# this is for updating:
|
# this is for tests:
|
||||||
Domain.objects.create(
|
dt = Domain.objects.create(
|
||||||
domain=TEST_HOST, # special: single-host update secret!
|
domain=TESTDOMAIN, # special: test-domain update secret!
|
||||||
nameserver_ip=NAMESERVER_IP,
|
nameserver_ip=NAMESERVER_IP,
|
||||||
nameserver_update_algorithm=NAMESERVER_UPDATE_ALGORITHM,
|
nameserver_update_algorithm=NAMESERVER_UPDATE_ALGORITHM,
|
||||||
nameserver_update_secret=NAMESERVER_UPDATE_SECRET,
|
nameserver_update_secret=NAMESERVER_UPDATE_SECRET,
|
||||||
@ -63,9 +84,9 @@ def db_init(db): # note: db is a predefined fixture and required here to have t
|
|||||||
created_by=u2,
|
created_by=u2,
|
||||||
)
|
)
|
||||||
# a Host for api / session update tests
|
# a Host for api / session update tests
|
||||||
h = Host(subdomain='test', domain=d, created_by=u)
|
h = Host(subdomain='test', domain=dt, created_by=u)
|
||||||
h.generate_secret(secret=TEST_SECRET)
|
h.generate_secret(secret=TEST_SECRET)
|
||||||
h2 = Host(subdomain='test2', domain=d, created_by=u2)
|
h2 = Host(subdomain='test2', domain=dt, created_by=u2)
|
||||||
h2.generate_secret(secret=TEST_SECRET2)
|
h2.generate_secret(secret=TEST_SECRET2)
|
||||||
|
|
||||||
# "update other service" ddns_client feature
|
# "update other service" ddns_client feature
|
||||||
|
@ -8,8 +8,9 @@ from nsupdate.main.dnstools import query_ns
|
|||||||
from nsupdate.main.models import Domain
|
from nsupdate.main.models import Domain
|
||||||
|
|
||||||
|
|
||||||
TEST_HOST = "test.nsupdate.info"
|
TESTDOMAIN = "tests.nsupdate.info"
|
||||||
TEST_HOST2 = "test2.nsupdate.info"
|
TEST_HOST = "test." + TESTDOMAIN
|
||||||
|
TEST_HOST2 = "test2." + TESTDOMAIN
|
||||||
TEST_SECRET = "secret"
|
TEST_SECRET = "secret"
|
||||||
|
|
||||||
USERNAME = 'test'
|
USERNAME = 'test'
|
||||||
@ -83,7 +84,7 @@ def test_nic_update_authorized(client):
|
|||||||
|
|
||||||
|
|
||||||
def test_nic_update_authorized_ns_unavailable(client):
|
def test_nic_update_authorized_ns_unavailable(client):
|
||||||
d = Domain.objects.get(domain=TEST_HOST)
|
d = Domain.objects.get(domain=TESTDOMAIN)
|
||||||
d.available = False # simulate DNS unavailability
|
d.available = False # simulate DNS unavailability
|
||||||
d.save()
|
d.save()
|
||||||
response = client.get(reverse('nic_update'),
|
response = client.get(reverse('nic_update'),
|
||||||
|
@ -14,8 +14,7 @@ from ..dnstools import add, delete, update, query_ns, rev_lookup, parse_name, up
|
|||||||
|
|
||||||
# see also conftest.py
|
# see also conftest.py
|
||||||
BASEDOMAIN = 'nsupdate.info'
|
BASEDOMAIN = 'nsupdate.info'
|
||||||
TEST_HOST = 'test.' + BASEDOMAIN # you can ONLY update THIS host in unit tests
|
INVALID_HOST = 'test999.' + BASEDOMAIN # this can't get updated
|
||||||
INVALID_HOST = 'test999.' + BASEDOMAIN # therefore, this can't get updated
|
|
||||||
|
|
||||||
|
|
||||||
def remove_records(host, records=('A', 'AAAA', )):
|
def remove_records(host, records=('A', 'AAAA', )):
|
||||||
@ -29,8 +28,8 @@ def remove_records(host, records=('A', 'AAAA', )):
|
|||||||
|
|
||||||
|
|
||||||
class TestIntelligentUpdater(object):
|
class TestIntelligentUpdater(object):
|
||||||
def test_double_update(self):
|
def test_double_update(self, ddns_fqdn):
|
||||||
host, ip = TEST_HOST, '1.2.3.4'
|
host, ip = ddns_fqdn, '1.2.3.4'
|
||||||
remove_records(host)
|
remove_records(host)
|
||||||
# first update with this IP, should work without issue:
|
# first update with this IP, should work without issue:
|
||||||
update(host, ip)
|
update(host, ip)
|
||||||
@ -41,8 +40,8 @@ class TestIntelligentUpdater(object):
|
|||||||
|
|
||||||
|
|
||||||
class TestIntelligentAdder(object):
|
class TestIntelligentAdder(object):
|
||||||
def test_double_add_same(self):
|
def test_double_add_same(self, ddns_fqdn):
|
||||||
host, ip = TEST_HOST, '1.2.3.4'
|
host, ip = ddns_fqdn, '1.2.3.4'
|
||||||
remove_records(host)
|
remove_records(host)
|
||||||
# first add with this IP, should work without issue:
|
# first add with this IP, should work without issue:
|
||||||
add(host, ip)
|
add(host, ip)
|
||||||
@ -51,8 +50,8 @@ class TestIntelligentAdder(object):
|
|||||||
# trying to add again with same IP should raise
|
# trying to add again with same IP should raise
|
||||||
add(host, ip)
|
add(host, ip)
|
||||||
|
|
||||||
def test_double_add_different(self):
|
def test_double_add_different(self, ddns_fqdn):
|
||||||
host, ip = TEST_HOST, '1.2.3.4'
|
host, ip = ddns_fqdn, '1.2.3.4'
|
||||||
remove_records(host)
|
remove_records(host)
|
||||||
# first add with this IP, should work without issue:
|
# first add with this IP, should work without issue:
|
||||||
add(host, ip)
|
add(host, ip)
|
||||||
@ -64,8 +63,8 @@ class TestIntelligentAdder(object):
|
|||||||
|
|
||||||
|
|
||||||
class TestIntelligentDeleter(object):
|
class TestIntelligentDeleter(object):
|
||||||
def test_delete(self):
|
def test_delete(self, ddns_fqdn):
|
||||||
host, ip = TEST_HOST, '1.2.3.4'
|
host, ip = ddns_fqdn, '1.2.3.4'
|
||||||
# make sure the host is there
|
# make sure the host is there
|
||||||
update_ns(host, 'A', ip, action='add')
|
update_ns(host, 'A', ip, action='add')
|
||||||
delete(host)
|
delete(host)
|
||||||
@ -73,13 +72,13 @@ class TestIntelligentDeleter(object):
|
|||||||
with pytest.raises(NXDOMAIN):
|
with pytest.raises(NXDOMAIN):
|
||||||
query_ns(host, 'A')
|
query_ns(host, 'A')
|
||||||
|
|
||||||
def test_double_delete(self):
|
def test_double_delete(self, ddns_fqdn):
|
||||||
host = TEST_HOST
|
host = ddns_fqdn
|
||||||
remove_records(host)
|
remove_records(host)
|
||||||
delete(host) # hmm, this doesn't raise NXDOMAIN!?
|
delete(host) # hmm, this doesn't raise NXDOMAIN!?
|
||||||
|
|
||||||
def test_delete_typed(self):
|
def test_delete_typed(self, ddns_fqdn):
|
||||||
host, ip4, ip6 = TEST_HOST, '1.2.3.4', '::42'
|
host, ip4, ip6 = ddns_fqdn, '1.2.3.4', '::42'
|
||||||
# make sure the host is there
|
# make sure the host is there
|
||||||
update_ns(host, 'A', ip4, action='add')
|
update_ns(host, 'A', ip4, action='add')
|
||||||
update_ns(host, 'AAAA', ip6, action='add')
|
update_ns(host, 'AAAA', ip6, action='add')
|
||||||
@ -94,19 +93,19 @@ class TestIntelligentDeleter(object):
|
|||||||
|
|
||||||
|
|
||||||
class TestQuery(object):
|
class TestQuery(object):
|
||||||
def test_queries_ok(self):
|
def test_queries_ok(self, ddns_fqdn):
|
||||||
host, ipv4, ipv6 = TEST_HOST, '42.42.42.42', '::23'
|
host, ipv4, ipv6 = ddns_fqdn, '42.42.42.42', '::23'
|
||||||
remove_records(host)
|
remove_records(host)
|
||||||
with pytest.raises(NXDOMAIN):
|
with pytest.raises(NXDOMAIN):
|
||||||
query_ns(TEST_HOST, 'A')
|
query_ns(ddns_fqdn, 'A')
|
||||||
with pytest.raises(NXDOMAIN):
|
with pytest.raises(NXDOMAIN):
|
||||||
query_ns(TEST_HOST, 'AAAA')
|
query_ns(ddns_fqdn, 'AAAA')
|
||||||
add(host, ipv4)
|
add(host, ipv4)
|
||||||
assert query_ns(TEST_HOST, 'A') == ipv4
|
assert query_ns(ddns_fqdn, 'A') == ipv4
|
||||||
with pytest.raises(NoAnswer):
|
with pytest.raises(NoAnswer):
|
||||||
query_ns(TEST_HOST, 'AAAA')
|
query_ns(ddns_fqdn, 'AAAA')
|
||||||
add(host, ipv6)
|
add(host, ipv6)
|
||||||
assert query_ns(TEST_HOST, 'AAAA') == ipv6
|
assert query_ns(ddns_fqdn, 'AAAA') == ipv6
|
||||||
|
|
||||||
|
|
||||||
class TestReverseLookup(object):
|
class TestReverseLookup(object):
|
||||||
@ -137,8 +136,8 @@ class TestUpdate(object):
|
|||||||
assert str(origin) == 'bar.baz.org' + '.'
|
assert str(origin) == 'bar.baz.org' + '.'
|
||||||
assert str(relname) == 'foo'
|
assert str(relname) == 'foo'
|
||||||
|
|
||||||
def test_add_del_v4(self):
|
def test_add_del_v4(self, ddns_fqdn):
|
||||||
host, ip = TEST_HOST, '1.1.1.1'
|
host, ip = ddns_fqdn, '1.1.1.1'
|
||||||
remove_records(host)
|
remove_records(host)
|
||||||
response = update_ns(host, 'A', ip, action='add', ttl=60)
|
response = update_ns(host, 'A', ip, action='add', ttl=60)
|
||||||
print(response)
|
print(response)
|
||||||
@ -148,19 +147,19 @@ class TestUpdate(object):
|
|||||||
with pytest.raises(NXDOMAIN):
|
with pytest.raises(NXDOMAIN):
|
||||||
query_ns(host, 'A') == ip
|
query_ns(host, 'A') == ip
|
||||||
|
|
||||||
def test_update_v4(self):
|
def test_update_v4(self, ddns_fqdn):
|
||||||
host, ip = TEST_HOST, '2.2.2.2'
|
host, ip = ddns_fqdn, '2.2.2.2'
|
||||||
response = update_ns(host, 'A', ip, action='upd', ttl=60)
|
response = update_ns(host, 'A', ip, action='upd', ttl=60)
|
||||||
print(response)
|
print(response)
|
||||||
assert query_ns(host, 'A') == ip
|
assert query_ns(host, 'A') == ip
|
||||||
|
|
||||||
host, ip = TEST_HOST, '3.3.3.3'
|
host, ip = ddns_fqdn, '3.3.3.3'
|
||||||
response = update_ns(host, 'A', ip, action='upd', ttl=60)
|
response = update_ns(host, 'A', ip, action='upd', ttl=60)
|
||||||
print(response)
|
print(response)
|
||||||
assert query_ns(host, 'A') == ip
|
assert query_ns(host, 'A') == ip
|
||||||
|
|
||||||
def test_add_del_v6(self):
|
def test_add_del_v6(self, ddns_fqdn):
|
||||||
host, ip = TEST_HOST, '::1'
|
host, ip = ddns_fqdn, '::1'
|
||||||
remove_records(host)
|
remove_records(host)
|
||||||
response = update_ns(host, 'AAAA', ip, action='add', ttl=60)
|
response = update_ns(host, 'AAAA', ip, action='add', ttl=60)
|
||||||
print(response)
|
print(response)
|
||||||
@ -170,24 +169,24 @@ class TestUpdate(object):
|
|||||||
with pytest.raises(NXDOMAIN):
|
with pytest.raises(NXDOMAIN):
|
||||||
query_ns(host, 'AAAA') == ip
|
query_ns(host, 'AAAA') == ip
|
||||||
|
|
||||||
def test_update_v6(self):
|
def test_update_v6(self, ddns_fqdn):
|
||||||
host, ip = TEST_HOST, '::2'
|
host, ip = ddns_fqdn, '::2'
|
||||||
response = update_ns(host, 'AAAA', ip, action='upd', ttl=60)
|
response = update_ns(host, 'AAAA', ip, action='upd', ttl=60)
|
||||||
print(response)
|
print(response)
|
||||||
assert query_ns(host, 'AAAA') == ip
|
assert query_ns(host, 'AAAA') == ip
|
||||||
|
|
||||||
host, ip = TEST_HOST, '::3'
|
host, ip = ddns_fqdn, '::3'
|
||||||
response = update_ns(host, 'AAAA', ip, action='upd', ttl=60)
|
response = update_ns(host, 'AAAA', ip, action='upd', ttl=60)
|
||||||
print(response)
|
print(response)
|
||||||
assert query_ns(host, 'AAAA') == ip
|
assert query_ns(host, 'AAAA') == ip
|
||||||
|
|
||||||
def test_update_mixed(self):
|
def test_update_mixed(self, ddns_fqdn):
|
||||||
host4, ip4 = TEST_HOST, '4.4.4.4'
|
host4, ip4 = ddns_fqdn, '4.4.4.4'
|
||||||
response = update_ns(host4, 'A', ip4, action='upd', ttl=60)
|
response = update_ns(host4, 'A', ip4, action='upd', ttl=60)
|
||||||
print(response)
|
print(response)
|
||||||
assert query_ns(host4, 'A') == ip4
|
assert query_ns(host4, 'A') == ip4
|
||||||
|
|
||||||
host6, ip6 = TEST_HOST, '::4'
|
host6, ip6 = ddns_fqdn, '::4'
|
||||||
response = update_ns(host6, 'AAAA', ip6, action='upd', ttl=60)
|
response = update_ns(host6, 'AAAA', ip6, action='upd', ttl=60)
|
||||||
print(response)
|
print(response)
|
||||||
assert query_ns(host6, 'AAAA') == ip6
|
assert query_ns(host6, 'AAAA') == ip6
|
||||||
@ -195,7 +194,7 @@ class TestUpdate(object):
|
|||||||
# make sure the v4 is unchanged
|
# make sure the v4 is unchanged
|
||||||
assert query_ns(host4, 'A') == ip4
|
assert query_ns(host4, 'A') == ip4
|
||||||
|
|
||||||
host4, ip4 = TEST_HOST, '5.5.5.5'
|
host4, ip4 = ddns_fqdn, '5.5.5.5'
|
||||||
response = update_ns(host4, 'A', ip4, action='upd', ttl=60)
|
response = update_ns(host4, 'A', ip4, action='upd', ttl=60)
|
||||||
print(response)
|
print(response)
|
||||||
assert query_ns(host4, 'A') == ip4
|
assert query_ns(host4, 'A') == ip4
|
||||||
@ -204,7 +203,7 @@ class TestUpdate(object):
|
|||||||
assert query_ns(host6, 'AAAA') == ip6
|
assert query_ns(host6, 'AAAA') == ip6
|
||||||
|
|
||||||
def test_bad_update(self):
|
def test_bad_update(self):
|
||||||
# test whether we ONLY can update the TEST_HOST
|
# test whether we ONLY can update the TESTDOMAIN
|
||||||
with pytest.raises(DnsUpdateError):
|
with pytest.raises(DnsUpdateError):
|
||||||
response = update_ns(INVALID_HOST, 'A', '6.6.6.6', action='upd', ttl=60)
|
response = update_ns(INVALID_HOST, 'A', '6.6.6.6', action='upd', ttl=60)
|
||||||
print(response)
|
print(response)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user