diff options
-rw-r--r-- | func/driver.py | 14 | ||||
-rw-r--r-- | func/env_setup.py | 67 | ||||
-rw-r--r-- | tests/driver_test.py | 15 |
3 files changed, 36 insertions, 60 deletions
diff --git a/func/driver.py b/func/driver.py index 63a9c369..4ce402a4 100644 --- a/func/driver.py +++ b/func/driver.py @@ -7,8 +7,8 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## import os -import json import logging +from func.ansible_api import AnsibleApi class Driver: @@ -54,12 +54,12 @@ class Driver: return special_json def run_ansible_playbook(self, benchmark, extra_vars): - extra_vars_json = json.dumps(dict(extra_vars.items())) - logging.info(extra_vars_json) - run_play = 'ansible-playbook ./benchmarks/playbooks/{0}.yaml' \ - ' --private-key=./data/QtipKey -i ./data/hosts --extra-vars \'{1}\'' \ - .format(benchmark, extra_vars_json) - os.system(run_play) + logging.info(extra_vars) + ansible_api = AnsibleApi() + ansible_api.execute_playbook('./data/hosts', + './benchmarks/playbooks/{0}.yaml'.format(benchmark), + './data/QtipKey', extra_vars) + return ansible_api.get_detail_playbook_stats() def drive_bench(self, benchmark, roles, benchmark_fname, benchmark_detail=None, pip_dict=None, proxy_info=None): diff --git a/func/env_setup.py b/func/env_setup.py index ea49337d..96f984cb 100644 --- a/func/env_setup.py +++ b/func/env_setup.py @@ -57,52 +57,31 @@ class Env_setup: f_name_2.close() @staticmethod - def ssh_test(lister): - print 'list: ', lister - for k, v in lister: - ip_var = k - print '\nBeginning SSH Test!\n' - if v != '': - print ('\nSSH->>>>> {0} {1}\n'.format(k, v)) - time.sleep(2) - - ssh_c = 'ssh-keyscan {0} >> ~/.ssh/known_hosts'.format(k) - os.system(ssh_c) - ssh_cmd = './data/qtip_creds.sh {0}'.format(ip_var) - print ssh_cmd - os.system(ssh_cmd) - for infinity in range(100): - try: - ssh = paramiko.SSHClient() - ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - ssh.connect(k, key_filename='./data/QtipKey') - stdin, stdout, stderr = ssh.exec_command('ls') + def ssh_test(hosts): + for ip, pw in hosts: + print '\nBeginning SSH Test: %s \n' % ip + os.system('ssh-keyscan %s >> ~/.ssh/known_hosts' % ip) + time.sleep(2) + + ssh_cmd = './data/qtip_creds.sh %s' % ip + print "run command: %s " % ssh_cmd + os.system(ssh_cmd) + + ssh = paramiko.SSHClient() + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + ssh.connect(ip, key_filename='./data/QtipKey') + + for attempts in range(100): + try: + stdin, stdout, stderr = ssh.exec_command('uname') + if not stderr.readlines(): print('SSH successful') - for line in stdout: - print '... ' + line.strip('\n') break - except socket.error: - print 'Retrying aSSH %s' % infinity - time.sleep(1) - if v == '': - print ('SSH->>>>>', k) - ssh_c = 'ssh-keyscan {0} >> ~/.ssh/known_hosts'.format(k) - - time.sleep(3) - os.system(ssh_c) - - for infinity in range(10): - try: - ssh = paramiko.SSHClient() - ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - ssh.connect(k, key_filename='./data/QtipKey') - stdin, stdout, stderr = ssh.exec_command('ls') - print('SSH successful') - for line in stdout: - print '... ' + line.strip('\n') - break - except socket.error: - print 'Retrying SSH %s' % infinity + except socket.error: + print 'SSH is still unavailable, retry!' + time.sleep(2) + if attempts == 99: + print "Try 99 times, SSH failed: %s" % ip @staticmethod def ping_test(lister, attempts=30): diff --git a/tests/driver_test.py b/tests/driver_test.py index a5b13588..9517d26d 100644 --- a/tests/driver_test.py +++ b/tests/driver_test.py @@ -1,7 +1,6 @@ import pytest import mock import os -import json from func.driver import Driver @@ -61,18 +60,16 @@ class TestClass: 'bandwidthGbps': 0, "role": "2-host"}]) ]) - @mock.patch('func.driver.os.system') - def test_driver_success(self, mock_system, test_input, expected): - mock_system.return_value = True + @mock.patch('func.driver.AnsibleApi') + def test_driver_success(self, mock_ansible, test_input, expected): + mock_ansible.execute_playbook.return_value = True k = mock.patch.dict(os.environ, {'INSTALLER_TYPE': test_input[6], 'PWD': '/home'}) k.start() dri = Driver() dri.drive_bench(test_input[0], test_input[1], test_input[2], test_input[3], test_input[4], test_input[5]) - call_list = mock_system.call_args_list + call_list = mock_ansible.execute_playbook.call_args_list k.stop() - print call_list for call in call_list: call_args, call_kwargs = call - real_call = call_args[0].split('extra-vars \'')[1] - real_call = real_call[0: len(real_call) - 1] - assert json.loads(real_call) == json.loads(json.dumps(expected[call_list.index(call)])) + real_call = call_args[3] + assert real_call == expected[call_list.index(call)] |