Bugfix: json format transform for report
[bottlenecks.git] / testsuites / posca / run_posca.py
1 #!/usr/bin/env python
2 ##############################################################################
3 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10 '''This file realize the function of how to run posca.
11 In this file, The first thing is to read testcase config
12 for example: you could run this by use
13 posca_run('testcase', "Which testcase you will run")
14 posca_run('teststory', "Which story you will run")
15 and if you run "python run_posca", this will run testcase,
16 posca_factor_system_bandwidth by default.'''
17
18 import importlib
19 import sys
20 import os
21
22 from oslo_serialization import jsonutils
23 import json
24 import requests
25 import datetime
26
27 import utils.parser as conf_parser
28 import utils.logger as log
29 INTERPRETER = "/usr/bin/python"
30
31 LOG = log.Logger(__name__).getLogger()
32 # ------------------------------------------------------
33 # run testcase in posca
34 # ------------------------------------------------------
35
36
37 def posca_testcase_run(testcase_script, test_config):
38
39     module_string = "testsuites.posca.testcase_script.%s" % (testcase_script)
40     module = importlib.import_module(module_string)
41     module.run(test_config)
42
43
44 def report(testcase, start_date, stop_date, criteria, details_doc):
45     headers = {'Content-type': 'application/json'}
46     results = {
47         "project_name": "bottlenecks",
48         "case_name": testcase,
49         "description": ("test results for " + testcase),
50         "pod_name": os.environ.get('NODE_NAME', 'unknown'),
51         "installer": os.environ.get('INSTALLER_TYPE', 'unknown'),
52         "version": os.environ.get('BRANCH', 'unknown'),
53         "build_tag": os.environ.get('BUILD_TAG', 'unknown'),
54         "stop_date": str(stop_date),
55         "start_date": str(start_date),
56         "criteria": criteria,
57         "scenario": os.environ.get('DEPLOY_SCENARIO', 'unknown')
58     }
59     results['details'] = {"test_results": details_doc}
60
61     target = "http://testresults.opnfv.org/test/api/v1/results"
62     timeout = 5
63
64     try:
65         LOG.debug('Test result : %s', jsonutils.dump_as_bytes(results))
66         print ('Start posting test results to community MongoDB')
67         res = requests.post(target,
68                             data=jsonutils.dump_as_bytes(results),
69                             headers=headers,
70                             timeout=timeout)
71         LOG.debug('Test result posting finished with status code'
72                   ' %d.' % res.status_code)
73         print ('Test results posting finished with status code'
74                ' %d.' % res.status_code)
75     except Exception as err:
76         LOG.exception('Failed to record result data: %s', err)
77
78
79 def posca_run(test_level, test_name, REPORT="False"):
80     if test_level == "testcase":
81         config = conf_parser.Parser.testcase_read("posca", test_name)
82     elif test_level == "teststory":
83         config = conf_parser.Parser.story_read("posca", test_name)
84     for testcase in config:
85         LOG.info("Begin to run %s testcase in POSCA testsuite", testcase)
86         config[testcase]['out_file'] =\
87             conf_parser.Parser.testcase_out_dir(testcase)
88         start_date = datetime.datetime.now()
89         posca_testcase_run(testcase, config[testcase])
90         stop_date = datetime.datetime.now()
91         LOG.info("End of %s testcase in POSCA testsuite", testcase)
92
93         criteria = "FAIL"
94         if REPORT == "True":
95             details_doc = []
96             if os.path.exists(config[testcase]['out_file']):
97                 with open(config[testcase]['out_file']) as details_result:
98                     details_doc =[json.loads(data) for data in details_result.readlines()] # noqa
99                     if len(details_doc):
100                         criteria = "PASS"
101             report(testcase, start_date, stop_date, criteria, details_doc)
102
103
104 def main():
105     test_level = sys.argv[1]
106     test_name = sys.argv[2]
107     REPORT = sys.argv[3]
108     posca_run(test_level, test_name, REPORT)
109
110
111 if __name__ == '__main__':
112     main()