Start restful server auto in docker 05/22405/4
authorzhifeng.jiang <jiang.zhifeng@zte.com.cn>
Sat, 24 Sep 2016 06:01:46 +0000 (14:01 +0800)
committerzhifeng.jiang <jiang.zhifeng@zte.com.cn>
Wed, 28 Sep 2016 15:59:53 +0000 (23:59 +0800)
modification:
    Add start restful server in dockerfile
    Add result and detail_result in server job status
    Add job result assert in unit test
JIRA:QTIP-99

Change-Id: I27108eb930eba1bb72c04216f468a81202179ee0
Signed-off-by: zhifeng.jiang <jiang.zhifeng@zte.com.cn>
docker/Dockerfile
restful_server/db.py
restful_server/qtip_server.py
tests/driver_test.py
tests/qtip_server_test.py

index fc0e57c..7c4b18f 100644 (file)
@@ -12,6 +12,7 @@ LABEL version="0.1" description="OPNFV QTIP Docker container"
 
 ENV REPOS_DIR /home/opnfv/repos
 ENV QTIP_DIR /home/opnfv/repos/qtip
+ENV PYTHONPATH /home/opnfv/repos/qtip
 WORKDIR /home/opnfv
 
 
@@ -57,4 +58,4 @@ RUN git clone https://gerrit.opnfv.org/gerrit/releng $REPOS_DIR/releng
 
 RUN pip install -r $REPOS_DIR/qtip/requirements.txt
 
-
+CMD cd $REPOS_DIR/qtip && python restful_server/qtip_server.py>/var/log/qtip/run.log
index 916fc03..cf6ebfb 100644 (file)
@@ -7,6 +7,7 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 from datetime import datetime
+from operator import add
 import uuid
 
 jobs = {}
@@ -28,7 +29,8 @@ def create_job(args):
                'end_time': None,
                'state': 'processing',
                'state_detail': [],
-               'result': []}
+               'result': None,
+               'result_detail': []}
         jobs[job['job_id']] = job
         return job['job_id']
 
@@ -54,6 +56,8 @@ def get_job_info(job_id):
 def finish_job(job_id):
     jobs[job_id]['end_time'] = str(datetime.now())
     jobs[job_id]['state'] = 'finished'
+    jobs[job_id]['result'] = reduce(add, map(lambda x: x['result'],
+                                             jobs[job_id]['result_detail']))
     del threads[job_id]
 
 
@@ -61,8 +65,9 @@ def update_job_state_detail(job_id, state_detail):
     jobs[job_id]['state_detail'] = state_detail
 
 
-def update_job_result(job_id, result):
-    jobs[job_id]['result'] = result
+def update_job_result_detail(job_id, benchmark, result):
+    result['benchmark'] = benchmark
+    jobs[job_id]['result_detail'].append(result)
 
 
 def is_job_timeout(job_id):
index b03c8f1..734a471 100644 (file)
@@ -163,8 +163,12 @@ default is 'compute'
             if db.is_job_timeout(job_id) or stop_event.is_set():
                 break
             db.update_benmark_state_in_state_detail(job_id, benchmark, 'processing')
-            args_handler.prepare_and_run_benchmark(installer_type, '/home',
-                                                   args_handler.get_benchmark_path(pod_name, suite_name, benchmark))
+            result = args_handler.prepare_and_run_benchmark(installer_type,
+                                                            '/home',
+                                                            args_handler.get_benchmark_path(pod_name,
+                                                                                            suite_name,
+                                                                                            benchmark))
+            db.update_job_result_detail(job_id, benchmark, copy(result))
             db.update_benmark_state_in_state_detail(job_id, benchmark, 'finished')
         db.finish_job(job_id)
 
@@ -173,4 +177,4 @@ api.add_resource(JobList, '/api/v1.0/jobs')
 api.add_resource(Job, '/api/v1.0/jobs/<string:id>')
 
 if __name__ == "__main__":
-    app.run()
+    app.run(host='0.0.0.0')
index 9162ca1..5ea5dac 100644 (file)
@@ -63,13 +63,30 @@ class TestClass:
            "role": "2-host"}])
     ])
     @mock.patch('func.driver.AnsibleApi.execute_playbook')
-    def test_driver_success(self, mock_ansible, test_input, expected):
+    @mock.patch('func.driver.AnsibleApi.get_detail_playbook_stats')
+    def test_driver_success(self, mock_stats, mock_ansible, test_input, expected):
         mock_ansible.return_value = True
+        mock_stats.return_value = [(u'10.20.6.14', {'unreachable': 0,
+                                                    'skipped': 13,
+                                                    'ok': 27,
+                                                    'changed': 26,
+                                                    'failures': 0}),
+                                   ('localhost', {'unreachable': 0,
+                                                  'skipped': 0,
+                                                  'ok': 6,
+                                                  'changed': 6,
+                                                  'failures': 0}),
+                                   (u'10.20.6.13', {'unreachable': 0,
+                                                    'skipped': 13,
+                                                    'ok': 27,
+                                                    'changed': 26,
+                                                    'failures': 0})]
         dri = Driver()
-        dri.drive_bench(test_input[0], test_input[1], test_input[2], test_input[3],
-                        test_input[4], test_input[5], test_input[6], test_input[7])
+        result = dri.drive_bench(test_input[0], test_input[1], test_input[2], test_input[3],
+                                 test_input[4], test_input[5], test_input[6], test_input[7])
         call_list = mock_ansible.call_args_list
         for call in call_list:
             call_args, call_kwargs = call
             real_call = call_args[3]
             assert real_call == expected[call_list.index(call)]
+        assert result['result'] == 0
index 3f70a1f..511d209 100644 (file)
@@ -41,7 +41,7 @@ class TestClass:
                            {'state': 'finished', 'benchmark': 'ramspeed_bm.yaml'},
                            {'state': 'finished', 'benchmark': 'dpi_bm.yaml'},
                            {'state': 'finished', 'benchmark': 'ssl_bm.yaml'}],
-          'result': []}),
+          'result': 0}),
         ({'installer_type': 'fuel',
           'installer_ip': '10.20.0.2',
           'pod_name': 'zte-pod1',
@@ -61,10 +61,27 @@ class TestClass:
                            {u'state': u'finished', u'benchmark': u'ramspeed_vm.yaml'},
                            {u'state': u'finished', u'benchmark': u'dpi_vm.yaml'},
                            {u'state': u'finished', u'benchmark': u'ssl_vm.yaml'}],
-          'result': []})
+          'result': 0})
     ])
     @mock.patch('restful_server.qtip_server.args_handler.prepare_and_run_benchmark')
     def test_post_get_delete_job_successful(self, mock_args_handler, app_client, body, expected):
+        mock_args_handler.return_value = {'result': 0,
+                                          'detail': {'host': [(u'10.20.6.14', {'unreachable': 0,
+                                                                               'skipped': 13,
+                                                                               'ok': 27,
+                                                                               'changed': 26,
+                                                                               'failures': 0}),
+                                                              ('localhost', {'unreachable': 0,
+                                                                             'skipped': 0,
+                                                                             'ok': 6,
+                                                                             'changed': 6,
+                                                                             'failures': 0}),
+                                                              (u'10.20.6.13', {'unreachable': 0,
+                                                                               'skipped': 13,
+                                                                               'ok': 27,
+                                                                               'changed': 26,
+                                                                               'failures': 0})]}}
+
         reply = app_client.post("/api/v1.0/jobs", data=body)
         print(reply.data)
         id = json.loads(reply.data)['job_id']