base_host_test.py
Go to the documentation of this file.
"""
mbed SDK
Copyright (c) 2011-2016 ARM Limited
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import inspect
from time import time
from inspect import isfunction, ismethod
class BaseHostTestAbstract(object):
""" Base class for each host-test test cases with standard
setup, test and teardown set of functions
"""
name = '' # name of the host test (used for local registration)
__event_queue = None # To main even loop
__dut_event_queue = None # To DUT
script_location = None # Path to source file used to load host test
__config = {}
def __notify_prn(self, text):
if self.__event_queue:
self.__event_queue.put(('__notify_prn', text, time()))
def __notify_conn_lost(self, text):
if self.__event_queue:
self.__event_queue.put(('__notify_conn_lost', text, time()))
def __notify_sync_failed(self, text):
if self.__event_queue:
self.__event_queue.put(('__notify_sync_failed', text, time()))
def __notify_dut(self, key, value):
"""! Send data over serial to DUT """
if self.__dut_event_queue:
self.__dut_event_queue.put((key, value, time()))
def notify_complete(self, result=None):
"""! Notify main even loop that host test finished processing
@param result True for success, False failure. If None - no action in main even loop
"""
if self.__event_queue:
self.__event_queue.put(('__notify_complete', result, time()))
def reset_dut(self, value):
"""
Reset device under test
:return:
"""
if self.__event_queue:
self.__event_queue.put(('__reset_dut', value, time()))
def reset(self):
"""
Reset the device under test and continue running the host test
:return:
"""
if self.__event_queue:
self.__event_queue.put(("__reset", "0", time()))
def notify_conn_lost(self, text):
"""! Notify main even loop that there was a DUT-host test connection error
@param consume If True htrun will process (consume) all remaining events
"""
self.__notify_conn_lost(text)
def log(self, text):
"""! Send log message to main event loop """
self.__notify_prn(text)
def send_kv(self, key, value):
"""! Send Key-Value data to DUT """
self.__notify_dut(key, value)
def setup_communication(self, event_queue, dut_event_queue, config={}):
"""! Setup queues used for IPC """
self.__event_queue = event_queue # To main even loop
self.__dut_event_queue = dut_event_queue # To DUT
self.__config__config = config
def get_config_item(self, name):
"""
Return test config
:param name:
:return:
"""
return self.__config__config.get(name, None)
def setup(self):
"""! Setup your tests and callbacks """
raise NotImplementedError
def result(self):
"""! Returns host test result (True, False or None) """
raise NotImplementedError
def teardown(self):
"""! Blocking always guaranteed test teardown """
raise NotImplementedError
def event_callback(key):
"""
Decorator for defining a event callback method. Adds a property attribute "event_key" with value as the passed key.
:param key:
:return:
"""
def decorator(func):
func.event_key = key
return func
return decorator
class HostTestCallbackBase(BaseHostTestAbstract):
def __init__(self):
BaseHostTestAbstract.__init__(self)
self.__callbacks = {}
self.__restricted_callbacks = [
'__coverage_start',
'__testcase_start',
'__testcase_finish',
'__testcase_summary',
'__exit',
'__exit_event_queue'
]
self.__consume_by_default = [
'__coverage_start',
'__testcase_start',
'__testcase_finish',
'__testcase_count',
'__testcase_name',
'__testcase_summary',
'__rxd_line',
]
self.__assign_default_callbacks()
self.__assign_decorated_callbacks()
def __callback_default(self, key, value, timestamp):
"""! Default callback """
#self.log("CALLBACK: key=%s, value=%s, timestamp=%f"% (key, value, timestamp))
pass
def __default_end_callback(self, key, value, timestamp):
"""
Default handler for event 'end' that gives test result from target.
This callback is not decorated as we don't know then in what order this
callback would be registered. We want to let users over write this callback.
Hence it should be registered before registering user defined callbacks.
:param key:
:param value:
:param timestamp:
:return:
"""
self.notify_complete(value == 'success')
def __assign_default_callbacks(self):
"""! Assigns default callback handlers """
for key in self.__consume_by_default:
self.__callbacks[key] = self.__callback_default
# Register default handler for event 'end' before assigning user defined callbacks to let users over write it.
self.register_callback('end', self.__default_end_callback)
def __assign_decorated_callbacks(self):
"""
It looks for any callback methods decorated with @event_callback
Example:
Define a method with @event_callback decorator like:
@event_callback('<event key>')
def event_handler(self, key, value, timestamp):
do something..
:return:
"""
for name, method in inspect.getmembers(self, inspect.ismethod):
key = getattr(method, 'event_key', None)
if key:
self.register_callback(key, method)
def register_callback(self, key, callback, force=False):
"""! Register callback for a specific event (key: event name)
@param key String with name of the event
@param callback Callable which will be registstered for event "key"
@param force God mode
"""
# Non-string keys are not allowed
if type(key) is not str:
raise TypeError("event non-string keys are not allowed")
# And finally callback should be callable
if not callable(callback):
raise TypeError("event callback should be callable")
# Check if callback has all three required parameters (key, value, timestamp)
# When callback is class method should have 4 arguments (self, key, value, timestamp)
if ismethod(callback):
arg_count = callback.__code__.co_argcount
if arg_count != 4:
err_msg = "callback 'self.%s('%s', ...)' defined with %d arguments"% (callback.__name__, key, arg_count)
err_msg += ", should have 4 arguments: self.%s(self, key, value, timestamp)"% callback.__name__
raise TypeError(err_msg)
# When callback is just a function should have 3 arguments func(key, value, timestamp)
if isfunction(callback):
arg_count = callback.__code__.co_argcount
if arg_count != 3:
err_msg = "callback '%s('%s', ...)' defined with %d arguments"% (callback.__name__, key, arg_count)
err_msg += ", should have 3 arguments: %s(key, value, timestamp)"% callback.__name__
raise TypeError(err_msg)
if not force:
# Event starting with '__' are reserved
if key.startswith('__'):
raise ValueError("event key starting with '__' are reserved")
# We predefined few callbacks you can't use
if key in self.__restricted_callbacks:
raise ValueError("we predefined few callbacks you can't use e.g. '%s'"% key)
self.__callbacks[key] = callback
def get_callbacks(self):
return self.__callbacks
def setup(self):
pass
def result(self):
pass
def teardown(self):
pass
class BaseHostTest(HostTestCallbackBase):
__BaseHostTest_Called = False
def base_host_test_inited(self):
""" This function will check if BaseHostTest ctor was called
Call to BaseHostTest is required in order to force required
interfaces implementation.
@return Returns True if ctor was called (ok behaviour)
"""
return self.__BaseHostTest_Called__BaseHostTest_Called
def __init__(self):
HostTestCallbackBase.__init__(self)
self.__BaseHostTest_Called__BaseHostTest_Called = True
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract
Definition base_host_test.py:23
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract.get_config_item
get_config_item(self, name)
Definition base_host_test.py:94
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract.send_kv
send_kv(self, key, value)
Send Key-Value data to DUT.
Definition base_host_test.py:84
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract.result
result(self)
Returns host test result (True, False or None)
Definition base_host_test.py:107
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract.__dut_event_queue
__dut_event_queue
Definition base_host_test.py:30
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract.setup
setup(self)
Setup your tests and callbacks.
Definition base_host_test.py:103
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract.reset
reset(self)
Definition base_host_test.py:66
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract.__event_queue
__event_queue
Definition base_host_test.py:29
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract.notify_complete
notify_complete(self, result=None)
Notify main even loop that host test finished processing.
Definition base_host_test.py:51
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract.teardown
teardown(self)
Blocking always guaranteed test teardown.
Definition base_host_test.py:111
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract.log
log(self, text)
Send log message to main event loop.
Definition base_host_test.py:80
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract.__config
dict __config
Definition base_host_test.py:32
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract.notify_conn_lost
notify_conn_lost(self, text)
Notify main even loop that there was a DUT-host test connection error.
Definition base_host_test.py:74
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract.__notify_prn
__notify_prn(self, text)
Definition base_host_test.py:34
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract.reset_dut
reset_dut(self, value)
Definition base_host_test.py:58
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract.__config
__config
Definition base_host_test.py:92
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract.__notify_dut
__notify_dut(self, key, value)
Definition base_host_test.py:46
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract.__notify_conn_lost
__notify_conn_lost(self, text)
Definition base_host_test.py:38
mbed_host_tests.host_tests.base_host_test.BaseHostTestAbstract.setup_communication
setup_communication(self, event_queue, dut_event_queue, config={})
Setup queues used for IPC.
Definition base_host_test.py:88
mbed_host_tests.host_tests.base_host_test.BaseHostTest.__BaseHostTest_Called
bool __BaseHostTest_Called
Definition base_host_test.py:258
mbed_host_tests.host_tests.base_host_test.BaseHostTest.__BaseHostTest_Called
__BaseHostTest_Called
Definition base_host_test.py:270
mbed_host_tests.host_tests.base_host_test.BaseHostTest.__init__
__init__(self)
Definition base_host_test.py:268
mbed_host_tests.host_tests.base_host_test.BaseHostTest.base_host_test_inited
base_host_test_inited(self)
Definition base_host_test.py:260
mbed_host_tests.host_tests.base_host_test.HostTestCallbackBase
Definition base_host_test.py:129
mbed_host_tests.host_tests.base_host_test.HostTestCallbackBase.get_callbacks
get_callbacks(self)
Definition base_host_test.py:243
mbed_host_tests.host_tests.base_host_test.HostTestCallbackBase.setup
setup(self)
Setup your tests and callbacks.
Definition base_host_test.py:246
mbed_host_tests.host_tests.base_host_test.HostTestCallbackBase.__restricted_callbacks
__restricted_callbacks
Definition base_host_test.py:134
mbed_host_tests.host_tests.base_host_test.HostTestCallbackBase.result
result(self)
Returns host test result (True, False or None)
Definition base_host_test.py:249
mbed_host_tests.host_tests.base_host_test.HostTestCallbackBase.teardown
teardown(self)
Blocking always guaranteed test teardown.
Definition base_host_test.py:252
mbed_host_tests.host_tests.base_host_test.HostTestCallbackBase.__init__
__init__(self)
Definition base_host_test.py:131
mbed_host_tests.host_tests.base_host_test.HostTestCallbackBase.__consume_by_default
__consume_by_default
Definition base_host_test.py:143
mbed_host_tests.host_tests.base_host_test.HostTestCallbackBase.__assign_default_callbacks
__assign_default_callbacks(self)
Definition base_host_test.py:175
mbed_host_tests.host_tests.base_host_test.HostTestCallbackBase.__assign_decorated_callbacks
__assign_decorated_callbacks(self)
Definition base_host_test.py:182
mbed_host_tests.host_tests.base_host_test.HostTestCallbackBase.register_callback
register_callback(self, key, callback, force=False)
Register callback for a specific event (key: event name)
Definition base_host_test.py:200
mbed_host_tests.host_tests.base_host_test.HostTestCallbackBase.__callbacks
__callbacks
Definition base_host_test.py:133
object