b1dba1403d880a736e9b5813d7452a3aa32cdc43
[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         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     except Exception as err:
73         LOG.exception('Failed to record result data: %s', err)
74
75
76 def posca_run(test_level, test_name, REPORT="False"):
77     if test_level == "testcase":
78         config = conf_parser.Parser.testcase_read("posca", test_name)
79     elif test_level == "teststory":
80         config = conf_parser.Parser.story_read("posca", test_name)
81     for testcase in config:
82         LOG.info("Begin to run %s testcase in POSCA testsuite", testcase)
83         config[testcase]['out_file'] =\
84             conf_parser.Parser.testcase_out_dir(testcase)
85         start_date = datetime.datetime.now()
86         posca_testcase_run(testcase, config[testcase])
87         stop_date = datetime.datetime.now()
88         LOG.info("End of %s testcase in POSCA testsuite", testcase)
89
90         criteria = "FAIL"
91         if REPORT == "True":
92             details_doc = []
93             if os.path.exists(config[testcase]['out_file']):
94                 with open(config[testcase]['out_file']) as details_result:
95                     details_doc =[json.loads(data) for data in details_result.readlines()] # noqa
96                     if len(details_doc):
97                         criteria = "PASS"
98             report(testcase, start_date, stop_date, criteria, details_doc)
99
100
101 def main():
102     test_level = sys.argv[1]
103     test_name = sys.argv[2]
104     REPORT = sys.argv[3]
105     posca_run(test_level, test_name, REPORT)
106
107
108 if __name__ == '__main__':
109     main()