From 8e5149c36651eaded5d06a32fd94e78fc2e3dcb0 Mon Sep 17 00:00:00 2001 From: Florence Blanc-Renaud Date: Thu, 17 Jan 2019 11:10:52 +0100 Subject: [PATCH] ipatests: add test for replica in forward zone Scenario: install a replica with DNS, with the replica part of a forward zone. The replica installation should proceed successfully and avoid trying to add a DNS record for the replica in the forward zone, as the forward zone is not managed by IPA DNS. Test added to nightly definitions. Related to https://pagure.io/freeipa/issue/7369 Reviewed-By: Francois Cami Reviewed-By: Christian Heimes --- .../test_replica_promotion.py | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/ipatests/test_integration/test_replica_promotion.py b/ipatests/test_integration/test_replica_promotion.py index 7fdc12dc4a4269772c77ff543239be49c46d199a..c635d932bc92ed8c0a147379718933aabaae0f16 100644 --- a/ipatests/test_integration/test_replica_promotion.py +++ b/ipatests/test_integration/test_replica_promotion.py @@ -644,3 +644,101 @@ class TestSubCAkeyReplication(IntegrationTest): ssl_cmd = ['openssl', 'x509', '-text', '-in', TEST_CRT_FILE] ssl = replica.run_command(ssl_cmd) assert 'Issuer: CN = {}'.format(self.SUBCA) in ssl.stdout_text + + +def update_etc_hosts(host, ip, old_hostname, new_hostname): + '''Adds or update /etc/hosts + + If /etc/hosts contains an entry for old_hostname, replace it with + new_hostname. + If /etc/hosts did not contain the entry, create one for new_hostname with + the provided ip. + The function makes a backup in /etc/hosts.sav + + :param host the machine on which /etc/hosts needs to be update_dns_records + :param ip the ip address for the new record + :param old_hostname the hostname to replace + :param new_hostname the new hostname to put in /etc/hosts + ''' + # Make a backup + host.run_command(['/usr/bin/cp', + paths.HOSTS, + '%s.sav' % paths.HOSTS]) + contents = host.get_file_contents(paths.HOSTS, encoding='utf-8') + # If /etc/hosts already contains old_hostname, simply replace + pattern = r'^(.*\s){}(\s)'.format(old_hostname) + new_contents, mods = re.subn(pattern, r'\1{}\2'.format(new_hostname), + contents, flags=re.MULTILINE) + # If it didn't contain any entry for old_hostname, just add new_hostname + if mods == 0: + short = new_hostname.split(".", 1)[0] + new_contents = new_contents + "\n{}\t{} {}\n".format(ip, + new_hostname, + short) + host.put_file_contents(paths.HOSTS, new_contents) + + +def restore_etc_hosts(host): + '''Restores /etc/hosts.sav into /etc/hosts + ''' + host.run_command(['/usr/bin/mv', + '%s.sav' % paths.HOSTS, + paths.HOSTS], + raiseonerr=False) + + +class TestReplicaInForwardZone(IntegrationTest): + """ + Pagure Reference: https://pagure.io/freeipa/issue/7369 + + Scenario: install a replica whose name is in a forwarded zone + """ + + forwardzone = 'forward.test' + num_replicas = 1 + + @classmethod + def install(cls, mh): + tasks.install_master(cls.master, setup_dns=True) + + def test_replica_install_in_forward_zone(self): + master = self.master + replica = self.replicas[0] + + # Create a forward zone on the master + master.run_command(['ipa', 'dnsforwardzone-add', self.forwardzone, + '--skip-overlap-check', + '--forwarder', master.config.dns_forwarder]) + + # Configure the client with a name in the forwardzone + r_shortname = replica.hostname.split(".", 1)[0] + r_new_hostname = '{}.{}'.format(r_shortname, + self.forwardzone) + + # Update /etc/hosts on the master with an entry for the replica + # otherwise replica conncheck would fail + update_etc_hosts(master, replica.ip, replica.hostname, + r_new_hostname) + # Remove the replica previous hostname from /etc/hosts + # and add the replica new hostname + # otherwise replica install will complain because + # hostname does not match + update_etc_hosts(replica, replica.ip, replica.hostname, + r_new_hostname) + + try: + # install client with a hostname in the forward zone + tasks.install_client(self.master, replica, + extra_args=['--hostname', r_new_hostname]) + + replica.run_command(['ipa-replica-install', + '--principal', replica.config.admin_name, + '--admin-password', + replica.config.admin_password, + '--setup-dns', + '--forwarder', master.config.dns_forwarder, + '-U']) + finally: + # Restore /etc/hosts on master and replica + restore_etc_hosts(master) + restore_etc_hosts(replica) -- 2.20.1