2016-01-15 18:15:26 +00:00
|
|
|
# Copyright (c) 2015 Stephen Warren
|
|
|
|
# Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
# Test operation of shell commands relating to environment variables.
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
# FIXME: This might be useful for other tests;
|
|
|
|
# perhaps refactor it into ConsoleBase or some other state object?
|
|
|
|
class StateTestEnv(object):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Container that represents the state of all U-Boot environment variables.
|
2016-01-15 18:15:26 +00:00
|
|
|
This enables quick determination of existant/non-existant variable
|
|
|
|
names.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
def __init__(self, u_boot_console):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Initialize a new StateTestEnv object.
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
u_boot_console: A U-Boot console.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
self.u_boot_console = u_boot_console
|
|
|
|
self.get_env()
|
|
|
|
self.set_var = self.get_non_existent_var()
|
|
|
|
|
|
|
|
def get_env(self):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Read all current environment variables from U-Boot.
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
response = self.u_boot_console.run_command('printenv')
|
|
|
|
self.env = {}
|
|
|
|
for l in response.splitlines():
|
|
|
|
if not '=' in l:
|
|
|
|
continue
|
|
|
|
(var, value) = l.strip().split('=', 1)
|
|
|
|
self.env[var] = value
|
|
|
|
|
|
|
|
def get_existent_var(self):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Return the name of an environment variable that exists.
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The name of an environment variable.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
for var in self.env:
|
|
|
|
return var
|
|
|
|
|
|
|
|
def get_non_existent_var(self):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Return the name of an environment variable that does not exist.
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The name of an environment variable.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
n = 0
|
|
|
|
while True:
|
|
|
|
var = 'test_env_' + str(n)
|
|
|
|
if var not in self.env:
|
|
|
|
return var
|
|
|
|
n += 1
|
|
|
|
|
test/py: move U-Boot respawn trigger to the test core
Prior to this change, U-Boot was lazilly (re-)spawned if/when a test
attempted to interact with it, and no active connection existed. This
approach was simple, yet had the disadvantage that U-Boot might be
spawned in the middle of a test function, e.g. after the test had already
performed actions such as creating data files, etc. In that case, this
could cause the log to contain the sequence (1) some test logs, (2)
U-Boot's boot process, (3) the rest of that test's logs. This isn't
optimally readable. This issue will affect the upcoming DFU and enhanced
UMS tests.
This change converts u_boot_console to be a function-scoped fixture, so
that pytest attempts to re-create the object for each test invocation.
This allows the fixture factory function to ensure that U-Boot is spawned
prior to every test. In practice, the same object is returned each time
so there is essentially no additional overhead due to this change.
This allows us to remove:
- The explicit ensure_spawned() call from test_sleep, since the core now
ensures that the spawn happens before the test code is executed.
- The laxy calls to ensure_spawned() in the u_boot_console_*
implementations.
The one downside is that test_env's "state_ttest_env" fixture must be
converted to a function-scoped fixture too, since a module-scoped fixture
cannot use a function-scoped fixture. To avoid overhead, we use the same
trick of returning the same object each time.
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Acked-by: Simon Glass <sjg@chromium.org>
2016-01-22 19:30:08 +00:00
|
|
|
ste = None
|
|
|
|
@pytest.fixture(scope='function')
|
2016-01-15 18:15:26 +00:00
|
|
|
def state_test_env(u_boot_console):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""pytest fixture to provide a StateTestEnv object to tests."""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
test/py: move U-Boot respawn trigger to the test core
Prior to this change, U-Boot was lazilly (re-)spawned if/when a test
attempted to interact with it, and no active connection existed. This
approach was simple, yet had the disadvantage that U-Boot might be
spawned in the middle of a test function, e.g. after the test had already
performed actions such as creating data files, etc. In that case, this
could cause the log to contain the sequence (1) some test logs, (2)
U-Boot's boot process, (3) the rest of that test's logs. This isn't
optimally readable. This issue will affect the upcoming DFU and enhanced
UMS tests.
This change converts u_boot_console to be a function-scoped fixture, so
that pytest attempts to re-create the object for each test invocation.
This allows the fixture factory function to ensure that U-Boot is spawned
prior to every test. In practice, the same object is returned each time
so there is essentially no additional overhead due to this change.
This allows us to remove:
- The explicit ensure_spawned() call from test_sleep, since the core now
ensures that the spawn happens before the test code is executed.
- The laxy calls to ensure_spawned() in the u_boot_console_*
implementations.
The one downside is that test_env's "state_ttest_env" fixture must be
converted to a function-scoped fixture too, since a module-scoped fixture
cannot use a function-scoped fixture. To avoid overhead, we use the same
trick of returning the same object each time.
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Acked-by: Simon Glass <sjg@chromium.org>
2016-01-22 19:30:08 +00:00
|
|
|
global ste
|
|
|
|
if not ste:
|
|
|
|
ste = StateTestEnv(u_boot_console)
|
|
|
|
return ste
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
def unset_var(state_test_env, var):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Unset an environment variable.
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
This both executes a U-Boot shell command and updates a StateTestEnv
|
|
|
|
object.
|
|
|
|
|
|
|
|
Args:
|
2016-01-28 17:18:03 +00:00
|
|
|
state_test_env: The StateTestEnv object to update.
|
2016-01-15 18:15:26 +00:00
|
|
|
var: The variable name to unset.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
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):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Set an environment variable.
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
This both executes a U-Boot shell command and updates a StateTestEnv
|
|
|
|
object.
|
|
|
|
|
|
|
|
Args:
|
2016-01-28 17:18:03 +00:00
|
|
|
state_test_env: The StateTestEnv object to update.
|
2016-01-15 18:15:26 +00:00
|
|
|
var: The variable name to set.
|
|
|
|
value: The value to set the variable to.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
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):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Validate that a variable is not set, using U-Boot shell commands.
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
var: The variable name to test.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
response = state_test_env.u_boot_console.run_command('echo $%s' % var)
|
|
|
|
assert response == ''
|
|
|
|
|
|
|
|
def validate_set(state_test_env, var, value):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Validate that a variable is set, using U-Boot shell commands.
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
var: The variable name to test.
|
|
|
|
value: The value the variable is expected to have.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
# echo does not preserve leading, internal, or trailing whitespace in the
|
|
|
|
# value. printenv does, and hence allows more complete testing.
|
|
|
|
response = state_test_env.u_boot_console.run_command('printenv %s' % var)
|
|
|
|
assert response == ('%s=%s' % (var, value))
|
|
|
|
|
|
|
|
def test_env_echo_exists(state_test_env):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Test echoing a variable that exists."""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
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):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Test echoing a variable that doesn't exist."""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
var = state_test_env.set_var
|
|
|
|
validate_empty(state_test_env, var)
|
|
|
|
|
|
|
|
def test_env_printenv_non_existent(state_test_env):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Test printenv error message for non-existant variables."""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
var = state_test_env.set_var
|
|
|
|
c = state_test_env.u_boot_console
|
|
|
|
with c.disable_check('error_notification'):
|
|
|
|
response = c.run_command('printenv %s' % var)
|
|
|
|
assert(response == '## Error: "%s" not defined' % var)
|
|
|
|
|
|
|
|
def test_env_unset_non_existent(state_test_env):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Test unsetting a nonexistent variable."""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
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):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Test set a non-existant variable."""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
var = state_test_env.set_var
|
|
|
|
value = 'foo'
|
|
|
|
set_var(state_test_env, var, value)
|
|
|
|
validate_set(state_test_env, var, value)
|
|
|
|
|
|
|
|
def test_env_set_existing(state_test_env):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Test setting an existant variable."""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
var = state_test_env.set_var
|
|
|
|
value = 'bar'
|
|
|
|
set_var(state_test_env, var, value)
|
|
|
|
validate_set(state_test_env, var, value)
|
|
|
|
|
|
|
|
def test_env_unset_existing(state_test_env):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Test unsetting a variable."""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
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):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Test expanding a variable that contains a space in its value."""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
var_space = None
|
|
|
|
var_test = None
|
|
|
|
try:
|
|
|
|
var_space = state_test_env.get_non_existent_var()
|
|
|
|
set_var(state_test_env, var_space, ' ')
|
|
|
|
|
|
|
|
var_test = state_test_env.get_non_existent_var()
|
|
|
|
value = ' 1${%(var_space)s}${%(var_space)s} 2 ' % locals()
|
|
|
|
set_var(state_test_env, var_test, value)
|
|
|
|
value = ' 1 2 '
|
|
|
|
validate_set(state_test_env, var_test, value)
|
|
|
|
finally:
|
|
|
|
if var_space:
|
|
|
|
unset_var(state_test_env, var_space)
|
|
|
|
if var_test:
|
|
|
|
unset_var(state_test_env, var_test)
|