BA
/
BaBoRo_test2
Backup 1
mbed-os/tools/test_webapi.py@0:02dd72d1d465, 2018-04-24 (annotated)
- Committer:
- borlanic
- Date:
- Tue Apr 24 11:45:18 2018 +0000
- Revision:
- 0:02dd72d1d465
BaBoRo_test2 - backup 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:02dd72d1d465 | 1 | """ |
borlanic | 0:02dd72d1d465 | 2 | mbed SDK |
borlanic | 0:02dd72d1d465 | 3 | Copyright (c) 2011-2014 ARM Limited |
borlanic | 0:02dd72d1d465 | 4 | |
borlanic | 0:02dd72d1d465 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
borlanic | 0:02dd72d1d465 | 6 | you may not use this file except in compliance with the License. |
borlanic | 0:02dd72d1d465 | 7 | You may obtain a copy of the License at |
borlanic | 0:02dd72d1d465 | 8 | |
borlanic | 0:02dd72d1d465 | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
borlanic | 0:02dd72d1d465 | 10 | |
borlanic | 0:02dd72d1d465 | 11 | Unless required by applicable law or agreed to in writing, software |
borlanic | 0:02dd72d1d465 | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
borlanic | 0:02dd72d1d465 | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
borlanic | 0:02dd72d1d465 | 14 | See the License for the specific language governing permissions and |
borlanic | 0:02dd72d1d465 | 15 | limitations under the License. |
borlanic | 0:02dd72d1d465 | 16 | |
borlanic | 0:02dd72d1d465 | 17 | Author: Przemyslaw Wirkus <Przemyslaw.wirkus@arm.com> |
borlanic | 0:02dd72d1d465 | 18 | """ |
borlanic | 0:02dd72d1d465 | 19 | |
borlanic | 0:02dd72d1d465 | 20 | import sys |
borlanic | 0:02dd72d1d465 | 21 | import json |
borlanic | 0:02dd72d1d465 | 22 | import optparse |
borlanic | 0:02dd72d1d465 | 23 | from flask import Flask |
borlanic | 0:02dd72d1d465 | 24 | from os.path import join, abspath, dirname |
borlanic | 0:02dd72d1d465 | 25 | |
borlanic | 0:02dd72d1d465 | 26 | # Be sure that the tools directory is in the search path |
borlanic | 0:02dd72d1d465 | 27 | ROOT = abspath(join(dirname(__file__), "..")) |
borlanic | 0:02dd72d1d465 | 28 | sys.path.insert(0, ROOT) |
borlanic | 0:02dd72d1d465 | 29 | |
borlanic | 0:02dd72d1d465 | 30 | # Imports related to mbed build api |
borlanic | 0:02dd72d1d465 | 31 | from tools.utils import construct_enum |
borlanic | 0:02dd72d1d465 | 32 | from tools.build_api import mcu_toolchain_matrix |
borlanic | 0:02dd72d1d465 | 33 | |
borlanic | 0:02dd72d1d465 | 34 | # Imports from TEST API |
borlanic | 0:02dd72d1d465 | 35 | from test_api import SingleTestRunner |
borlanic | 0:02dd72d1d465 | 36 | from test_api import SingleTestExecutor |
borlanic | 0:02dd72d1d465 | 37 | from test_api import get_json_data_from_file |
borlanic | 0:02dd72d1d465 | 38 | from test_api import print_muts_configuration_from_json |
borlanic | 0:02dd72d1d465 | 39 | from test_api import print_test_configuration_from_json |
borlanic | 0:02dd72d1d465 | 40 | from test_api import get_avail_tests_summary_table |
borlanic | 0:02dd72d1d465 | 41 | from test_api import get_default_test_options_parser |
borlanic | 0:02dd72d1d465 | 42 | |
borlanic | 0:02dd72d1d465 | 43 | |
borlanic | 0:02dd72d1d465 | 44 | class SingleTestRunnerWebService(SingleTestRunner): |
borlanic | 0:02dd72d1d465 | 45 | def __init__(self): |
borlanic | 0:02dd72d1d465 | 46 | super(SingleTestRunnerWebService, self).__init__() |
borlanic | 0:02dd72d1d465 | 47 | |
borlanic | 0:02dd72d1d465 | 48 | # With this lock we should control access to certain resources inside this class |
borlanic | 0:02dd72d1d465 | 49 | self.resource_lock = thread.allocate_lock() |
borlanic | 0:02dd72d1d465 | 50 | |
borlanic | 0:02dd72d1d465 | 51 | self.RestRequest = construct_enum(REST_MUTS='muts', |
borlanic | 0:02dd72d1d465 | 52 | REST_TEST_SPEC='test_spec', |
borlanic | 0:02dd72d1d465 | 53 | REST_TEST_RESULTS='test_results') |
borlanic | 0:02dd72d1d465 | 54 | |
borlanic | 0:02dd72d1d465 | 55 | def get_rest_result_template(self, result, command, success_code): |
borlanic | 0:02dd72d1d465 | 56 | """ Returns common part of every web service request |
borlanic | 0:02dd72d1d465 | 57 | """ |
borlanic | 0:02dd72d1d465 | 58 | result = {"result" : result, |
borlanic | 0:02dd72d1d465 | 59 | "command" : command, |
borlanic | 0:02dd72d1d465 | 60 | "success_code": success_code} # 0 - OK, >0 - Error number |
borlanic | 0:02dd72d1d465 | 61 | return result |
borlanic | 0:02dd72d1d465 | 62 | |
borlanic | 0:02dd72d1d465 | 63 | # REST API handlers for Flask framework |
borlanic | 0:02dd72d1d465 | 64 | def rest_api_status(self): |
borlanic | 0:02dd72d1d465 | 65 | """ Returns current test execution status. E.g. running / finished etc. |
borlanic | 0:02dd72d1d465 | 66 | """ |
borlanic | 0:02dd72d1d465 | 67 | with self.resource_lock: |
borlanic | 0:02dd72d1d465 | 68 | pass |
borlanic | 0:02dd72d1d465 | 69 | |
borlanic | 0:02dd72d1d465 | 70 | def rest_api_config(self): |
borlanic | 0:02dd72d1d465 | 71 | """ Returns configuration passed to SingleTest executor |
borlanic | 0:02dd72d1d465 | 72 | """ |
borlanic | 0:02dd72d1d465 | 73 | with self.resource_lock: |
borlanic | 0:02dd72d1d465 | 74 | pass |
borlanic | 0:02dd72d1d465 | 75 | |
borlanic | 0:02dd72d1d465 | 76 | def rest_api_log(self): |
borlanic | 0:02dd72d1d465 | 77 | """ Returns current test log """ |
borlanic | 0:02dd72d1d465 | 78 | with self.resource_lock: |
borlanic | 0:02dd72d1d465 | 79 | pass |
borlanic | 0:02dd72d1d465 | 80 | |
borlanic | 0:02dd72d1d465 | 81 | def rest_api_request_handler(self, request_type): |
borlanic | 0:02dd72d1d465 | 82 | """ Returns various data structures. Both static and mutable during test |
borlanic | 0:02dd72d1d465 | 83 | """ |
borlanic | 0:02dd72d1d465 | 84 | result = {} |
borlanic | 0:02dd72d1d465 | 85 | success_code = 0 |
borlanic | 0:02dd72d1d465 | 86 | with self.resource_lock: |
borlanic | 0:02dd72d1d465 | 87 | if request_type == self.RestRequest.REST_MUTS: |
borlanic | 0:02dd72d1d465 | 88 | result = self.muts # Returns MUTs |
borlanic | 0:02dd72d1d465 | 89 | elif request_type == self.RestRequest.REST_TEST_SPEC: |
borlanic | 0:02dd72d1d465 | 90 | result = self.test_spec # Returns Test Specification |
borlanic | 0:02dd72d1d465 | 91 | elif request_type == self.RestRequest.REST_TEST_RESULTS: |
borlanic | 0:02dd72d1d465 | 92 | pass # Returns test results |
borlanic | 0:02dd72d1d465 | 93 | else: |
borlanic | 0:02dd72d1d465 | 94 | success_code = -1 |
borlanic | 0:02dd72d1d465 | 95 | return json.dumps(self.get_rest_result_template(result, 'request/' + request_type, success_code), indent=4) |
borlanic | 0:02dd72d1d465 | 96 | |
borlanic | 0:02dd72d1d465 | 97 | |
borlanic | 0:02dd72d1d465 | 98 | def singletest_in_webservice_mode(): |
borlanic | 0:02dd72d1d465 | 99 | # TODO Implement this web service functionality |
borlanic | 0:02dd72d1d465 | 100 | pass |
borlanic | 0:02dd72d1d465 | 101 | |
borlanic | 0:02dd72d1d465 | 102 | |
borlanic | 0:02dd72d1d465 | 103 | def get_default_test_webservice_options_parser(): |
borlanic | 0:02dd72d1d465 | 104 | """ Get test script web service options used by CLI, webservices etc. |
borlanic | 0:02dd72d1d465 | 105 | """ |
borlanic | 0:02dd72d1d465 | 106 | parser = get_default_test_options_parser() |
borlanic | 0:02dd72d1d465 | 107 | |
borlanic | 0:02dd72d1d465 | 108 | # Things related to web services offered by test suite scripts |
borlanic | 0:02dd72d1d465 | 109 | parser.add_argument('', '--rest-api', |
borlanic | 0:02dd72d1d465 | 110 | dest='rest_api_enabled', |
borlanic | 0:02dd72d1d465 | 111 | default=False, |
borlanic | 0:02dd72d1d465 | 112 | action="store_true", |
borlanic | 0:02dd72d1d465 | 113 | help='Enables REST API.') |
borlanic | 0:02dd72d1d465 | 114 | |
borlanic | 0:02dd72d1d465 | 115 | parser.add_argument('', '--rest-api-port', |
borlanic | 0:02dd72d1d465 | 116 | dest='rest_api_port_no', |
borlanic | 0:02dd72d1d465 | 117 | type=int, |
borlanic | 0:02dd72d1d465 | 118 | help='Sets port for REST API interface') |
borlanic | 0:02dd72d1d465 | 119 | |
borlanic | 0:02dd72d1d465 | 120 | return parser |
borlanic | 0:02dd72d1d465 | 121 | |
borlanic | 0:02dd72d1d465 | 122 | ''' |
borlanic | 0:02dd72d1d465 | 123 | if __name__ == '__main__': |
borlanic | 0:02dd72d1d465 | 124 | # Command line options |
borlanic | 0:02dd72d1d465 | 125 | parser = get_default_test_options_parser() |
borlanic | 0:02dd72d1d465 | 126 | |
borlanic | 0:02dd72d1d465 | 127 | parser.description = """This script allows you to run mbed defined test cases for particular MCU(s) and corresponding toolchain(s).""" |
borlanic | 0:02dd72d1d465 | 128 | parser.epilog = """Example: singletest.py -i test_spec.json -M muts_all.json""" |
borlanic | 0:02dd72d1d465 | 129 | |
borlanic | 0:02dd72d1d465 | 130 | (opts, args) = parser.parse_args() |
borlanic | 0:02dd72d1d465 | 131 | |
borlanic | 0:02dd72d1d465 | 132 | # Print summary / information about automation test status |
borlanic | 0:02dd72d1d465 | 133 | if opts.test_automation_report: |
borlanic | 0:02dd72d1d465 | 134 | print get_avail_tests_summary_table() |
borlanic | 0:02dd72d1d465 | 135 | exit(0) |
borlanic | 0:02dd72d1d465 | 136 | |
borlanic | 0:02dd72d1d465 | 137 | # Print summary / information about automation test status |
borlanic | 0:02dd72d1d465 | 138 | if opts.test_case_report: |
borlanic | 0:02dd72d1d465 | 139 | test_case_report_cols = ['id', 'automated', 'description', 'peripherals', 'host_test', 'duration', 'source_dir'] |
borlanic | 0:02dd72d1d465 | 140 | print get_avail_tests_summary_table(cols=test_case_report_cols, result_summary=False, join_delim='\n') |
borlanic | 0:02dd72d1d465 | 141 | exit(0) |
borlanic | 0:02dd72d1d465 | 142 | |
borlanic | 0:02dd72d1d465 | 143 | # Only prints matrix of supported toolchains |
borlanic | 0:02dd72d1d465 | 144 | if opts.supported_toolchains: |
borlanic | 0:02dd72d1d465 | 145 | print mcu_toolchain_matrix(platform_filter=opts.general_filter_regex) |
borlanic | 0:02dd72d1d465 | 146 | exit(0) |
borlanic | 0:02dd72d1d465 | 147 | |
borlanic | 0:02dd72d1d465 | 148 | # Open file with test specification |
borlanic | 0:02dd72d1d465 | 149 | # test_spec_filename tells script which targets and their toolchain(s) |
borlanic | 0:02dd72d1d465 | 150 | # should be covered by the test scenario |
borlanic | 0:02dd72d1d465 | 151 | test_spec = get_json_data_from_file(opts.test_spec_filename) if opts.test_spec_filename else None |
borlanic | 0:02dd72d1d465 | 152 | if test_spec is None: |
borlanic | 0:02dd72d1d465 | 153 | if not opts.test_spec_filename: |
borlanic | 0:02dd72d1d465 | 154 | parser.print_help() |
borlanic | 0:02dd72d1d465 | 155 | exit(-1) |
borlanic | 0:02dd72d1d465 | 156 | |
borlanic | 0:02dd72d1d465 | 157 | # Get extra MUTs if applicable |
borlanic | 0:02dd72d1d465 | 158 | MUTs = get_json_data_from_file(opts.muts_spec_filename) if opts.muts_spec_filename else None |
borlanic | 0:02dd72d1d465 | 159 | |
borlanic | 0:02dd72d1d465 | 160 | if MUTs is None: |
borlanic | 0:02dd72d1d465 | 161 | if not opts.muts_spec_filename: |
borlanic | 0:02dd72d1d465 | 162 | parser.print_help() |
borlanic | 0:02dd72d1d465 | 163 | exit(-1) |
borlanic | 0:02dd72d1d465 | 164 | |
borlanic | 0:02dd72d1d465 | 165 | # Only prints read MUTs configuration |
borlanic | 0:02dd72d1d465 | 166 | if MUTs and opts.verbose_test_configuration_only: |
borlanic | 0:02dd72d1d465 | 167 | print "MUTs configuration in %s:"% opts.muts_spec_filename |
borlanic | 0:02dd72d1d465 | 168 | print print_muts_configuration_from_json(MUTs) |
borlanic | 0:02dd72d1d465 | 169 | |
borlanic | 0:02dd72d1d465 | 170 | print "Test specification in %s:"% opts.test_spec_filename |
borlanic | 0:02dd72d1d465 | 171 | print print_test_configuration_from_json(test_spec) |
borlanic | 0:02dd72d1d465 | 172 | exit(0) |
borlanic | 0:02dd72d1d465 | 173 | |
borlanic | 0:02dd72d1d465 | 174 | # Verbose test specification and MUTs configuration |
borlanic | 0:02dd72d1d465 | 175 | if MUTs and opts.verbose: |
borlanic | 0:02dd72d1d465 | 176 | print print_muts_configuration_from_json(MUTs) |
borlanic | 0:02dd72d1d465 | 177 | if test_spec and opts.verbose: |
borlanic | 0:02dd72d1d465 | 178 | print print_test_configuration_from_json(test_spec) |
borlanic | 0:02dd72d1d465 | 179 | |
borlanic | 0:02dd72d1d465 | 180 | if opts.only_build_tests: |
borlanic | 0:02dd72d1d465 | 181 | # We are skipping testing phase, and suppress summary |
borlanic | 0:02dd72d1d465 | 182 | opts.suppress_summary = True |
borlanic | 0:02dd72d1d465 | 183 | |
borlanic | 0:02dd72d1d465 | 184 | single_test = SingleTestRunner(_global_loops_count=opts.test_global_loops_value, |
borlanic | 0:02dd72d1d465 | 185 | _test_loops_list=opts.test_loops_list, |
borlanic | 0:02dd72d1d465 | 186 | _muts=MUTs, |
borlanic | 0:02dd72d1d465 | 187 | _test_spec=test_spec, |
borlanic | 0:02dd72d1d465 | 188 | _opts_goanna_for_mbed_sdk=opts.goanna_for_mbed_sdk, |
borlanic | 0:02dd72d1d465 | 189 | _opts_goanna_for_tests=opts.goanna_for_tests, |
borlanic | 0:02dd72d1d465 | 190 | _opts_shuffle_test_order=opts.shuffle_test_order, |
borlanic | 0:02dd72d1d465 | 191 | _opts_shuffle_test_seed=opts.shuffle_test_seed, |
borlanic | 0:02dd72d1d465 | 192 | _opts_test_by_names=opts.test_by_names, |
borlanic | 0:02dd72d1d465 | 193 | _opts_test_only_peripheral=opts.test_only_peripheral, |
borlanic | 0:02dd72d1d465 | 194 | _opts_test_only_common=opts.test_only_common, |
borlanic | 0:02dd72d1d465 | 195 | _opts_verbose_skipped_tests=opts.verbose_skipped_tests, |
borlanic | 0:02dd72d1d465 | 196 | _opts_verbose_test_result_only=opts.verbose_test_result_only, |
borlanic | 0:02dd72d1d465 | 197 | _opts_verbose=opts.verbose, |
borlanic | 0:02dd72d1d465 | 198 | _opts_firmware_global_name=opts.firmware_global_name, |
borlanic | 0:02dd72d1d465 | 199 | _opts_only_build_tests=opts.only_build_tests, |
borlanic | 0:02dd72d1d465 | 200 | _opts_suppress_summary=opts.suppress_summary, |
borlanic | 0:02dd72d1d465 | 201 | _opts_test_x_toolchain_summary=opts.test_x_toolchain_summary, |
borlanic | 0:02dd72d1d465 | 202 | _opts_copy_method=opts.copy_method |
borlanic | 0:02dd72d1d465 | 203 | ) |
borlanic | 0:02dd72d1d465 | 204 | |
borlanic | 0:02dd72d1d465 | 205 | try: |
borlanic | 0:02dd72d1d465 | 206 | st_exec_thread = SingleTestExecutor(single_test) |
borlanic | 0:02dd72d1d465 | 207 | except KeyboardInterrupt, e: |
borlanic | 0:02dd72d1d465 | 208 | print "\n[CTRL+c] exit" |
borlanic | 0:02dd72d1d465 | 209 | st_exec_thread.start() |
borlanic | 0:02dd72d1d465 | 210 | |
borlanic | 0:02dd72d1d465 | 211 | if opts.rest_api_enabled: |
borlanic | 0:02dd72d1d465 | 212 | # Enable REST API |
borlanic | 0:02dd72d1d465 | 213 | |
borlanic | 0:02dd72d1d465 | 214 | app = Flask(__name__) |
borlanic | 0:02dd72d1d465 | 215 | |
borlanic | 0:02dd72d1d465 | 216 | @app.route('/') |
borlanic | 0:02dd72d1d465 | 217 | def hello_world(): |
borlanic | 0:02dd72d1d465 | 218 | return 'Hello World!' |
borlanic | 0:02dd72d1d465 | 219 | |
borlanic | 0:02dd72d1d465 | 220 | @app.route('/status') |
borlanic | 0:02dd72d1d465 | 221 | def rest_api_status(): |
borlanic | 0:02dd72d1d465 | 222 | return single_test.rest_api_status() # TODO |
borlanic | 0:02dd72d1d465 | 223 | |
borlanic | 0:02dd72d1d465 | 224 | @app.route('/config') |
borlanic | 0:02dd72d1d465 | 225 | def rest_api_config(): |
borlanic | 0:02dd72d1d465 | 226 | return single_test.rest_api_config() # TODO |
borlanic | 0:02dd72d1d465 | 227 | |
borlanic | 0:02dd72d1d465 | 228 | @app.route('/log') |
borlanic | 0:02dd72d1d465 | 229 | def rest_api_log(): |
borlanic | 0:02dd72d1d465 | 230 | return single_test.rest_api_log() # TODO |
borlanic | 0:02dd72d1d465 | 231 | |
borlanic | 0:02dd72d1d465 | 232 | @app.route('/request/<request_type>') # 'muts', 'test_spec', 'test_results' |
borlanic | 0:02dd72d1d465 | 233 | def rest_api_request_handler(request_type): |
borlanic | 0:02dd72d1d465 | 234 | result = single_test.rest_api_request_handler(request_type) # TODO |
borlanic | 0:02dd72d1d465 | 235 | return result |
borlanic | 0:02dd72d1d465 | 236 | |
borlanic | 0:02dd72d1d465 | 237 | rest_api_port = int(opts.rest_api_port_no) if opts.rest_api_port_no else 5555 |
borlanic | 0:02dd72d1d465 | 238 | app.debug = False |
borlanic | 0:02dd72d1d465 | 239 | app.run(port=rest_api_port) # Blocking Flask REST API web service |
borlanic | 0:02dd72d1d465 | 240 | else: |
borlanic | 0:02dd72d1d465 | 241 | st_exec_thread.join() |
borlanic | 0:02dd72d1d465 | 242 | |
borlanic | 0:02dd72d1d465 | 243 | ''' |