From caf903b94d3cec1e79f995107304011c1b569eee Mon Sep 17 00:00:00 2001
From: Ludwig Krispenz <lkrispen@redhat.com>
Date: Fri, 17 Apr 2015 14:39:42 +0200
Subject: [PATCH] testcase for ticket 48149

---
 dirsrvtests/tickets/ticket48149_test.py | 198 ++++++++++++++++++++++++++++++++
 1 file changed, 198 insertions(+)
 create mode 100644 dirsrvtests/tickets/ticket48149_test.py

diff --git a/dirsrvtests/tickets/ticket48149_test.py b/dirsrvtests/tickets/ticket48149_test.py
new file mode 100644
index 0000000..d1e2757
--- /dev/null
+++ b/dirsrvtests/tickets/ticket48149_test.py
@@ -0,0 +1,198 @@
+import os
+import sys
+import time
+import ldap
+import logging
+import pytest
+from lib389 import DirSrv, Entry, tools, tasks
+from lib389.tools import DirSrvTools
+from lib389._constants import *
+from lib389.properties import *
+from lib389.tasks import *
+from lib389.utils import *
+
+logging.getLogger(__name__).setLevel(logging.DEBUG)
+log = logging.getLogger(__name__)
+
+installation1_prefix = None
+
+backends = [('more', 'dc=more,'+DEFAULT_SUFFIX),
+             ('moremore', 'dc=moremore,'+DEFAULT_SUFFIX),
+             ('moremoremore', 'dc=moremoremore,'+DEFAULT_SUFFIX),
+             ('sub', 'dc=sub,'+DEFAULT_SUFFIX),
+             ('subsub', 'dc=sub2,'+DEFAULT_SUFFIX),
+             ('subsubsub', 'dc=sub3,'+DEFAULT_SUFFIX)]
+
+extrabackend = [('extralongsubsufffixbackendname','dc=extra,'+DEFAULT_SUFFIX)]
+allbackends = backends  + extrabackend
+
+userattrs = ['uid',
+             'givenName',
+             'sn',
+             'cn',
+             'mail',
+             'homePhone',
+             'mobile',
+             'street',
+             'l',
+             'st',
+             'userpassword']
+
+addindexattrs = ['mail',
+             'homePhone',
+             'mobile',
+             'street',
+             'l',
+             'st',
+             'userpassword']
+
+class TopologyStandalone(object):
+    def __init__(self, standalone):
+        standalone.open()
+        self.standalone = standalone
+
+
+@pytest.fixture(scope="module")
+def topology(request):
+    global installation1_prefix
+    if installation1_prefix:
+        args_instance[SER_DEPLOYED_DIR] = installation1_prefix
+
+    # Creating standalone instance ...
+    standalone = DirSrv(verbose=False)
+    args_instance[SER_HOST] = HOST_STANDALONE
+    args_instance[SER_PORT] = PORT_STANDALONE
+    args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
+    args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
+    args_standalone = args_instance.copy()
+    standalone.allocate(args_standalone)
+    instance_standalone = standalone.exists()
+    if instance_standalone:
+        standalone.delete()
+    standalone.create()
+    standalone.open()
+
+    # Clear out the tmp dir
+    standalone.clearTmpDir(__file__)
+
+    return TopologyStandalone(standalone)
+
+def init_backend(topology,backend_name,suffix_name):
+    '''
+    Create a suffix and populate with a few entries
+    '''
+
+    topology.standalone.backend.create(suffix_name, {BACKEND_NAME: backend_name})
+    topology.standalone.mappingtree.create(suffix_name, bename=backend_name, parent=DEFAULT_SUFFIX)
+    for attr in addindexattrs:
+        args = {INDEX_TYPE: 'eq'}
+        topology.standalone.index.create(suffix=suffix_name, attr=attr, args=args)
+        args = {TASK_WAIT: True}
+        rc = topology.standalone.tasks.reindex(suffix=suffix_name, attrname=attr, args=args)
+
+    try:
+        topology.standalone.add_s(Entry((suffix_name, {
+                                        'objectclass': "top domain".split()})))
+        topology.standalone.add_s(Entry(('ou=people,'+suffix_name, {
+                                        'objectclass': "top organizationalunit".split(),
+                                        'ou': 'people'})))
+        topology.standalone.add_s(Entry(('uid=user.9999,ou=people,'+suffix_name, {
+                                        'objectclass': "top person organizationalPerson inetOrgPerson".split(),
+                                        'givenName': 'Charlene',
+                                        'sn': 'Raynard',
+                                        'cn': 'Charlene Raynard',
+                                        'initials': 'CR',
+                                        'uid': 'user.9999',
+                                        'mail': 'user.9999@example.com',
+                                        'telephoneNumber': '299-686-4462',
+                                        'homePhone': '629-654-0785',
+                                        'pager': '761-299-9105',
+                                        'mobile': '625-242-3739',
+                                        'street': '13289 Forest Street',
+                                        'l': 'Lubbock',
+                                        'st': 'FL',
+                                        'postalCode': '25112',
+                                        'postalAddress': 'Charlene Raynard$13289 Forest Street$Lubbock, FL  25112',
+                                        'description': 'This is the description for Charlene Raynard.',
+                                        'userPassword': 'password'})))
+
+    except ldap.LDAPError, e:
+        log.error('Failed to add %s: %s' % (USER1_DN, e.message['desc']))
+        assert False
+
+def simple_search(topology, suffix_name, attr, value='hello', async=False):
+    '''
+    execute a search for a given attr to touch the index
+    '''
+    if async:
+        msgid = topology.standalone.search(suffix_name, ldap.SCOPE_SUBTREE, "(%s=%s)" % (attr,value))
+    else: 
+        ents = topology.standalone.search_s(suffix_name, ldap.SCOPE_SUBTREE, "(%s=%s)" % (attr,value))
+        msgid = 0
+    return msgid
+
+def test_ticket48149_startup(topology):
+    '''
+    Initialize backends and config
+    '''
+    for (backend, suffix) in allbackends:
+        log.info('Initializing backend %s for suffix %s' % (backend,suffix))
+        init_backend(topology, backend, suffix)
+        
+    try:
+        topology.standalone.modify_s('cn=config', [(ldap.MOD_REPLACE, 'nsslapd-maxthreadsperconn', '20')])
+    except ldap.LDAPError, e:
+        log.error('Failed to modify cn=config: error ' + e.message['desc'])
+        assert False
+
+
+
+def test_ticket48149_run(topology):
+    '''
+    Try to execute cn=monitor searches and indexed
+    searches which will open an index file
+    in parallel
+    '''
+
+    # step 1 - restart server
+    topology.standalone.restart(timeout=120)
+
+    # step 2 - search for all attrs, except one to 
+    # have all index files open
+    for (backend, suffix) in backends:
+        log.info('Initial searches for suffix %s' % suffix)
+        for attr in userattrs:
+            simple_search(topology,suffix,attr)
+
+    # setp 3 run async index searches for cn=monitor
+    #        and attributes in backend with extralong name
+    msgid = {}
+    for (backend, suffix) in extrabackend:
+        for  attr in userattrs:
+            msgid[attr] = simple_search(topology,'cn=config','cn',value='monitor',async=True)
+            simple_search(topology,suffix,attr,async=True)
+
+    # step 4 wait for result of cn=monitor search
+    for  attr in userattrs:
+        rtype, rdata, rmsgid = topology.standalone.result2(msgid[attr])
+        log.info("%d results" % len(rdata))
+    log.info('Test complete')
+
+def test_ticket48149_final(topology):
+    topology.standalone.delete()
+    log.info('Testcase PASSED')
+
+
+def run_isolated():
+    global installation1_prefix
+    installation1_prefix = None
+
+    topo = topology(True)
+    test_ticket48149_startup(topo)
+    test_ticket48149_run(topo)
+    test_ticket48149_final(topo)
+
+
+if __name__ == '__main__':
+    run_isolated()
+
-- 
1.7.11.7

