Skip to content

Commit

Permalink
make the tests use a test zone
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ThomasWaldmann committed Dec 25, 2013
1 parent 7de76a1 commit 852e541
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 47 deletions.
37 changes: 29 additions & 8 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

# this is to create a Domain entries in the database, so they can be used for unit tests:
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_HOST2 = 'test2.' + BASEDOMAIN
TEST_HOST2 = 'test2.' + TESTDOMAIN
TEST_SECRET2 = "somethingelse"
NAMESERVER_IP = "85.10.192.104"
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_PUBLIC = True

Expand All @@ -29,6 +30,26 @@

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
@pytest.fixture(autouse=True)
Expand All @@ -44,9 +65,9 @@ def db_init(db): # note: db is a predefined fixture and required here to have t
u.save()
u2 = user_model.objects.create_user(USERNAME2, 'test@example.org', PASSWORD)
u2.save()
# this is for updating:
Domain.objects.create(
domain=TEST_HOST, # special: single-host update secret!
# this is for tests:
dt = Domain.objects.create(
domain=TESTDOMAIN, # special: test-domain update secret!
nameserver_ip=NAMESERVER_IP,
nameserver_update_algorithm=NAMESERVER_UPDATE_ALGORITHM,
nameserver_update_secret=NAMESERVER_UPDATE_SECRET,
Expand All @@ -63,9 +84,9 @@ def db_init(db): # note: db is a predefined fixture and required here to have t
created_by=u2,
)
# 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)
h2 = Host(subdomain='test2', domain=d, created_by=u2)
h2 = Host(subdomain='test2', domain=dt, created_by=u2)
h2.generate_secret(secret=TEST_SECRET2)

# "update other service" ddns_client feature
Expand Down
7 changes: 4 additions & 3 deletions nsupdate/api/_tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from nsupdate.main.models import Domain


TEST_HOST = "test.nsupdate.info"
TEST_HOST2 = "test2.nsupdate.info"
TESTDOMAIN = "tests.nsupdate.info"
TEST_HOST = "test." + TESTDOMAIN
TEST_HOST2 = "test2." + TESTDOMAIN
TEST_SECRET = "secret"

USERNAME = 'test'
Expand Down Expand Up @@ -83,7 +84,7 @@ def test_nic_update_authorized(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.save()
response = client.get(reverse('nic_update'),
Expand Down
71 changes: 35 additions & 36 deletions nsupdate/main/_tests/test_dnstools.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

# see also conftest.py
BASEDOMAIN = 'nsupdate.info'
TEST_HOST = 'test.' + BASEDOMAIN # you can ONLY update THIS host in unit tests
INVALID_HOST = 'test999.' + BASEDOMAIN # therefore, this can't get updated
INVALID_HOST = 'test999.' + BASEDOMAIN # this can't get updated


def remove_records(host, records=('A', 'AAAA', )):
Expand All @@ -29,8 +28,8 @@ def remove_records(host, records=('A', 'AAAA', )):


class TestIntelligentUpdater(object):
def test_double_update(self):
host, ip = TEST_HOST, '1.2.3.4'
def test_double_update(self, ddns_fqdn):
host, ip = ddns_fqdn, '1.2.3.4'
remove_records(host)
# first update with this IP, should work without issue:
update(host, ip)
Expand All @@ -41,8 +40,8 @@ def test_double_update(self):


class TestIntelligentAdder(object):
def test_double_add_same(self):
host, ip = TEST_HOST, '1.2.3.4'
def test_double_add_same(self, ddns_fqdn):
host, ip = ddns_fqdn, '1.2.3.4'
remove_records(host)
# first add with this IP, should work without issue:
add(host, ip)
Expand All @@ -51,8 +50,8 @@ def test_double_add_same(self):
# trying to add again with same IP should raise
add(host, ip)

def test_double_add_different(self):
host, ip = TEST_HOST, '1.2.3.4'
def test_double_add_different(self, ddns_fqdn):
host, ip = ddns_fqdn, '1.2.3.4'
remove_records(host)
# first add with this IP, should work without issue:
add(host, ip)
Expand All @@ -64,22 +63,22 @@ def test_double_add_different(self):


class TestIntelligentDeleter(object):
def test_delete(self):
host, ip = TEST_HOST, '1.2.3.4'
def test_delete(self, ddns_fqdn):
host, ip = ddns_fqdn, '1.2.3.4'
# 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')

def test_double_delete(self):
host = TEST_HOST
def test_double_delete(self, ddns_fqdn):
host = ddns_fqdn
remove_records(host)
delete(host) # hmm, this doesn't raise NXDOMAIN!?

def test_delete_typed(self):
host, ip4, ip6 = TEST_HOST, '1.2.3.4', '::42'
def test_delete_typed(self, ddns_fqdn):
host, ip4, ip6 = ddns_fqdn, '1.2.3.4', '::42'
# make sure the host is there
update_ns(host, 'A', ip4, action='add')
update_ns(host, 'AAAA', ip6, action='add')
Expand All @@ -94,19 +93,19 @@ def test_delete_typed(self):


class TestQuery(object):
def test_queries_ok(self):
host, ipv4, ipv6 = TEST_HOST, '42.42.42.42', '::23'
def test_queries_ok(self, ddns_fqdn):
host, ipv4, ipv6 = ddns_fqdn, '42.42.42.42', '::23'
remove_records(host)
with pytest.raises(NXDOMAIN):
query_ns(TEST_HOST, 'A')
query_ns(ddns_fqdn, 'A')
with pytest.raises(NXDOMAIN):
query_ns(TEST_HOST, 'AAAA')
query_ns(ddns_fqdn, 'AAAA')
add(host, ipv4)
assert query_ns(TEST_HOST, 'A') == ipv4
assert query_ns(ddns_fqdn, 'A') == ipv4
with pytest.raises(NoAnswer):
query_ns(TEST_HOST, 'AAAA')
query_ns(ddns_fqdn, 'AAAA')
add(host, ipv6)
assert query_ns(TEST_HOST, 'AAAA') == ipv6
assert query_ns(ddns_fqdn, 'AAAA') == ipv6


class TestReverseLookup(object):
Expand Down Expand Up @@ -137,8 +136,8 @@ def test_parse_with_origin(self):
assert str(origin) == 'bar.baz.org' + '.'
assert str(relname) == 'foo'

def test_add_del_v4(self):
host, ip = TEST_HOST, '1.1.1.1'
def test_add_del_v4(self, ddns_fqdn):
host, ip = ddns_fqdn, '1.1.1.1'
remove_records(host)
response = update_ns(host, 'A', ip, action='add', ttl=60)
print(response)
Expand All @@ -148,19 +147,19 @@ def test_add_del_v4(self):
with pytest.raises(NXDOMAIN):
query_ns(host, 'A') == ip

def test_update_v4(self):
host, ip = TEST_HOST, '2.2.2.2'
def test_update_v4(self, ddns_fqdn):
host, ip = ddns_fqdn, '2.2.2.2'
response = update_ns(host, 'A', ip, action='upd', ttl=60)
print(response)
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)
print(response)
assert query_ns(host, 'A') == ip

def test_add_del_v6(self):
host, ip = TEST_HOST, '::1'
def test_add_del_v6(self, ddns_fqdn):
host, ip = ddns_fqdn, '::1'
remove_records(host)
response = update_ns(host, 'AAAA', ip, action='add', ttl=60)
print(response)
Expand All @@ -170,32 +169,32 @@ def test_add_del_v6(self):
with pytest.raises(NXDOMAIN):
query_ns(host, 'AAAA') == ip

def test_update_v6(self):
host, ip = TEST_HOST, '::2'
def test_update_v6(self, ddns_fqdn):
host, ip = ddns_fqdn, '::2'
response = update_ns(host, 'AAAA', ip, action='upd', ttl=60)
print(response)
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)
print(response)
assert query_ns(host, 'AAAA') == ip

def test_update_mixed(self):
host4, ip4 = TEST_HOST, '4.4.4.4'
def test_update_mixed(self, ddns_fqdn):
host4, ip4 = ddns_fqdn, '4.4.4.4'
response = update_ns(host4, 'A', ip4, action='upd', ttl=60)
print(response)
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)
print(response)
assert query_ns(host6, 'AAAA') == ip6

# make sure the v4 is unchanged
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)
print(response)
assert query_ns(host4, 'A') == ip4
Expand All @@ -204,7 +203,7 @@ def test_update_mixed(self):
assert query_ns(host6, 'AAAA') == ip6

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):
response = update_ns(INVALID_HOST, 'A', '6.6.6.6', action='upd', ttl=60)
print(response)

0 comments on commit 852e541

Please sign in to comment.