Merge ~woutervb/charm-grafana:bug/1873105 into charm-grafana:master

Proposed by Wouter van Bommel
Status: Superseded
Proposed branch: ~woutervb/charm-grafana:bug/1873105
Merge into: charm-grafana:master
Diff against target: 122 lines (+45/-14)
5 files modified
.gitignore (+1/-0)
reactive/grafana.py (+11/-12)
tests/unit/requirements.txt (+3/-0)
tests/unit/test_grafana.py (+30/-1)
tox.ini (+0/-1)
Reviewer Review Type Date Requested Status
Alvaro Uria (community) Needs Fixing
Peter Sabaini (community) Approve
Review via email: mp+382426@code.launchpad.net

This proposal has been superseded by a proposal from 2020-07-23.

Commit message

Replace the process monitoring with functional

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Peter Sabaini (peter-sabaini) wrote :

Two minor comments inline, otherwise lgtm, cheers

review: Approve
~woutervb/charm-grafana:bug/1873105 updated
73de6b4... by Wouter van Bommel

Minor update in the way tests are run.

With the changes created, we no longer need to specify the PYTHONPATH
in the functional tests

Revision history for this message
Alvaro Uria (aluria) wrote :

Hi, there are a few things to be reviewed:
* Makefile does not have a unittest target (probably because unit tests didn't exist). However, tox.ini does have a "unit" env. I think the Makefile misses the target, running "tox -e unit"

* Functional tests miss nagios-{{series}} and nrpe-{{series}} in the overlay.yaml.j2 template. It would be good to test when grafana gets related to nrpe, by checking that the check_grafana_http.cfg file exists in /etc/nagios/nrpe.d. There are examples of such check (file_stat) in ie. charm-openstack-service-checks

* Functional tests don't test a deployment of cs:grafana + juju upgrade-charm to the local version. I spotted a bug that can be seen at [1]. "nrpe-external-master.available" state gets removed and wipe_nrpe_checks is called. Looking at the debug lines, the http interface checks endpoint.website.joined state and removes website.available. I don't see any trace of calling the nrpe-external-master interface and do a similar thing (which OTOH only occurs when n-e-m-relation-{departed,broken} hooks are triggered - and debug lines don't show such run).

On this 3rd test (upgrade-charm) it would be could to verify that the old checks (check_grafana-server.cfg [apt] or check_snap.grafana.grafana.cfg [snap]) are removed (backward compatibility with a previous deployed environment). On fresh deploys, such checks won't exist, only check_grafana_http.cfg.

1. https://pastebin.ubuntu.com/p/2smQt4X3Wd/

review: Needs Fixing
Revision history for this message
Alvaro Uria (aluria) wrote :

BTW, when I tried to workaround the issue, by manually setting the nrpe-external-master.available flag, I got this error [1].

1. https://pastebin.ubuntu.com/p/Fj5zzRJt2C/

Revision history for this message
Xav Paice (xavpaice) wrote :

Change needs a rebase, and rework to the tests. WIP.

Unmerged commits

73de6b4... by Wouter van Bommel

Minor update in the way tests are run.

With the changes created, we no longer need to specify the PYTHONPATH
in the functional tests

194e953... by Wouter van Bommel

Replace the process monitoring with functional

By checking that the grafana server actually responds to http calls, we
can ensure that both the process is running and functional.

Added unittest for the creation of the nagios check, updated unittest to
make it possible

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/.gitignore b/.gitignore
index 32e2995..34da750 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,3 +20,4 @@ repo-info
2020
21# reports21# reports
22report/*22report/*
23.unit-state.db
diff --git a/reactive/grafana.py b/reactive/grafana.py
index 303b10e..09b7a31 100644
--- a/reactive/grafana.py
+++ b/reactive/grafana.py
@@ -218,6 +218,7 @@ def config_changed():
218 remove_state('grafana.admin_password.set')218 remove_state('grafana.admin_password.set')
219 if config.changed('install_file') and config.get('install_file'):219 if config.changed('install_file') and config.get('install_file'):
220 remove_state('grafana.installed')220 remove_state('grafana.installed')
221 remove_state('grafana.nagios-setup.completed')
221222
222223
223def check_ports(new_port):224def check_ports(new_port):
@@ -371,21 +372,19 @@ def restart_grafana():
371372
372@when('grafana.started')373@when('grafana.started')
373@when('nrpe-external-master.available')374@when('nrpe-external-master.available')
374def update_nrpe_config(svc):375@when_not('grafana.nagios-setup.completed')
375 kv = unitdata.kv()376def update_nrpe_config(_):
376 source = kv.get('install_method')
377 if source in ('snap', 'apt'):
378 svcname = SVCNAME[source]
379 else:
380 hookenv.status_set('blocked', 'Unsupported install_method')
381 return
382 # python-dbus is used by check_upstart_job
383 fetch.apt_install('python-dbus', fatal=True)
384 hostname = nrpe.get_nagios_hostname()377 hostname = nrpe.get_nagios_hostname()
385 current_unit = nrpe.get_nagios_unit_name()
386 nrpe_setup = nrpe.NRPE(hostname=hostname)378 nrpe_setup = nrpe.NRPE(hostname=hostname)
387 nrpe.add_init_service_checks(nrpe_setup, [svcname], current_unit)379
380 # write the http check
381 nrpe_setup.add_check(
382 'grafana_http',
383 'Grafana HTTP check',
384 'check_http -I 127.0.0.1 -p {} -u /login'.format(hookenv.config()['port'])
385 )
388 nrpe_setup.write()386 nrpe_setup.write()
387 set_state('grafana.nagios-setup.completed')
389388
390389
391@when_not('nrpe-external-master.available')390@when_not('nrpe-external-master.available')
diff --git a/tests/unit/requirements.txt b/tests/unit/requirements.txt
index 29f9318..cb975bd 100644
--- a/tests/unit/requirements.txt
+++ b/tests/unit/requirements.txt
@@ -4,3 +4,6 @@ mock
4pytest4pytest
5pytest-cov5pytest-cov
6requests6requests
7jsondiff
8pbkdf2
9pycrypto
diff --git a/tests/unit/test_grafana.py b/tests/unit/test_grafana.py
index e03d9e1..bed2a4a 100644
--- a/tests/unit/test_grafana.py
+++ b/tests/unit/test_grafana.py
@@ -1,7 +1,9 @@
1from os.path import isfile1from os.path import isfile
2import unittest2import unittest
3from unittest.mock import call
4from unittest import mock
35
4from charms.layer.grafana import (6from lib.charms.layer.grafana import (
5 download_file,7 download_file,
6 get_cmd_output,8 get_cmd_output,
7 get_deb_package_version,9 get_deb_package_version,
@@ -37,3 +39,30 @@ class GrafanaTestCase(unittest.TestCase):
37 self.assertTrue(isfile(path))39 self.assertTrue(isfile(path))
38 version = get_deb_package_version(path)40 version = get_deb_package_version(path)
39 self.assertEqual(version, '2.10-2')41 self.assertEqual(version, '2.10-2')
42
43
44class TestGrafanaNagios(unittest.TestCase):
45 @mock.patch('charmhelpers.contrib.charmsupport.nrpe.NRPE')
46 @mock.patch('charmhelpers.contrib.charmsupport.nrpe.get_nagios_hostname')
47 @mock.patch('charmhelpers.core.hookenv.config')
48 def test_update_nrpe_config(self,
49 mock_hookenv_config,
50 mock_get_nagios_hostname,
51 mock_nrpe,
52 *args):
53 import sys
54 sys.modules['charms.layer'] = mock.MagicMock()
55 sys.modules['charms.layer.grafana'] = mock.MagicMock()
56 from reactive import grafana
57 mock_hookenv_config.return_value = {'port': 1234}
58 mock_get_nagios_hostname.return_value = 'testunit'
59 svc = object()
60 grafana.update_nrpe_config(svc)
61 calls = [
62 call(hostname='testunit'),
63 call().add_check('grafana_http',
64 'Grafana HTTP check',
65 'check_http -I 127.0.0.1 -p 1234 -u /login'),
66 call().write()
67 ]
68 mock_nrpe.assert_has_calls(calls)
diff --git a/tox.ini b/tox.ini
index 288fd77..898ca41 100644
--- a/tox.ini
+++ b/tox.ini
@@ -18,7 +18,6 @@ commands = pytest -v --ignore {toxinidir}/tests/functional \
18 --cov-report=html:report/html18 --cov-report=html:report/html
19deps = -r{toxinidir}/tests/unit/requirements.txt19deps = -r{toxinidir}/tests/unit/requirements.txt
20 -r{toxinidir}/requirements.txt20 -r{toxinidir}/requirements.txt
21setenv = PYTHONPATH={toxinidir}/lib
2221
23[testenv:functional]22[testenv:functional]
24passenv =23passenv =

Subscribers

People subscribed via source and target branches

to all changes: