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