From 53f92c8269e2f6fc90712aed74a27f19fef57b1f Mon Sep 17 00:00:00 2001 From: Kyr Shatskyy Date: Fri, 13 Dec 2019 13:14:00 +0100 Subject: [PATCH] test/fake_fs: fix tests for py3 compatibility Signed-off-by: Kyr Shatskyy --- teuthology/test/fake_fs.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/teuthology/test/fake_fs.py b/teuthology/test/fake_fs.py index 5c764484a..5432d4141 100644 --- a/teuthology/test/fake_fs.py +++ b/teuthology/test/fake_fs.py @@ -1,7 +1,13 @@ -from cStringIO import StringIO +from io import BytesIO from contextlib import closing +try: + FileNotFoundError, NotADirectoryError +except NameError: + FileNotFoundError = NotADirectoryError = OSError + + def make_fake_fstools(fake_filesystem): """ Build fake versions of os.listdir(), os.isfile(), etc. for use in @@ -41,13 +47,13 @@ def make_fake_fstools(fake_filesystem): while '/' in remainder: next_dir, remainder = remainder.split('/', 1) if next_dir not in subdict: - raise OSError( + raise FileNotFoundError( '[Errno 2] No such file or directory: %s' % next_dir) subdict = subdict.get(next_dir) if not isinstance(subdict, dict): - raise OSError('[Errno 20] Not a directory: %s' % next_dir) + raise NotADirectoryError('[Errno 20] Not a directory: %s' % next_dir) if subdict and not remainder: - return subdict.keys() + return list(subdict) return [] def fake_isfile(path, fsdict=False): @@ -58,7 +64,7 @@ def make_fake_fstools(fake_filesystem): subdict = fsdict for component in components: if component not in subdict: - raise OSError( + raise FileNotFoundError( '[Errno 2] No such file or directory: %s' % component) subdict = subdict.get(component) return subdict is None or isinstance(subdict, str) @@ -80,7 +86,7 @@ def make_fake_fstools(fake_filesystem): if isinstance(subdict, dict): raise IOError('[Errno 21] Is a directory: %s' % path) elif subdict is None: - return closing(StringIO('')) - return closing(StringIO(subdict)) + return closing(BytesIO(b'')) + return closing(BytesIO(subdict.encode())) return fake_exists, fake_listdir, fake_isfile, fake_isdir, fake_open -- 2.47.3