Skip to content

Commit 7bc89f6

Browse files
committed
Refactoring: Continue refactoring for separate CLI and webservice API / functionalities in test suite. Separated by subclassing: 'one responsibility per class'
1 parent 72fafcf commit 7bc89f6

File tree

3 files changed

+107
-83
lines changed

3 files changed

+107
-83
lines changed

workspace_tools/singletest.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@
4242
4343
{
4444
"1" : {"mcu": "LPC1768",
45-
"port":"COM4", "disk":"J:\\",
46-
"peripherals": ["TMP102", "digital_loop", "port_loop", "analog_loop", "SD"]
45+
"port":"COM4",
46+
"disk":"J:\\",
47+
"peripherals": ["TMP102", "digital_loop", "port_loop", "analog_loop", "SD"]
4748
},
4849
4950
"2" : {"mcu": "KL25Z",
50-
"port":"COM7", "disk":"G:\\",
51-
"peripherals": ["digital_loop", "port_loop", "analog_loop"]
51+
"port":"COM7",
52+
"disk":"G:\\",
53+
"peripherals": ["digital_loop", "port_loop", "analog_loop"]
5254
}
5355
}
5456
@@ -69,8 +71,6 @@
6971
exit(-1)
7072

7173
import sys
72-
import optparse
73-
from time import time
7474
from os.path import join, abspath, dirname
7575

7676
# Be sure that the tools directory is in the search path
@@ -82,6 +82,7 @@
8282

8383
# Imports from TEST API
8484
from workspace_tools.test_api import SingleTestRunner
85+
from workspace_tools.test_api import singletest_in_cli_mode
8586
from workspace_tools.test_api import get_json_data_from_file
8687
from workspace_tools.test_api import print_muts_configuration_from_json
8788
from workspace_tools.test_api import print_test_configuration_from_json
@@ -178,16 +179,5 @@ def get_version():
178179
_opts_copy_method=opts.copy_method
179180
)
180181

181-
start = time()
182-
# Execute tests depending on options and filter applied
183-
test_summary, shuffle_seed = single_test.execute()
184-
elapsed_time = time() - start
185-
# Human readable summary
186-
if not single_test.opts_suppress_summary:
187-
# prints well-formed summary with results (SQL table like)
188-
print single_test.generate_test_summary(test_summary, shuffle_seed)
189-
if single_test.opts_test_x_toolchain_summary:
190-
# prints well-formed summary with results (SQL table like)
191-
# table shows text x toolchain test result matrix
192-
print single_test.generate_test_summary_by_target(test_summary, shuffle_seed)
193-
print "Completed in %d sec"% (elapsed_time)
182+
# Runs test suite in CLI mode
183+
singletest_in_cli_mode(single_test)

workspace_tools/test_api.py

Lines changed: 20 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import json
2424
import pprint
2525
import random
26-
import thread
2726
import optparse
2827
import threading
2928
from types import ListType
@@ -41,7 +40,6 @@
4140
from workspace_tools.paths import HOST_TESTS
4241
from workspace_tools.tests import TEST_MAP
4342
from workspace_tools.tests import TESTS
44-
from workspace_tools.utils import construct_enum
4543
from workspace_tools.targets import TARGET_MAP
4644
from workspace_tools.build_api import build_project, build_mbed_libs, build_lib
4745
from workspace_tools.build_api import get_target_supported_toolchains
@@ -71,7 +69,7 @@ def stop(self):
7169

7270

7371
class SingleTestExecutor(threading.Thread):
74-
""" Single test class separate thread usage """
72+
""" Example: Single test class in separate thread usage """
7573
def __init__(self, single_test):
7674
self.single_test = single_test
7775
threading.Thread.__init__(self)
@@ -128,7 +126,7 @@ class SingleTestRunner(object):
128126

129127
def __init__(self,
130128
_global_loops_count=1,
131-
_test_loops_list="",
129+
_test_loops_list=None,
132130
_muts={},
133131
_test_spec={},
134132
_opts_goanna_for_mbed_sdk=None,
@@ -185,50 +183,6 @@ def __init__(self,
185183
self.opts_test_x_toolchain_summary = _opts_test_x_toolchain_summary
186184
self.opts_copy_method = _opts_copy_method
187185

188-
# With this lock we should control access to certain resources inside this class
189-
self.resource_lock = thread.allocate_lock()
190-
191-
self.RestRequest = construct_enum(REST_MUTS='muts',
192-
REST_TEST_SPEC='test_spec',
193-
REST_TEST_RESULTS='test_results')
194-
195-
def get_rest_result_template(self, result, command, success_code):
196-
result = {"result": result,
197-
"command" : command,
198-
"success_code": success_code}
199-
return result
200-
201-
# REST API handlers for Flask framework
202-
def rest_api_status(self):
203-
""" Returns current test execution status. E.g. running / finished etc. """
204-
with self.resource_lock:
205-
pass
206-
207-
def rest_api_config(self):
208-
""" Returns configuration passed to SingleTest executor """
209-
with self.resource_lock:
210-
pass
211-
212-
def rest_api_log(self):
213-
""" Returns current test log """
214-
with self.resource_lock:
215-
pass
216-
217-
def rest_api_request_handler(self, request_type):
218-
""" Returns various data structures. Both static and mutable during test """
219-
result = {}
220-
success_code = 0
221-
with self.resource_lock:
222-
if request_type == self.RestRequest.REST_MUTS:
223-
result = self.muts # Returns MUTs
224-
elif request_type == self.RestRequest.REST_TEST_SPEC:
225-
result = self.test_spec # Returns Test Specification
226-
elif request_type == self.RestRequest.REST_TEST_RESULTS:
227-
pass # Returns test results
228-
else:
229-
success_code = -1
230-
return json.dumps(self.get_rest_result_template(result, 'request/' + request_type, success_code), indent=4)
231-
232186
def shuffle_random_func(self):
233187
return self.shuffle_random_seed
234188

@@ -274,7 +228,7 @@ def execute(self):
274228

275229
build_dir = join(BUILD_DIR, "test", target, toolchain)
276230

277-
# Enumerate through all tests
231+
# Enumerate through all tests and shuffle test order if requested
278232
test_map_keys = TEST_MAP.keys()
279233
if self.opts_shuffle_test_order:
280234
random.shuffle(test_map_keys, self.shuffle_random_func)
@@ -974,6 +928,23 @@ def progress_bar(percent_progress, saturation=0):
974928
return str_progress
975929

976930

931+
def singletest_in_cli_mode(single_test):
932+
""" Runs SingleTestRunner object in CLI (Command line interface) mode """
933+
start = time()
934+
# Execute tests depending on options and filter applied
935+
test_summary, shuffle_seed = single_test.execute()
936+
elapsed_time = time() - start
937+
# Human readable summary
938+
if not single_test.opts_suppress_summary:
939+
# prints well-formed summary with results (SQL table like)
940+
print single_test.generate_test_summary(test_summary, shuffle_seed)
941+
if single_test.opts_test_x_toolchain_summary:
942+
# prints well-formed summary with results (SQL table like)
943+
# table shows text x toolchain test result matrix
944+
print single_test.generate_test_summary_by_target(test_summary, shuffle_seed)
945+
print "Completed in %d sec"% (elapsed_time)
946+
947+
977948
def get_default_test_options_parser():
978949
""" Get common test script options used by CLI, webservices etc. """
979950
parser = optparse.OptionParser()
@@ -1089,18 +1060,6 @@ def get_default_test_options_parser():
10891060
default=None,
10901061
help='For some commands you can use filter to filter out results')
10911062

1092-
1093-
# Things related to webservices offered by test suite scripts
1094-
#parser.add_option('', '--rest-api',
1095-
# dest='rest_api_enabled',
1096-
# default=False,
1097-
# action="store_true",
1098-
# help='Enables REST API.')
1099-
1100-
#parser.add_option('', '--rest-api-port',
1101-
# dest='rest_api_port_no',
1102-
# help='Sets port for REST API interface')
1103-
11041063
parser.add_option('', '--verbose-skipped',
11051064
dest='verbose_skipped_tests',
11061065
default=False,

workspace_tools/test_webapi.py

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""
1919

2020
import sys
21+
import json
2122
import optparse
2223
from flask import Flask
2324
from os.path import join, abspath, dirname
@@ -27,6 +28,7 @@
2728
sys.path.insert(0, ROOT)
2829

2930
# Imports related to mbed build api
31+
from workspace_tools.utils import construct_enum
3032
from workspace_tools.build_api import mcu_toolchain_matrix
3133

3234
# Imports from TEST API
@@ -38,7 +40,80 @@
3840
from test_api import get_avail_tests_summary_table
3941
from test_api import get_default_test_options_parser
4042

41-
"""
43+
44+
class SingleTestRunnerWebService(SingleTestRunner):
45+
def __init__(self):
46+
super(SingleTestRunnerWebService, self).__init__()
47+
48+
# With this lock we should control access to certain resources inside this class
49+
self.resource_lock = thread.allocate_lock()
50+
51+
self.RestRequest = construct_enum(REST_MUTS='muts',
52+
REST_TEST_SPEC='test_spec',
53+
REST_TEST_RESULTS='test_results')
54+
55+
def get_rest_result_template(self, result, command, success_code):
56+
""" Returns common part of every web service request """
57+
result = {"result": result,
58+
"command" : command,
59+
"success_code": success_code} # 0 - OK, >0 - Error number
60+
return result
61+
62+
# REST API handlers for Flask framework
63+
def rest_api_status(self):
64+
""" Returns current test execution status. E.g. running / finished etc. """
65+
with self.resource_lock:
66+
pass
67+
68+
def rest_api_config(self):
69+
""" Returns configuration passed to SingleTest executor """
70+
with self.resource_lock:
71+
pass
72+
73+
def rest_api_log(self):
74+
""" Returns current test log """
75+
with self.resource_lock:
76+
pass
77+
78+
def rest_api_request_handler(self, request_type):
79+
""" Returns various data structures. Both static and mutable during test """
80+
result = {}
81+
success_code = 0
82+
with self.resource_lock:
83+
if request_type == self.RestRequest.REST_MUTS:
84+
result = self.muts # Returns MUTs
85+
elif request_type == self.RestRequest.REST_TEST_SPEC:
86+
result = self.test_spec # Returns Test Specification
87+
elif request_type == self.RestRequest.REST_TEST_RESULTS:
88+
pass # Returns test results
89+
else:
90+
success_code = -1
91+
return json.dumps(self.get_rest_result_template(result, 'request/' + request_type, success_code), indent=4)
92+
93+
94+
def singletest_in_webservice_mode():
95+
# TODO Implement this web service functionality
96+
pass
97+
98+
99+
def get_default_test_webservice_options_parser():
100+
""" Get test script web service options used by CLI, webservices etc. """
101+
parser = get_default_test_options_parser()
102+
103+
# Things related to web services offered by test suite scripts
104+
parser.add_option('', '--rest-api',
105+
dest='rest_api_enabled',
106+
default=False,
107+
action="store_true",
108+
help='Enables REST API.')
109+
110+
parser.add_option('', '--rest-api-port',
111+
dest='rest_api_port_no',
112+
help='Sets port for REST API interface')
113+
114+
return parser
115+
116+
'''
42117
if __name__ == '__main__':
43118
# Command line options
44119
parser = get_default_test_options_parser()
@@ -129,7 +204,7 @@
129204
130205
if opts.rest_api_enabled:
131206
# Enable REST API
132-
207+
133208
app = Flask(__name__)
134209
135210
@app.route('/')
@@ -159,4 +234,4 @@ def rest_api_request_handler(request_type):
159234
else:
160235
st_exec_thread.join()
161236
162-
"""
237+
'''

0 commit comments

Comments
 (0)