From 58af7284915a94bafb6c675fe50046ad5bb81a90 Mon Sep 17 00:00:00 2001
From: "Thierry bordaz (tbordaz)" <tbordaz@redhat.com>
Date: Thu, 12 Dec 2013 17:45:24 +0100
Subject: [PATCH] Ticket 47628:  port testcases to new DirSrv interface

Bug Description:
	After ticket https://fedorahosted.org/389/ticket/47625, the DirSrv interface changed.
	The test cases needs to be ported to this new interface

Fix Description:

https://fedorahosted.org/389/ticket/47628

Reviewed by: ?

Platforms tested: <plat>

Flag Day: no

Doc impact: no
---
 dirsrvtests/tickets/constants.py        |  37 ++++++-----
 dirsrvtests/tickets/finalizer.py        |  37 +++--------
 dirsrvtests/tickets/ticket47490_test.py | 112 ++++++++++++++++----------------
 dirsrvtests/tickets/ticket47560_test.py |  78 ++++++++++------------
 4 files changed, 122 insertions(+), 142 deletions(-)

diff --git a/dirsrvtests/tickets/constants.py b/dirsrvtests/tickets/constants.py
index 44b9b3a..f4fe8fc 100644
--- a/dirsrvtests/tickets/constants.py
+++ b/dirsrvtests/tickets/constants.py
@@ -5,11 +5,13 @@ Created on Oct 31, 2013
 '''
 import os
 from lib389 import DN_DM
+from lib389._constants import *
+from lib389.properties import *
 
-LOCALHOST = "localhost.localdomain"
 SUFFIX    = 'dc=example,dc=com'
 PASSWORD  = 'password'
 
+
 # Used for standalone topology
 HOST_STANDALONE = LOCALHOST
 PORT_STANDALONE = 33389
@@ -45,22 +47,23 @@ PORT_CONSUMER_2 = 55389
 SERVERID_CONSUMER_2 = 'consumer_2'
 
 # Each defined instance above must be added in that list 
-ALL_INSTANCES = [ {'host': HOST_STANDALONE, 'port': PORT_STANDALONE, 'serverid': SERVERID_STANDALONE},
-                  {'host': HOST_MASTER,     'port': PORT_MASTER,     'serverid': SERVERID_MASTER},
-                  {'host': HOST_CONSUMER,   'port': PORT_CONSUMER,   'serverid': SERVERID_CONSUMER},
-                  {'host': HOST_MASTER_1,   'port': PORT_MASTER_1,   'serverid': SERVERID_MASTER_1},
-                  {'host': HOST_MASTER_2,   'port': PORT_MASTER_2,   'serverid': SERVERID_MASTER_2},
-                  {'host': HOST_CONSUMER_1, 'port': PORT_CONSUMER_1, 'serverid': SERVERID_CONSUMER_1},
-                  {'host': HOST_CONSUMER_2, 'port': PORT_CONSUMER_2, 'serverid': SERVERID_CONSUMER_2},
+ALL_INSTANCES = [ {SER_HOST: HOST_STANDALONE, SER_PORT: PORT_STANDALONE, SER_SERVERID_PROP: SERVERID_STANDALONE},
+                  {SER_HOST: HOST_MASTER,     SER_PORT: PORT_MASTER,     SER_SERVERID_PROP: SERVERID_MASTER},
+                  {SER_HOST: HOST_CONSUMER,   SER_PORT: PORT_CONSUMER,   SER_SERVERID_PROP: SERVERID_CONSUMER},
+                  {SER_HOST: HOST_MASTER_1,   SER_PORT: PORT_MASTER_1,   SER_SERVERID_PROP: SERVERID_MASTER_1},
+                  {SER_HOST: HOST_MASTER_2,   SER_PORT: PORT_MASTER_2,   SER_SERVERID_PROP: SERVERID_MASTER_2},
+                  {SER_HOST: HOST_CONSUMER_1, SER_PORT: PORT_CONSUMER_1, SER_SERVERID_PROP: SERVERID_CONSUMER_1},
+                  {SER_HOST: HOST_CONSUMER_2, SER_PORT: PORT_CONSUMER_2, SER_SERVERID_PROP: SERVERID_CONSUMER_2},
                  ]
 # This is a template
 args_instance = {
-                   'prefix': os.environ.get('PREFIX', None),
-                   'backupdir': os.environ.get('BACKUPDIR', "/tmp"),
-                   'newrootdn': DN_DM,
-                   'newrootpw': PASSWORD,
-                   'newhost': LOCALHOST,
-                   'newport': 389,
-                   'newinstance': "template",
-                   'newsuffix': SUFFIX,
-                   'no_admin': True}
+                   SER_DEPLOYED_DIR: os.environ.get('PREFIX', None),
+                   SER_BACKUP_INST_DIR: os.environ.get('BACKUPDIR', DEFAULT_BACKUPDIR),
+                   SER_ROOT_DN: DN_DM,
+                   SER_ROOT_PW: PASSWORD,
+                   SER_HOST: LOCALHOST,
+                   SER_PORT: DEFAULT_PORT,
+                   SER_SERVERID_PROP: "template",
+                   SER_CREATION_SUFFIX: DEFAULT_SUFFIX}
+
+
diff --git a/dirsrvtests/tickets/finalizer.py b/dirsrvtests/tickets/finalizer.py
index 23356dd..72e0c0f 100644
--- a/dirsrvtests/tickets/finalizer.py
+++ b/dirsrvtests/tickets/finalizer.py
@@ -15,46 +15,27 @@ import pytest
 from lib389 import DirSrv, Entry, tools
 from lib389.tools import DirSrvTools
 from lib389._constants import DN_DM
+from lib389.properties import *
 from constants import *
 
 log = logging.getLogger(__name__)
 
 global installation_prefix
-
-
-def _remove_instance(args):
-    
-    # check the instance parameters
-    args_instance['newhost'] = args.get('host', None)
-    if not args_instance['newhost']:
-        raise ValueError("host not defined")
-    
-    args_instance['newport'] = args.get('port', None)
-    if not args_instance['newport']:
-        raise ValueError("port not defined")
-    
-    args_instance['newinstance'] = args.get('serverid', None)
-    if not args_instance['newinstance']:
-        raise ValueError("serverid not defined")
-
-    args_instance['prefix'] = args.get('prefix', None)
-
-    # Get the status of the instance and remove it if it exists
-    instance   = DirSrvTools.existsInstance(args_instance)
-    if instance:
-        log.debug("_remove_instance %s %s:%d" % (instance.serverId, instance.host, instance.port))
-        DirSrvTools.removeInstance(instance)
-
+installation_prefix=os.getenv('PREFIX')
 
 def test_finalizer():
     global installation_prefix
     
     # for each defined instance, remove it
-    for instance in ALL_INSTANCES:
+    for args_instance in ALL_INSTANCES:
         if installation_prefix:
             # overwrite the environment setting
-            instance['prefix'] = installation_prefix
-        _remove_instance(instance)
+            args_instance[SER_DEPLOYED_DIR] = installation_prefix
+            
+        instance = DirSrv(verbose=True)
+        instance.allocate(args_instance)
+        if instance.exists():
+            instance.delete()
         
 def run_isolated():
     '''
diff --git a/dirsrvtests/tickets/ticket47490_test.py b/dirsrvtests/tickets/ticket47490_test.py
index 4fa8ffd..7e59a84 100644
--- a/dirsrvtests/tickets/ticket47490_test.py
+++ b/dirsrvtests/tickets/ticket47490_test.py
@@ -16,6 +16,7 @@ import re
 from lib389 import DirSrv, Entry, tools
 from lib389.tools import DirSrvTools
 from lib389._constants import *
+from lib389.properties import *
 from constants import *
 
 logging.getLogger(__name__).setLevel(logging.DEBUG)
@@ -30,27 +31,14 @@ MUST_NEW = "(postalAddress $ preferredLocale $ telexNumber)"
 MAY_OLD  = "(postalCode $ street)"
 MAY_NEW  = "(postalCode $ street $ postOfficeBox)"
 
-def _ds_create_instance(args):
-    # create the standalone instance
-    return tools.DirSrvTools.createInstance(args, verbose=False)
-
-def _ds_rebind_instance(dirsrv):
-    args_instance['prefix']      = dirsrv.prefix
-    args_instance['backupdir']   = dirsrv.backupdir
-    args_instance['newrootdn']   = dirsrv.binddn
-    args_instance['newrootpw']   = dirsrv.bindpw
-    args_instance['newhost']     = dirsrv.host
-    args_instance['newport']     = dirsrv.port
-    args_instance['newinstance'] = dirsrv.serverId
-    args_instance['newsuffix']   = SUFFIX
-    args_instance['no_admin']    = True
-
-    return tools.DirSrvTools.createInstance(args_instance)
 
 class TopologyMasterConsumer(object):
     def __init__(self, master, consumer):
-        self.master = _ds_rebind_instance(master)
-        self.consumer = _ds_rebind_instance(consumer)
+        master.open()
+        self.master = master
+        
+        consumer.open()
+        self.consumer = consumer
 
 def pattern_errorlog(file, log_pattern):
     try:
@@ -155,51 +143,63 @@ def topology(request):
     global installation_prefix
 
     if installation_prefix:
-        args_instance['prefix'] = installation_prefix
+        args_instance[SER_DEPLOYED_DIR] = installation_prefix
+        
+    master   = DirSrv(verbose=False)
+    consumer = DirSrv(verbose=False)
     
     # Args for the master instance
-    args_instance['newhost'] = HOST_MASTER
-    args_instance['newport'] = PORT_MASTER
-    args_instance['newinstance'] = SERVERID_MASTER
+    args_instance[SER_HOST] = HOST_MASTER
+    args_instance[SER_PORT] = PORT_MASTER
+    args_instance[SER_SERVERID_PROP] = SERVERID_MASTER
     args_master = args_instance.copy()
+    master.allocate(args_master)
     
     # Args for the consumer instance
-    args_instance['newhost'] = HOST_CONSUMER
-    args_instance['newport'] = PORT_CONSUMER
-    args_instance['newinstance'] = SERVERID_CONSUMER
+    args_instance[SER_HOST] = HOST_CONSUMER
+    args_instance[SER_PORT] = PORT_CONSUMER
+    args_instance[SER_SERVERID_PROP] = SERVERID_CONSUMER
     args_consumer = args_instance.copy()
+    consumer.allocate(args_consumer)
 
     
     # Get the status of the backups
-    backup_master   = DirSrvTools.existsBackup(args_master)
-    backup_consumer = DirSrvTools.existsBackup(args_consumer)
+    backup_master   = master.checkBackupFS()
+    backup_consumer = consumer.checkBackupFS()
     
     # Get the status of the instance and restart it if it exists
-    instance_master   = DirSrvTools.existsInstance(args_master)
+    instance_master   = master.exists()
     if instance_master:
-        DirSrvTools.stop(instance_master, timeout=10)
-        DirSrvTools.start(instance_master, timeout=10)
+        master.stop(timeout=10)
+        master.start(timeout=10)
         
-    instance_consumer = DirSrvTools.existsInstance(args_consumer)
+    instance_consumer = consumer.exists()
     if instance_consumer:
-        DirSrvTools.stop(instance_consumer, timeout=10)
-        DirSrvTools.start(instance_consumer, timeout=10)
+        consumer.stop(timeout=10)
+        consumer.start(timeout=10)
     
     if backup_master and backup_consumer:
         # The backups exist, assuming they are correct 
         # we just re-init the instances with them
-        master   = _ds_create_instance(args_master)
-        consumer = _ds_create_instance(args_consumer)
+        if not instance_master:
+            master.create()
+            # Used to retrieve configuration information (dbdir, confdir...)
+            master.open()
+        
+        if not instance_consumer:
+            consumer.create()
+            # Used to retrieve configuration information (dbdir, confdir...)
+            consumer.open()
         
         # restore master from backup
-        DirSrvTools.stop(master, timeout=10)
-        DirSrvTools.instanceRestoreFS(master, backup_master)
-        DirSrvTools.start(master, timeout=10)
+        master.stop(timeout=10)
+        master.restoreFS(backup_master)
+        master.start(timeout=10)
         
         # restore consumer from backup
-        DirSrvTools.stop(consumer, timeout=10)
-        DirSrvTools.instanceRestoreFS(consumer, backup_consumer)
-        DirSrvTools.start(consumer, timeout=10)
+        consumer.stop(timeout=10)
+        consumer.restoreFS(backup_consumer)
+        consumer.start(timeout=10)
     else:
         # We should be here only in two conditions
         #      - This is the first time a test involve master-consumer
@@ -210,19 +210,21 @@ def topology(request):
         # Remove all the backups. So even if we have a specific backup file
         # (e.g backup_master) we clear all backups that an instance my have created
         if backup_master:
-            DirSrvTools.clearInstanceBackupFS(dirsrv=instance_master)
+            master.clearBackupFS()
         if backup_consumer:
-            DirSrvTools.clearInstanceBackupFS(dirsrv=instance_consumer)
+            consumer.clearBackupFS()
         
         # Remove all the instances
         if instance_master:
-            DirSrvTools.removeInstance(instance_master)
+            master.delete()
         if instance_consumer:
-            DirSrvTools.removeInstance(instance_consumer)
-            
-        # Create the instance
-        master   = _ds_create_instance(args_master)
-        consumer = _ds_create_instance(args_consumer)
+            consumer.delete()
+                        
+        # Create the instances
+        master.create()
+        master.open()
+        consumer.create()
+        consumer.open()
     
         # 
         # Now prepare the Master-Consumer topology
@@ -258,13 +260,13 @@ def topology(request):
                 loop += 1
                 
         # Time to create the backups
-        DirSrvTools.stop(master, timeout=10)
-        master.backupfile = DirSrvTools.instanceBackupFS(master)
-        DirSrvTools.start(master, timeout=10)
+        master.stop(timeout=10)
+        master.backupfile = master.backupFS()
+        master.start(timeout=10)
         
-        DirSrvTools.stop(consumer, timeout=10)
-        consumer.backupfile = DirSrvTools.instanceBackupFS(consumer)
-        DirSrvTools.start(consumer, timeout=10)
+        consumer.stop(timeout=10)
+        consumer.backupfile = consumer.backupFS()
+        consumer.start(timeout=10)
     
     # 
     # Here we have two instances master and consumer
diff --git a/dirsrvtests/tickets/ticket47560_test.py b/dirsrvtests/tickets/ticket47560_test.py
index 1561f21..c11233c 100644
--- a/dirsrvtests/tickets/ticket47560_test.py
+++ b/dirsrvtests/tickets/ticket47560_test.py
@@ -10,32 +10,17 @@ 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
 
-def _ds_create_instance(args):
-    # create the standalone instance
-    return tools.DirSrvTools.createInstance(args, verbose=False)
-
-def _ds_rebind_instance(dirsrv):
-    args_instance['prefix']      = dirsrv.prefix
-    args_instance['backupdir']   = dirsrv.backupdir
-    args_instance['newrootdn']   = dirsrv.binddn
-    args_instance['newrootpw']   = dirsrv.bindpw
-    args_instance['newhost']     = dirsrv.host
-    args_instance['newport']     = dirsrv.port
-    args_instance['newinstance'] = dirsrv.serverId
-    args_instance['newsuffix']   = SUFFIX
-    args_instance['no_admin']    = True
-
-    return tools.DirSrvTools.createInstance(args_instance)
-
 class TopologyStandalone(object):
     def __init__(self, standalone):
-        self.standalone = _ds_rebind_instance(standalone)
+        standalone.open()
+        self.standalone = standalone
 
 
 @pytest.fixture(scope="module")
@@ -49,7 +34,7 @@ def topology(request):
             If standalone instance exists:
                 restart it
             If backup of standalone exists:
-                create or rebind to standalone
+                create/rebind to standalone
 
                 restore standalone instance from backup
             else:
@@ -62,33 +47,39 @@ def topology(request):
     global installation_prefix
 
     if installation_prefix:
-        args_instance['prefix'] = installation_prefix
+        args_instance[SER_DEPLOYED_DIR] = installation_prefix
+    
+    standalone = DirSrv(verbose=False)
     
     # Args for the standalone instance
-    args_instance['newhost'] = HOST_STANDALONE
-    args_instance['newport'] = PORT_STANDALONE
-    args_instance['newinstance'] = SERVERID_STANDALONE
+    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   = DirSrvTools.existsBackup(args_standalone)
+    backup_standalone = standalone.checkBackupFS()
     
     # Get the status of the instance and restart it if it exists
-    instance_standalone   = DirSrvTools.existsInstance(args_standalone)
+    instance_standalone   = standalone.exists()
     if instance_standalone:
         # assuming the instance is already stopped, just wait 5 sec max
-        DirSrvTools.stop(instance_standalone, timeout=5)
-        DirSrvTools.start(instance_standalone, timeout=10)
+        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
-        standalone   = _ds_create_instance(args_standalone)
+        if not instance_standalone:
+            standalone.create()
+            # Used to retrieve configuration information (dbdir, confdir...)
+            standalone.open()
         
         # restore standalone instance from backup
-        DirSrvTools.stop(standalone, timeout=10)
-        DirSrvTools.instanceRestoreFS(standalone, backup_standalone)
-        DirSrvTools.start(standalone, timeout=10)
+        standalone.stop(timeout=10)
+        standalone.restoreFS(backup_standalone)
+        standalone.start(timeout=10)
         
     else:
         # We should be here only in two conditions
@@ -99,19 +90,22 @@ def topology(request):
         # 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:
-            DirSrvTools.clearInstanceBackupFS(dirsrv=instance_standalone)
+            standalone.clearBackupFS()
         
         # Remove the instance
         if instance_standalone:
-            DirSrvTools.removeInstance(instance_standalone)
+            standalone.delete()
             
         # Create the instance
-        standalone   = _ds_create_instance(args_standalone)
+        standalone.create()
+        
+        # Used to retrieve configuration information (dbdir, confdir...)
+        standalone.open()
                 
         # Time to create the backups
-        DirSrvTools.stop(standalone, timeout=10)
-        standalone.backupfile = DirSrvTools.instanceBackupFS(standalone)
-        DirSrvTools.start(standalone, timeout=10)
+        standalone.stop(timeout=10)
+        standalone.backupfile = standalone.backupFS()
+        standalone.start(timeout=10)
     
     # 
     # Here we have standalone instance up and running
@@ -155,13 +149,13 @@ def test_ticket47560(topology):
         MEMBEROF_PLUGIN_DN = 'cn=MemberOf Plugin,cn=plugins,cn=config'
         replace = [(ldap.MOD_REPLACE, 'nsslapd-pluginEnabled', value)]
         topology.standalone.modify_s(MEMBEROF_PLUGIN_DN, replace)
-        DirSrvTools.stop(topology.standalone, verbose=False, timeout=120)
+        topology.standalone.stop(timeout=120)
         time.sleep(1)
-        DirSrvTools.start(topology.standalone, verbose=False, timeout=120)
+        topology.standalone.start(timeout=120)
         time.sleep(3)
             
         # need to reopen a connection toward the instance
-        topology.standalone = _ds_rebind_instance(topology.standalone)
+        topology.standalone.open()
         
     def _test_ticket47560_setup():
         """
@@ -285,7 +279,7 @@ def test_ticket47560(topology):
     assert result_successful == True
 
 def test_ticket47560_final(topology):
-    DirSrvTools.stop(topology.standalone, timeout=10)
+    topology.standalone.stop(timeout=10)
     
 
 
-- 
1.7.11.7

