From: Dan Mick Date: Wed, 25 May 2016 17:25:21 +0000 (-0700) Subject: test_suite: recode to use patch.start() X-Git-Tag: 1.1.0~608^2~1 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=92e89b67846e3c2143c1475fef870d4da468b0ed;p=teuthology.git test_suite: recode to use patch.start() This lets us do all the setup/teardown in an organized way, in class instance methods, so the test cases don't have to deal with the mock arguments; all they do is pass the fake_fs to start_patchers to get the right fake_fs data for their particular test. Signed-off-by: Dan Mick --- diff --git a/teuthology/suite.py b/teuthology/suite.py index 843d7c6bbe..926cfabff2 100644 --- a/teuthology/suite.py +++ b/teuthology/suite.py @@ -991,12 +991,7 @@ def generate_combinations(path, mat, generate_from, generate_to): matrix.generate_paths(path, output, combine_path))) return ret -def build_matrix(path, - _exists=os.path.exists, - _isfile=os.path.isfile, - _isdir=os.path.isdir, - _listdir=os.listdir, - subset=None): +def build_matrix(path, subset=None): """ Return a list of items descibed by path such that if the list of items is chunked into mincyclicity pieces, each piece is still a @@ -1030,26 +1025,18 @@ def build_matrix(path, of chosen subitems. :param path: The path to search for yaml fragments - :param _exists: Custom os.path.exists(); for testing only - :param _isfile: Custom os.path.isfile(); for testing only - :param _isdir: Custom os.path.isdir(); for testing only - :param _listdir: Custom os.listdir(); for testing only :param subset: (index, outof) """ - mat, first, matlimit = _get_matrix( - path, _exists, _isfile, _isdir, _listdir, subset) + mat, first, matlimit = _get_matrix(path, subset) return generate_combinations(path, mat, first, matlimit) -def _get_matrix(path, _exists=os.path.exists, _isfile=os.path.isfile, - _isdir=os.path.isdir, - _listdir=os.listdir, - subset=None): +def _get_matrix(path, subset=None): mat = None first = None matlimit = None if subset: (index, outof) = subset - mat = _build_matrix(path, _exists, _isfile, _isdir, _listdir, mincyclicity=outof) + mat = _build_matrix(path, mincyclicity=outof) first = (mat.size() / outof) * index if index == outof or index == outof - 1: matlimit = mat.size() @@ -1057,22 +1044,21 @@ def _get_matrix(path, _exists=os.path.exists, _isfile=os.path.isfile, matlimit = (mat.size() / outof) * (index + 1) else: first = 0 - mat = _build_matrix(path, _exists, _isfile, _isdir, _listdir) + mat = _build_matrix(path) matlimit = mat.size() return mat, first, matlimit -def _build_matrix(path, _exists=os.path.exists, _isfile=os.path.isfile, - _isdir=os.path.isdir, _listdir=os.listdir, mincyclicity=0, item=''): - if not _exists(path): +def _build_matrix(path, mincyclicity=0, item=''): + if not os.path.exists(path): raise IOError('%s does not exist' % path) - if _isfile(path): + if os.path.isfile(path): if path.endswith('.yaml'): return matrix.Base(item) return None - if _isdir(path): + if os.path.isdir(path): if path.endswith('.disable'): return None - files = sorted(_listdir(path)) + files = sorted(os.listdir(path)) if len(files) == 0: return None if '+' in files: @@ -1082,10 +1068,6 @@ def _build_matrix(path, _exists=os.path.exists, _isfile=os.path.isfile, for fn in sorted(files): submat = _build_matrix( os.path.join(path, fn), - _exists, - _isfile, - _isdir, - _listdir, mincyclicity, fn) if submat is not None: @@ -1098,10 +1080,6 @@ def _build_matrix(path, _exists=os.path.exists, _isfile=os.path.isfile, for fn in sorted(files): submat = _build_matrix( os.path.join(path, fn), - _exists, - _isfile, - _isdir, - _listdir, mincyclicity=0, item=fn) if submat is not None: @@ -1118,10 +1096,6 @@ def _build_matrix(path, _exists=os.path.exists, _isfile=os.path.isfile, for fn in sorted(files): submat = _build_matrix( os.path.join(path, fn), - _exists, - _isfile, - _isdir, - _listdir, mincyclicity, fn) if submat is None: diff --git a/teuthology/test/test_describe_tests.py b/teuthology/test/test_describe_tests.py index c85e0f857f..19cb11e92c 100644 --- a/teuthology/test/test_describe_tests.py +++ b/teuthology/test/test_describe_tests.py @@ -107,7 +107,7 @@ class TestDescribeTests(object): def setup(self): self.fake_exists, self.fake_listdir, self.fake_isfile, - self.fake_isdir, self.fake_open = make_fake_fstools(realistic_fs) + self.fake_isdir, self.fake_open = make_fake_fstools(realistic_fs) @staticmethod def assert_expected_combo_headers(headers): diff --git a/teuthology/test/test_suite.py b/teuthology/test/test_suite.py index ca2a047c75..9256ca72af 100644 --- a/teuthology/test/test_suite.py +++ b/teuthology/test/test_suite.py @@ -1,7 +1,7 @@ from copy import deepcopy from datetime import datetime -from mock import patch, Mock, DEFAULT +from mock import patch, Mock, DEFAULT, MagicMock from fake_fs import make_fake_fstools from teuthology import suite @@ -436,6 +436,37 @@ class TestDistroDefaults(object): class TestBuildMatrix(object): + + patchpoints = [ + 'os.path.exists', + 'os.listdir', + 'os.path.isfile', + 'os.path.isdir', + '__builtin__.open', + ] + + def setup(self): + self.mocks = dict() + self.patchers = dict() + for ppoint in self.__class__.patchpoints: + self.mocks[ppoint] = MagicMock() + self.patchers[ppoint] = patch(ppoint, self.mocks[ppoint]) + + def start_patchers(self, fake_fs): + fake_fns = make_fake_fstools(fake_fs) + # relies on fake_fns being in same order as patchpoints + for ppoint, fn in zip(self.__class__.patchpoints, fake_fns): + self.mocks[ppoint].side_effect = fn + for patcher in self.patchers.values(): + patcher.start() + + def stop_patchers(self): + for patcher in self.patchers.values(): + patcher.stop() + + def teardown(self): + self.stop_patchers() + def fragment_occurences(self, jobs, fragment): # What fraction of jobs contain fragment? count = 0 @@ -463,9 +494,8 @@ class TestBuildMatrix(object): }, }, } - fake_exists, fake_listdir, fake_isfile, fake_isdir, _ = make_fake_fstools(fake_fs) - result = suite.build_matrix('d0_0', fake_exists, fake_isfile, fake_isdir, - fake_listdir) + self.start_patchers(fake_fs) + result = suite.build_matrix('d0_0') assert len(result) == 1 def test_convolve_2x2(self): @@ -482,9 +512,8 @@ class TestBuildMatrix(object): }, }, } - fake_exists, fake_listdir, fake_isfile, fake_isdir, _ = make_fake_fstools(fake_fs) - result = suite.build_matrix('d0_0', fake_exists, fake_isfile, fake_isdir, - fake_listdir) + self.start_patchers(fake_fs) + result = suite.build_matrix('d0_0') assert len(result) == 4 assert self.fragment_occurences(result, 'd1_1_1.yaml') == 0.5 @@ -506,9 +535,8 @@ class TestBuildMatrix(object): }, }, } - fake_exists, fake_listdir, fake_isfile, fake_isdir, _ = make_fake_fstools(fake_fs) - result = suite.build_matrix('d0_0', fake_exists, fake_isfile, fake_isdir, - fake_listdir) + self.start_patchers(fake_fs) + result = suite.build_matrix('d0_0') assert len(result) == 8 assert self.fragment_occurences(result, 'd1_2_0.yaml') == 0.5 @@ -531,9 +559,8 @@ class TestBuildMatrix(object): }, }, } - fake_exists, fake_listdir, fake_isfile, fake_isdir, _ = make_fake_fstools(fake_fs) - result = suite.build_matrix('d0_0', fake_exists, fake_isfile, fake_isdir, - fake_listdir) + self.start_patchers(fake_fs) + result = suite.build_matrix('d0_0') assert len(result) == 8 assert self.fragment_occurences(result, 'd1_2_2.yaml') == 0.25 @@ -557,9 +584,8 @@ class TestBuildMatrix(object): }, }, } - fake_exists, fake_listdir, fake_isfile, fake_isdir, _ = make_fake_fstools(fake_fs) - result = suite.build_matrix('d0_0', fake_exists, fake_isfile, fake_isdir, - fake_listdir) + self.start_patchers(fake_fs) + result = suite.build_matrix('d0_0') assert len(result) == 2 for i in result: assert 'd0_0/d1_2/d1_2_0.yaml' in i[1] @@ -594,9 +620,8 @@ class TestBuildMatrix(object): }, }, } - fake_exists, fake_listdir, fake_isfile, fake_isdir, _ = make_fake_fstools(fake_fs) - result = suite.build_matrix('teuthology/no-ceph', fake_exists, fake_isfile, - fake_isdir, fake_listdir) + self.start_patchers(fake_fs) + result = suite.build_matrix('teuthology/no-ceph') assert len(result) == 11 assert self.fragment_occurences(result, 'vps.yaml') == 1 / 11.0 @@ -627,9 +652,10 @@ class TestBuildMatrix(object): }, }, } - fake_exists, fake_listdir, fake_isfile, fake_isdir, _ = make_fake_fstools(fake_fs) - result = suite.build_matrix('teuthology/no-ceph', fake_exists, fake_isfile, - fake_isdir, fake_listdir) + self.start_patchers(fake_fs) + result = suite.build_matrix('teuthology/no-ceph') + self.stop_patchers() + fake_fs2 = { 'teuthology': { 'no-ceph': { @@ -658,9 +684,8 @@ class TestBuildMatrix(object): }, }, } - fake_exists2, fake_listdir2, fake_isfile2, fake_isdir2, _ = make_fake_fstools(fake_fs2) - result2 = suite.build_matrix('teuthology/no-ceph', fake_exists2, fake_isfile2, - fake_isdir2, fake_listdir2) + self.start_patchers(fake_fs2) + result2 = suite.build_matrix('teuthology/no-ceph') assert len(result) == 11 assert len(result2) == len(result) @@ -691,9 +716,10 @@ class TestBuildMatrix(object): }, }, } - fake_exists, fake_listdir, fake_isfile, fake_isdir, _ = make_fake_fstools(fake_fs) - result = suite.build_matrix('teuthology/no-ceph', fake_exists, fake_isfile, - fake_isdir, fake_listdir) + self.start_patchers(fake_fs) + result = suite.build_matrix('teuthology/no-ceph') + self.stop_patchers() + fake_fs2 = { 'teuthology': { 'no-ceph': { @@ -728,9 +754,8 @@ class TestBuildMatrix(object): }, }, } - fake_exists2, fake_listdir2, fake_isfile2, fake_isdir2, _ = make_fake_fstools(fake_fs2) - result2 = suite.build_matrix('teuthology/no-ceph', fake_exists2, fake_isfile2, - fake_isdir2, fake_listdir2) + self.start_patchers(fake_fs2) + result2 = suite.build_matrix('teuthology/no-ceph') assert len(result) == 11 assert len(result2) == len(result) @@ -750,9 +775,8 @@ class TestBuildMatrix(object): 'tasks': {'cfuse_workunit_suites_fsstress.yaml': None}, }, } - fake_exists, fake_listdir, fake_isfile, fake_isdir, _ = make_fake_fstools(fake_fs) - result = suite.build_matrix('thrash', fake_exists, fake_isfile, - fake_isdir, fake_listdir) + self.start_patchers(fake_fs) + result = suite.build_matrix('thrash') assert len(result) == 1 assert self.fragment_occurences(result, 'base.yaml') == 1 fragments = result[0][1] @@ -760,6 +784,35 @@ class TestBuildMatrix(object): assert fragments[1] == 'thrash/ceph-thrash/default.yaml' class TestSubset(object): + patchpoints = [ + 'os.path.exists', + 'os.listdir', + 'os.path.isfile', + 'os.path.isdir', + '__builtin__.open', + ] + + def setup(self): + self.mocks = dict() + self.patchers = dict() + for ppoint in self.__class__.patchpoints: + self.mocks[ppoint] = MagicMock() + self.patchers[ppoint] = patch(ppoint, self.mocks[ppoint]) + + def start_patchers(self, fake_fs): + fake_fns = make_fake_fstools(fake_fs) + # relies on fake_fns being in same order as patchpoints + for ppoint, fn in zip(self.__class__.patchpoints, fake_fns): + self.mocks[ppoint].side_effect = fn + for patcher in self.patchers.values(): + patcher.start() + + def stop_patchers(self): + for patcher in self.patchers.values(): + patcher.stop() + + # test_random() manages start/stop patchers on its own; no teardown + MAX_FACETS = 10 MAX_FANOUT = 3 MAX_DEPTH = 3 @@ -810,10 +863,8 @@ class TestSubset(object): @staticmethod def generate_description_list(tree, subset): - fake_exists, fake_listdir, fake_isfile, fake_isdir, _ = make_fake_fstools(tree) mat, first, matlimit = suite._get_matrix( - 'root', _exists=fake_exists, _isfile=fake_isfile, _isdir=fake_isdir, - _listdir=fake_listdir, subset=subset) + 'root', subset=subset) return [i[0] for i in suite.generate_combinations( 'root', mat, first, matlimit)], mat, first, matlimit @@ -868,8 +919,10 @@ class TestSubset(object): self.MAX_FANOUT, self.MAX_DEPTH) subset = self.generate_subset(self.MAX_SUBSET) + self.start_patchers(tree) dlist, mat, first, matlimit = self.generate_description_list(tree, subset) self.verify_facets(tree, dlist, subset, mat, first, matlimit) + self.stop_patchers() @patch('subprocess.check_output') def test_git_branch_exists(m_check_output):