Merge lp:~james-w/charms/precise/haproxy/metrics into lp:charms/haproxy

Proposed by James Westby on 2014-05-29
Status: Merged
Merged at revision: 80
Proposed branch: lp:~james-w/charms/precise/haproxy/metrics
Merge into: lp:charms/haproxy
Diff against target: 112 lines (+84/-1)
3 files modified
hooks/hooks.py (+6/-1)
hooks/tests/test_install.py (+56/-0)
hooks/tests/test_upgrade_charm.py (+22/-0)
To merge this branch: bzr merge lp:~james-w/charms/precise/haproxy/metrics
Reviewer Review Type Date Requested Status
Charles Butler (community) 2014-05-29 Approve on 2014-06-13
Review via email: mp+221408@code.launchpad.net

Description of the change

Hi,

When adding the metrics support I missed installing the python-jinja2
package on upgrades.

This fixes that by calling the install hook on upgrades.

It also adds some unit tests for upgrade/install.

Thanks,

James

To post a comment you must log in.
Charles Butler (lazypower) wrote :

Looks good to me James, thanks for the contribution + tests

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/hooks.py'
2--- hooks/hooks.py 2014-05-27 22:43:44 +0000
3+++ hooks/hooks.py 2014-05-29 14:59:59 +0000
4@@ -746,6 +746,7 @@
5 # Hook functions
6 ###############################################################################
7 def install_hook():
8+ # Run both during initial install and during upgrade-charm.
9 if not os.path.exists(default_haproxy_service_config_dir):
10 os.mkdir(default_haproxy_service_config_dir, 0600)
11
12@@ -973,7 +974,11 @@
13 def main(hook_name):
14 if hook_name == "install":
15 install_hook()
16- elif hook_name in ("config-changed", "upgrade-charm"):
17+ elif hook_name == "upgrade-charm":
18+ install_hook()
19+ config_changed()
20+ update_nrpe_config()
21+ elif hook_name == "config-changed":
22 config_changed()
23 update_nrpe_config()
24 elif hook_name == "start":
25
26=== added file 'hooks/tests/test_install.py'
27--- hooks/tests/test_install.py 1970-01-01 00:00:00 +0000
28+++ hooks/tests/test_install.py 2014-05-29 14:59:59 +0000
29@@ -0,0 +1,56 @@
30+from mock import patch
31+import os
32+from testtools import TestCase
33+
34+import hooks
35+
36+
37+class InstallTests(TestCase):
38+
39+ def setUp(self):
40+ super(InstallTests, self).setUp()
41+ self.apt_install = self.patch_hook('apt_install')
42+ self.ensure_package_status = self.patch_hook('ensure_package_status')
43+ self.enable_haproxy = self.patch_hook('enable_haproxy')
44+ self.config_get = self.patch_hook('config_get')
45+ path_exists = patch.object(os.path, "exists")
46+ self.path_exists = path_exists.start()
47+ self.path_exists.return_value = True
48+ self.addCleanup(path_exists.stop)
49+
50+ def patch_hook(self, hook_name):
51+ mock_controller = patch.object(hooks, hook_name)
52+ mock = mock_controller.start()
53+ self.addCleanup(mock_controller.stop)
54+ return mock
55+
56+ @patch('os.mkdir')
57+ def test_makes_config_dir(self, mkdir):
58+ self.path_exists.return_value = False
59+ hooks.install_hook()
60+ self.path_exists.assert_called_once_with(
61+ hooks.default_haproxy_service_config_dir)
62+ mkdir.assert_called_once_with(
63+ hooks.default_haproxy_service_config_dir, 0600)
64+
65+ @patch('os.mkdir')
66+ def test_config_dir_already_exists(self, mkdir):
67+ hooks.install_hook()
68+ self.path_exists.assert_called_once_with(
69+ hooks.default_haproxy_service_config_dir)
70+ self.assertFalse(mkdir.called)
71+
72+ def test_install_packages(self):
73+ hooks.install_hook()
74+ self.apt_install.assert_called_once_with(
75+ ['haproxy', 'python-jinja2'], fatal=True)
76+
77+ def test_ensures_package_status(self):
78+ hooks.install_hook()
79+ self.config_get.assert_called_once_with('package_status')
80+ self.ensure_package_status.assert_called_once_with(
81+ hooks.service_affecting_packages, self.config_get.return_value)
82+
83+ def test_calls_enable_haproxy(self):
84+ hooks.install_hook()
85+ self.enable_haproxy.assert_called_once_with()
86
87=== added file 'hooks/tests/test_upgrade_charm.py'
88--- hooks/tests/test_upgrade_charm.py 1970-01-01 00:00:00 +0000
89+++ hooks/tests/test_upgrade_charm.py 2014-05-29 14:59:59 +0000
90@@ -0,0 +1,22 @@
91+from mock import patch
92+from testtools import TestCase
93+
94+import hooks
95+
96+
97+class UpgradeCharmTests(TestCase):
98+
99+ def patch_hook(self, hook_name):
100+ mock_controller = patch.object(hooks, hook_name)
101+ mock = mock_controller.start()
102+ self.addCleanup(mock_controller.stop)
103+ return mock
104+
105+ def test_calls_hooks(self):
106+ install_hook = self.patch_hook('install_hook')
107+ config_changed = self.patch_hook('config_changed')
108+ update_nrpe_config = self.patch_hook('update_nrpe_config')
109+ hooks.main('upgrade-charm')
110+ install_hook.assert_called_once_with()
111+ config_changed.assert_called_once_with()
112+ update_nrpe_config.assert_called_once_with()

Subscribers

People subscribed via source and target branches