From dffd56d1d270e4797e43272a6c9000b8b8aeaf29 Mon Sep 17 00:00:00 2001 From: Paul Burton Date: Thu, 14 Sep 2017 14:34:43 -0700 Subject: [PATCH 1/6] test/py: Make print statements python 3.x safe In python 3.x print must be called as a function rather than used as a statement. Update uses of print to the function call syntax in order to be python 3.x safe. Signed-off-by: Paul Burton Reviewed-by: Stephen Warren --- test/py/conftest.py | 2 +- test/py/test.py | 6 ++++-- test/py/tests/test_fit.py | 12 +++++++----- test/py/u_boot_console_sandbox.py | 2 +- test/py/u_boot_spawn.py | 2 +- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/test/py/conftest.py b/test/py/conftest.py index 446d8cb..e591f58 100644 --- a/test/py/conftest.py +++ b/test/py/conftest.py @@ -586,7 +586,7 @@ def pytest_runtest_protocol(item, nextitem): # is fixed, if this exception still exists, it will then be logged as # part of the test's stdout. import traceback - print 'Exception occurred while logging runtest status:' + print('Exception occurred while logging runtest status:') traceback.print_exc() # FIXME: Can we force a test failure here? diff --git a/test/py/test.py b/test/py/test.py index 04baf8d..a514094 100755 --- a/test/py/test.py +++ b/test/py/test.py @@ -7,6 +7,8 @@ # Wrapper script to invoke pytest with the directory name that contains the # U-Boot tests. +from __future__ import print_function + import os import os.path import sys @@ -26,7 +28,7 @@ except: traceback.print_exc() # Hint to the user that they likely simply haven't installed the required # dependencies. - print >>sys.stderr, ''' + print(''' exec(py.test) failed; perhaps you are missing some dependencies? -See test/py/README.md for the list.''' +See test/py/README.md for the list.''', file=sys.stderr) sys.exit(1) diff --git a/test/py/tests/test_fit.py b/test/py/tests/test_fit.py index e407ccc..a0f9350 100755 --- a/test/py/tests/test_fit.py +++ b/test/py/tests/test_fit.py @@ -3,6 +3,8 @@ # # Sanity check of the FIT handling in U-Boot +from __future__ import print_function + import os import pytest import struct @@ -153,7 +155,7 @@ def test_fit(u_boot_console): src = make_fname('u-boot.dts') dtb = make_fname('u-boot.dtb') with open(src, 'w') as fd: - print >> fd, base_fdt + print(base_fdt, file=fd) util.run_and_log(cons, ['dtc', src, '-O', 'dtb', '-o', dtb]) return dtb @@ -167,7 +169,7 @@ def test_fit(u_boot_console): """ its = make_fname('test.its') with open(its, 'w') as fd: - print >> fd, base_its % params + print(base_its % params, file=fd) return its def make_fit(mkimage, params): @@ -186,7 +188,7 @@ def test_fit(u_boot_console): its = make_its(params) util.run_and_log(cons, [mkimage, '-f', its, fit]) with open(make_fname('u-boot.dts'), 'w') as fd: - print >> fd, base_fdt + print(base_fdt, file=fd) return fit def make_kernel(filename, text): @@ -202,7 +204,7 @@ def test_fit(u_boot_console): for i in range(100): data += 'this %s %d is unlikely to boot\n' % (text, i) with open(fname, 'w') as fd: - print >> fd, data + print(data, file=fd) return fname def make_ramdisk(filename, text): @@ -216,7 +218,7 @@ def test_fit(u_boot_console): for i in range(100): data += '%s %d was seldom used in the middle ages\n' % (text, i) with open(fname, 'w') as fd: - print >> fd, data + print(data, file=fd) return fname def find_matching(text, match): diff --git a/test/py/u_boot_console_sandbox.py b/test/py/u_boot_console_sandbox.py index a616cfb..c901397 100644 --- a/test/py/u_boot_console_sandbox.py +++ b/test/py/u_boot_console_sandbox.py @@ -41,7 +41,7 @@ class ConsoleSandbox(ConsoleBase): bcfg = self.config.buildconfig config_spl = bcfg.get('config_spl', 'n') == 'y' fname = '/spl/u-boot-spl' if config_spl else '/u-boot' - print fname + print(fname) cmd = [] if self.config.gdbserver: cmd += ['gdbserver', self.config.gdbserver] diff --git a/test/py/u_boot_spawn.py b/test/py/u_boot_spawn.py index 108498a..77a010a 100644 --- a/test/py/u_boot_spawn.py +++ b/test/py/u_boot_spawn.py @@ -58,7 +58,7 @@ class Spawn(object): os.chdir(cwd) os.execvp(args[0], args) except: - print 'CHILD EXECEPTION:' + print('CHILD EXECEPTION:') import traceback traceback.print_exc() finally: From b8c455500a08c75c4809e523d348027e72cda7ec Mon Sep 17 00:00:00 2001 From: Paul Burton Date: Thu, 14 Sep 2017 14:34:44 -0700 Subject: [PATCH 2/6] test/py: Use range() rather than xrange() In python 3.x the xrange() function has been removed, and range() returns an iterator much like Python 2.x's xrange(). Simply use range() in place of xrange() in order to work on both python 2.x & 3.x. This will mean a small cost on python 2.x since range() will return a list there rather than an iterator, but the cost should be negligible. Signed-off-by: Paul Burton Reviewed-by: Stephen Warren --- test/py/u_boot_console_sandbox.py | 2 +- test/py/u_boot_spawn.py | 6 +++--- test/py/u_boot_utils.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/py/u_boot_console_sandbox.py b/test/py/u_boot_console_sandbox.py index c901397..778f6d0 100644 --- a/test/py/u_boot_console_sandbox.py +++ b/test/py/u_boot_console_sandbox.py @@ -81,7 +81,7 @@ class ConsoleSandbox(ConsoleBase): p = self.p self.p = None - for i in xrange(100): + for i in range(100): ret = not p.isalive() if ret: break diff --git a/test/py/u_boot_spawn.py b/test/py/u_boot_spawn.py index 77a010a..b011a3e 100644 --- a/test/py/u_boot_spawn.py +++ b/test/py/u_boot_spawn.py @@ -134,7 +134,7 @@ class Spawn(object): the expected time. """ - for pi in xrange(len(patterns)): + for pi in range(len(patterns)): if type(patterns[pi]) == type(''): patterns[pi] = re.compile(patterns[pi]) @@ -143,7 +143,7 @@ class Spawn(object): while True: earliest_m = None earliest_pi = None - for pi in xrange(len(patterns)): + for pi in range(len(patterns)): pattern = patterns[pi] m = pattern.search(self.buf) if not m: @@ -198,7 +198,7 @@ class Spawn(object): """ os.close(self.fd) - for i in xrange(100): + for i in range(100): if not self.isalive(): break time.sleep(0.1) diff --git a/test/py/u_boot_utils.py b/test/py/u_boot_utils.py index bb31e57..d68d1dd 100644 --- a/test/py/u_boot_utils.py +++ b/test/py/u_boot_utils.py @@ -120,7 +120,7 @@ def wait_until_open_succeeds(fn): An open file handle to the file. """ - for i in xrange(100): + for i in range(100): fh = attempt_to_open_file(fn) if fh: return fh @@ -143,7 +143,7 @@ def wait_until_file_open_fails(fn, ignore_errors): Nothing. """ - for i in xrange(100): + for i in range(100): fh = attempt_to_open_file(fn) if not fh: return From 052ca37daa20a9825d7ce905d632e349f434058d Mon Sep 17 00:00:00 2001 From: Paul Burton Date: Thu, 14 Sep 2017 14:34:45 -0700 Subject: [PATCH 3/6] test/py: Import 'configparser' lower case to be python 3.x safe In python 3.x the configparser module is named with all lower case. Import it as such in order to avoid errors when running on python 3.x, and fall back to the CamelCase version in order to keep working with python 2.x. Signed-off-by: Paul Burton Reviewed-by: Stephen Warren --- test/py/conftest.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/py/conftest.py b/test/py/conftest.py index e591f58..5c658b8 100644 --- a/test/py/conftest.py +++ b/test/py/conftest.py @@ -18,11 +18,15 @@ import os import os.path import pytest from _pytest.runner import runtestprotocol -import ConfigParser import re import StringIO import sys +try: + import configparser +except: + import ConfigParser as configparser + # Globals: The HTML log file, and the connection to the U-Boot console. log = None console = None @@ -166,7 +170,7 @@ def pytest_configure(config): with open(dot_config, 'rt') as f: ini_str = '[root]\n' + f.read() ini_sio = StringIO.StringIO(ini_str) - parser = ConfigParser.RawConfigParser() + parser = configparser.RawConfigParser() parser.readfp(ini_sio) ubconfig.buildconfig.update(parser.items('root')) From 57bf9bea340690724df9de8ad82bf0d716efaa5b Mon Sep 17 00:00:00 2001 From: Paul Burton Date: Thu, 14 Sep 2017 14:34:48 -0700 Subject: [PATCH 4/6] test/py: fit: Open files as binary files The read_file() function in test_fit is used with files that are not text files, as well as some that are. It is never used in a way that requires it to decode text files to characters, so open all files in binary mode such that read() doesn't attempt to decode characters for files which are not text files. Without this test_fit fails on python 3.x when reading an FDT in run_fit_test() with: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 0: invalid continuation byte Signed-off-by: Paul Burton Reviewed-by: Stephen Warren --- test/py/tests/test_fit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/py/tests/test_fit.py b/test/py/tests/test_fit.py index a0f9350..34696e9 100755 --- a/test/py/tests/test_fit.py +++ b/test/py/tests/test_fit.py @@ -143,7 +143,7 @@ def test_fit(u_boot_console): Returns: Contents of file as a string """ - with open(fname, 'r') as fd: + with open(fname, 'rb') as fd: return fd.read() def make_dtb(): From 9f9e8a4dda0b09574a9188a5576a8768e3d818be Mon Sep 17 00:00:00 2001 From: Paul Burton Date: Thu, 14 Sep 2017 14:34:49 -0700 Subject: [PATCH 5/6] test/py: hush_if_test: Use open() in place of file() In python 3.x the file() function has been removed. Use open() instead, which works on both python 2.x & 3.x, and is described as the preferred method of opening a file by python 2.x documentation anyway. Signed-off-by: Paul Burton Reviewed-by: Stephen Warren --- test/py/tests/test_hush_if_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/py/tests/test_hush_if_test.py b/test/py/tests/test_hush_if_test.py index 1196e0a..bba8d41 100644 --- a/test/py/tests/test_hush_if_test.py +++ b/test/py/tests/test_hush_if_test.py @@ -148,7 +148,7 @@ def test_hush_if_test_host_file_exists(u_boot_console): exec_hush_if(u_boot_console, expr, False) try: - with file(test_file, 'wb'): + with open(test_file, 'wb'): pass assert os.path.exists(test_file) From 8793631ec13ee9e6c7189a7bdca38dde7b4390a8 Mon Sep 17 00:00:00 2001 From: Paul Burton Date: Thu, 14 Sep 2017 14:34:50 -0700 Subject: [PATCH 6/6] test/py: vboot: Remove stderr redirect from openssl command The openssl command specified in test_with_algo() ultimately ends up being run by RunAndLog::run(), which uses it to construct a Popen object with the default shell=False. The stderr redirect in the command is therefore simply passed to openssl as an argument. With at least openssl 1.1.0f this causes openssl, and therefore test_vboot, to fail with: genpkey: Use -help for summary. Exit code: 1 Any stderr output ought to be captured & stored in the RunAndLog object's output field and returned from run() via run_and_log() to test_with_algo() which then ignores it anyway, so we can drop the shell-like redirection with no ill effects. With this fix test_vboot now passes for me. Signed-off-by: Paul Burton Reviewed-by: Stephen Warren --- test/py/tests/test_vboot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/py/tests/test_vboot.py b/test/py/tests/test_vboot.py index ee939f2..3009529 100644 --- a/test/py/tests/test_vboot.py +++ b/test/py/tests/test_vboot.py @@ -177,8 +177,8 @@ def test_vboot(u_boot_console): public_exponent = 65537 util.run_and_log(cons, 'openssl genpkey -algorithm RSA -out %sdev.key ' '-pkeyopt rsa_keygen_bits:2048 ' - '-pkeyopt rsa_keygen_pubexp:%d ' - '2>/dev/null' % (tmpdir, public_exponent)) + '-pkeyopt rsa_keygen_pubexp:%d' % + (tmpdir, public_exponent)) # Create a certificate containing the public key util.run_and_log(cons, 'openssl req -batch -new -x509 -key %sdev.key -out '