2014-05-08 16:59:35 +00:00
|
|
|
import os
|
|
|
|
import time
|
2014-10-03 14:02:11 +00:00
|
|
|
import socket
|
2014-10-04 13:23:04 +00:00
|
|
|
from functools import partial
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-10-03 14:02:11 +00:00
|
|
|
from django.conf import settings as djsettings
|
|
|
|
from django.core.urlresolvers import reverse
|
2014-05-08 16:59:35 +00:00
|
|
|
from selenium.webdriver.support.select import Select
|
|
|
|
|
2015-04-05 10:46:24 +00:00
|
|
|
from orchestra.contrib.orchestration.models import Server, Route
|
2014-10-10 14:39:46 +00:00
|
|
|
from orchestra.utils.tests import BaseLiveServerTestCase, random_ascii, snapshot_on_error, save_response_on_error
|
2015-04-05 10:46:24 +00:00
|
|
|
from orchestra.utils.sys import run
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-09-10 16:53:09 +00:00
|
|
|
from ... import settings, utils, backends
|
|
|
|
from ...models import Domain, Record
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
2014-10-04 13:23:04 +00:00
|
|
|
run = partial(run, display=False)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DomainTestMixin(object):
|
2014-10-03 14:02:11 +00:00
|
|
|
MASTER_SERVER = os.environ.get('ORCHESTRA_MASTER_SERVER', 'localhost')
|
|
|
|
SLAVE_SERVER = os.environ.get('ORCHESTRA_SLAVE_SERVER', 'localhost')
|
|
|
|
MASTER_SERVER_ADDR = socket.gethostbyname(MASTER_SERVER)
|
|
|
|
SLAVE_SERVER_ADDR = socket.gethostbyname(SLAVE_SERVER)
|
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def setUp(self):
|
2014-10-03 14:02:11 +00:00
|
|
|
djsettings.DEBUG = True
|
2014-05-08 16:59:35 +00:00
|
|
|
super(DomainTestMixin, self).setUp()
|
|
|
|
self.domain_name = 'orchestra%s.lan' % random_ascii(10)
|
|
|
|
self.domain_records = (
|
|
|
|
(Record.MX, '10 mail.orchestra.lan.'),
|
|
|
|
(Record.MX, '20 mail2.orchestra.lan.'),
|
|
|
|
(Record.NS, 'ns1.%s.' % self.domain_name),
|
|
|
|
(Record.NS, 'ns2.%s.' % self.domain_name),
|
|
|
|
)
|
|
|
|
self.domain_update_records = (
|
|
|
|
(Record.MX, '30 mail3.orchestra.lan.'),
|
|
|
|
(Record.MX, '40 mail4.orchestra.lan.'),
|
|
|
|
(Record.NS, 'ns1.%s.' % self.domain_name),
|
|
|
|
(Record.NS, 'ns2.%s.' % self.domain_name),
|
|
|
|
)
|
2014-10-03 14:02:11 +00:00
|
|
|
self.ns1_name = 'ns1.%s' % self.domain_name
|
|
|
|
self.ns1_records = (
|
2014-10-06 14:57:02 +00:00
|
|
|
(Record.A, self.SLAVE_SERVER_ADDR),
|
2014-05-08 16:59:35 +00:00
|
|
|
)
|
2014-10-03 14:02:11 +00:00
|
|
|
self.ns2_name = 'ns2.%s' % self.domain_name
|
|
|
|
self.ns2_records = (
|
2014-10-06 14:57:02 +00:00
|
|
|
(Record.A, self.MASTER_SERVER_ADDR),
|
2014-05-08 16:59:35 +00:00
|
|
|
)
|
2014-10-03 14:02:11 +00:00
|
|
|
self.www_name = 'www.%s' % self.domain_name
|
|
|
|
self.www_records = (
|
2014-05-08 16:59:35 +00:00
|
|
|
(Record.CNAME, 'external.server.org.'),
|
|
|
|
)
|
2014-10-03 14:02:11 +00:00
|
|
|
self.django_domain_name = 'django%s.lan' % random_ascii(10)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
def add_route(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def add(self, domain_name, records):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def delete(self, domain_name, records):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def update(self, domain_name, records):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def validate_add(self, server_addr, domain_name):
|
|
|
|
context = {
|
|
|
|
'domain_name': domain_name,
|
|
|
|
'server_addr': server_addr
|
|
|
|
}
|
|
|
|
dig_soa = 'dig @%(server_addr)s %(domain_name)s SOA|grep "\sSOA\s"'
|
|
|
|
soa = run(dig_soa % context).stdout.split()
|
|
|
|
# testdomain.org. 3600 IN SOA ns.example.com. hostmaster.example.com. 2014021100 86400 7200 2419200 3600
|
|
|
|
self.assertEqual('%(domain_name)s.' % context, soa[0])
|
|
|
|
self.assertEqual('3600', soa[1])
|
|
|
|
self.assertEqual('IN', soa[2])
|
|
|
|
self.assertEqual('SOA', soa[3])
|
|
|
|
self.assertEqual('%s.' % settings.DOMAINS_DEFAULT_NAME_SERVER, soa[4])
|
|
|
|
hostmaster = utils.format_hostmaster(settings.DOMAINS_DEFAULT_HOSTMASTER)
|
|
|
|
self.assertEqual(hostmaster, soa[5])
|
|
|
|
|
|
|
|
dig_ns = 'dig @%(server_addr)s %(domain_name)s NS|grep "\sNS\s"'
|
|
|
|
name_servers = run(dig_ns % context).stdout
|
|
|
|
# testdomain.org. 3600 IN NS ns1.orchestra.lan.
|
|
|
|
ns_records = ['ns1.%s.' % self.domain_name, 'ns2.%s.' % self.domain_name]
|
|
|
|
self.assertEqual(2, len(name_servers.splitlines()))
|
|
|
|
for ns in name_servers.splitlines():
|
|
|
|
ns = ns.split()
|
|
|
|
# testdomain.org. 3600 IN NS ns1.orchestra.lan.
|
|
|
|
self.assertEqual('%(domain_name)s.' % context, ns[0])
|
|
|
|
self.assertEqual('3600', ns[1])
|
|
|
|
self.assertEqual('IN', ns[2])
|
|
|
|
self.assertEqual('NS', ns[3])
|
|
|
|
self.assertIn(ns[4], ns_records)
|
|
|
|
|
|
|
|
dig_mx = 'dig @%(server_addr)s %(domain_name)s MX|grep "\sMX\s"'
|
|
|
|
mail_servers = run(dig_mx % context).stdout
|
|
|
|
for mx in mail_servers.splitlines():
|
|
|
|
mx = mx.split()
|
|
|
|
# testdomain.org. 3600 IN NS ns1.orchestra.lan.
|
|
|
|
self.assertEqual('%(domain_name)s.' % context, mx[0])
|
|
|
|
self.assertEqual('3600', mx[1])
|
|
|
|
self.assertEqual('IN', mx[2])
|
|
|
|
self.assertEqual('MX', mx[3])
|
|
|
|
self.assertIn(mx[4], ['10', '20'])
|
|
|
|
self.assertIn(mx[5], ['mail2.orchestra.lan.', 'mail.orchestra.lan.'])
|
|
|
|
|
|
|
|
def validate_delete(self, server_addr, domain_name):
|
|
|
|
context = {
|
|
|
|
'domain_name': domain_name,
|
|
|
|
'server_addr': server_addr
|
|
|
|
}
|
|
|
|
dig_soa = 'dig @%(server_addr)s %(domain_name)s|grep "\sSOA\s"'
|
2015-05-09 17:08:45 +00:00
|
|
|
soa = run(dig_soa % context, valid_codes=(0, 1)).stdout
|
2014-05-08 16:59:35 +00:00
|
|
|
if soa:
|
|
|
|
soa = soa.split()
|
|
|
|
self.assertEqual('IN', soa[2])
|
|
|
|
self.assertEqual('SOA', soa[3])
|
|
|
|
self.assertNotEqual('%s.' % settings.DOMAINS_DEFAULT_NAME_SERVER, soa[4])
|
|
|
|
hostmaster = utils.format_hostmaster(settings.DOMAINS_DEFAULT_HOSTMASTER)
|
|
|
|
self.assertNotEqual(hostmaster, soa[5])
|
|
|
|
|
|
|
|
def validate_update(self, server_addr, domain_name):
|
|
|
|
context = {
|
|
|
|
'domain_name': domain_name,
|
|
|
|
'server_addr': server_addr
|
|
|
|
}
|
2014-10-10 14:39:46 +00:00
|
|
|
dig_soa = 'dig @%(server_addr)s %(domain_name)s SOA | grep "\sSOA\s"'
|
2014-05-08 16:59:35 +00:00
|
|
|
soa = run(dig_soa % context).stdout.split()
|
|
|
|
# testdomain.org. 3600 IN SOA ns.example.com. hostmaster.example.com. 2014021100 86400 7200 2419200 3600
|
|
|
|
self.assertEqual('%(domain_name)s.' % context, soa[0])
|
|
|
|
self.assertEqual('3600', soa[1])
|
|
|
|
self.assertEqual('IN', soa[2])
|
|
|
|
self.assertEqual('SOA', soa[3])
|
|
|
|
self.assertEqual('%s.' % settings.DOMAINS_DEFAULT_NAME_SERVER, soa[4])
|
|
|
|
hostmaster = utils.format_hostmaster(settings.DOMAINS_DEFAULT_HOSTMASTER)
|
|
|
|
self.assertEqual(hostmaster, soa[5])
|
|
|
|
|
2014-10-10 14:39:46 +00:00
|
|
|
dig_ns = 'dig @%(server_addr)s %(domain_name)s NS |grep "\sNS\s"'
|
2014-05-08 16:59:35 +00:00
|
|
|
name_servers = run(dig_ns % context).stdout
|
|
|
|
ns_records = ['ns1.%s.' % self.domain_name, 'ns2.%s.' % self.domain_name]
|
|
|
|
self.assertEqual(2, len(name_servers.splitlines()))
|
|
|
|
for ns in name_servers.splitlines():
|
|
|
|
ns = ns.split()
|
|
|
|
# testdomain.org. 3600 IN NS ns1.orchestra.lan.
|
|
|
|
self.assertEqual('%(domain_name)s.' % context, ns[0])
|
|
|
|
self.assertEqual('3600', ns[1])
|
|
|
|
self.assertEqual('IN', ns[2])
|
|
|
|
self.assertEqual('NS', ns[3])
|
|
|
|
self.assertIn(ns[4], ns_records)
|
|
|
|
|
2014-10-10 14:39:46 +00:00
|
|
|
dig_mx = 'dig @%(server_addr)s %(domain_name)s MX | grep "\sMX\s"'
|
2014-05-08 16:59:35 +00:00
|
|
|
mx = run(dig_mx % context).stdout.split()
|
|
|
|
# testdomain.org. 3600 IN MX 10 orchestra.lan.
|
|
|
|
self.assertEqual('%(domain_name)s.' % context, mx[0])
|
|
|
|
self.assertEqual('3600', mx[1])
|
|
|
|
self.assertEqual('IN', mx[2])
|
|
|
|
self.assertEqual('MX', mx[3])
|
|
|
|
self.assertIn(mx[4], ['30', '40'])
|
|
|
|
self.assertIn(mx[5], ['mail3.orchestra.lan.', 'mail4.orchestra.lan.'])
|
|
|
|
|
2014-10-10 14:55:48 +00:00
|
|
|
def validate_www_update(self, server_addr, domain_name):
|
|
|
|
context = {
|
|
|
|
'domain_name': domain_name,
|
|
|
|
'server_addr': server_addr
|
|
|
|
}
|
2014-10-10 14:39:46 +00:00
|
|
|
dig_cname = 'dig @%(server_addr)s www.%(domain_name)s CNAME | grep "\sCNAME\s"'
|
2014-05-08 16:59:35 +00:00
|
|
|
cname = run(dig_cname % context).stdout.split()
|
|
|
|
# testdomain.org. 3600 IN MX 10 orchestra.lan.
|
|
|
|
self.assertEqual('www.%(domain_name)s.' % context, cname[0])
|
|
|
|
self.assertEqual('3600', cname[1])
|
|
|
|
self.assertEqual('IN', cname[2])
|
|
|
|
self.assertEqual('CNAME', cname[3])
|
|
|
|
self.assertEqual('external.server.org.', cname[4])
|
|
|
|
|
|
|
|
def test_add(self):
|
2014-10-03 14:02:11 +00:00
|
|
|
self.add(self.ns1_name, self.ns1_records)
|
|
|
|
self.add(self.ns2_name, self.ns2_records)
|
2014-05-08 16:59:35 +00:00
|
|
|
self.add(self.domain_name, self.domain_records)
|
2014-10-17 20:03:41 +00:00
|
|
|
# self.addCleanup(self.delete, self.domain_name)
|
2014-10-03 14:02:11 +00:00
|
|
|
self.validate_add(self.MASTER_SERVER_ADDR, self.domain_name)
|
2014-10-04 13:23:04 +00:00
|
|
|
time.sleep(1)
|
2014-10-03 14:02:11 +00:00
|
|
|
self.validate_add(self.SLAVE_SERVER_ADDR, self.domain_name)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
def test_delete(self):
|
2014-10-03 14:02:11 +00:00
|
|
|
self.add(self.ns1_name, self.ns1_records)
|
|
|
|
self.add(self.ns2_name, self.ns2_records)
|
2014-05-08 16:59:35 +00:00
|
|
|
self.add(self.domain_name, self.domain_records)
|
|
|
|
self.delete(self.domain_name)
|
2014-10-03 14:02:11 +00:00
|
|
|
for name in [self.domain_name, self.ns1_name, self.ns2_name]:
|
|
|
|
self.validate_delete(self.MASTER_SERVER_ADDR, name)
|
|
|
|
self.validate_delete(self.SLAVE_SERVER_ADDR, name)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
def test_update(self):
|
2014-10-03 14:02:11 +00:00
|
|
|
self.add(self.ns1_name, self.ns1_records)
|
|
|
|
self.add(self.ns2_name, self.ns2_records)
|
2014-05-08 16:59:35 +00:00
|
|
|
self.add(self.domain_name, self.domain_records)
|
2014-10-17 20:03:41 +00:00
|
|
|
self.addCleanup(self.delete, self.domain_name)
|
2014-05-08 16:59:35 +00:00
|
|
|
self.update(self.domain_name, self.domain_update_records)
|
2014-10-04 21:31:00 +00:00
|
|
|
time.sleep(0.5)
|
2014-10-03 14:02:11 +00:00
|
|
|
self.validate_update(self.MASTER_SERVER_ADDR, self.domain_name)
|
2014-10-10 14:47:17 +00:00
|
|
|
time.sleep(5)
|
|
|
|
self.validate_update(self.SLAVE_SERVER_ADDR, self.domain_name)
|
2014-10-10 14:55:48 +00:00
|
|
|
self.add(self.www_name, self.www_records)
|
|
|
|
time.sleep(0.5)
|
|
|
|
self.validate_www_update(self.MASTER_SERVER_ADDR, self.domain_name)
|
|
|
|
time.sleep(5)
|
|
|
|
self.validate_www_update(self.SLAVE_SERVER_ADDR, self.domain_name)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
def test_add_add_delete_delete(self):
|
2014-10-03 14:02:11 +00:00
|
|
|
self.add(self.ns1_name, self.ns1_records)
|
|
|
|
self.add(self.ns2_name, self.ns2_records)
|
2014-05-08 16:59:35 +00:00
|
|
|
self.add(self.domain_name, self.domain_records)
|
2014-10-03 14:02:11 +00:00
|
|
|
self.add(self.django_domain_name, self.domain_records)
|
2014-05-08 16:59:35 +00:00
|
|
|
self.delete(self.domain_name)
|
2014-10-03 14:02:11 +00:00
|
|
|
self.validate_add(self.MASTER_SERVER_ADDR, self.django_domain_name)
|
|
|
|
self.validate_add(self.SLAVE_SERVER_ADDR, self.django_domain_name)
|
|
|
|
self.delete(self.django_domain_name)
|
|
|
|
self.validate_delete(self.MASTER_SERVER_ADDR, self.django_domain_name)
|
|
|
|
self.validate_delete(self.SLAVE_SERVER_ADDR, self.django_domain_name)
|
|
|
|
|
|
|
|
def test_bad_creation(self):
|
|
|
|
self.assertRaises((self.rest.ResponseStatusError, AssertionError),
|
|
|
|
self.add, self.domain_name, self.domain_records)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AdminDomainMixin(DomainTestMixin):
|
|
|
|
def setUp(self):
|
|
|
|
super(AdminDomainMixin, self).setUp()
|
|
|
|
self.add_route()
|
|
|
|
self.admin_login()
|
|
|
|
|
|
|
|
def _add_records(self, records):
|
|
|
|
self.selenium.find_element_by_link_text('Add another Record').click()
|
|
|
|
for i, record in zip(range(0, len(records)), records):
|
|
|
|
type, value = record
|
|
|
|
type_input = self.selenium.find_element_by_id('id_records-%d-type' % i)
|
|
|
|
type_select = Select(type_input)
|
|
|
|
type_select.select_by_value(type)
|
|
|
|
value_input = self.selenium.find_element_by_id('id_records-%d-value' % i)
|
|
|
|
value_input.clear()
|
|
|
|
value_input.send_keys(value)
|
|
|
|
return value_input
|
|
|
|
|
2014-10-03 14:02:11 +00:00
|
|
|
@snapshot_on_error
|
2014-05-08 16:59:35 +00:00
|
|
|
def add(self, domain_name, records):
|
2014-10-03 14:02:11 +00:00
|
|
|
add = reverse('admin:domains_domain_add')
|
|
|
|
url = self.live_server_url + add
|
2014-05-08 16:59:35 +00:00
|
|
|
self.selenium.get(url)
|
2014-10-03 14:02:11 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
name = self.selenium.find_element_by_id('id_name')
|
|
|
|
name.send_keys(domain_name)
|
2014-10-03 14:02:11 +00:00
|
|
|
|
|
|
|
account_input = self.selenium.find_element_by_id('id_account')
|
|
|
|
account_select = Select(account_input)
|
|
|
|
account_select.select_by_value(str(self.account.pk))
|
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
value_input = self._add_records(records)
|
|
|
|
value_input.submit()
|
|
|
|
self.assertNotEqual(url, self.selenium.current_url)
|
|
|
|
|
2014-10-03 14:02:11 +00:00
|
|
|
@snapshot_on_error
|
2014-05-08 16:59:35 +00:00
|
|
|
def delete(self, domain_name):
|
|
|
|
domain = Domain.objects.get(name=domain_name)
|
2014-10-07 13:08:59 +00:00
|
|
|
self.admin_delete(domain)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-10-03 14:02:11 +00:00
|
|
|
@snapshot_on_error
|
2014-05-08 16:59:35 +00:00
|
|
|
def update(self, domain_name, records):
|
|
|
|
domain = Domain.objects.get(name=domain_name)
|
2014-10-03 14:02:11 +00:00
|
|
|
change = reverse('admin:domains_domain_change', args=(domain.pk,))
|
|
|
|
url = self.live_server_url + change
|
2014-05-08 16:59:35 +00:00
|
|
|
self.selenium.get(url)
|
|
|
|
value_input = self._add_records(records)
|
|
|
|
value_input.submit()
|
|
|
|
self.assertNotEqual(url, self.selenium.current_url)
|
|
|
|
|
|
|
|
|
|
|
|
class RESTDomainMixin(DomainTestMixin):
|
|
|
|
def setUp(self):
|
|
|
|
super(RESTDomainMixin, self).setUp()
|
|
|
|
self.rest_login()
|
|
|
|
self.add_route()
|
|
|
|
|
2014-10-10 14:39:46 +00:00
|
|
|
@save_response_on_error
|
2014-05-08 16:59:35 +00:00
|
|
|
def add(self, domain_name, records):
|
|
|
|
records = [ dict(type=type, value=value) for type,value in records ]
|
|
|
|
self.rest.domains.create(name=domain_name, records=records)
|
|
|
|
|
2014-10-10 14:39:46 +00:00
|
|
|
@save_response_on_error
|
2014-05-08 16:59:35 +00:00
|
|
|
def delete(self, domain_name):
|
|
|
|
domain = Domain.objects.get(name=domain_name)
|
|
|
|
domain = self.rest.domains.retrieve(id=domain.pk)
|
|
|
|
domain.delete()
|
|
|
|
|
2014-10-10 14:39:46 +00:00
|
|
|
@save_response_on_error
|
2014-05-08 16:59:35 +00:00
|
|
|
def update(self, domain_name, records):
|
|
|
|
records = [ dict(type=type, value=value) for type,value in records ]
|
|
|
|
domains = self.rest.domains.retrieve(name=domain_name)
|
|
|
|
domain = domains.get()
|
|
|
|
domain.update(records=records)
|
|
|
|
|
|
|
|
|
|
|
|
class Bind9BackendMixin(object):
|
|
|
|
DEPENDENCIES = (
|
2015-04-05 10:46:24 +00:00
|
|
|
'orchestra.contrib.orchestration',
|
2014-05-08 16:59:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def add_route(self):
|
2014-10-03 14:02:11 +00:00
|
|
|
master = Server.objects.create(name=self.MASTER_SERVER, address=self.MASTER_SERVER_ADDR)
|
2014-05-08 16:59:35 +00:00
|
|
|
backend = backends.Bind9MasterDomainBackend.get_name()
|
|
|
|
Route.objects.create(backend=backend, match=True, host=master)
|
2014-10-03 14:02:11 +00:00
|
|
|
slave = Server.objects.create(name=self.SLAVE_SERVER, address=self.SLAVE_SERVER_ADDR)
|
2014-05-08 16:59:35 +00:00
|
|
|
backend = backends.Bind9SlaveDomainBackend.get_name()
|
|
|
|
Route.objects.create(backend=backend, match=True, host=slave)
|
|
|
|
|
|
|
|
|
|
|
|
class RESTBind9BackendDomainTest(Bind9BackendMixin, RESTDomainMixin, BaseLiveServerTestCase):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-10-03 14:02:11 +00:00
|
|
|
class AdminBind9BackendDomainTest(Bind9BackendMixin, AdminDomainMixin, BaseLiveServerTestCase):
|
2014-05-08 16:59:35 +00:00
|
|
|
pass
|