Merge "Move admin client initialization into stress-openstack"
diff --git a/tempest/stress/driver.py b/tempest/stress/driver.py
index a32525d..d37ab6d 100644
--- a/tempest/stress/driver.py
+++ b/tempest/stress/driver.py
@@ -25,19 +25,12 @@
 from tempest.openstack.common import log as logging
 from tempest.stress import cleanup
 
-admin_manager = clients.AdminManager()
-
 LOG = logging.getLogger(__name__)
 processes = []
 
 
-def do_ssh(command, host):
-    username = admin_manager.config.stress.target_ssh_user
-    key_filename = admin_manager.config.stress.target_private_key_path
-    if not (username and key_filename):
-        LOG.error('username and key_filename should not be empty')
-        return None
-    ssh_client = ssh.Client(host, username, key_filename=key_filename)
+def do_ssh(command, host, ssh_user, ssh_key=None):
+    ssh_client = ssh.Client(host, ssh_user, key_filename=ssh_key)
     try:
         return ssh_client.exec_command(command)
     except exceptions.SSHExecCommandFailed:
@@ -46,14 +39,14 @@
         return None
 
 
-def _get_compute_nodes(controller):
+def _get_compute_nodes(controller, ssh_user, ssh_key=None):
     """
     Returns a list of active compute nodes. List is generated by running
     nova-manage on the controller.
     """
     nodes = []
     cmd = "nova-manage service list | grep ^nova-compute"
-    output = do_ssh(cmd, controller)
+    output = do_ssh(cmd, controller, ssh_user, ssh_key)
     if not output:
         return nodes
     # For example: nova-compute xg11eth0 nova enabled :-) 2011-10-31 18:57:46
@@ -65,14 +58,15 @@
     return nodes
 
 
-def _has_error_in_logs(logfiles, nodes, stop_on_error=False):
+def _has_error_in_logs(logfiles, nodes, ssh_user, ssh_key=None,
+                       stop_on_error=False):
     """
     Detect errors in the nova log files on the controller and compute nodes.
     """
     grep = 'egrep "ERROR|TRACE" %s' % logfiles
     ret = False
     for node in nodes:
-        errors = do_ssh(grep, node)
+        errors = do_ssh(grep, node, ssh_user, ssh_key)
         if len(errors) > 0:
             LOG.error('%s: %s' % (node, errors))
             ret = True
@@ -88,18 +82,17 @@
     terminate_all_processes()
 
 
-def terminate_all_processes():
+def terminate_all_processes(check_interval=20):
     """
     Goes through the process list and terminates all child processes.
     """
-    log_check_interval = int(admin_manager.config.stress.log_check_interval)
     for process in processes:
         if process['process'].is_alive():
             try:
                 process['process'].terminate()
             except Exception:
                 pass
-    time.sleep(log_check_interval)
+    time.sleep(check_interval)
     for process in processes:
         if process['process'].is_alive():
             try:
@@ -115,15 +108,19 @@
     """
     Workload driver. Executes an action function against a nova-cluster.
     """
+    admin_manager = clients.AdminManager()
+
+    ssh_user = admin_manager.config.stress.target_ssh_user
+    ssh_key = admin_manager.config.stress.target_private_key_path
     logfiles = admin_manager.config.stress.target_logfiles
     log_check_interval = int(admin_manager.config.stress.log_check_interval)
     default_thread_num = int(admin_manager.config.stress.
                              default_thread_number_per_action)
     if logfiles:
         controller = admin_manager.config.stress.target_controller
-        computes = _get_compute_nodes(controller)
+        computes = _get_compute_nodes(controller, ssh_user, ssh_key)
         for node in computes:
-            do_ssh("rm -f %s" % logfiles, node)
+            do_ssh("rm -f %s" % logfiles, node, ssh_user, ssh_key)
     for test in tests:
         if test.get('use_admin', False):
             manager = admin_manager
@@ -196,7 +193,8 @@
 
         if not logfiles:
             continue
-        if _has_error_in_logs(logfiles, computes, stop_on_error):
+        if _has_error_in_logs(logfiles, computes, ssh_user, ssh_key,
+                              stop_on_error):
             had_errors = True
             break