aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzhifeng.jiang <jiang.zhifeng@zte.com.cn>2016-08-14 14:17:21 +0800
committerzhifeng.jiang <jiang.zhifeng@zte.com.cn>2016-08-18 15:33:47 +0800
commit27b463c5a9e50c610692c359e6ccdb982a6d1975 (patch)
treef4cbf5c78a5f00d8a9979ab04892f45f519ccbe4
parentf13e8325c532f63c48ceaefc740f09cb55adc026 (diff)
Code refactoring driver so that it can call ansible playbook by api.
JIRA:QTIP-99 Change-Id: Id182e586955bd5bc34cef787e71e1b6375c36dc9 Signed-off-by: zhifeng.jiang <jiang.zhifeng@zte.com.cn>
-rw-r--r--func/driver.py96
-rw-r--r--tests/driver_test.py75
2 files changed, 99 insertions, 72 deletions
diff --git a/func/driver.py b/func/driver.py
index 33dbe320..291591b1 100644
--- a/func/driver.py
+++ b/func/driver.py
@@ -8,53 +8,67 @@
##############################################################################
import os
import json
-from collections import defaultdict
+import logging
class Driver:
def __init__(self):
- print "Class driver initialized\n"
- print os.environ['PWD']
- self.dic_json = defaultdict()
+ logging.info("Class driver initialized\n")
+ logging.info(os.environ['PWD'])
+ self.installer_username = {'fuel': 'root',
+ 'joid': 'ubuntu',
+ 'apex': 'heat-admin'}
- def drive_bench(self, benchmark, roles, benchmark_fname, benchmark_detail=None, pip_dict=None, proxy_info=None):
+ @staticmethod
+ def merge_two_dicts(x, y):
+ '''
+ It is from http://stackoverflow.com/questions/38987/
+ how-can-i-merge-two-python-dictionaries-in-a-single-expression
+ '''
+ z = x.copy()
+ z.update(y)
+ return z
+
+ def get_common_var_json(self, benchmark_fname, benchmark_detail, pip_dict, proxy_info):
+ common_json = {'Dest_dir': 'results',
+ 'ip1': '',
+ 'ip2': '',
+ 'installer': str(os.environ['INSTALLER_TYPE']),
+ 'workingdir': str(os.environ['PWD']),
+ 'fname': str(benchmark_fname),
+ 'username': self.installer_username[str(os.environ['INSTALLER_TYPE'])]}
+ common_json.update(benchmark_detail) if benchmark_detail else None
+ common_json.update(proxy_info) if proxy_info else None
+ return common_json
+
+ def get_special_var_json(self, role, roles, benchmark_detail, pip_dict):
+ special_json = {}
+ index = roles.index(role) + 1
+ special_json.update({'role': role[0]})
+ private_ip = pip_dict[0][1] if pip_dict[0][1][0] else 'NONE'
+ map(lambda x: special_json.update({'ip' + str(index): x}), role[1])\
+ if benchmark_detail and (role[0] == '1-server') else None
+ map(lambda x: special_json.update({'privateip' + str(index): private_ip}), role[1])\
+ if benchmark_detail and (role[0] == '1-server') else None
+ 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)
+
+ def drive_bench(self, benchmark, roles, benchmark_fname,
+ benchmark_detail=None, pip_dict=None, proxy_info=None):
roles = sorted(roles)
pip_dict = sorted(pip_dict)
- result_dir = 'results'
- benchmark_name = benchmark + '.yaml'
- self.dic_json['Dest_dir'] = str(result_dir)
- self.dic_json['ip1'] = ''
- self.dic_json['ip2'] = ''
- self.dic_json['installer'] = str(os.environ['INSTALLER_TYPE'])
- self.dic_json['workingdir'] = str(os.environ['PWD'])
- self.dic_json['fname'] = str(benchmark_fname)
- self.dic_json['username'] = str('root')
-
- for key in proxy_info.keys():
- self.dic_json[key] = proxy_info[key]
-
- if os.environ['INSTALLER_TYPE'] == str('joid'):
- self.dic_json['username'] = str('ubuntu')
- if os.environ['INSTALLER_TYPE'] == str('apex'):
- self.dic_json['username'] = str('heat-admin')
- for k, v in benchmark_detail:
- self.dic_json[k] = v
- for k, v in roles:
- self.dic_json['role'] = k
- index = 1
- if benchmark_detail is not None:
- for values in v:
- if k == '1-server':
- print values, 'saving IP'
- self.dic_json['ip' + str(index)] = str(values)
- if pip_dict[0][1][0]:
- self.dic_json['privateip' + str(index)] = pip_dict[0][1]
- if not pip_dict[0][1][0]:
- self.dic_json['privateip' + str(index)] = 'NONE'
- index = index + 1
- dic_json = json.dumps(dict(self.dic_json.items()))
- print dic_json
- run_play = 'ansible-playbook ./benchmarks/playbooks/{0} --private-key=./data/QtipKey -i ./data/hosts --extra-vars \'{1}\''.format(benchmark_name, dic_json)
- os.system(run_play)
+ var_json = self.get_common_var_json(benchmark_fname, benchmark_detail, pip_dict, proxy_info)
+ map(lambda role: self.run_ansible_playbook
+ (benchmark, self.merge_two_dicts(var_json,
+ self.get_special_var_json(role, roles,
+ benchmark_detail,
+ pip_dict))), roles)
diff --git a/tests/driver_test.py b/tests/driver_test.py
index 39adc939..a5b13588 100644
--- a/tests/driver_test.py
+++ b/tests/driver_test.py
@@ -16,20 +16,20 @@ class TestClass:
'https_proxy': 'http://10.20.0.1:8118',
'no_proxy': 'localhost,127.0.0.1,10.20.*,192.168.*'},
'fuel'],
- {'Dest_dir': 'results',
- 'ip1': '',
- 'ip2': '',
- 'installer': 'fuel',
- 'workingdir': '/home',
- 'fname': 'iperf_bm.yaml',
- 'username': 'root',
- 'http_proxy': 'http://10.20.0.1:8118',
- 'https_proxy': 'http://10.20.0.1:8118',
- 'no_proxy': 'localhost,127.0.0.1,10.20.*,192.168.*',
- 'duration': 20,
- 'protocol': 'tcp',
- 'bandwidthGbps': 0,
- "role": "host"}),
+ [{'Dest_dir': 'results',
+ 'ip1': '',
+ 'ip2': '',
+ 'installer': 'fuel',
+ 'workingdir': '/home',
+ 'fname': 'iperf_bm.yaml',
+ 'username': 'root',
+ 'http_proxy': 'http://10.20.0.1:8118',
+ 'https_proxy': 'http://10.20.0.1:8118',
+ 'no_proxy': 'localhost,127.0.0.1,10.20.*,192.168.*',
+ 'duration': 20,
+ 'protocol': 'tcp',
+ 'bandwidthGbps': 0,
+ "role": "host"}]),
(["iperf",
[('1-server', ['10.20.0.13']), ('2-host', ['10.20.0.15'])],
"iperf_vm.yaml",
@@ -37,18 +37,29 @@ class TestClass:
[("10.20.0.13", [None]), ("10.20.0.15", [None])],
{},
'joid'],
- {'Dest_dir': 'results',
- 'ip1': '10.20.0.13',
- 'ip2': '',
- 'installer': 'joid',
- "privateip1": "NONE",
- 'workingdir': '/home',
- 'fname': 'iperf_vm.yaml',
- 'username': 'ubuntu',
- 'duration': 20,
- 'protocol': 'tcp',
- 'bandwidthGbps': 0,
- "role": "2-host"})
+ [{'Dest_dir': 'results',
+ 'ip1': '10.20.0.13',
+ 'ip2': '',
+ 'installer': 'joid',
+ "privateip1": "NONE",
+ 'workingdir': '/home',
+ 'fname': 'iperf_vm.yaml',
+ 'username': 'ubuntu',
+ 'duration': 20,
+ 'protocol': 'tcp',
+ 'bandwidthGbps': 0,
+ "role": "1-server"},
+ {'Dest_dir': 'results',
+ 'ip1': '',
+ 'ip2': '',
+ 'installer': 'joid',
+ 'workingdir': '/home',
+ 'fname': 'iperf_vm.yaml',
+ 'username': 'ubuntu',
+ 'duration': 20,
+ 'protocol': 'tcp',
+ 'bandwidthGbps': 0,
+ "role": "2-host"}])
])
@mock.patch('func.driver.os.system')
def test_driver_success(self, mock_system, test_input, expected):
@@ -57,9 +68,11 @@ class TestClass:
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 = mock_system.call_args
+ call_list = mock_system.call_args_list
k.stop()
- 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))
+ 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)]))