3Copyright (c) 2011-2016 ARM Limited
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
20from inspect
import isfunction, ismethod
24 """ Base class for each host-test test cases with standard
25 setup, test and teardown set of functions
30 __dut_event_queue =
None
31 script_location =
None
34 def __notify_prn(self, text):
38 def __notify_conn_lost(self, text):
42 def __notify_sync_failed(self, text):
44 self.
__event_queue.put((
'__notify_sync_failed', text, time()))
46 def __notify_dut(self, key, value):
47 """! Send data over serial to DUT """
52 """! Notify main even loop that host test finished processing
53 @param result True for success, False failure. If None - no action in main even loop
60 Reset device under test
68 Reset the device under test and continue running the host test
75 """! Notify main even loop that there was a DUT-host test connection error
76 @param consume If True htrun will process (consume) all remaining events
81 """! Send log message to main event loop """
85 """! Send Key-Value data to DUT """
89 """! Setup queues used for IPC """
101 return self.
__config.get(name,
None)
104 """! Setup your tests and callbacks """
105 raise NotImplementedError
108 """! Returns host test result (True, False or None) """
109 raise NotImplementedError
112 """! Blocking always guaranteed test teardown """
113 raise NotImplementedError
118 Decorator for defining a event callback method. Adds a property attribute "event_key" with value as the passed key.
132 BaseHostTestAbstract.__init__(self)
138 '__testcase_summary',
149 '__testcase_summary',
156 def __callback_default(self, key, value, timestamp):
157 """! Default callback """
161 def __default_end_callback(self, key, value, timestamp):
163 Default handler for event 'end' that gives test result from target.
164 This callback is not decorated as we don't know then in what order this
165 callback would be registered. We want to let users over write this callback.
166 Hence it should be registered before registering user defined callbacks.
173 self.notify_complete(value ==
'success')
175 def __assign_default_callbacks(self):
176 """! Assigns default callback handlers """
177 for key
in self.__consume_by_default:
178 self.__callbacks[key] = self.__callback_default
180 self.register_callback(
'end', self.__default_end_callback)
182 def __assign_decorated_callbacks(self):
184 It looks for any callback methods decorated with @event_callback
187 Define a method with @event_callback decorator like:
189 @event_callback('<event key>')
190 def event_handler(self, key, value, timestamp):
195 for name, method
in inspect.getmembers(self, inspect.ismethod):
196 key = getattr(method,
'event_key',
None)
198 self.register_callback(key, method)
201 """! Register callback for a specific event (key: event name)
202 @param key String with name of the event
203 @param callback Callable which will be registstered for event "key"
204 @param force God mode
208 if type(key)
is not str:
209 raise TypeError(
"event non-string keys are not allowed")
212 if not callable(callback):
213 raise TypeError(
"event callback should be callable")
217 if ismethod(callback):
218 arg_count = callback.__code__.co_argcount
220 err_msg =
"callback 'self.%s('%s', ...)' defined with %d arguments"% (callback.__name__, key, arg_count)
221 err_msg +=
", should have 4 arguments: self.%s(self, key, value, timestamp)"% callback.__name__
222 raise TypeError(err_msg)
225 if isfunction(callback):
226 arg_count = callback.__code__.co_argcount
228 err_msg =
"callback '%s('%s', ...)' defined with %d arguments"% (callback.__name__, key, arg_count)
229 err_msg +=
", should have 3 arguments: %s(key, value, timestamp)"% callback.__name__
230 raise TypeError(err_msg)
234 if key.startswith(
'__'):
235 raise ValueError(
"event key starting with '__' are reserved")
239 raise ValueError(
"we predefined few callbacks you can't use e.g. '%s'"% key)
256class BaseHostTest(HostTestCallbackBase):
258 __BaseHostTest_Called =
False
261 """ This function will check if BaseHostTest ctor was called
262 Call to BaseHostTest is required in order to force required
263 interfaces implementation.
264 @return Returns True if ctor was called (ok behaviour)
269 HostTestCallbackBase.__init__(self)
get_config_item(self, name)
send_kv(self, key, value)
Send Key-Value data to DUT.
result(self)
Returns host test result (True, False or None)
setup(self)
Setup your tests and callbacks.
notify_complete(self, result=None)
Notify main even loop that host test finished processing.
teardown(self)
Blocking always guaranteed test teardown.
log(self, text)
Send log message to main event loop.
notify_conn_lost(self, text)
Notify main even loop that there was a DUT-host test connection error.
__notify_dut(self, key, value)
__notify_conn_lost(self, text)
setup_communication(self, event_queue, dut_event_queue, config={})
Setup queues used for IPC.
bool __BaseHostTest_Called
base_host_test_inited(self)
setup(self)
Setup your tests and callbacks.
list __restricted_callbacks
result(self)
Returns host test result (True, False or None)
teardown(self)
Blocking always guaranteed test teardown.
list __consume_by_default
__assign_default_callbacks(self)
__assign_decorated_callbacks(self)
register_callback(self, key, callback, force=False)
Register callback for a specific event (key: event name)