From: Zack Cerza Date: Wed, 30 May 2018 21:01:50 +0000 (-0600) Subject: Add some dashboard tests X-Git-Tag: v2.0~23^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=a19476d254105780d41d6368309e87ca1f31eedb;p=cephmetrics.git Add some dashboard tests Signed-off-by: Zack Cerza --- diff --git a/dashboards/mgr-prometheus/tests/__init__.py b/dashboards/mgr-prometheus/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dashboards/mgr-prometheus/tests/test_mgr_dashboards.py b/dashboards/mgr-prometheus/tests/test_mgr_dashboards.py new file mode 100644 index 0000000..4d417f4 --- /dev/null +++ b/dashboards/mgr-prometheus/tests/test_mgr_dashboards.py @@ -0,0 +1,66 @@ +import pytest + +from .util import TestDashboards, get_dashboards + + +def walk(obj, callback, parent_key=None, path=None): + if path is None: + path = '.' + if isinstance(obj, dict): + for key, value in obj.items(): + walk( + value, + callback, + parent_key=key, + path='{}["{}"]'.format(path, key), + ) + elif isinstance(obj, list): + for i in range(len(obj)): + walk( + obj[i], + callback, + parent_key=parent_key, + path='{}[{}]'.format(path, i), + ) + else: + callback(obj, parent_key, path) + + +class TestMgrDashboards(TestDashboards): + dashboards = get_dashboards() + + @pytest.mark.parametrize("name", dashboards.keys()) + def test_type(self, name): + assert name + obj = self.dashboards[name] + assert type(obj) is dict + + @pytest.mark.parametrize("name", dashboards.keys()) + def test_no_collectd(self, name): + def test(item, pkey, path): + if type(item) in (basestring, unicode): + assert 'collectd' not in item + walk(self.dashboards[name], test) + + @pytest.mark.parametrize("name", dashboards.keys()) + def test_no_ds_local(self, name): + def test(item, pkey, path): + if type(item) in (basestring, unicode): + assert '${DS_LOCAL}' not in item + walk(self.dashboards[name], test) + + @pytest.mark.parametrize("name", dashboards.keys()) + def test_no_influxdb_dstype(self, name): + def test(item, pkey, path): + if pkey == 'dsType' and type(item) in (basestring, unicode): + assert 'influxdb' not in item + walk(self.dashboards[name], test) + + @pytest.mark.parametrize("name", dashboards.keys()) + def test_no_influxdb_query(self, name): + def test(item, pkey, path): + if pkey == 'query': + assert 'SELECT' not in item + assert 'FROM' not in item + assert 'WHERE' not in item + walk(self.dashboards[name], test) diff --git a/dashboards/mgr-prometheus/tests/util.py b/dashboards/mgr-prometheus/tests/util.py new file mode 120000 index 0000000..47f38a1 --- /dev/null +++ b/dashboards/mgr-prometheus/tests/util.py @@ -0,0 +1 @@ +../../tests/util.py \ No newline at end of file diff --git a/dashboards/tests/util.py b/dashboards/tests/util.py new file mode 100644 index 0000000..5b04e88 --- /dev/null +++ b/dashboards/tests/util.py @@ -0,0 +1,27 @@ +import os +import json + + +def get_dashboards(): + dashboards = dict() + db_dir = os.path.realpath( + os.path.join( + os.path.dirname(__file__), + '..', + ) + ) + for item in os.listdir(db_dir): + if item.endswith('.json'): + db_path = os.path.join( + os.path.dirname(__file__), + '..', + item, + ) + dashboards[item] = json.loads( + open(db_path).read() + ) + return dashboards + + +class TestDashboards(object): + dashboards = None diff --git a/tox.ini b/tox.ini index 4f33963..b6d8eb3 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] skipsdist = True -envlist=ansible-lint,ansible-syntax,flake8 +envlist=ansible-lint,ansible-syntax,flake8,dashboards [testenv:ansible-lint] install_command = pip install --upgrade {opts} {packages} @@ -37,3 +37,11 @@ deps= changedir=ansible commands= py.test -v -n auto --connection=ansible --ansible-inventory {posargs} ./roles/ + +[testenv:dashboards] +install_command = pip install --upgrade {opts} {packages} +deps= + pytest +changedir=dashboards +commands= + py.test -v ./