From dba7707183d58bc2d7b8544efdb799b1dd2f5f9c Mon Sep 17 00:00:00 2001
From: "Thierry bordaz (tbordaz)" <tbordaz@redhat.com>
Date: Fri, 21 Feb 2014 09:50:26 +0100
Subject: [PATCH] Ticket 47699 - test case

Bug Description:

Fix Description:

<ticket URL>

Reviewed by: ?

Platforms tested: <plat>

Flag Day: no

Doc impact: no
---
 dirsrvtests/tickets/ticket47699_test.py | 210 ++++++++++++++++++++++++++++++++
 1 file changed, 210 insertions(+)
 create mode 100644 dirsrvtests/tickets/ticket47699_test.py

diff --git a/dirsrvtests/tickets/ticket47699_test.py b/dirsrvtests/tickets/ticket47699_test.py
new file mode 100644
index 0000000..c4033a7
--- /dev/null
+++ b/dirsrvtests/tickets/ticket47699_test.py
@@ -0,0 +1,210 @@
+import os
+import sys
+import time
+import ldap
+import logging
+import socket
+import time
+import logging
+import re
+import pytest
+from lib389 import DirSrv, Entry, tools
+from lib389.tools import DirSrvTools
+from lib389._constants import *
+from lib389.properties import *
+from constants import *
+
+log = logging.getLogger(__name__)
+
+installation_prefix = None
+
+PRECEDENCE_VALUE  = '33'
+PLUGIN_REGISTERED = 'referint preop plugin'
+
+class TopologyStandalone(object):
+    def __init__(self, standalone):
+        standalone.open()
+        self.standalone = standalone
+
+
+@pytest.fixture(scope="module")
+def topology(request):
+    '''
+        This fixture is used to standalone topology for the 'module'.
+        At the beginning, It may exists a standalone instance.
+        It may also exists a backup for the standalone instance.
+    
+        Principle:
+            If standalone instance exists:
+                restart it
+            If backup of standalone exists:
+                create/rebind to standalone
+
+                restore standalone instance from backup
+            else:
+                Cleanup everything
+                    remove instance
+                    remove backup
+                Create instance
+                Create backup
+    '''
+    global installation_prefix
+
+    if installation_prefix:
+        args_instance[SER_DEPLOYED_DIR] = installation_prefix
+    
+    standalone = DirSrv(verbose=False)
+    
+    # Args for the standalone instance
+    args_instance[SER_HOST] = HOST_STANDALONE
+    args_instance[SER_PORT] = PORT_STANDALONE
+    args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
+    args_standalone = args_instance.copy()
+    standalone.allocate(args_standalone)
+        
+    # Get the status of the backups
+    backup_standalone = standalone.checkBackupFS()
+    
+    # Get the status of the instance and restart it if it exists
+    instance_standalone   = standalone.exists()
+    if instance_standalone:
+        # assuming the instance is already stopped, just wait 5 sec max
+        standalone.stop(timeout=5)
+        standalone.start(timeout=10)
+    
+    if backup_standalone:
+        # The backup exist, assuming it is correct 
+        # we just re-init the instance with it
+        if not instance_standalone:
+            standalone.create()
+            # Used to retrieve configuration information (dbdir, confdir...)
+            standalone.open()
+        
+        # restore standalone instance from backup
+        standalone.stop(timeout=10)
+        standalone.restoreFS(backup_standalone)
+        standalone.start(timeout=10)
+        
+    else:
+        # We should be here only in two conditions
+        #      - This is the first time a test involve standalone instance
+        #      - Something weird happened (instance/backup destroyed)
+        #        so we discard everything and recreate all
+        
+        # Remove the backup. So even if we have a specific backup file
+        # (e.g backup_standalone) we clear backup that an instance may have created
+        if backup_standalone:
+            standalone.clearBackupFS()
+        
+        # Remove the instance
+        if instance_standalone:
+            standalone.delete()
+            
+        # Create the instance
+        standalone.create()
+        
+        # Used to retrieve configuration information (dbdir, confdir...)
+        standalone.open()
+                
+        # Time to create the backups
+        standalone.stop(timeout=10)
+        standalone.backupfile = standalone.backupFS()
+        standalone.start(timeout=10)
+    
+    # 
+    # Here we have standalone instance up and running
+    # Either coming from a backup recovery
+    # or from a fresh (re)init
+    # Time to return the topology
+    return TopologyStandalone(standalone)
+
+
+def test_ticket47699_init(topology):
+    """
+        Enable plugin log for Error log
+        
+    """
+
+    
+    # enable acl error logging
+    mod = [(ldap.MOD_REPLACE, 'nsslapd-errorlog-level', '65536')]
+    topology.standalone.modify_s(DN_CONFIG, mod)
+    
+
+def test_ticket47699_set_precedence(topology):
+    '''
+        Referential integrity plugin register a plugin callback PLUGIN_REGISTERED
+        (call slapi_register_plugin)
+        so use it to check that the precedence is propagated.
+        This test case:
+            - set the precedence to PRECEDENCE_VALUE (33)
+            - Enable the plugin
+            - restart the server
+    '''
+    ents = topology.standalone.plugins.list(name=PLUGIN_REFER_INTEGRITY)
+    assert len(ents) == 1
+    
+    topology.standalone.plugins.setProperties(plugin_dn=ents[0].dn, properties={PLUGIN_PRECEDENCE: PRECEDENCE_VALUE})
+    topology.standalone.plugins.enable(name=PLUGIN_REFER_INTEGRITY)
+    topology.standalone.stop(timeout=120)
+    time.sleep(1)
+    topology.standalone.start(timeout=120)
+
+    
+def test_ticket47699_check_precedence(topology):
+    '''
+        After restart, look into the error log, the precedence applied to
+        PLUGIN_REGISTERED.
+        It should be the one defined for 'Referential integrity plugin': 33
+    '''
+    logfp = open(topology.standalone.errlog, 'r')
+    logfp.seek(0, os.SEEK_SET)
+    
+    # find in the error log the 
+    found = False
+    pattern = '%s (precedence: ' % PLUGIN_REGISTERED
+    line = logfp.readline()
+    while line:
+        if line.find(pattern) >= 0:
+            found = True
+            break
+        line = logfp.readline()
+    
+    assert found
+    
+    # now get the precedence value and verify its value
+    str_match = '.*referint preop plugin \(precedence: ([0-9]+)'
+    match = re.match(str_match, line)
+    assert len(match.groups()) == 1
+    precedence = match.group(1)
+    assert PRECEDENCE_VALUE == precedence
+    
+
+def test_ticket47699_final(topology):
+    topology.standalone.stop(timeout=10)
+    
+
+
+def run_isolated():
+    '''
+        run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
+        To run isolated without py.test, you need to 
+            - edit this file and comment '@pytest.fixture' line before 'topology' function.
+            - set the installation prefix
+            - run this program
+    '''
+    global installation_prefix
+    installation_prefix =  None
+        
+    topo = topology(True)
+    test_ticket47699_init(topo)
+
+    test_ticket47699_set_precedence(topo)
+    test_ticket47699_check_precedence(topo)
+    
+    test_ticket47699_final(topo)
+
+
+if __name__ == '__main__':
+    run_isolated()
+
-- 
1.7.11.7

