Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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