Call ansible playbook by ansible api instead of os.system 73/20273/2
authorzhifeng.jiang <jiang.zhifeng@zte.com.cn>
Sat, 3 Sep 2016 08:30:42 +0000 (16:30 +0800)
committerzhifeng.jiang <jiang.zhifeng@zte.com.cn>
Sun, 4 Sep 2016 00:10:13 +0000 (08:10 +0800)
JIRA:QTIP-99

Change-Id: Ife53a48d4af1fd3c60efc0673611321b3bc487a7
Signed-off-by: zhifeng.jiang <jiang.zhifeng@zte.com.cn>
func/driver.py
tests/driver_test.py

index 63a9c36..4ce402a 100644 (file)
@@ -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):
index a5b1358..9517d26 100644 (file)
@@ -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)]