From: Kyr Shatskyy Date: Fri, 13 Dec 2019 12:14:00 +0000 (+0100) Subject: test/fake_fs: fix tests for py3 compatibility X-Git-Tag: 1.1.0~177^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=53f92c8269e2f6fc90712aed74a27f19fef57b1f;p=teuthology.git test/fake_fs: fix tests for py3 compatibility Signed-off-by: Kyr Shatskyy --- diff --git a/teuthology/test/fake_fs.py b/teuthology/test/fake_fs.py index 5c764484a1..5432d41416 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