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