1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import hooks
__author__ = 'Jorge Niedbalski R. <jorge.niedbalski@canonical.com>'
_HERE = os.path.abspath(os.path.dirname(__file__))
try:
import unittest
import mock
except ImportError as ex:
raise ImportError("Please install unittest and mock modules")
TO_PATCH = [
"apt_install",
"install",
"service_restart",
"service_start",
"open_port",
"service_stop",
"close_port",
"juju_log",
"charm_dir",
"config_get",
"nrpe"
]
class HooksTestCase(unittest.TestCase):
def setUp(self):
unittest.TestCase.setUp(self)
self.patch_all()
self.juju_log.return_value = True
self.apt_install.return_value = True
self.charm_dir.return_value = os.path.join(_HERE, '..')
def patch(self, method):
_m = mock.patch.object(hooks, method)
_mock = _m.start()
self.addCleanup(_m.stop)
return _mock
def patch_all(self):
for method in TO_PATCH:
setattr(self, method, self.patch(method))
def test_install_hook(self):
"""Check if install hooks is correctly executed
"""
hooks.hooks.execute(['install'])
self.apt_install.assert_called_with(['rsyslog', 'rsyslog-relp',
'python-jinja2'],
options=["-f"])
def test_upgrade_charm(self):
"""Check if charm upgrade hooks is correctly executed
"""
hooks.hooks.execute(['upgrade-charm'])
self.install.assert_called()
def test_start_charm(self):
"""Check if start hook is correctly executed
"""
hooks.hooks.execute(['start'])
self.service_start.assert_called_with("rsyslog")
expected = [mock.call(514, protocol="UDP")]
self.assertEquals(self.open_port.call_args_list,
expected)
def test_stop_charm(self):
"""Check if start hooks is correctly executed
"""
hooks.hooks.execute(['stop'])
self.service_stop.assert_called_with("rsyslog")
expected = [mock.call(514, protocol="UDP")]
self.assertTrue(self.close_port.call_args_list == expected)
def _tpl_has_value(self, template, value):
return hooks.get_config_template(template).render(
**hooks.get_changed_config()).find(value) > 0
def test_config_nova_disabled(self):
"""Check if configuration templates are created correctly \
( nova_logs: False )
"""
self.apt_install.return_value = True
self.charm_dir.return_value = os.path.join(_HERE, '..')
config = {
"forward_host": "",
"forward_options": "",
"forward_port": "514",
"forward_protocol": "tcp",
"forward_selector": "*.*",
"syslog_rotate": 7,
"messages_rotate": 4,
"nova_logs": False,
"protocol": "udp",
}
def config_side(*args, **kwargs):
return config[args[0]]
self.config_get.side_effect = config_side
self.assertFalse(self._tpl_has_value("60-aggregator.conf", "nova"))
self.assertTrue(self._tpl_has_value("rsyslog.conf", "7"))
self.assertTrue(self._tpl_has_value("rsyslog.conf", "4"))
def test_config_nova_enabled(self):
"""Check if configuration templates are created correctly \
( nova_logs: True )
"""
config = {
"forward_host": "",
"forward_options": "",
"forward_port": "514",
"forward_protocol": "tcp",
"forward_selector": "*.*",
"syslog_rotate": 7,
"messages_rotate": 4,
"nova_logs": True,
'protocol': "udp",
}
def config_side(*args, **kwargs):
return config[args[0]]
self.config_get.side_effect = config_side
self.assertTrue(self._tpl_has_value("60-aggregator.conf", "nova"))
def test_config_changed(self):
"""Check if config-changed is executed correctly"""
_open = mock.mock_open(read_data=b'foo')
with mock.patch('builtins.open', _open, create=True):
hooks.config_changed()
# I'm not quite sure why config_changed appears to be called twice but
# it does behave properly and only make changes once
expected = [
mock.call(os.path.join(hooks.get_template_dir(),
'60-aggregator.conf'), 'rb'),
mock.call(os.path.join(hooks.get_template_dir(),
'60-aggregator.conf'), 'rb'),
mock.call(os.path.join(hooks.DEFAULT_RSYSLOG_PATH,
'60-aggregator.conf'), 'w'),
mock.call(os.path.join(hooks.get_template_dir(),
'70-forward.conf'), 'rb'),
mock.call(os.path.join(hooks.get_template_dir(),
'70-forward.conf'), 'rb'),
mock.call(os.path.join(hooks.DEFAULT_RSYSLOG_PATH,
'70-forward.conf'), 'w'),
mock.call(os.path.join(hooks.get_template_dir(),
'rsyslog.conf'), 'rb'),
mock.call(os.path.join(hooks.get_template_dir(),
'rsyslog.conf'), 'rb'),
mock.call(hooks.DEFAULT_LOGROTATE_PATH, 'w')
]
self.assertEquals(sorted(_open.call_args_list), sorted(expected))
self.service_restart.assert_called_once_with("rsyslog")
|