pgreco / rpms / ipa

Forked from forks/areguera/rpms/ipa 4 years ago
Clone

Blame SOURCES/0059-ipatests-add-test-for-replica-in-forward-zone.patch

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