Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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