def test_method_resolve_patched(self, Resolver): resolver = Resolver.return_value resolver.query.return_value = ['1.2.3.4'] dns = DnsUpdate('8.8.8.8', names, ipaddresses) ip = dns._resolve(4) ipaddress.ip_address(ip) self.assertEqual(ip, '1.2.3.4')
def test_method_set_record_updated(self, Resolver, Update, tcp): resolver = Resolver.return_value resolver.query.side_effect = [['1.2.3.4'], ['1.2.3.5']] update = Update.return_value dns = DnsUpdate('127.0.0.1', names, ipaddresses) dns.record_name = 'www' result = dns._set_record('1.2.3.5', 4) update.delete.assert_has_calls([ mock.call('www.example.com.', 'a'), mock.call('www.example.com.', 'aaaa'), ]) update.add.assert_called_with('www.example.com.', 300, 'a', '1.2.3.5') self.assertEqual(tcp.call_args[1]['where'], '127.0.0.1') Update.assert_called() self.assertEqual( result, { 'ip_version': 4, 'new_ip': '1.2.3.5', 'old_ip': '1.2.3.4', 'status': 'UPDATED' }, )
def test_method_set_record_error(self, Resolver, Update, tcp): resolver = Resolver.return_value resolver.query.return_value = ['1.2.3.4'] dns = DnsUpdate('127.0.0.1', names, ipaddresses) dns.record_name = 'www' result = dns._set_record('1.2.3.5', 4) self.assertEqual( result, {'ip_version': 4, 'new_ip': '1.2.3.5', 'old_ip': '1.2.3.4', 'status': 'DNS_SERVER_ERROR'}, )
def delete_dns_record(secret=None, fqdn=None, config=None): if not config: config = get_config() zones = config['zones'] authenticate(secret, config) names = raise_parameter_error(Names, NamesError, zones, fqdn=fqdn) delete = DnsUpdate( nameserver=config['nameserver'], names=names, ) if delete.delete(): return msg('Deleted "{}".'.format(names.fqdn), 'UPDATED') return msg('Deletion not successful "{}".'.format(names.fqdn), 'UNCHANGED')
def test_method_set_record_unchanged(self, Resolver, Update, tcp): resolver = Resolver.return_value resolver.query.return_value = ['1.2.3.4'] update = Update.return_value dns = DnsUpdate('127.0.0.1', names, ipaddresses) dns.record_name = 'www' result = dns._set_record('1.2.3.4', 4) update.delete.assert_not_called() update.add.assert_not_called() self.assertEqual( result, {'ip_version': 4, 'new_ip': '1.2.3.4', 'old_ip': '1.2.3.4', 'status': 'UNCHANGED'}, )
def update_dns_record(secret=None, fqdn=None, zone_name=None, record_name=None, ip_1=None, ip_2=None, ipv4=None, ipv6=None, ttl=None, config=None): """ Update a DNS record. :param str secret: A password like secret string. The secret string has to be at least 8 characters long and only alphnumeric characters are allowed. :param str fqdn: The Fully-Qualified Domain Name (e. g. ``www.example.com``). If you specify the argument ``fqdn``, you don’t have to specify the arguments ``zone_name`` and ``record_name``. :param str zone_name: The zone name (e. g. ``example.com``). You have to specify the argument ``record_name``. :param str record_name: The record name (e. g. ``www``). You have to specify the argument ``zone_name``. :param str ip_1: A IP address, can be version 4 or version 6. :param str ip_2: A second IP address, can be version 4 or version 6. Must be a different version than ``ip_1``. :param str ipv4: A IP address version 4. :param str ipv6: A IP address version 6. :param int ttl: Time to live. :param dict config: The configuration in the Python dictionary format (as returned by the function ``validate_config()``). """ if not config: config = get_config() zones = config['zones'] authenticate(secret, config) names = raise_parameter_error(Names, NamesError, zones, fqdn=fqdn, zone_name=zone_name, record_name=record_name) ip_addresses = raise_parameter_error(IpAddressContainer, IpAddressesError, ip_1=ip_1, ip_2=ip_2, ipv4=ipv4, ipv6=ipv6, request=flask.request) update = DnsUpdate( nameserver=config['nameserver'], names=names, ipaddresses=ip_addresses, ttl=ttl, ) results = update.update() messages = [] for result in results: message = 'fqdn: {} old_ip: {} new_ip: {}'.format( names.fqdn, result['old_ip'], result['new_ip'], ) messages.append(msg(message, result['status'])) return ''.join(messages)
def test_method_resolve_unpatched(self): _names = copy.deepcopy(names) _names.zone_name = 'google.com.' dns = DnsUpdate('8.8.8.8', _names, ipaddresses) ip = dns._resolve(4) ipaddress.ip_address(ip)
def test_method_convert_record_type(self): self.assertEqual(DnsUpdate._convert_record_type(4), 'a') self.assertEqual(DnsUpdate._convert_record_type(6), 'aaaa')
def test_method_build_tsigkeyring(self): du = DnsUpdate('127.0.0.1', names, ipaddresses) result = du._build_tsigkeyring(du.names.zone_name, du.names.tsig_key) for zone, tsig_key in result.items(): self.assertEqual(str(zone), 'example.com.') self.assertEqual(tsig_key, b'\xb4\xfc\xafd')