Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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