From: Zack Cerza Date: Tue, 17 Oct 2017 15:15:28 +0000 (-0600) Subject: Add some integration tests using testinfra X-Git-Tag: v1.0~1^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=a7bec625df1b99a63abd2ecf1381563478ec995a;p=cephmetrics.git Add some integration tests using testinfra Signed-off-by: Zack Cerza --- diff --git a/ansible/roles/ceph-collectd/tests/test_collectd.py b/ansible/roles/ceph-collectd/tests/test_collectd.py new file mode 100644 index 0000000..d83378b --- /dev/null +++ b/ansible/roles/ceph-collectd/tests/test_collectd.py @@ -0,0 +1,10 @@ +testinfra_hosts = ['!ceph-grafana'] + + +class TestCollectd(object): + def test_service_enabled(self, host): + assert host.service('collectd').is_enabled + assert host.service('collectd').is_running + + def test_logfile_present(self, host): + assert host.file('/var/log/collectd-cephmetrics.log').is_file diff --git a/ansible/roles/ceph-grafana/tests/test_grafana.py b/ansible/roles/ceph-grafana/tests/test_grafana.py new file mode 100644 index 0000000..adeed07 --- /dev/null +++ b/ansible/roles/ceph-grafana/tests/test_grafana.py @@ -0,0 +1,88 @@ +import json +import os +import pytest + +testinfra_hosts = ['ceph-grafana'] + + +class TestGrafana(object): + def get_ceph_hosts(self, host): + """ + Extract a list of FQDNs of Ceph hosts from the Ansible inventory + """ + groups = host.ansible.get_variables()['groups'] + ceph_groups = ('mdss', 'mgrs', 'mons', 'osds', 'rgws') + ceph_hosts = set() + for group in ceph_groups: + hosts = groups.get(group, list()) + map(ceph_hosts.add, hosts) + return list(ceph_hosts) + + @pytest.mark.parametrize( + "service", + ['carbon-cache', + dict(apt='apache2', yum='httpd'), + 'grafana-server'] + ) + def test_service_enabled(self, host, service): + """ Are the proper services enabled? """ + if isinstance(service, dict): + service = service[ + host.ansible('setup')['ansible_facts']['ansible_pkg_mgr']] + service = host.service(service) + assert service.is_running + assert service.is_enabled + + @pytest.mark.parametrize( + "proto,iface,port", + [ + ('tcp', '0.0.0.0', '2003'), # carbon + ('tcp', '0.0.0.0', '2004'), # carbon + ('tcp', '0.0.0.0', '8080'), # graphite + ('tcp', '0.0.0.0', '3000'), # grafana + ] + ) + def test_ports_open(self, host, proto, iface, port): + """ Are the proper ports open? """ + socket_spec = "%s://%s" % (proto, iface) + if iface: + socket_spec += ':' + socket_spec += port + assert host.socket(socket_spec).is_listening + + def test_whisper_data(self, host): + """ Does whisper data exist for each Ceph host? """ + whisper_dirs = [ + '/var/lib/carbon/whisper', + '/var/lib/graphite/whisper', + ] + for whisper_dir in whisper_dirs: + if host.file(whisper_dir).exists: + break + for ceph_host in self.get_ceph_hosts(host): + whisper_subdir = os.path.join( + whisper_dir, 'collectd', ceph_host.replace('.', '/') + ) + assert host.file(whisper_subdir).is_directory + cpu_metrics = [ + 'idle.wsp', 'nice.wsp', 'steal.wsp', 'user.wsp', + 'interrupt.wsp', 'softirq.wsp', 'system.wsp', 'wait.wsp', + ] + assert any([ + host.file(os.path.join( + whisper_subdir, 'cpu', 'percent', metric + )).is_file for metric in cpu_metrics + ]) + + def test_metrics_present(self, host): + """ Does graphite know about each Ceph host? """ + ceph_hosts = self.get_ceph_hosts(host) + out = host.check_output( + "curl http://localhost:8080/metrics/find?query=collectd.*") + obj = json.loads(out) + + def extract_hostname(fragment): + return fragment['text'] + metric_hosts = map(extract_hostname, obj) + assert sorted(map(lambda s: s.split('.')[0], ceph_hosts)) == \ + sorted(metric_hosts) diff --git a/tox.ini b/tox.ini index 4db87b8..0c8e78e 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,6 @@ [tox] skipsdist = True +envlist=ansible-lint,ansible-syntax,flake8 [testenv:ansible-lint] install_command = pip install --upgrade {opts} {packages} @@ -20,3 +21,19 @@ install_command = pip install --upgrade {opts} {packages} deps= flake8 commands=flake8 --select=F,E9 {posargs:*.py collectors tests} + +# Integration tests must operate against a live deployment. To run, simply: +# tox -e integration /path/to/inventory +# NOTE: A current limitation of these tests is that they assume that defaults +# were used for things like ports, usernames, etc. They do, however, +# support devel_mode=True/False. +[testenv:integration] +install_command = pip install --upgrade {opts} {packages} +deps= + ansible + pytest + pytest-xdist + testinfra +changedir=ansible +commands= + py.test -n auto --connection=ansible --ansible-inventory {posargs} ./roles/