2 ##############################################################################
3 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
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.'''
22 from oslo_serialization import jsonutils
26 import utils.parser as conf_parser
27 import utils.logger as log
28 INTERPRETER = "/usr/bin/python"
30 LOG = log.Logger(__name__).getLogger()
31 # ------------------------------------------------------
32 # run testcase in posca
33 # ------------------------------------------------------
36 def posca_testcase_run(testcase_script, test_config):
38 module_string = "testsuites.posca.testcase_script.%s" % (testcase_script)
39 module = importlib.import_module(module_string)
40 module.run(test_config)
43 def report(testcase, start_date, stop_date, criteria, details_doc):
44 headers = {'Content-type': 'application/json'}
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,
56 "scenario": os.environ.get('DEPLOY_SCENARIO', 'unknown')
58 results['details'] = {"test_results": details_doc}
60 target = "http://testresults.opnfv.org/test/api/v1/results"
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),
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)
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)
95 if os.path.exists(config[testcase]['out_file']):
96 with open(config[testcase]['out_file']) as details_result:
97 lines = details_result.readlines()
101 details_doc.append(l.replace('\n', ''))
102 report(testcase, start_date, stop_date, criteria, details_doc)
106 test_level = sys.argv[1]
107 test_name = sys.argv[2]
109 posca_run(test_level, test_name, REPORT)
112 if __name__ == '__main__':