]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
test_suite: recode to use patch.start()
authorDan Mick <dan.mick@redhat.com>
Wed, 25 May 2016 17:25:21 +0000 (10:25 -0700)
committerDan Mick <dan.mick@redhat.com>
Fri, 27 May 2016 22:13:45 +0000 (15:13 -0700)
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 <dan.mick@redhat.com>
teuthology/suite.py
teuthology/test/test_describe_tests.py
teuthology/test/test_suite.py

index 843d7c6bbe5bf0c0b3efd902e17114dc3b3e4951..926cfabff2342c46f1bb586da3e9fbf1f0b25041 100644 (file)
@@ -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:
index c85e0f857fb6d9bec9fd484b187dcf48c4d77615..19cb11e92ce8ce4956137179968dcf309194568d 100644 (file)
@@ -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):
index ca2a047c758f06e811c66dedbc21330611b91862..9256ca72af8b6582e1762bd936339a1c67d1a835 100644 (file)
@@ -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):