Merge lp:~bloodearnest/charms/trusty/logstash-forwarder/trunk into lp:~canonical-is-sa/charms/trusty/logstash-forwarder/trunk

Proposed by Simon Davy
Status: Merged
Merged at revision: 19
Proposed branch: lp:~bloodearnest/charms/trusty/logstash-forwarder/trunk
Merge into: lp:~canonical-is-sa/charms/trusty/logstash-forwarder/trunk
Diff against target: 92 lines (+36/-27)
2 files modified
hooks/hooks.py (+10/-14)
hooks/test_hooks.py (+26/-13)
To merge this branch: bzr merge lp:~bloodearnest/charms/trusty/logstash-forwarder/trunk
Reviewer Review Type Date Requested Status
Chris Stratford (community) Approve
Review via email: mp+320475@code.launchpad.net

Commit message

Fix the last change so it works in more complex environments, where there are multiple logstash-forwarder subordinates.

Description of the change

Fix the last change so it works in more complex environments, where there are multiple logstash-forwarder subordinates.

hookenvs.relations() can die in fire.

To post a comment you must log in.
Revision history for this message
Chris Stratford (chris-gondolin) wrote :

+1

review: Approve
19. By Chris Stratford

[bloodearnest,r=chriss] Fix the last change so it works in more complex environments, where there are multiple logstash-forwarder subordinates

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 2017-03-20 22:19:30 +0000
3+++ hooks/hooks.py 2017-03-21 10:19:17 +0000
4@@ -125,20 +125,16 @@
5 if model:
6 default_fields['juju_model'] = model
7
8- units = []
9- relations = hookenv.relations()
10- if relations['logs']:
11- units = [u for u in relations['logs'].values()[0]
12- if u != hookenv.local_unit()]
13-
14- if not units:
15- units = [u for u in relations['juju-info'].values()[0]
16- if u != hookenv.local_unit()]
17-
18- units = [u for u in units if u != hookenv.local_unit()]
19-
20- if units:
21- unit = units[0]
22+ unit = None
23+ logs = hookenv.relations_of_type('logs')
24+ if logs:
25+ unit = logs[0]['__unit__']
26+ else:
27+ juju_info = hookenv.relations_of_type('juju-info')
28+ if juju_info:
29+ unit = juju_info[0]['__unit__']
30+
31+ if unit:
32 default_fields['juju_unit'] = unit.replace('/', '-')
33 default_fields['juju_service'] = unit.split('/')[0]
34
35
36=== modified file 'hooks/test_hooks.py'
37--- hooks/test_hooks.py 2017-03-20 22:19:30 +0000
38+++ hooks/test_hooks.py 2017-03-21 10:19:17 +0000
39@@ -88,20 +88,15 @@
40 self.mock_gethostname.return_value = 'HOSTNAME'
41
42 self.mrelations = self.patch(
43- mock.patch.object(hookenv, 'relations')
44+ mock.patch.object(hookenv, 'relations_of_type')
45 )
46-
47- self.relations = {
48- 'logs': {
49- 'baz': {
50- 'logstash-forwarder/0': {},
51- 'remote/0': {},
52- }
53- }
54- }
55- self.mrelations.return_value = self.relations
56-
57- def test_logs_hooks(self):
58+ self.relations = {'juju-info': [{'__unit__': 'remote/0'}]}
59+ self.mrelations.side_effect = self.relations.get
60+
61+ host.write_file.reset_mock()
62+ host.service_restart.reset_mock()
63+
64+ def test_logs_hooks_with_juju_info_relation(self):
65 conf_path = hookenv.config("config_dir") + "/logstash_forwarder.conf"
66 host.write_file.reset_mock()
67 hooks.logs_relation_hooks()
68@@ -119,6 +114,24 @@
69 self.assertEqual(expected_fields, conf["files"][0]["fields"])
70 host.service_restart.assert_called_once_with('logstash-forwarder')
71
72+ def test_logs_hooks_with_logs_relation(self):
73+ conf_path = hookenv.config("config_dir") + "/logstash_forwarder.conf"
74+ self.relations['logs'] = [{'__unit__': 'logs-remote/0'}]
75+ hooks.logs_relation_hooks()
76+ self.assertTrue(host.write_file.called)
77+ self.assertEqual(conf_path, host.write_file.call_args[0][0])
78+ conf = json.loads(host.write_file.call_args[0][1])
79+ self.assertEqual("file1", conf["files"][0]["paths"][0])
80+ expected_fields = {
81+ u'juju_service': u'logs-remote',
82+ u'juju_unit': u'logs-remote-0',
83+ u'juju_model': u'test-model',
84+ u'hostname': u'HOSTNAME',
85+ u'type': u'type1',
86+ }
87+ self.assertEqual(expected_fields, conf["files"][0]["fields"])
88+ host.service_restart.assert_called_once_with('logstash-forwarder')
89+
90 def test_logs_relation_simple(self):
91 data = hooks.logs_relation()
92 expected = {

Subscribers

People subscribed via source and target branches

to all changes: