]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
test/fake_fs: fix tests for py3 compatibility 1382/head
authorKyr Shatskyy <kyrylo.shatskyy@gmail.com>
Fri, 13 Dec 2019 12:14:00 +0000 (13:14 +0100)
committerKyr Shatskyy <kyrylo.shatskyy@gmail.com>
Fri, 13 Dec 2019 16:49:38 +0000 (17:49 +0100)
Signed-off-by: Kyr Shatskyy <kyrylo.shatskyy@suse.com>
teuthology/test/fake_fs.py

index 5c764484a1e4c4314d8c524b079c1c16e083d359..5432d41416da81107082b07bf5906d2cca2d83fb 100644 (file)
@@ -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