From 65e18b670d28ed3c823a6f124eac35f6ea4c5ec1 Mon Sep 17 00:00:00 2001
From: Sankar Ramalingam <sramling@redhat.com>
Date: Sat, 16 Sep 2017 22:25:04 +0530
Subject: [PATCH] Ticket #48085 - CI tests - replication ruvstore

Description: Adding ruvstore tests for replication. Test cases check
if RUV details are backed up correctly and other test case checks, if
memoryruv and databaseruv are synced.

https://pagure.io/389-ds-base/issue/48085

Reviewed by: ?
---
 .../tests/suites/replication/ruvstore_test.py      | 152 +++++++++++++++++++++
 1 file changed, 152 insertions(+)
 create mode 100755 dirsrvtests/tests/suites/replication/ruvstore_test.py

diff --git a/dirsrvtests/tests/suites/replication/ruvstore_test.py b/dirsrvtests/tests/suites/replication/ruvstore_test.py
new file mode 100755
index 0000000..3e1c1d6
--- /dev/null
+++ b/dirsrvtests/tests/suites/replication/ruvstore_test.py
@@ -0,0 +1,152 @@
+# --- BEGIN COPYRIGHT BLOCK ---
+# Copyright (C) 2017 Red Hat, Inc.
+# All rights reserved.
+#
+# License: GPL (version 3 or any later version).
+# See LICENSE for details.
+# --- END COPYRIGHT BLOCK ---
+#
+import os, ldap, logging, pytest
+from lib389.replica import Replicas
+from lib389.idm.user import UserAccounts
+from lib389.topologies import topology_m2 as topo
+from lib389._constants import *
+
+TEST_ENTRY_NAME = 'rep2lusr'
+NEW_RDN_NAME = 'ruvusr'
+RUV_LIST = ['dn: nsuniqueid=ffffffff', 'nsUniqueId: ffffffff', 'nsds50ruv: {replicageneration}',
+            'nsds50ruv: {replica 1 ldap']
+USER_PROPERTIES = {
+        'uid': TEST_ENTRY_NAME,
+        'cn': TEST_ENTRY_NAME,
+        'sn': TEST_ENTRY_NAME,
+        'uidNumber': '1001',
+        'gidNumber': '2001',
+        'userpassword': PASSWORD,
+        'description': 'userdesc',
+        'homeDirectory': '/home/testuser'
+        }
+
+DEBUGGING = os.getenv('DEBUGGING', default=False)
+if DEBUGGING:
+    logging.getLogger(__name__).setLevel(logging.DEBUG)
+else:
+    logging.getLogger(__name__).setLevel(logging.INFO)
+log = logging.getLogger(__name__)
+
+
+def _perform_ldap_operations(topo):
+    """Add a test user, modify description, modrdn user and delete it"""
+
+    users = UserAccounts(topo.ms['master1'], DEFAULT_SUFFIX)
+    log.info('Adding user to master1')
+    tuser = users.create(properties=USER_PROPERTIES)
+    tuser.replace('description', 'newdesc')
+    log.info('Modify RDN of user {}'.format(tuser.dn))
+    try:
+        topo.ms['master1'].modrdn_s(tuser.dn, 'uid={}'.format(NEW_RDN_NAME), 0)
+    except ldap.LDAPError as e:
+        log.fatal('Failed to modrdn entry {}'.format(tuser.dn))
+        raise e
+    tuser = users.get(NEW_RDN_NAME)
+    log.info('Deleting user-{}'.format(tuser.dn))
+    tuser.delete()
+
+
+def _compare_memoryruv_and_databaseruv(topo, operation_type):
+    """Compare the memoryruv and databaseruv for ldap operations"""
+
+    log.info('Checking memory ruv for ldap-{} operation'.format(operation_type))
+    replica = Replicas(topo.ms["master1"])
+    replica_dn = replica.get_dn(DEFAULT_SUFFIX)
+    entry = replica.get_ruv_entry(DEFAULT_SUFFIX)
+    memory_ruv = entry.getValues('nsds50ruv')[1]
+
+    log.info('Checking database ruv for ldap-{} operation'.format(operation_type))
+    entry = topo.ms["master1"].search_s(replica_dn, ldap.SCOPE_BASE, 'objectClass=*', ['nsds50ruv'])
+    database_ruv = entry[0].getValues('nsds50ruv')[1]
+    assert memory_ruv == database_ruv
+
+
+def test_ruv_entry_backup(topo):
+    """Check if db2ldif stores the RUV details in the backup file
+
+    :id: cbe2c473-8578-4caf-ac0a-841140e41e66
+    :feature: Ruvstore
+    :setup: Replication with two masters.
+    :steps: 1. Add user to server.
+            2. Perform ldap modify, modrdn and delete operations.
+            3. Stop the server and backup the database using db2ldif task.
+            4. Start the server and check if correct RUV is stored in the backup file.
+    :expectedresults:
+            1. Add user should PASS.
+            2. Ldap operations should PASS.
+            3. Database backup using db2ldif task should PASS.
+            4. Backup file should contain the correct RUV details.
+    """
+
+    log.info('LDAP operations add, modify, modrdn and delete')
+    _perform_ldap_operations(topo)
+
+    output_file = os.path.join(topo.ms['master1'].get_ldif_dir(), 'master1.ldif')
+    log.info('Stopping the server instance to run db2ldif task')
+    topo.ms['master1'].stop()
+    topo.ms['master1'].db2ldif(bename=DEFAULT_BENAME, suffixes=[DEFAULT_SUFFIX], excludeSuffixes=[],
+            encrypt=False, repl_data=True, outputfile=output_file)
+    log.info('Starting the server after backup')
+    topo.ms['master1'].start()
+    with open(output_file) as fh:
+        contents = fh.read()
+        for ruv in RUV_LIST:
+            if ruv in contents:
+                log.info('RUV entry-{} is matching'.format(ruv))
+            else:
+                log.fatal('Failed to match RUV entry-{}'.format(ruv))
+                assert False
+
+
+def test_memoryruv_sync_with_databaseruv(topo):
+    """Check if memory ruv and database ruv are synced
+
+    :id: 5f38ac5f-6353-460d-bf60-49cafffda5b3
+    :feature: Ruvstore
+    :setup: Replication with two masters.
+    :steps: 1. Add user to server and compare memory ruv and database ruv.
+            2. Modify description of user and compare memory ruv and database ruv.
+            3. Modrdn of user and compare memory ruv and database ruv.
+            4. Delete user and compare memory ruv and database ruv.
+    :expectedresults:
+            1. For add user, the memory ruv and database ruv should be the same.
+            2. For modify operation, the memory ruv and database ruv should be the same.
+            3. For modrdn operation, the memory ruv and database ruv should be the same.
+            4. For delete operation, the memory ruv and database ruv should be the same.
+    """
+
+    log.info('Adding user-{} to master1'.format(TEST_ENTRY_NAME))
+    users = UserAccounts(topo.ms['master1'], DEFAULT_SUFFIX)
+    tuser = users.create(properties=USER_PROPERTIES)
+    _compare_memoryruv_and_databaseruv(topo, 'add')
+
+    log.info('Modify user {} description'.format(TEST_ENTRY_NAME))
+    tuser.replace('description', 'newdesc')
+    _compare_memoryruv_and_databaseruv(topo, 'modify')
+
+    log.info('Modify RDN of user {}'.format(tuser.dn))
+    try:
+        topo.ms['master1'].modrdn_s(tuser.dn, 'uid={}'.format(NEW_RDN_NAME), 0)
+    except ldap.LDAPError as e:
+        log.fatal('Failed to modrdn entry {}'.format(tuser.dn))
+        raise e
+    _compare_memoryruv_and_databaseruv(topo, 'modrdn')
+
+    tuser = users.get(NEW_RDN_NAME)
+    log.info('Delete user-{}'.format(tuser.dn))
+    tuser.delete()
+    _compare_memoryruv_and_databaseruv(topo, 'delete')
+
+
+if __name__ == '__main__':
+    # Run isolated
+    # -s for DEBUG mode
+    CURRENT_FILE = os.path.realpath(__file__)
+    pytest.main('-s {}'.format(CURRENT_FILE))
-- 
2.7.5

