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