Skip to content

Commit

Permalink
Merge pull request #4 from drkitty/assorted-dns-tests
Browse files Browse the repository at this point in the history
Assorted DNS tests (for real this time)
  • Loading branch information
Nathan Murrow committed May 30, 2014
2 parents 3847499 + 450cc1d commit 9c63b93
Show file tree
Hide file tree
Showing 9 changed files with 883 additions and 186 deletions.
33 changes: 33 additions & 0 deletions cyder/base/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from exceptions import AssertionError

from django.core.exceptions import ValidationError
from django.test import TestCase
from django.test.client import Client

Expand All @@ -17,6 +20,36 @@ def _pre_setup(self):
session['ctnrs'] = list(Ctnr.objects.all())
session.save()

def assertObjectsConflict(self, obj_create_list):
pairs = [(a,b)
for a in obj_create_list
for b in obj_create_list
if a != b]

for first, second in pairs:
x = first()
try:
second()
except ValidationError:
pass
else:
raise AssertionError(
"'{}' and '{}' do not conflict".format(first.name,
second.name))
x.delete()

def assertObjectsDontConflict(self, obj_create_list):
pairs = [(a,b)
for a in obj_create_list
for b in obj_create_list
if a != b]

for first, second in pairs:
x = first()
y = second()
y.delete()
x.delete()


class TestCase(TestCase, CyTestMixin):
"""
Expand Down
255 changes: 194 additions & 61 deletions cyder/cydns/address_record/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import ipaddr

from django.core.exceptions import ValidationError
from django.db import IntegrityError

import cyder.base.tests
from cyder.core.ctnr.models import Ctnr
from cyder.core.system.models import System
from cyder.cydns.tests.utils import create_basic_dns_data
from cyder.cydns.address_record.models import AddressRecord
from cyder.cydns.domain.models import Domain
from cyder.cydns.cname.models import CNAME
from cyder.cydns.domain.models import Domain, boot_strap_ipv6_reverse_domain
from cyder.cydns.ip.models import ipv6_to_longs
from cyder.cydns.nameserver.models import Nameserver
from cyder.cydns.domain.models import boot_strap_ipv6_reverse_domain
from cyder.cydns.ip.utils import ip_to_domain_name
from cyder.cydns.cname.models import CNAME
from cyder.cydns.nameserver.models import Nameserver
from cyder.cydhcp.interface.static_intr.models import StaticInterface
from cyder.cydhcp.network.models import Network
from cyder.cydhcp.range.models import Range
from cyder.cydhcp.vrf.models import Vrf


class AddressRecordTests(cyder.base.tests.TestCase):
Expand All @@ -29,70 +33,32 @@ def create_domain(self, name, ip_type=None, delegated=False):

def setUp(self):
self.ctnr, _ = Ctnr.objects.get_or_create(name='abloobloobloo')
self.arpa = self.create_domain(name='arpa')
self.arpa.save()
self.i_arpa = self.create_domain(name='in-addr.arpa')
self.i_arpa.save()
self.i6_arpa = self.create_domain(name='ip6.arpa')
self.i6_arpa.save()

create_basic_dns_data(dhcp=True)

self.osu_block = "633:105:F000:"
boot_strap_ipv6_reverse_domain("0.6.3")
try:
self.e = Domain(name='edu')
self.e.save()
except IntegrityError:
pass
try:
self.o_e = Domain(name='oregonstate.edu')
self.o_e.save()
except IntegrityError:
self.o_e = Domain.objects.filter(name='oregonstate.edu')[0]
pass
self.e = Domain(name='edu')
self.e.save()
self.o_e = Domain(name='oregonstate.edu')
self.o_e.save()

try:
self.f_o_e = Domain(name='foo.oregonstate.edu')
self.f_o_e.save()
except IntegrityError:
self.f_o_e = Domain.objects.filter(name='foo.oregonstate.edu')[0]
pass
self.f_o_e = Domain(name='foo.oregonstate.edu')
self.f_o_e.save()

try:
self.m_o_e = Domain(name='max.oregonstate.edu')
self.m_o_e.save()
except IntegrityError:
self.m_o_e = Domain.objects.filter(name='max.oregonstate.edu')[0]
pass
self.m_o_e = Domain(name='max.oregonstate.edu')
self.m_o_e.save()

try:
self.z_o_e = Domain(name='zax.oregonstate.edu')
self.z_o_e.save()
except IntegrityError:
self.z_o_e = Domain.objects.filter(name='zax.oregonstate.edu')[0]
pass
try:
self.g_o_e = Domain(name='george.oregonstate.edu')
self.g_o_e.save()
except IntegrityError:
self.g_o_e = Domain.objects.filter(name=
'george.oregonstate.edu')[0]
pass
self.z_o_e = Domain(name='zax.oregonstate.edu')
self.z_o_e.save()
self.g_o_e = Domain(name='george.oregonstate.edu')
self.g_o_e.save()

try:
self._128 = self.create_domain(name='128')
self._128.save()
except IntegrityError:
raise Exception
self._128 = self.create_domain.objects.filter(name='128')[0]
pass
self._128 = self.create_domain(name='128')
self._128.save()

try:
self._128_193 = self.create_domain(name='128.193')
self._128_193.save()
except IntegrityError:
raise Exception
self._128_193 = Domain.objects.filter(name='128.193')[0]
pass
self._128_193 = self.create_domain(name='128.193')
self._128_193.save()

def build_ar(self, *args, **kwargs):
kwargs['ctnr'] = self.ctnr
Expand Down Expand Up @@ -650,3 +616,170 @@ def test_delete_with_cname_pointing_to_a(self):
cn.save()
self.assertRaises(ValidationError, a.delete)
a.delete(check_cname=False)

def test_domain_ctnr(self):
"""Test that an AR's domain must be in the AR's ctnr"""
c1 = Ctnr(name='test_ctnr1')
c1.full_clean()
c1.save()
c2 = Ctnr(name='test_ctnr2')
c2.full_clean()
c2.save()

c1.domains.add(self.o_e)

a1 = AddressRecord(label='foo', domain=self.o_e, ip_str='128.193.0.2',
ctnr=c1)
a1.full_clean()
a1.save()

with self.assertRaises(ValidationError):
a2 = AddressRecord(label='foo', domain=self.o_e,
ip_str='128.193.0.3', ctnr=c2)
a2.full_clean()
a2.save()

def test_duplicate_names(self):
"""Test that AddressRecords can have the same name iff in the same Ctnr
"""
c1 = Ctnr(name='test_ctnr1')
c1.full_clean()
c1.save()
c2 = Ctnr(name='test_ctnr2')
c2.full_clean()
c2.save()

def create_ar1():
a = AddressRecord(label='foo', domain=self.o_e,
ip_str='128.193.0.2', ctnr=c1)
a.full_clean()
a.save()
return a
create_ar1.name = "AddressRecord 1"

def create_ar2():
a = AddressRecord(label='foo', domain=self.o_e,
ip_str='128.193.0.3', ctnr=c1)
a.full_clean()
a.save()
return a
create_ar2.name = "AddressRecord 2"

def create_ar3():
a = AddressRecord(label='foo', domain=self.o_e,
ip_str='128.193.0.4', ctnr=c2)
a.full_clean()
a.save()
return a
create_ar3.name = "AddressRecord 3"

self.assertObjectsDontConflict((create_ar1, create_ar2))
self.assertObjectsConflict((create_ar1, create_ar3))
self.assertObjectsConflict((create_ar2, create_ar3))

def test_address_record_conflicts_with_cname(self):
"""Test that an AddressRecord and a CNAME can't have the same name"""
def create_cname():
cn = CNAME(label='bar', domain=self.o_e,
target='foo.oregonstate.edu', ctnr=self.ctnr)
cn.full_clean()
cn.save()
return cn
create_cname.name = 'CNAME'

def create_ar():
a = AddressRecord(label='bar', domain=self.o_e,
ip_str='128.193.0.2', ctnr=self.ctnr)
a.full_clean()
a.save()
return a
create_ar.name = 'AddressRecord'

self.assertObjectsConflict((create_cname, create_ar))

def test_same_name_as_static_interface(self):
"""Test that ARs and SIs can share a name iff they have the same ctnr
"""
n = Network(vrf=Vrf.objects.get(name='test_vrf'), ip_type='4',
network_str='128.193.0.0/24')
n.full_clean()
n.save()

r = Range(network=n, range_type='st', start_str='128.193.0.2',
end_str='128.193.0.100')
r.full_clean()
r.save()

c1 = Ctnr(name='test_ctnr1')
c1.full_clean()
c1.save()
c2 = Ctnr(name='test_ctnr2')
c2.full_clean()
c2.save()

def create_si():
s = System(name='test_system')
s.full_clean()
s.save()

si = StaticInterface(
mac='be:ef:fa:ce:11:11', label='foo1', domain=self.o_e,
ip_str='128.193.0.2', ip_type='4', system=s,
ctnr=c1)
si.full_clean()
si.save()

return si
create_si.name = 'StaticInterface'

def create_ar_same_ctnr():
a = AddressRecord(label='foo1', domain=self.o_e,
ip_str='128.193.0.3', ctnr=c1)
a.full_clean()
a.save()
return a
create_ar_same_ctnr.name = 'AddressRecord with same ctnr'

def create_ar_different_ctnr():
a = AddressRecord(label='foo1', domain=self.o_e,
ip_str='128.193.0.3', ctnr=c2)
a.full_clean()
a.save()
return a
create_ar_different_ctnr.name = 'AddressRecord with different ctnr'

self.assertObjectsDontConflict((create_si, create_ar_same_ctnr))
self.assertObjectsConflict((create_si, create_ar_different_ctnr))

def test_target_validation(self):
"""Test that the target must be an IP address"""
valid_ips = (
('10.234.30.253', '4'),
('128.193.0.3', '4'),
('fe80::e1c9:1:228d:d8', '6'),
)

for ip_str, ip_type in valid_ips:
a = AddressRecord(label='foo', domain=self.o_e,
ip_str=ip_str, ip_type=ip_type, ctnr=self.ctnr)
a.full_clean()
a.save()
a.delete()

invalid_ips = (
('fe80::e1c9:1:228d:d8', '4'),
('10.234,30.253', '4'),
('10.234.30', '4'),
('128.193', '4'),
('10.234.30.253', '6'),
('fe80:e1c9:1:228d:d8', '6'),
('fe80::e1c9:1:228d:d8::91c2', '6'),
('fe801::e1c9:1:228d:d8', '6'),
)

for ip_str, ip_type in invalid_ips:
with self.assertRaises(ValidationError):
a = AddressRecord(label='foo', domain=self.o_e,
ip_str=ip_str, ip_type=ip_type)
a.full_clean()
a.save()
Loading

0 comments on commit 9c63b93

Please sign in to comment.