]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
teuthology-tree: add unit tests
authorJosh Durgin <jdurgin@redhat.com>
Sat, 21 Nov 2015 00:31:19 +0000 (16:31 -0800)
committerJosh Durgin <jdurgin@redhat.com>
Wed, 9 Dec 2015 21:04:54 +0000 (13:04 -0800)
Signed-off-by: Josh Durgin <jdurgin@redhat.com>
teuthology/test/test_tree.py [new file with mode: 0644]
teuthology/tree.py

diff --git a/teuthology/test/test_tree.py b/teuthology/test/test_tree.py
new file mode 100644 (file)
index 0000000..1f6a2d9
--- /dev/null
@@ -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': ''}
index e4655829c8780ce1b3017555c9a41fe9a4872c46..51745185eaf3d2398806cb007656d073e1dc464e 100644 (file)
@@ -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