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