Python's coding style docs indicate to use " not ' for docstrings.
test/py has other violations of the coding style docs, since the docs
specify a stranger style than I would expect, but nobody has complained
about those yet:-)
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
console = None
def mkdir_p(path):
- '''Create a directory path.
+ """Create a directory path.
This includes creating any intermediate/parent directories. Any errors
caused due to already extant directories are ignored.
Returns:
Nothing.
- '''
+ """
try:
os.makedirs(path)
raise
def pytest_addoption(parser):
- '''pytest hook: Add custom command-line options to the cmdline parser.
+ """pytest hook: Add custom command-line options to the cmdline parser.
Args:
parser: The pytest command-line parser.
Returns:
Nothing.
- '''
+ """
parser.addoption('--build-dir', default=None,
help='U-Boot build directory (O=)')
help='Compile U-Boot before running tests')
def pytest_configure(config):
- '''pytest hook: Perform custom initialization at startup time.
+ """pytest hook: Perform custom initialization at startup time.
Args:
config: The pytest configuration.
Returns:
Nothing.
- '''
+ """
global log
global console
console = u_boot_console_exec_attach.ConsoleExecAttach(log, ubconfig)
def pytest_generate_tests(metafunc):
- '''pytest hook: parameterize test functions based on custom rules.
+ """pytest hook: parameterize test functions based on custom rules.
If a test function takes parameter(s) (fixture names) of the form brd__xxx
or env__xxx, the brd and env configuration dictionaries are consulted to
Returns:
Nothing.
- '''
+ """
subconfigs = {
'brd': console.config.brd,
@pytest.fixture(scope='function')
def u_boot_console(request):
- '''Generate the value of a test's u_boot_console fixture.
+ """Generate the value of a test's u_boot_console fixture.
Args:
request: The pytest request.
Returns:
The fixture value.
- '''
+ """
console.ensure_spawned()
return console
tests_passed = set()
def pytest_itemcollected(item):
- '''pytest hook: Called once for each test found during collection.
+ """pytest hook: Called once for each test found during collection.
This enables our custom result analysis code to see the list of all tests
that should eventually be run.
Returns:
Nothing.
- '''
+ """
tests_not_run.add(item.name)
def cleanup():
- '''Clean up all global state.
+ """Clean up all global state.
Executed (via atexit) once the entire test process is complete. This
includes logging the status of all tests, and the identity of any failed
Returns:
Nothing.
- '''
+ """
if console:
console.close()
atexit.register(cleanup)
def setup_boardspec(item):
- '''Process any 'boardspec' marker for a test.
+ """Process any 'boardspec' marker for a test.
Such a marker lists the set of board types that a test does/doesn't
support. If tests are being executed on an unsupported board, the test is
Returns:
Nothing.
- '''
+ """
mark = item.get_marker('boardspec')
if not mark:
pytest.skip('board not supported')
def setup_buildconfigspec(item):
- '''Process any 'buildconfigspec' marker for a test.
+ """Process any 'buildconfigspec' marker for a test.
Such a marker lists some U-Boot configuration feature that the test
requires. If tests are being executed on an U-Boot build that doesn't
Returns:
Nothing.
- '''
+ """
mark = item.get_marker('buildconfigspec')
if not mark:
pytest.skip('.config feature not enabled')
def pytest_runtest_setup(item):
- '''pytest hook: Configure (set up) a test item.
+ """pytest hook: Configure (set up) a test item.
Called once for each test to perform any custom configuration. This hook
is used to skip the test if certain conditions apply.
Returns:
Nothing.
- '''
+ """
log.start_section(item.name)
setup_boardspec(item)
setup_buildconfigspec(item)
def pytest_runtest_protocol(item, nextitem):
- '''pytest hook: Called to execute a test.
+ """pytest hook: Called to execute a test.
This hook wraps the standard pytest runtestprotocol() function in order
to acquire visibility into, and record, each test function's result.
Returns:
A list of pytest reports (test result data).
- '''
+ """
reports = runtestprotocol(item, nextitem=nextitem)
failed = None
mod_dir = os.path.dirname(os.path.abspath(__file__))
class LogfileStream(object):
- '''A file-like object used to write a single logical stream of data into
+ """A file-like object used to write a single logical stream of data into
a multiplexed log file. Objects of this type should be created by factory
- functions in the Logfile class rather than directly.'''
+ functions in the Logfile class rather than directly."""
def __init__(self, logfile, name, chained_file):
- '''Initialize a new object.
+ """Initialize a new object.
Args:
logfile: The Logfile object to log to.
Returns:
Nothing.
- '''
+ """
self.logfile = logfile
self.name = name
self.chained_file = chained_file
def close(self):
- '''Dummy function so that this class is "file-like".
+ """Dummy function so that this class is "file-like".
Args:
None.
Returns:
Nothing.
- '''
+ """
pass
def write(self, data, implicit=False):
- '''Write data to the log stream.
+ """Write data to the log stream.
Args:
data: The data to write tot he file.
Returns:
Nothing.
- '''
+ """
self.logfile.write(self, data, implicit)
if self.chained_file:
self.chained_file.write(data)
def flush(self):
- '''Flush the log stream, to ensure correct log interleaving.
+ """Flush the log stream, to ensure correct log interleaving.
Args:
None.
Returns:
Nothing.
- '''
+ """
self.logfile.flush()
if self.chained_file:
self.chained_file.flush()
class RunAndLog(object):
- '''A utility object used to execute sub-processes and log their output to
+ """A utility object used to execute sub-processes and log their output to
a multiplexed log file. Objects of this type should be created by factory
- functions in the Logfile class rather than directly.'''
+ functions in the Logfile class rather than directly."""
def __init__(self, logfile, name, chained_file):
- '''Initialize a new object.
+ """Initialize a new object.
Args:
logfile: The Logfile object to log to.
Returns:
Nothing.
- '''
+ """
self.logfile = logfile
self.name = name
self.chained_file = chained_file
def close(self):
- '''Clean up any resources managed by this object.'''
+ """Clean up any resources managed by this object."""
pass
def run(self, cmd, cwd=None, ignore_errors=False):
- '''Run a command as a sub-process, and log the results.
+ """Run a command as a sub-process, and log the results.
Args:
cmd: The command to execute.
Returns:
Nothing.
- '''
+ """
msg = "+" + " ".join(cmd) + "\n"
if self.chained_file:
raise exception
class SectionCtxMgr(object):
- '''A context manager for Python's "with" statement, which allows a certain
+ """A context manager for Python's "with" statement, which allows a certain
portion of test code to be logged to a separate section of the log file.
Objects of this type should be created by factory functions in the Logfile
- class rather than directly.'''
+ class rather than directly."""
def __init__(self, log, marker):
- '''Initialize a new object.
+ """Initialize a new object.
Args:
log: The Logfile object to log to.
Returns:
Nothing.
- '''
+ """
self.log = log
self.marker = marker
self.log.end_section(self.marker)
class Logfile(object):
- '''Generates an HTML-formatted log file containing multiple streams of
- data, each represented in a well-delineated/-structured fashion.'''
+ """Generates an HTML-formatted log file containing multiple streams of
+ data, each represented in a well-delineated/-structured fashion."""
def __init__(self, fn):
- '''Initialize a new object.
+ """Initialize a new object.
Args:
fn: The filename to write to.
Returns:
Nothing.
- '''
+ """
self.f = open(fn, "wt")
self.last_stream = None
""")
def close(self):
- '''Close the log file.
+ """Close the log file.
After calling this function, no more data may be written to the log.
Returns:
Nothing.
- '''
+ """
self.f.write("""\
</tt>
"".join(chr(c) for c in range(127, 256)))
def _escape(self, data):
- '''Render data format suitable for inclusion in an HTML document.
+ """Render data format suitable for inclusion in an HTML document.
This includes HTML-escaping certain characters, and translating
control characters to a hexadecimal representation.
Returns:
An escaped version of the data.
- '''
+ """
data = data.replace(chr(13), "")
data = "".join((c in self._nonprint) and ("%%%02x" % ord(c)) or
return data
def _terminate_stream(self):
- '''Write HTML to the log file to terminate the current stream's data.
+ """Write HTML to the log file to terminate the current stream's data.
Args:
None.
Returns:
Nothing.
- '''
+ """
self.cur_evt += 1
if not self.last_stream:
self.last_stream = None
def _note(self, note_type, msg):
- '''Write a note or one-off message to the log file.
+ """Write a note or one-off message to the log file.
Args:
note_type: The type of note. This must be a value supported by the
Returns:
Nothing.
- '''
+ """
self._terminate_stream()
self.f.write("<div class=\"" + note_type + "\">\n<pre>")
self.f.write("\n</pre></div>\n")
def start_section(self, marker):
- '''Begin a new nested section in the log file.
+ """Begin a new nested section in the log file.
Args:
marker: The name of the section that is starting.
Returns:
Nothing.
- '''
+ """
self._terminate_stream()
self.blocks.append(marker)
"\">Section: " + blk_path + "</div>\n")
def end_section(self, marker):
- '''Terminate the current nested section in the log file.
+ """Terminate the current nested section in the log file.
This function validates proper nesting of start_section() and
end_section() calls. If a mismatch is found, an exception is raised.
Returns:
Nothing.
- '''
+ """
if (not self.blocks) or (marker != self.blocks[-1]):
raise Exception("Block nesting mismatch: \"%s\" \"%s\"" %
self.blocks.pop()
def section(self, marker):
- '''Create a temporary section in the log file.
+ """Create a temporary section in the log file.
This function creates a context manager for Python's "with" statement,
which allows a certain portion of test code to be logged to a separate
Returns:
A context manager object.
- '''
+ """
return SectionCtxMgr(self, marker)
def error(self, msg):
- '''Write an error note to the log file.
+ """Write an error note to the log file.
Args:
msg: A message describing the error.
Returns:
Nothing.
- '''
+ """
self._note("error", msg)
def warning(self, msg):
- '''Write an warning note to the log file.
+ """Write an warning note to the log file.
Args:
msg: A message describing the warning.
Returns:
Nothing.
- '''
+ """
self._note("warning", msg)
def info(self, msg):
- '''Write an informational note to the log file.
+ """Write an informational note to the log file.
Args:
msg: An informational message.
Returns:
Nothing.
- '''
+ """
self._note("info", msg)
def action(self, msg):
- '''Write an action note to the log file.
+ """Write an action note to the log file.
Args:
msg: A message describing the action that is being logged.
Returns:
Nothing.
- '''
+ """
self._note("action", msg)
def status_pass(self, msg):
- '''Write a note to the log file describing test(s) which passed.
+ """Write a note to the log file describing test(s) which passed.
Args:
msg: A message describing passed test(s).
Returns:
Nothing.
- '''
+ """
self._note("status-pass", msg)
def status_skipped(self, msg):
- '''Write a note to the log file describing skipped test(s).
+ """Write a note to the log file describing skipped test(s).
Args:
msg: A message describing passed test(s).
Returns:
Nothing.
- '''
+ """
self._note("status-skipped", msg)
def status_fail(self, msg):
- '''Write a note to the log file describing failed test(s).
+ """Write a note to the log file describing failed test(s).
Args:
msg: A message describing passed test(s).
Returns:
Nothing.
- '''
+ """
self._note("status-fail", msg)
def get_stream(self, name, chained_file=None):
- '''Create an object to log a single stream's data into the log file.
+ """Create an object to log a single stream's data into the log file.
This creates a "file-like" object that can be written to in order to
write a single stream's data to the log file. The implementation will
Returns:
A file-like object.
- '''
+ """
return LogfileStream(self, name, chained_file)
def get_runner(self, name, chained_file=None):
- '''Create an object that executes processes and logs their output.
+ """Create an object that executes processes and logs their output.
Args:
name: The name of this sub-process.
Returns:
A RunAndLog object.
- '''
+ """
return RunAndLog(self, name, chained_file)
def write(self, stream, data, implicit=False):
- '''Write stream data into the log file.
+ """Write stream data into the log file.
This function should only be used by instances of LogfileStream or
RunAndLog.
Returns:
Nothing.
- '''
+ """
if stream != self.last_stream:
self._terminate_stream()
self.last_stream = stream
def flush(self):
- '''Flush the log stream, to ensure correct log interleaving.
+ """Flush the log stream, to ensure correct log interleaving.
Args:
None.
Returns:
Nothing.
- '''
+ """
self.f.flush()
# command prompt.
def test_version(u_boot_console):
- '''Test that the "version" command prints the U-Boot version.'''
+ """Test that the "version" command prints the U-Boot version."""
# "version" prints the U-Boot sign-on message. This is usually considered
# an error, so that any unexpected reboot causes an error. Here, this
import pytest
import u_boot_utils
-'''
+"""
Note: This test relies on:
a) boardenv_* to contain configuration values to define which USB ports are
(You may wish to change the group ID instead of setting the permissions wide
open. All that matters is that the user ID running the test can access the
device.)
-'''
+"""
# The set of file sizes to test. These values trigger various edge-cases such
# as one less than, equal to, and one greater than typical USB max packet
@pytest.mark.buildconfigspec('cmd_dfu')
def test_dfu(u_boot_console, env__usb_dev_port, env__dfu_config):
- '''Test the "dfu" command; the host system must be able to enumerate a USB
+ """Test the "dfu" command; the host system must be able to enumerate a USB
device when "dfu" is running, various DFU transfers are tested, and the
USB device must disappear when "dfu" is aborted.
Returns:
Nothing.
- '''
+ """
def start_dfu():
- '''Start U-Boot's dfu shell command.
+ """Start U-Boot's dfu shell command.
This also waits for the host-side USB enumeration process to complete.
Returns:
Nothing.
- '''
+ """
fh = u_boot_utils.attempt_to_open_file(
env__usb_dev_port['host_usb_dev_node'])
fh.close()
def stop_dfu(ignore_errors):
- '''Stop U-Boot's dfu shell command from executing.
+ """Stop U-Boot's dfu shell command from executing.
This also waits for the host-side USB de-enumeration process to
complete.
Returns:
Nothing.
- '''
+ """
try:
u_boot_console.log.action(
raise
def run_dfu_util(alt_setting, fn, up_dn_load_arg):
- '''Invoke dfu-util on the host.
+ """Invoke dfu-util on the host.
Args:
alt_setting: The DFU "alternate setting" identifier to interact
Returns:
Nothing.
- '''
+ """
cmd = ['dfu-util', '-a', str(alt_setting), up_dn_load_arg, fn]
if 'host_usb_port_path' in env__usb_dev_port:
u_boot_console.wait_for('Ctrl+C to exit ...')
def dfu_write(alt_setting, fn):
- '''Write a file to the target board using DFU.
+ """Write a file to the target board using DFU.
Args:
alt_setting: The DFU "alternate setting" identifier to interact
Returns:
Nothing.
- '''
+ """
run_dfu_util(alt_setting, fn, '-D')
def dfu_read(alt_setting, fn):
- '''Read a file from the target board using DFU.
+ """Read a file from the target board using DFU.
Args:
alt_setting: The DFU "alternate setting" identifier to interact
Returns:
Nothing.
- '''
+ """
# dfu-util fails reads/uploads if the host file already exists
if os.path.exists(fn):
run_dfu_util(alt_setting, fn, '-U')
def dfu_write_read_check(size):
- '''Test DFU transfers of a specific size of data
+ """Test DFU transfers of a specific size of data
This function first writes data to the board then reads it back and
compares the written and read back data. Measures are taken to avoid
Returns:
Nothing.
- '''
+ """
test_f = u_boot_utils.PersistentRandomFile(u_boot_console,
'dfu_%d.bin' % size, size)
# FIXME: This might be useful for other tests;
# perhaps refactor it into ConsoleBase or some other state object?
class StateTestEnv(object):
- '''Container that represents the state of all U-Boot environment variables.
+ """Container that represents the state of all U-Boot environment variables.
This enables quick determination of existant/non-existant variable
names.
- '''
+ """
def __init__(self, u_boot_console):
- '''Initialize a new StateTestEnv object.
+ """Initialize a new StateTestEnv object.
Args:
u_boot_console: A U-Boot console.
Returns:
Nothing.
- '''
+ """
self.u_boot_console = u_boot_console
self.get_env()
self.set_var = self.get_non_existent_var()
def get_env(self):
- '''Read all current environment variables from U-Boot.
+ """Read all current environment variables from U-Boot.
Args:
None.
Returns:
Nothing.
- '''
+ """
response = self.u_boot_console.run_command('printenv')
self.env = {}
self.env[var] = value
def get_existent_var(self):
- '''Return the name of an environment variable that exists.
+ """Return the name of an environment variable that exists.
Args:
None.
Returns:
The name of an environment variable.
- '''
+ """
for var in self.env:
return var
def get_non_existent_var(self):
- '''Return the name of an environment variable that does not exist.
+ """Return the name of an environment variable that does not exist.
Args:
None.
Returns:
The name of an environment variable.
- '''
+ """
n = 0
while True:
ste = None
@pytest.fixture(scope='function')
def state_test_env(u_boot_console):
- '''pytest fixture to provide a StateTestEnv object to tests.'''
+ """pytest fixture to provide a StateTestEnv object to tests."""
global ste
if not ste:
return ste
def unset_var(state_test_env, var):
- '''Unset an environment variable.
+ """Unset an environment variable.
This both executes a U-Boot shell command and updates a StateTestEnv
object.
Returns:
Nothing.
- '''
+ """
state_test_env.u_boot_console.run_command('setenv %s' % var)
if var in state_test_env.env:
del state_test_env.env[var]
def set_var(state_test_env, var, value):
- '''Set an environment variable.
+ """Set an environment variable.
This both executes a U-Boot shell command and updates a StateTestEnv
object.
Returns:
Nothing.
- '''
+ """
state_test_env.u_boot_console.run_command('setenv %s "%s"' % (var, value))
state_test_env.env[var] = value
def validate_empty(state_test_env, var):
- '''Validate that a variable is not set, using U-Boot shell commands.
+ """Validate that a variable is not set, using U-Boot shell commands.
Args:
var: The variable name to test.
Returns:
Nothing.
- '''
+ """
response = state_test_env.u_boot_console.run_command('echo $%s' % var)
assert response == ''
def validate_set(state_test_env, var, value):
- '''Validate that a variable is set, using U-Boot shell commands.
+ """Validate that a variable is set, using U-Boot shell commands.
Args:
var: The variable name to test.
Returns:
Nothing.
- '''
+ """
# echo does not preserve leading, internal, or trailing whitespace in the
# value. printenv does, and hence allows more complete testing.
assert response == ('%s=%s' % (var, value))
def test_env_echo_exists(state_test_env):
- '''Test echoing a variable that exists.'''
+ """Test echoing a variable that exists."""
var = state_test_env.get_existent_var()
value = state_test_env.env[var]
validate_set(state_test_env, var, value)
def test_env_echo_non_existent(state_test_env):
- '''Test echoing a variable that doesn't exist.'''
+ """Test echoing a variable that doesn't exist."""
var = state_test_env.set_var
validate_empty(state_test_env, var)
def test_env_printenv_non_existent(state_test_env):
- '''Test printenv error message for non-existant variables.'''
+ """Test printenv error message for non-existant variables."""
var = state_test_env.set_var
c = state_test_env.u_boot_console
assert(response == '## Error: "%s" not defined' % var)
def test_env_unset_non_existent(state_test_env):
- '''Test unsetting a nonexistent variable.'''
+ """Test unsetting a nonexistent variable."""
var = state_test_env.get_non_existent_var()
unset_var(state_test_env, var)
validate_empty(state_test_env, var)
def test_env_set_non_existent(state_test_env):
- '''Test set a non-existant variable.'''
+ """Test set a non-existant variable."""
var = state_test_env.set_var
value = 'foo'
validate_set(state_test_env, var, value)
def test_env_set_existing(state_test_env):
- '''Test setting an existant variable.'''
+ """Test setting an existant variable."""
var = state_test_env.set_var
value = 'bar'
validate_set(state_test_env, var, value)
def test_env_unset_existing(state_test_env):
- '''Test unsetting a variable.'''
+ """Test unsetting a variable."""
var = state_test_env.set_var
unset_var(state_test_env, var)
validate_empty(state_test_env, var)
def test_env_expansion_spaces(state_test_env):
- '''Test expanding a variable that contains a space in its value.'''
+ """Test expanding a variable that contains a space in its value."""
var_space = None
var_test = None
# SPDX-License-Identifier: GPL-2.0
def test_help(u_boot_console):
- '''Test that the "help" command can be executed.'''
+ """Test that the "help" command can be executed."""
u_boot_console.run_command('help')
)
def exec_hush_if(u_boot_console, expr, result):
- '''Execute a shell "if" command, and validate its result.'''
+ """Execute a shell "if" command, and validate its result."""
cmd = 'if ' + expr + '; then echo true; else echo false; fi'
response = u_boot_console.run_command(cmd)
@pytest.mark.buildconfigspec('sys_hush_parser')
def test_hush_if_test_setup(u_boot_console):
- '''Set up environment variables used during the "if" tests.'''
+ """Set up environment variables used during the "if" tests."""
u_boot_console.run_command('setenv ut_var_nonexistent')
u_boot_console.run_command('setenv ut_var_exists 1')
@pytest.mark.buildconfigspec('sys_hush_parser')
@pytest.mark.parametrize('expr,result', subtests)
def test_hush_if_test(u_boot_console, expr, result):
- '''Test a single "if test" condition.'''
+ """Test a single "if test" condition."""
exec_hush_if(u_boot_console, expr, result)
@pytest.mark.buildconfigspec('sys_hush_parser')
def test_hush_if_test_teardown(u_boot_console):
- '''Clean up environment variables used during the "if" tests.'''
+ """Clean up environment variables used during the "if" tests."""
u_boot_console.run_command('setenv ut_var_exists')
# Of those, only UMS currently allows file removal though.
@pytest.mark.boardspec('sandbox')
def test_hush_if_test_host_file_exists(u_boot_console):
- '''Test the "if test -e" shell command.'''
+ """Test the "if test -e" shell command."""
test_file = u_boot_console.config.result_dir + \
'/creating_this_file_breaks_u_boot_tests'
@pytest.mark.buildconfigspec('cmd_memory')
def test_md(u_boot_console):
- '''Test that md reads memory as expected, and that memory can be modified
- using the mw command.'''
+ """Test that md reads memory as expected, and that memory can be modified
+ using the mw command."""
ram_base = u_boot_utils.find_ram_base(u_boot_console)
addr = '%08x' % ram_base
@pytest.mark.buildconfigspec('cmd_memory')
def test_md_repeat(u_boot_console):
- '''Test command repeat (via executing an empty command) operates correctly
- for "md"; the command must repeat and dump an incrementing address.'''
+ """Test command repeat (via executing an empty command) operates correctly
+ for "md"; the command must repeat and dump an incrementing address."""
ram_base = u_boot_utils.find_ram_base(u_boot_console)
addr_base = '%08x' % ram_base
import pytest
import u_boot_utils
-'''
+"""
Note: This test relies on boardenv_* containing configuration values to define
which the network environment available for testing. Without this, this test
will be automatically skipped.
"size": 5058624,
"crc32": "c2244b26",
}
-'''
+"""
net_set_up = False
def test_net_pre_commands(u_boot_console):
- '''Execute any commands required to enable network hardware.
+ """Execute any commands required to enable network hardware.
These commands are provided by the boardenv_* file; see the comment at the
beginning of this file.
- '''
+ """
init_usb = u_boot_console.config.env.get('env__net_uses_usb', False)
if init_usb:
@pytest.mark.buildconfigspec('cmd_dhcp')
def test_net_dhcp(u_boot_console):
- '''Test the dhcp command.
+ """Test the dhcp command.
The boardenv_* file may be used to enable/disable this test; see the
comment at the beginning of this file.
- '''
+ """
test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False)
if not test_dhcp:
@pytest.mark.buildconfigspec('net')
def test_net_setup_static(u_boot_console):
- '''Set up a static IP configuration.
+ """Set up a static IP configuration.
The configuration is provided by the boardenv_* file; see the comment at
the beginning of this file.
- '''
+ """
env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None)
if not env_vars:
@pytest.mark.buildconfigspec('cmd_ping')
def test_net_ping(u_boot_console):
- '''Test the ping command.
+ """Test the ping command.
The $serverip (as set up by either test_net_dhcp or test_net_setup_static)
is pinged. The test validates that the host is alive, as reported by the
ping command's output.
- '''
+ """
if not net_set_up:
pytest.skip("Network not initialized")
@pytest.mark.buildconfigspec('cmd_net')
def test_net_tftpboot(u_boot_console):
- '''Test the tftpboot command.
+ """Test the tftpboot command.
A file is downloaded from the TFTP server, its size and optionally its
CRC32 are validated.
The details of the file to download are provided by the boardenv_* file;
see the comment at the beginning of this file.
- '''
+ """
if not net_set_up:
pytest.skip("Network not initialized")
@pytest.mark.boardspec('sandbox')
@pytest.mark.buildconfigspec('reset')
def test_reset(u_boot_console):
- '''Test that the "reset" command exits sandbox process.'''
+ """Test that the "reset" command exits sandbox process."""
u_boot_console.run_command('reset', wait_for_prompt=False)
assert(u_boot_console.validate_exited())
@pytest.mark.boardspec('sandbox')
def test_ctrl_c(u_boot_console):
- '''Test that sending SIGINT to sandbox causes it to exit.'''
+ """Test that sending SIGINT to sandbox causes it to exit."""
u_boot_console.kill(signal.SIGINT)
assert(u_boot_console.validate_exited())
# Test basic shell functionality, such as commands separate by semi-colons.
def test_shell_execute(u_boot_console):
- '''Test any shell command.'''
+ """Test any shell command."""
response = u_boot_console.run_command('echo hello')
assert response.strip() == 'hello'
def test_shell_semicolon_two(u_boot_console):
- '''Test two shell commands separate by a semi-colon.'''
+ """Test two shell commands separate by a semi-colon."""
cmd = 'echo hello; echo world'
response = u_boot_console.run_command(cmd)
assert response.index('hello') < response.index('world')
def test_shell_semicolon_three(u_boot_console):
- '''Test three shell commands separate by a semi-colon, with variable
- expansion dependencies between them.'''
+ """Test three shell commands separate by a semi-colon, with variable
+ expansion dependencies between them."""
cmd = 'setenv list 1; setenv list ${list}2; setenv list ${list}3; ' + \
'echo ${list}'
u_boot_console.run_command('setenv list')
def test_shell_run(u_boot_console):
- '''Test the "run" shell command.'''
+ """Test the "run" shell command."""
u_boot_console.run_command('setenv foo \"setenv monty 1; setenv python 2\"')
u_boot_console.run_command('run foo')
import time
def test_sleep(u_boot_console):
- '''Test the sleep command, and validate that it sleeps for approximately
- the correct amount of time.'''
+ """Test the sleep command, and validate that it sleeps for approximately
+ the correct amount of time."""
# 3s isn't too long, but is enough to cross a few second boundaries.
sleep_time = 3
import time
import u_boot_utils
-'''
+"""
Note: This test relies on:
a) boardenv_* to contain configuration values to define which USB ports are
This entry is only needed if any block_devs above contain a
writable_fs_partition value.
-'''
+"""
@pytest.mark.buildconfigspec('cmd_usb_mass_storage')
def test_ums(u_boot_console, env__usb_dev_port, env__block_devs):
- '''Test the "ums" command; the host system must be able to enumerate a UMS
+ """Test the "ums" command; the host system must be able to enumerate a UMS
device when "ums" is running, block and optionally file I/O are tested,
and this device must disappear when "ums" is aborted.
Returns:
Nothing.
- '''
+ """
have_writable_fs_partition = 'writable_fs_partition' in env__block_devs[0]
if not have_writable_fs_partition:
mounted_test_fn = mount_point + '/' + mount_subdir + test_f.fn
def start_ums():
- '''Start U-Boot's ums shell command.
+ """Start U-Boot's ums shell command.
This also waits for the host-side USB enumeration process to complete.
Returns:
Nothing.
- '''
+ """
u_boot_console.log.action(
'Starting long-running U-Boot ums shell command')
fh.close()
def mount():
- '''Mount the block device that U-Boot exports.
+ """Mount the block device that U-Boot exports.
Args:
None.
Returns:
Nothing.
- '''
+ """
u_boot_console.log.action('Mounting exported UMS device')
cmd = ('/bin/mount', host_ums_part_node)
u_boot_utils.run_and_log(u_boot_console, cmd)
def umount(ignore_errors):
- '''Unmount the block device that U-Boot exports.
+ """Unmount the block device that U-Boot exports.
Args:
ignore_errors: Ignore any errors. This is useful if an error has
Returns:
Nothing.
- '''
+ """
u_boot_console.log.action('Unmounting UMS device')
cmd = ('/bin/umount', host_ums_part_node)
u_boot_utils.run_and_log(u_boot_console, cmd, ignore_errors)
def stop_ums(ignore_errors):
- '''Stop U-Boot's ums shell command from executing.
+ """Stop U-Boot's ums shell command from executing.
This also waits for the host-side USB de-enumeration process to
complete.
Returns:
Nothing.
- '''
+ """
u_boot_console.log.action(
'Stopping long-running U-Boot ums shell command')
# SPDX-License-Identifier: GPL-2.0
def test_unknown_command(u_boot_console):
- '''Test that executing an unknown command causes U-Boot to print an
- error.'''
+ """Test that executing an unknown command causes U-Boot to print an
+ error."""
# The "unknown command" error is actively expected here,
# so error detection for it is disabled.
pattern_error_notification = re.compile('## Error: ')
class ConsoleDisableCheck(object):
- '''Context manager (for Python's with statement) that temporarily disables
+ """Context manager (for Python's with statement) that temporarily disables
the specified console output error check. This is useful when deliberately
executing a command that is known to trigger one of the error checks, in
order to test that the error condition is actually raised. This class is
used internally by ConsoleBase::disable_check(); it is not intended for
- direct usage.'''
+ direct usage."""
def __init__(self, console, check_type):
self.console = console
self.console.disable_check_count[self.check_type] -= 1
class ConsoleBase(object):
- '''The interface through which test functions interact with the U-Boot
+ """The interface through which test functions interact with the U-Boot
console. This primarily involves executing shell commands, capturing their
results, and checking for common error conditions. Some common utilities
- are also provided too.'''
+ are also provided too."""
def __init__(self, log, config, max_fifo_fill):
- '''Initialize a U-Boot console connection.
+ """Initialize a U-Boot console connection.
Can only usefully be called by sub-classes.
Returns:
Nothing.
- '''
+ """
self.log = log
self.config = config
self.at_prompt_logevt = None
def close(self):
- '''Terminate the connection to the U-Boot console.
+ """Terminate the connection to the U-Boot console.
This function is only useful once all interaction with U-Boot is
complete. Once this function is called, data cannot be sent to or
Returns:
Nothing.
- '''
+ """
if self.p:
self.p.close()
def run_command(self, cmd, wait_for_echo=True, send_nl=True,
wait_for_prompt=True):
- '''Execute a command via the U-Boot console.
+ """Execute a command via the U-Boot console.
The command is always sent to U-Boot.
The output from U-Boot during command execution. In other
words, the text U-Boot emitted between the point it echod the
command string and emitted the subsequent command prompts.
- '''
+ """
if self.at_prompt and \
self.at_prompt_logevt != self.logstream.logfile.cur_evt:
raise
def ctrlc(self):
- '''Send a CTRL-C character to U-Boot.
+ """Send a CTRL-C character to U-Boot.
This is useful in order to stop execution of long-running synchronous
commands such as "ums".
Returns:
Nothing.
- '''
+ """
self.log.action('Sending Ctrl-C')
self.run_command(chr(3), wait_for_echo=False, send_nl=False)
def wait_for(self, text):
- '''Wait for a pattern to be emitted by U-Boot.
+ """Wait for a pattern to be emitted by U-Boot.
This is useful when a long-running command such as "dfu" is executing,
and it periodically emits some text that should show up at a specific
Returns:
Nothing.
- '''
+ """
if type(text) == type(''):
text = re.escape(text)
self.p.expect([text])
def drain_console(self):
- '''Read from and log the U-Boot console for a short time.
+ """Read from and log the U-Boot console for a short time.
U-Boot's console output is only logged when the test code actively
waits for U-Boot to emit specific data. There are cases where tests
Returns:
Nothing.
- '''
+ """
# If we are already not connected to U-Boot, there's nothing to drain.
# This should only happen when a previous call to run_command() or
self.p.timeout = orig_timeout
def ensure_spawned(self):
- '''Ensure a connection to a correctly running U-Boot instance.
+ """Ensure a connection to a correctly running U-Boot instance.
This may require spawning a new Sandbox process or resetting target
hardware, as defined by the implementation sub-class.
Returns:
Nothing.
- '''
+ """
if self.p:
return
raise
def cleanup_spawn(self):
- '''Shut down all interaction with the U-Boot instance.
+ """Shut down all interaction with the U-Boot instance.
This is used when an error is detected prior to re-establishing a
connection with a fresh U-Boot instance.
Returns:
Nothing.
- '''
+ """
try:
if self.p:
self.p = None
def validate_version_string_in_text(self, text):
- '''Assert that a command's output includes the U-Boot signon message.
+ """Assert that a command's output includes the U-Boot signon message.
This is primarily useful for validating the "version" command without
duplicating the signon text regex in a test function.
Returns:
Nothing. An exception is raised if the validation fails.
- '''
+ """
assert(self.u_boot_version_string in text)
def disable_check(self, check_type):
- '''Temporarily disable an error check of U-Boot's output.
+ """Temporarily disable an error check of U-Boot's output.
Create a new context manager (for use with the "with" statement) which
temporarily disables a particular console output error check.
Returns:
A context manager object.
- '''
+ """
return ConsoleDisableCheck(self, check_type)
from u_boot_console_base import ConsoleBase
class ConsoleExecAttach(ConsoleBase):
- '''Represents a physical connection to a U-Boot console, typically via a
+ """Represents a physical connection to a U-Boot console, typically via a
serial port. This implementation executes a sub-process to attach to the
console, expecting that the stdin/out of the sub-process will be forwarded
to/from the physical hardware. This approach isolates the test infra-
structure from the user-/installation-specific details of how to
- communicate with, and the identity of, serial ports etc.'''
+ communicate with, and the identity of, serial ports etc."""
def __init__(self, log, config):
- '''Initialize a U-Boot console connection.
+ """Initialize a U-Boot console connection.
Args:
log: A multiplexed_log.Logfile instance.
Returns:
Nothing.
- '''
+ """
# The max_fifo_fill value might need tweaking per-board/-SoC?
# 1 would be safe anywhere, but is very slow (a pexpect issue?).
runner.close()
def get_spawn(self):
- '''Connect to a fresh U-Boot instance.
+ """Connect to a fresh U-Boot instance.
The target board is reset, so that U-Boot begins running from scratch.
Returns:
A u_boot_spawn.Spawn object that is attached to U-Boot.
- '''
+ """
args = [self.config.board_type, self.config.board_identity]
s = Spawn(['u-boot-test-console'] + args)
from u_boot_console_base import ConsoleBase
class ConsoleSandbox(ConsoleBase):
- '''Represents a connection to a sandbox U-Boot console, executed as a sub-
- process.'''
+ """Represents a connection to a sandbox U-Boot console, executed as a sub-
+ process."""
def __init__(self, log, config):
- '''Initialize a U-Boot console connection.
+ """Initialize a U-Boot console connection.
Args:
log: A multiplexed_log.Logfile instance.
Returns:
Nothing.
- '''
+ """
super(ConsoleSandbox, self).__init__(log, config, max_fifo_fill=1024)
def get_spawn(self):
- '''Connect to a fresh U-Boot instance.
+ """Connect to a fresh U-Boot instance.
A new sandbox process is created, so that U-Boot begins running from
scratch.
Returns:
A u_boot_spawn.Spawn object that is attached to U-Boot.
- '''
+ """
return Spawn([self.config.build_dir + '/u-boot'])
def kill(self, sig):
- '''Send a specific Unix signal to the sandbox process.
+ """Send a specific Unix signal to the sandbox process.
Args:
sig: The Unix signal to send to the process.
Returns:
Nothing.
- '''
+ """
self.log.action('kill %d' % sig)
self.p.kill(sig)
def validate_exited(self):
- '''Determine whether the sandbox process has exited.
+ """Determine whether the sandbox process has exited.
If required, this function waits a reasonable time for the process to
exit.
Returns:
Boolean indicating whether the process has exited.
- '''
+ """
p = self.p
self.p = None
import time
class Timeout(Exception):
- '''An exception sub-class that indicates that a timeout occurred.'''
+ """An exception sub-class that indicates that a timeout occurred."""
pass
class Spawn(object):
- '''Represents the stdio of a freshly created sub-process. Commands may be
+ """Represents the stdio of a freshly created sub-process. Commands may be
sent to the process, and responses waited for.
- '''
+ """
def __init__(self, args):
- '''Spawn (fork/exec) the sub-process.
+ """Spawn (fork/exec) the sub-process.
Args:
args: array of processs arguments. argv[0] is the command to execute.
Returns:
Nothing.
- '''
+ """
self.waited = False
self.buf = ''
self.poll.register(self.fd, select.POLLIN | select.POLLPRI | select.POLLERR | select.POLLHUP | select.POLLNVAL)
def kill(self, sig):
- '''Send unix signal "sig" to the child process.
+ """Send unix signal "sig" to the child process.
Args:
sig: The signal number to send.
Returns:
Nothing.
- '''
+ """
os.kill(self.pid, sig)
def isalive(self):
- '''Determine whether the child process is still running.
+ """Determine whether the child process is still running.
Args:
None.
Returns:
Boolean indicating whether process is alive.
- '''
+ """
if self.waited:
return False
return False
def send(self, data):
- '''Send data to the sub-process's stdin.
+ """Send data to the sub-process's stdin.
Args:
data: The data to send to the process.
Returns:
Nothing.
- '''
+ """
os.write(self.fd, data)
def expect(self, patterns):
- '''Wait for the sub-process to emit specific data.
+ """Wait for the sub-process to emit specific data.
This function waits for the process to emit one pattern from the
supplied list of patterns, or for a timeout to occur.
Notable exceptions:
Timeout, if the process did not emit any of the patterns within
the expected time.
- '''
+ """
for pi in xrange(len(patterns)):
if type(patterns[pi]) == type(''):
self.logfile_read.flush()
def close(self):
- '''Close the stdio connection to the sub-process.
+ """Close the stdio connection to the sub-process.
This also waits a reasonable time for the sub-process to stop running.
Returns:
Nothing.
- '''
+ """
os.close(self.fd)
for i in xrange(100):
import time
def md5sum_data(data):
- '''Calculate the MD5 hash of some data.
+ """Calculate the MD5 hash of some data.
Args:
data: The data to hash.
Returns:
The hash of the data, as a binary string.
- '''
+ """
h = hashlib.md5()
h.update(data)
return h.digest()
def md5sum_file(fn, max_length=None):
- '''Calculate the MD5 hash of the contents of a file.
+ """Calculate the MD5 hash of the contents of a file.
Args:
fn: The filename of the file to hash.
Returns:
The hash of the file content, as a binary string.
- '''
+ """
with open(fn, 'rb') as fh:
if max_length:
return md5sum_data(data)
class PersistentRandomFile(object):
- '''Generate and store information about a persistent file containing
- random data.'''
+ """Generate and store information about a persistent file containing
+ random data."""
def __init__(self, u_boot_console, fn, size):
- '''Create or process the persistent file.
+ """Create or process the persistent file.
If the file does not exist, it is generated.
Returns:
Nothing.
- '''
+ """
self.fn = fn
self.content_hash = md5sum_data(data)
def attempt_to_open_file(fn):
- '''Attempt to open a file, without throwing exceptions.
+ """Attempt to open a file, without throwing exceptions.
Any errors (exceptions) that occur during the attempt to open the file
are ignored. This is useful in order to test whether a file (in
Returns:
An open file handle to the file, or None if the file could not be
opened.
- '''
+ """
try:
return open(fn, 'rb')
return None
def wait_until_open_succeeds(fn):
- '''Poll until a file can be opened, or a timeout occurs.
+ """Poll until a file can be opened, or a timeout occurs.
Continually attempt to open a file, and return when this succeeds, or
raise an exception after a timeout.
Returns:
An open file handle to the file.
- '''
+ """
for i in xrange(100):
fh = attempt_to_open_file(fn)
raise Exception('File could not be opened')
def wait_until_file_open_fails(fn, ignore_errors):
- '''Poll until a file cannot be opened, or a timeout occurs.
+ """Poll until a file cannot be opened, or a timeout occurs.
Continually attempt to open a file, and return when this fails, or
raise an exception after a timeout.
Returns:
Nothing.
- '''
+ """
for i in xrange(100):
fh = attempt_to_open_file(fn)
raise Exception('File can still be opened')
def run_and_log(u_boot_console, cmd, ignore_errors=False):
- '''Run a command and log its output.
+ """Run a command and log its output.
Args:
u_boot_console: A console connection to U-Boot.
Returns:
Nothing.
- '''
+ """
runner = u_boot_console.log.get_runner(cmd[0], sys.stdout)
runner.run(cmd, ignore_errors=ignore_errors)
ram_base = None
def find_ram_base(u_boot_console):
- '''Find the running U-Boot's RAM location.
+ """Find the running U-Boot's RAM location.
Probe the running U-Boot to determine the address of the first bank
of RAM. This is useful for tests that test reading/writing RAM, or
Returns:
The address of U-Boot's first RAM bank, as an integer.
- '''
+ """
global ram_base
if u_boot_console.config.buildconfig.get('config_cmd_bdi', 'n') != 'y':