[U-Boot] [PATCH v2 1/2] test/py: Allow to pass u_boot_log instead of console for run_and_log

The runner actually has no console dependency, only on the log provided by the console. Accept both u_boot_console or a multiplexed_log.
Signed-off-by: Stefan Brüns stefan.bruens@rwth-aachen.de --- test/py/u_boot_utils.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
diff --git a/test/py/u_boot_utils.py b/test/py/u_boot_utils.py index 2ba4baed07..b9a72ab1de 100644 --- a/test/py/u_boot_utils.py +++ b/test/py/u_boot_utils.py @@ -153,11 +153,11 @@ def wait_until_file_open_fails(fn, ignore_errors): return raise Exception('File can still be opened')
-def run_and_log(u_boot_console, cmd, ignore_errors=False): +def run_and_log(u_boot_console_or_log, cmd, ignore_errors=False): """Run a command and log its output.
Args: - u_boot_console: A console connection to U-Boot. + u_boot_console_or_log: A console connection to U-Boot, or a LogFile. cmd: The command to run, as an array of argv[], or a string. If a string, note that it is split up so that quoted spaces will not be preserved. E.g. "fred and" becomes ['"fred', 'and"'] @@ -171,25 +171,33 @@ def run_and_log(u_boot_console, cmd, ignore_errors=False): """ if isinstance(cmd, str): cmd = cmd.split() - runner = u_boot_console.log.get_runner(cmd[0], sys.stdout) + if hasattr(u_boot_console_or_log, 'get_runner'): + get_runner = u_boot_console_or_log.get_runner + else: + get_runner = u_boot_console_or_log.log.get_runner + runner = get_runner(cmd[0], sys.stdout) output = runner.run(cmd, ignore_errors=ignore_errors) runner.close() return output
-def run_and_log_expect_exception(u_boot_console, cmd, retcode, msg): +def run_and_log_expect_exception(u_boot_console_or_log, cmd, retcode, msg): """Run a command that is expected to fail.
This runs a command and checks that it fails with the expected return code and exception method. If not, an exception is raised.
Args: - u_boot_console: A console connection to U-Boot. + u_boot_console_or_log: A console connection to U-Boot, or a LogFile. cmd: The command to run, as an array of argv[]. retcode: Expected non-zero return code from the command. msg: String that should be contained within the command's output. """ + if hasattr(u_boot_console_or_log, 'get_runner'): + get_runner = u_boot_console_or_log.get_runner + else: + get_runner = u_boot_console_or_log.log.get_runner + runner = get_runner(cmd[0], sys.stdout) try: - runner = u_boot_console.log.get_runner(cmd[0], sys.stdout) runner.run(cmd) except Exception as e: assert(retcode == runner.exit_status)

Hi Stefan,
On 4 December 2016 at 19:52, Stefan Brüns stefan.bruens@rwth-aachen.de wrote:
The runner actually has no console dependency, only on the log provided by the console. Accept both u_boot_console or a multiplexed_log.
In that case I wonder if it would be better to change it to a log, only?
I will leave it for Stephen to comment on this.
Signed-off-by: Stefan Brüns stefan.bruens@rwth-aachen.de
test/py/u_boot_utils.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
Regards, Simon

On Dienstag, 6. Dezember 2016 22:47:42 CET Simon Glass wrote:
Hi Stefan,
On 4 December 2016 at 19:52, Stefan Brüns stefan.bruens@rwth-aachen.de
wrote:
The runner actually has no console dependency, only on the log provided by the console. Accept both u_boot_console or a multiplexed_log.
In that case I wonder if it would be better to change it to a log, only?
I will leave it for Stephen to comment on this.
Only accepting a log would require to change all callers, but that's a trivial task. If that's the preferred solution, I will prepare a patch.
Kind regards,
Stefan

Hi Stefan,
On 10 December 2016 at 14:35, Stefan Bruens stefan.bruens@rwth-aachen.de wrote:
On Dienstag, 6. Dezember 2016 22:47:42 CET Simon Glass wrote:
Hi Stefan,
On 4 December 2016 at 19:52, Stefan Brüns stefan.bruens@rwth-aachen.de
wrote:
The runner actually has no console dependency, only on the log provided by the console. Accept both u_boot_console or a multiplexed_log.
In that case I wonder if it would be better to change it to a log, only?
I will leave it for Stephen to comment on this.
Only accepting a log would require to change all callers, but that's a trivial task. If that's the preferred solution, I will prepare a patch.
I haven't looked in detail but I don't see a problem with changing the callers.
Regards, Simon

From: Stefan Brüns stefan.bruens@rwth-aachen.de
The runner actually has no console dependency, only on the log provided by the console.
Signed-off-by: Stefan Brüns stefan.bruens@rwth-aachen.de --- Alternate approach to the previous patch, always pass the logfile, change all callers accordingly.
test/py/tests/test_dfu.py | 2 +- test/py/tests/test_ums.py | 10 +++++----- test/py/tests/test_vboot.py | 18 +++++++++--------- test/py/u_boot_utils.py | 12 ++++++------ 4 files changed, 21 insertions(+), 21 deletions(-)
diff --git a/test/py/tests/test_dfu.py b/test/py/tests/test_dfu.py index 585e6b29d7..b4f9a32abc 100644 --- a/test/py/tests/test_dfu.py +++ b/test/py/tests/test_dfu.py @@ -206,7 +206,7 @@ def test_dfu(u_boot_console, env__usb_dev_port, env__dfu_config): cmd = ['dfu-util', '-a', alt_setting, up_dn_load_arg, fn] if 'host_usb_port_path' in env__usb_dev_port: cmd += ['-p', env__usb_dev_port['host_usb_port_path']] - u_boot_utils.run_and_log(u_boot_console, cmd) + u_boot_utils.run_and_log(u_boot_console.log, cmd) u_boot_console.wait_for('Ctrl+C to exit ...')
def dfu_write(alt_setting, fn): diff --git a/test/py/tests/test_ums.py b/test/py/tests/test_ums.py index 8c3ee2b053..54c47b0aee 100644 --- a/test/py/tests/test_ums.py +++ b/test/py/tests/test_ums.py @@ -156,7 +156,7 @@ def test_ums(u_boot_console, env__usb_dev_port, env__block_devs):
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) + u_boot_utils.run_and_log(u_boot_console.log, cmd)
def umount(ignore_errors): """Unmount the block device that U-Boot exports. @@ -173,7 +173,7 @@ def test_ums(u_boot_console, env__usb_dev_port, env__block_devs):
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) + u_boot_utils.run_and_log(u_boot_console.log, cmd, ignore_errors)
def stop_ums(ignore_errors): """Stop U-Boot's ums shell command from executing. @@ -207,11 +207,11 @@ def test_ums(u_boot_console, env__usb_dev_port, env__block_devs): mount() u_boot_console.log.action('Writing test file via UMS') cmd = ('rm', '-f', mounted_test_fn) - u_boot_utils.run_and_log(u_boot_console, cmd) + u_boot_utils.run_and_log(u_boot_console.log, cmd) if os.path.exists(mounted_test_fn): raise Exception('Could not rm target UMS test file') cmd = ('cp', test_f.abs_fn, mounted_test_fn) - u_boot_utils.run_and_log(u_boot_console, cmd) + u_boot_utils.run_and_log(u_boot_console.log, cmd) ignore_cleanup_errors = False finally: umount(ignore_errors=ignore_cleanup_errors) @@ -226,7 +226,7 @@ def test_ums(u_boot_console, env__usb_dev_port, env__block_devs): u_boot_console.log.action('Reading test file back via UMS') read_back_hash = u_boot_utils.md5sum_file(mounted_test_fn) cmd = ('rm', '-f', mounted_test_fn) - u_boot_utils.run_and_log(u_boot_console, cmd) + u_boot_utils.run_and_log(u_boot_console.log, cmd) ignore_cleanup_errors = False finally: umount(ignore_errors=ignore_cleanup_errors) diff --git a/test/py/tests/test_vboot.py b/test/py/tests/test_vboot.py index 6e62820743..0f893f1e91 100644 --- a/test/py/tests/test_vboot.py +++ b/test/py/tests/test_vboot.py @@ -50,7 +50,7 @@ def test_vboot(u_boot_console): dts: Device tree file to compile. """ dtb = dts.replace('.dts', '.dtb') - util.run_and_log(cons, 'dtc %s %s%s -O dtb ' + util.run_and_log(cons.log, 'dtc %s %s%s -O dtb ' '-o %s%s' % (dtc_args, datadir, dts, tmpdir, dtb))
def run_bootm(sha_algo, test_type, expect_string, boots): @@ -85,7 +85,7 @@ def test_vboot(u_boot_console): Args: its: Filename containing .its source. """ - util.run_and_log(cons, [mkimage, '-D', dtc_args, '-f', + util.run_and_log(cons.log, [mkimage, '-D', dtc_args, '-f', '%s%s' % (datadir, its), fit])
def sign_fit(sha_algo): @@ -99,7 +99,7 @@ def test_vboot(u_boot_console): use. """ cons.log.action('%s: Sign images' % sha_algo) - util.run_and_log(cons, [mkimage, '-F', '-k', tmpdir, '-K', dtb, + util.run_and_log(cons.log, [mkimage, '-F', '-k', tmpdir, '-K', dtb, '-r', fit])
def test_with_algo(sha_algo): @@ -140,23 +140,23 @@ def test_vboot(u_boot_console):
cons.log.action('%s: Check signed config on the host' % sha_algo)
- util.run_and_log(cons, [fit_check_sign, '-f', fit, '-k', tmpdir, + util.run_and_log(cons.log, [fit_check_sign, '-f', fit, '-k', tmpdir, '-k', dtb])
# Increment the first byte of the signature, which should cause failure - sig = util.run_and_log(cons, 'fdtget -t bx %s %s value' % + sig = util.run_and_log(cons.log, 'fdtget -t bx %s %s value' % (fit, sig_node)) byte_list = sig.split() byte = int(byte_list[0], 16) byte_list[0] = '%x' % (byte + 1) sig = ' '.join(byte_list) - util.run_and_log(cons, 'fdtput -t bx %s %s value %s' % + util.run_and_log(cons.log, 'fdtput -t bx %s %s value %s' % (fit, sig_node, sig))
run_bootm(sha_algo, 'Signed config with bad hash', 'Bad Data Hash', False)
cons.log.action('%s: Check bad config on the host' % sha_algo) - util.run_and_log_expect_exception(cons, [fit_check_sign, '-f', fit, + util.run_and_log_expect_exception(cons.log, [fit_check_sign, '-f', fit, '-k', dtb], 1, 'Failed to verify required signature')
cons = u_boot_console @@ -172,13 +172,13 @@ def test_vboot(u_boot_console):
# Create an RSA key pair public_exponent = 65537 - util.run_and_log(cons, 'openssl genpkey -algorithm RSA -out %sdev.key ' + util.run_and_log(cons.log, 'openssl genpkey -algorithm RSA -out %sdev.key ' '-pkeyopt rsa_keygen_bits:2048 ' '-pkeyopt rsa_keygen_pubexp:%d ' '2>/dev/null' % (tmpdir, public_exponent))
# Create a certificate containing the public key - util.run_and_log(cons, 'openssl req -batch -new -x509 -key %sdev.key -out ' + util.run_and_log(cons.log, 'openssl req -batch -new -x509 -key %sdev.key -out ' '%sdev.crt' % (tmpdir, tmpdir))
# Create a number kernel image with zeroes diff --git a/test/py/u_boot_utils.py b/test/py/u_boot_utils.py index 2ba4baed07..4c6dae3b4a 100644 --- a/test/py/u_boot_utils.py +++ b/test/py/u_boot_utils.py @@ -153,11 +153,11 @@ def wait_until_file_open_fails(fn, ignore_errors): return raise Exception('File can still be opened')
-def run_and_log(u_boot_console, cmd, ignore_errors=False): +def run_and_log(log, cmd, ignore_errors=False): """Run a command and log its output.
Args: - u_boot_console: A console connection to U-Boot. + log: A LogFile instance. cmd: The command to run, as an array of argv[], or a string. If a string, note that it is split up so that quoted spaces will not be preserved. E.g. "fred and" becomes ['"fred', 'and"'] @@ -171,25 +171,25 @@ def run_and_log(u_boot_console, cmd, ignore_errors=False): """ if isinstance(cmd, str): cmd = cmd.split() - runner = u_boot_console.log.get_runner(cmd[0], sys.stdout) + runner = log.get_runner(cmd[0], sys.stdout) output = runner.run(cmd, ignore_errors=ignore_errors) runner.close() return output
-def run_and_log_expect_exception(u_boot_console, cmd, retcode, msg): +def run_and_log_expect_exception(log, cmd, retcode, msg): """Run a command that is expected to fail.
This runs a command and checks that it fails with the expected return code and exception method. If not, an exception is raised.
Args: - u_boot_console: A console connection to U-Boot. + log: A LogFile instance. cmd: The command to run, as an array of argv[]. retcode: Expected non-zero return code from the command. msg: String that should be contained within the command's output. """ + runner = log.get_runner(cmd[0], sys.stdout) try: - runner = u_boot_console.log.get_runner(cmd[0], sys.stdout) runner.run(cmd) except Exception as e: assert(retcode == runner.exit_status)

On 12/11/2016 02:58 PM, Stefan Brüns wrote:
From: Stefan Brüns stefan.bruens@rwth-aachen.de
The runner actually has no console dependency, only on the log provided by the console.
Acked-by: Stephen Warren swarren@nvidia.com

On 12/04/2016 05:52 PM, Stefan Brüns wrote:
The runner actually has no console dependency, only on the log provided by the console. Accept both u_boot_console or a multiplexed_log.
Either this approach or updating all callers to always pass the log object is fine by me. So if you need it,
Acked-by: Stephen Warren swarren@nvidia.com
One nit:
diff --git a/test/py/u_boot_utils.py b/test/py/u_boot_utils.py
- if hasattr(u_boot_console_or_log, 'get_runner'):
get_runner = u_boot_console_or_log.get_runner
- else:
get_runner = u_boot_console_or_log.log.get_runner
- runner = get_runner(cmd[0], sys.stdout)
- if hasattr(u_boot_console_or_log, 'get_runner'):
get_runner = u_boot_console_or_log.get_runner
- else:
get_runner = u_boot_console_or_log.log.get_runner
- runner = get_runner(cmd[0], sys.stdout)
It would be nice to put that into a utility function so as not to duplicate it, if you keep this approach.
participants (4)
-
Simon Glass
-
Stefan Bruens
-
Stefan Brüns
-
Stephen Warren