Add reporting to MongoDB
[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 requests
24 import datetime
25
26 import utils.parser as conf_parser
27 import utils.logger as log
28 INTERPRETER = "/usr/bin/python"
29
30 LOG = log.Logger(__name__).getLogger()
31 # ------------------------------------------------------
32 # run testcase in posca
33 # ------------------------------------------------------
34
35
36 def posca_testcase_run(testcase_script, test_config):
37
38     module_string = "testsuites.posca.testcase_script.%s" % (testcase_script)
39     module = importlib.import_module(module_string)
40     module.run(test_config)
41
42
43 def report(testcase, start_date, stop_date, criteria, details_doc):
44     headers = {'Content-type': 'application/json'}
45     results = {
46         "project_name": "bottlenecks",
47         "case_name": testcase,
48         "description": ("test results for " + testcase),
49         "pod_name": os.environ.get('NODE_NAME', 'unknown'),
50         "installer": os.environ.get('INSTALLER_TYPE', 'unknown'),
51         "version": os.environ.get('BRANCH', 'unknown'),
52         "build_tag": os.environ.get('BUILD_TAG', 'unknown'),
53         "stop_date": stop_date,
54         "start_date": start_date,
55         "criteria": criteria,
56         "scenario": os.environ.get('DEPLOY_SCENARIO', 'unknown')
57     }
58     results['details'] = {"test_results": details_doc}
59
60     target = "http://testresults.opnfv.org/test/api/v1/results"
61     timeout = 5
62
63     try:
64         LOG.debug('Test result : %s', jsonutils.dump_as_bytes(results))
65         print ('Start posting test results to community MongoDB')
66         res = requests.post(target,
67                             data=jsonutils.dump_as_bytes(results),
68                             headers=headers,
69                             timeout=timeout)
70         LOG.debug('Test result posting finished with status code'
71                   ' %d.' % res.status_code)
72         print ('Test results posting finished with status code'
73                ' %d.' % res.status_code)
74     except Exception as err:
75         LOG.exception('Failed to record result data: %s', err)
76
77
78 def posca_run(test_level, test_name, REPORT="False"):
79     if test_level == "testcase":
80         config = conf_parser.Parser.testcase_read("posca", test_name)
81     elif test_level == "teststory":
82         config = conf_parser.Parser.story_read("posca", test_name)
83     for testcase in config:
84         LOG.info("Begin to run %s testcase in POSCA testsuite", testcase)
85         config[testcase]['out_file'] =\
86             conf_parser.Parser.testcase_out_dir(testcase)
87         start_date = datetime.datetime.now()
88         posca_testcase_run(testcase, config[testcase])
89         stop_date = datetime.datetime.now()
90         LOG.info("End of %s testcase in POSCA testsuite", testcase)
91
92         criteria = "FAIL"
93         if REPORT == "True":
94             details_doc = []
95             if os.path.exists(config[testcase]['out_file']):
96                 with open(config[testcase]['out_file']) as details_result:
97                     lines = details_result.readlines()
98                     if len(lines):
99                         criteria = "PASS"
100                         for l in lines:
101                             details_doc.append(l.replace('\n', ''))
102             report(testcase, start_date, stop_date, criteria, details_doc)
103
104
105 def main():
106     test_level = sys.argv[1]
107     test_name = sys.argv[2]
108     REPORT = sys.argv[3]
109     posca_run(test_level, test_name, REPORT)
110
111
112 if __name__ == '__main__':
113     main()