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