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
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()
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:
for fn in sorted(files):
submat = _build_matrix(
os.path.join(path, fn),
- _exists,
- _isfile,
- _isdir,
- _listdir,
mincyclicity,
fn)
if submat is not None:
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:
for fn in sorted(files):
submat = _build_matrix(
os.path.join(path, fn),
- _exists,
- _isfile,
- _isdir,
- _listdir,
mincyclicity,
fn)
if submat is None:
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
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
},
},
}
- 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):
},
},
}
- 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
},
},
}
- 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
},
},
}
- 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
},
},
}
- 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]
},
},
}
- 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
},
},
}
- 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': {
},
},
}
- 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)
},
},
}
- 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': {
},
},
}
- 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)
'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]
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
@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
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):