Merge lp:~gandelman-a/charm-helpers/restart_on_change_in_order into lp:charm-helpers

Proposed by Adam Gandelman
Status: Merged
Merged at revision: 34
Proposed branch: lp:~gandelman-a/charm-helpers/restart_on_change_in_order
Merge into: lp:charm-helpers
Diff against target: 71 lines (+32/-5)
2 files modified
charmhelpers/core/host.py (+3/-1)
tests/core/test_host.py (+29/-4)
To merge this branch: bzr merge lp:~gandelman-a/charm-helpers/restart_on_change_in_order
Reviewer Review Type Date Requested Status
James Page Approve
Review via email: mp+171010@code.launchpad.net

Description of the change

Users of core.host.restart_on_change() should be able to take special care to create a restart_map using OrderedDict, to describe ordering of service restarts on config file changes. The current function uses set() on the list of services to be restarted and breaks any attempt at restarting in a desired order.

To post a comment you must log in.
Revision history for this message
James Page (james-page) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/core/host.py'
2--- charmhelpers/core/host.py 2013-06-20 21:21:42 +0000
3+++ charmhelpers/core/host.py 2013-06-24 03:29:27 +0000
4@@ -12,6 +12,8 @@
5 import subprocess
6 import hashlib
7
8+from collections import OrderedDict
9+
10 from hookenv import log, execution_environment
11
12
13@@ -255,7 +257,7 @@
14 for path in restart_map:
15 if checksums[path] != file_hash(path):
16 restarts += restart_map[path]
17- for service_name in list(set(restarts)):
18+ for service_name in list(OrderedDict.fromkeys(restarts)):
19 service('restart', service_name)
20 return wrapped_f
21 return wrap
22
23=== modified file 'tests/core/test_host.py'
24--- tests/core/test_host.py 2013-06-20 21:21:42 +0000
25+++ tests/core/test_host.py 2013-06-24 03:29:27 +0000
26@@ -1,3 +1,4 @@
27+from collections import OrderedDict
28 from contextlib import contextmanager
29 import os
30 import subprocess
31@@ -696,12 +697,36 @@
32 make_some_changes()
33
34 # Restart should only happen once per service
35- service.assert_has_calls([
36- call('restart', 'test-service2'),
37- call('restart', 'test-service')
38- ])
39+ for svc in ['test-service2', 'test-service']:
40+ c = call('restart', svc)
41+ self.assertEquals(1, service.call_args_list.count(c))
42
43 exists.assert_has_calls([
44 call(file_name_one),
45 call(file_name_two)
46 ])
47+
48+ @patch.object(host, 'service')
49+ @patch('os.path.exists')
50+ def test_multiservice_restart_on_change_in_order(self, exists, service):
51+ restart_map = OrderedDict([
52+ ('/etc/cinder/cinder.conf', ['some-api']),
53+ ('/etc/haproxy/haproxy.conf', ['haproxy'])
54+ ])
55+ exists.side_effect = [False, True, True, True]
56+
57+ @host.restart_on_change(restart_map)
58+ def make_some_changes():
59+ pass
60+
61+ with patch_open() as (mock_open, mock_file):
62+ mock_file.read.side_effect = ['exists', 'missing', 'exists2']
63+ make_some_changes()
64+
65+ # Restarts should happen in the order they are described in the
66+ # restart map.
67+ expected = [
68+ call('restart', 'some-api'),
69+ call('restart', 'haproxy')
70+ ]
71+ self.assertEquals(expected, service.call_args_list)

Subscribers

People subscribed via source and target branches