From: Josh Durgin Date: Sat, 21 Nov 2015 00:31:19 +0000 (-0800) Subject: teuthology-tree: add unit tests X-Git-Tag: 1.1.0~727^2~15 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=84f52d8355d09f0b3a88ca717c23934f5666f996;p=teuthology.git teuthology-tree: add unit tests Signed-off-by: Josh Durgin --- diff --git a/teuthology/test/test_tree.py b/teuthology/test/test_tree.py new file mode 100644 index 000000000..1f6a2d96e --- /dev/null +++ b/teuthology/test/test_tree.py @@ -0,0 +1,135 @@ +# -*- coding: utf-8 -*- +from fake_fs import make_fake_fstools +from teuthology.tree import tree_with_info, extract_info + +realistic_fs = { + 'basic': { + '%': None, + 'base': { + 'install.yaml': + """# desc: install ceph +install: +""" + }, + 'clusters': { + 'fixed-1.yaml': + """# desc: single node cluster +roles: +- [osd.0, osd.1, osd.2, mon.a, mon.b, mon.c, client.0] +""" + }, + 'workloads': { + 'rbd_api_tests_old_format.yaml': + """# desc: c/c++ librbd api tests with format 1 images +# rbd_features: none +overrides: + ceph: + conf: + client: + rbd default format: 1 +tasks: +- workunit: + env: + RBD_FEATURES: 0 + clients: + client.0: + - rbd/test_librbd.sh +""", + 'rbd_api_tests.yaml': + """# desc: c/c++ librbd api tests with default settings +# rbd_features: default +tasks: +- workunit: + clients: + client.0: + - rbd/test_librbd.sh +""", + }, + }, +} + + +expected_tree = """├── % +├── base +│ └── install.yaml +├── clusters +│ └── fixed-1.yaml +└── workloads + ├── rbd_api_tests.yaml + └── rbd_api_tests_old_format.yaml""".split('\n') + + +expected_desc = [ + '', + '', + ' install ceph', + '', + ' single node cluster', + '', + ' c/c++ librbd api tests with default settings', + ' c/c++ librbd api tests with format 1 images', +] + + +expected_rbd_features = [ + '', + '', + '', + '', + '', + '', + ' default', + ' none', +] + + +class TestTree(object): + + def test_no_filters(self): + fake_listdir, _, fake_isdir, fake_open = \ + make_fake_fstools(realistic_fs) + rows = tree_with_info('basic', [], '', [], fake_listdir, + fake_isdir, fake_open) + assert rows == [ [x] for x in expected_tree] + + def test_single_filter(self): + fake_listdir, _, fake_isdir, fake_open = \ + make_fake_fstools(realistic_fs) + rows = tree_with_info('basic', ['desc'], '', [], fake_listdir, + fake_isdir, fake_open) + assert rows == map(list, zip(expected_tree, expected_desc)) + + rows = tree_with_info('basic', ['rbd_features'], '', [], + fake_listdir, fake_isdir, fake_open) + assert rows == map(list, zip(expected_tree, expected_rbd_features)) + + def test_no_matching(self): + fake_listdir, _, fake_isdir, fake_open = \ + make_fake_fstools(realistic_fs) + rows = tree_with_info('basic', ['extra'], '', [], fake_listdir, + fake_isdir, fake_open) + assert rows == map(list, zip(expected_tree, [''] * len(expected_tree))) + + def test_multiple_filters(self): + fake_listdir, _, fake_isdir, fake_open = \ + make_fake_fstools(realistic_fs) + rows = tree_with_info('basic', ['desc', 'rbd_features'], '', [], + fake_listdir, fake_isdir, fake_open) + assert rows == map(list, zip(expected_tree, + expected_desc, + expected_rbd_features)) + + rows = tree_with_info('basic', ['rbd_features', 'desc'], '', [], + fake_listdir, fake_isdir, fake_open) + assert rows == map(list, zip(expected_tree, + expected_rbd_features, + expected_desc)) + + def test_extract_info_dir(self): + simple_fs = {'a': {'b': '# foo:'}} + _, _, fake_isdir, fake_open = make_fake_fstools(simple_fs) + info = extract_info('a', [], fake_isdir, fake_open) + assert info == {} + + info = extract_info('a', ['foo', 'bar'], fake_isdir, fake_open) + assert info == {'foo': '', 'bar': ''} diff --git a/teuthology/tree.py b/teuthology/tree.py index e4655829c..51745185e 100644 --- a/teuthology/tree.py +++ b/teuthology/tree.py @@ -15,11 +15,11 @@ def main(args): table.add_row(row) print(table) -def extract_info(file_name, filters): +def extract_info(file_name, filters, _isdir=os.path.isdir, _open=open): result = {f: '' for f in filters} - if os.path.isdir(file_name): + if _isdir(file_name): return result - with file(file_name, 'r') as f: + with _open(file_name, 'r') as f: for line in f: for filt in filters: prefix = '# ' + filt + ':' @@ -29,8 +29,10 @@ def extract_info(file_name, filters): result[filt] += line[len(prefix):].rstrip('\n') return result -def tree_with_info(cur_dir, filters, prefix, rows): - files = sorted(os.listdir(cur_dir)) +def tree_with_info(cur_dir, filters, prefix, rows, + _listdir=os.listdir, _isdir=os.path.isdir, + _open=open): + files = sorted(_listdir(cur_dir)) for i, f in enumerate(files): path = os.path.join(cur_dir, f) if i == len(files) - 1: @@ -39,10 +41,11 @@ def tree_with_info(cur_dir, filters, prefix, rows): else: file_pad = '├── ' dir_pad = '│ ' - info = extract_info(path, filters) + info = extract_info(path, filters, _isdir, _open) tree_node = prefix + file_pad + f meta = [info[f] for f in filters] rows.append([tree_node] + meta) - if os.path.isdir(path): - tree_with_info(path, filters, prefix + dir_pad, rows) + if _isdir(path): + tree_with_info(path, filters, prefix + dir_pad, rows, + _listdir, _isdir, _open) return rows