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
1diff --git a/.gitignore b/.gitignore
2index 32e2995..34da750 100644
3--- a/.gitignore
4+++ b/.gitignore
5@@ -20,3 +20,4 @@ repo-info
6
7 # reports
8 report/*
9+.unit-state.db
10diff --git a/reactive/grafana.py b/reactive/grafana.py
11index 303b10e..09b7a31 100644
12--- a/reactive/grafana.py
13+++ b/reactive/grafana.py
14@@ -218,6 +218,7 @@ def config_changed():
15 remove_state('grafana.admin_password.set')
16 if config.changed('install_file') and config.get('install_file'):
17 remove_state('grafana.installed')
18+ remove_state('grafana.nagios-setup.completed')
19
20
21 def check_ports(new_port):
22@@ -371,21 +372,19 @@ def restart_grafana():
23
24 @when('grafana.started')
25 @when('nrpe-external-master.available')
26-def update_nrpe_config(svc):
27- kv = unitdata.kv()
28- source = kv.get('install_method')
29- if source in ('snap', 'apt'):
30- svcname = SVCNAME[source]
31- else:
32- hookenv.status_set('blocked', 'Unsupported install_method')
33- return
34- # python-dbus is used by check_upstart_job
35- fetch.apt_install('python-dbus', fatal=True)
36+@when_not('grafana.nagios-setup.completed')
37+def update_nrpe_config(_):
38 hostname = nrpe.get_nagios_hostname()
39- current_unit = nrpe.get_nagios_unit_name()
40 nrpe_setup = nrpe.NRPE(hostname=hostname)
41- nrpe.add_init_service_checks(nrpe_setup, [svcname], current_unit)
42+
43+ # write the http check
44+ nrpe_setup.add_check(
45+ 'grafana_http',
46+ 'Grafana HTTP check',
47+ 'check_http -I 127.0.0.1 -p {} -u /login'.format(hookenv.config()['port'])
48+ )
49 nrpe_setup.write()
50+ set_state('grafana.nagios-setup.completed')
51
52
53 @when_not('nrpe-external-master.available')
54diff --git a/tests/unit/requirements.txt b/tests/unit/requirements.txt
55index 29f9318..cb975bd 100644
56--- a/tests/unit/requirements.txt
57+++ b/tests/unit/requirements.txt
58@@ -4,3 +4,6 @@ mock
59 pytest
60 pytest-cov
61 requests
62+jsondiff
63+pbkdf2
64+pycrypto
65diff --git a/tests/unit/test_grafana.py b/tests/unit/test_grafana.py
66index e03d9e1..bed2a4a 100644
67--- a/tests/unit/test_grafana.py
68+++ b/tests/unit/test_grafana.py
69@@ -1,7 +1,9 @@
70 from os.path import isfile
71 import unittest
72+from unittest.mock import call
73+from unittest import mock
74
75-from charms.layer.grafana import (
76+from lib.charms.layer.grafana import (
77 download_file,
78 get_cmd_output,
79 get_deb_package_version,
80@@ -37,3 +39,30 @@ class GrafanaTestCase(unittest.TestCase):
81 self.assertTrue(isfile(path))
82 version = get_deb_package_version(path)
83 self.assertEqual(version, '2.10-2')
84+
85+
86+class TestGrafanaNagios(unittest.TestCase):
87+ @mock.patch('charmhelpers.contrib.charmsupport.nrpe.NRPE')
88+ @mock.patch('charmhelpers.contrib.charmsupport.nrpe.get_nagios_hostname')
89+ @mock.patch('charmhelpers.core.hookenv.config')
90+ def test_update_nrpe_config(self,
91+ mock_hookenv_config,
92+ mock_get_nagios_hostname,
93+ mock_nrpe,
94+ *args):
95+ import sys
96+ sys.modules['charms.layer'] = mock.MagicMock()
97+ sys.modules['charms.layer.grafana'] = mock.MagicMock()
98+ from reactive import grafana
99+ mock_hookenv_config.return_value = {'port': 1234}
100+ mock_get_nagios_hostname.return_value = 'testunit'
101+ svc = object()
102+ grafana.update_nrpe_config(svc)
103+ calls = [
104+ call(hostname='testunit'),
105+ call().add_check('grafana_http',
106+ 'Grafana HTTP check',
107+ 'check_http -I 127.0.0.1 -p 1234 -u /login'),
108+ call().write()
109+ ]
110+ mock_nrpe.assert_has_calls(calls)
111diff --git a/tox.ini b/tox.ini
112index 288fd77..898ca41 100644
113--- a/tox.ini
114+++ b/tox.ini
115@@ -18,7 +18,6 @@ commands = pytest -v --ignore {toxinidir}/tests/functional \
116 --cov-report=html:report/html
117 deps = -r{toxinidir}/tests/unit/requirements.txt
118 -r{toxinidir}/requirements.txt
119-setenv = PYTHONPATH={toxinidir}/lib
120
121 [testenv:functional]
122 passenv =

Subscribers

People subscribed via source and target branches

to all changes: