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