From: Josh Durgin Date: Fri, 20 Nov 2015 22:13:07 +0000 (-0800) Subject: test: move fs fakes to a common file X-Git-Tag: 1.1.0~727^2~17 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=ea6f26b7e975be763fb3b41e31de9a322c3e2fdc;p=teuthology.git test: move fs fakes to a common file This will be useful for other tests shortly. Signed-off-by: Josh Durgin --- diff --git a/teuthology/test/fake_fs.py b/teuthology/test/fake_fs.py new file mode 100644 index 000000000..e3f099080 --- /dev/null +++ b/teuthology/test/fake_fs.py @@ -0,0 +1,67 @@ + +def make_fake_fstools(fake_filesystem): + """ + Build a fake listdir() and isfile(), to be used instead of + os.listir() and os.isfile() + + An example fake_filesystem value: + >>> fake_fs = { + 'a_directory': { + 'another_directory': { + 'a_file': None, + 'another_file': None, + }, + 'random_file': None, + 'yet_another_directory': { + 'empty_directory': {}, + }, + }, + } + + >>> fake_listdir = make_fake_listdir(fake_fs) + >>> fake_listdir('a_directory/yet_another_directory') + ['empty_directory'] + >>> fake_isfile('a_directory/yet_another_directory') + False + + :param fake_filesystem: A dict representing a filesystem layout + """ + assert isinstance(fake_filesystem, dict) + + def fake_listdir(path, fsdict=False): + if fsdict is False: + fsdict = fake_filesystem + + remainder = path.strip('/') + '/' + subdict = fsdict + while '/' in remainder: + next_dir, remainder = remainder.split('/', 1) + if next_dir not in subdict: + raise OSError( + '[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) + if subdict and not remainder: + return subdict.keys() + return [] + + def fake_isfile(path, fsdict=False): + if fsdict is False: + fsdict = fake_filesystem + + components = path.strip('/').split('/') + subdict = fsdict + for component in components: + if component not in subdict: + raise OSError( + '[Errno 2] No such file or directory: %s' % component) + subdict = subdict.get(component) + if subdict is None: + return True + else: + return False + + def fake_isdir(path, fsdict=False): + return not fake_isfile(path) + return fake_listdir, fake_isfile, fake_isdir diff --git a/teuthology/test/test_suite.py b/teuthology/test/test_suite.py index e1ecd4ce7..a0a5b0875 100644 --- a/teuthology/test/test_suite.py +++ b/teuthology/test/test_suite.py @@ -3,6 +3,7 @@ from datetime import datetime from mock import patch, Mock, DEFAULT +from fake_fs import make_fake_fstools from teuthology import suite from scripts.suite import main from teuthology.config import config @@ -358,74 +359,6 @@ class TestDistroDefaults(object): 'rpm') -def make_fake_fstools(fake_filesystem): - """ - Build a fake listdir() and isfile(), to be used instead of - os.listir() and os.isfile() - - An example fake_filesystem value: - >>> fake_fs = { - 'a_directory': { - 'another_directory': { - 'a_file': None, - 'another_file': None, - }, - 'random_file': None, - 'yet_another_directory': { - 'empty_directory': {}, - }, - }, - } - - >>> fake_listdir = make_fake_listdir(fake_fs) - >>> fake_listdir('a_directory/yet_another_directory') - ['empty_directory'] - >>> fake_isfile('a_directory/yet_another_directory') - False - - :param fake_filesystem: A dict representing a filesystem layout - """ - assert isinstance(fake_filesystem, dict) - - def fake_listdir(path, fsdict=False): - if fsdict is False: - fsdict = fake_filesystem - - remainder = path.strip('/') + '/' - subdict = fsdict - while '/' in remainder: - next_dir, remainder = remainder.split('/', 1) - if next_dir not in subdict: - raise OSError( - '[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) - if subdict and not remainder: - return subdict.keys() - return [] - - def fake_isfile(path, fsdict=False): - if fsdict is False: - fsdict = fake_filesystem - - components = path.strip('/').split('/') - subdict = fsdict - for component in components: - if component not in subdict: - raise OSError( - '[Errno 2] No such file or directory: %s' % component) - subdict = subdict.get(component) - if subdict is None: - return True - else: - return False - - def fake_isdir(path, fsdict=False): - return not fake_isfile(path) - return fake_listdir, fake_isfile, fake_isdir - - class TestBuildMatrix(object): def fragment_occurences(self, jobs, fragment): # What fraction of jobs contain fragment?