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