Merge ~hloeung/content-cache-charm:tests into content-cache-charm:master

Proposed by Haw Loeung
Status: Merged
Approved by: Haw Loeung
Approved revision: ba007c8f857c2498af39464bf0bdb68bd3803a40
Merged at revision: 625d609c90a83c68d2066cc01a6fbf237bcfacb0
Proposed branch: ~hloeung/content-cache-charm:tests
Merge into: content-cache-charm:master
Diff against target: 275 lines (+113/-15)
4 files modified
reactive/content_cache.py (+7/-3)
tests/unit/files/config_test_config.txt (+6/-0)
tests/unit/files/nginx_config_rendered_test_output-site9.local.txt (+7/-0)
tests/unit/test_content_cache.py (+93/-12)
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Content Cache Charmers Pending
Review via email: mp+378490@code.launchpad.net

Commit message

Unit tests, unit tests, unit tests...

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
Stuart Bishop (stub) wrote :

yup, yup. yup.

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 625d609c90a83c68d2066cc01a6fbf237bcfacb0

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/reactive/content_cache.py b/reactive/content_cache.py
2index 4640300..d18a489 100644
3--- a/reactive/content_cache.py
4+++ b/reactive/content_cache.py
5@@ -445,7 +445,10 @@ def sites_from_config(sites_yaml, sites_secrets=None, blacklist_ports=None):
6 sites = interpolate_secrets(conf, sites_secrets)
7 cache_port = 0
8 backend_port = 0
9+ new_sites = {}
10 for site, site_conf in sites.items():
11+ if not site_conf:
12+ continue
13 (cache_port, unused_backend_port) = utils.next_port_pair(
14 cache_port, backend_port, blacklist_ports=blacklist_ports
15 )
16@@ -456,7 +459,8 @@ def sites_from_config(sites_yaml, sites_secrets=None, blacklist_ports=None):
17 cache_port, backend_port, blacklist_ports=blacklist_ports
18 )
19 loc_conf['backend_port'] = backend_port
20- return sites
21+ new_sites[site] = site_conf
22+ return new_sites
23
24
25 def secrets_from_config(secrets_yaml):
26@@ -503,9 +507,9 @@ def _interpolate_secrets_origin_headers(headers, secrets):
27 return headers
28
29
30-def update_logrotate(service, retention, dateext=True):
31+def update_logrotate(service, retention, dateext=True, **kwargs):
32 conf_path = os.path.join('/etc/logrotate.d', service)
33- write_file(utils.logrotate(conf_path, retention=retention, dateext=dateext), conf_path)
34+ write_file(utils.logrotate(conf_path, retention=retention, dateext=dateext), conf_path, **kwargs)
35
36
37 def copy_file(source_path, dest_path, **kwargs):
38diff --git a/tests/unit/files/config_test_config.txt b/tests/unit/files/config_test_config.txt
39index 7543ec1..8f8a551 100644
40--- a/tests/unit/files/config_test_config.txt
41+++ b/tests/unit/files/config_test_config.txt
42@@ -115,3 +115,9 @@ site9.local:
43 backend-inter-time: '1m'
44 backends:
45 - 127.0.1.15:80
46+ /private/content:
47+ extra-config:
48+ - root /srv/example1.com/content/
49+ - autoindex on
50+ - auth_request /auth
51+ nagios-expect: 401 Unauthorized
52diff --git a/tests/unit/files/nginx_config_rendered_test_output-site9.local.txt b/tests/unit/files/nginx_config_rendered_test_output-site9.local.txt
53index b208bdc..8216d1c 100644
54--- a/tests/unit/files/nginx_config_rendered_test_output-site9.local.txt
55+++ b/tests/unit/files/nginx_config_rendered_test_output-site9.local.txt
56@@ -21,6 +21,13 @@ server {
57 }
58
59
60+ location /private/content {
61+ root /srv/example1.com/content/;
62+ autoindex on;
63+ auth_request /auth;
64+ }
65+
66+
67 access_log /var/log/nginx/site9.local-access.log content_cache;
68 error_log /var/log/nginx/site9.local-error.log;
69 }
70diff --git a/tests/unit/test_content_cache.py b/tests/unit/test_content_cache.py
71index d449749..5251c34 100644
72--- a/tests/unit/test_content_cache.py
73+++ b/tests/unit/test_content_cache.py
74@@ -58,10 +58,6 @@ class TestCharm(unittest.TestCase):
75 self.addCleanup(patcher.stop)
76 self.mock_cpu_count.return_value = 4
77
78- patcher = mock.patch('reactive.content_cache.update_logrotate')
79- self.mock_update_logrotate = patcher.start()
80- self.addCleanup(patcher.stop)
81-
82 patcher = mock.patch('time.sleep')
83 self.mock_time_sleep = patcher.start()
84 self.addCleanup(patcher.stop)
85@@ -146,16 +142,25 @@ class TestCharm(unittest.TestCase):
86 service_reload.assert_not_called()
87
88 @mock.patch('charms.reactive.clear_flag')
89- def test_configure_nginx_no_sites(self, clear_flag):
90+ @mock.patch('reactive.content_cache.update_logrotate')
91+ def test_configure_nginx_no_sites(self, logrotation, clear_flag):
92 '''Test correct flags are set when no sites defined to configure Nginx'''
93 content_cache.configure_nginx(self.tmpdir)
94 status.blocked.assert_called()
95 clear_flag.assert_called_once_with('content_cache.active')
96
97+ status.reset_mock()
98+ clear_flag.reset_mock()
99+ self.mock_config.return_value = {'sites': 'site1:'}
100+ content_cache.configure_nginx(self.tmpdir)
101+ status.blocked.assert_called()
102+ clear_flag.assert_called_once_with('content_cache.active')
103+
104 @mock.patch('charmhelpers.core.hookenv.close_port')
105 @mock.patch('charmhelpers.core.hookenv.opened_ports')
106 @mock.patch('reactive.content_cache.service_start_or_reload')
107- def test_configure_nginx_sites(self, service_start_or_reload, opened_ports, close_port):
108+ @mock.patch('reactive.content_cache.update_logrotate')
109+ def test_configure_nginx_sites(self, logrotation, service_start_or_reload, opened_ports, close_port):
110 '''Test configuration of Nginx sites'''
111 with open('tests/unit/files/config_test_config.txt', 'r', encoding='utf-8') as f:
112 ngx_config = f.read()
113@@ -218,7 +223,8 @@ class TestCharm(unittest.TestCase):
114 @mock.patch('charmhelpers.core.hookenv.close_port')
115 @mock.patch('charmhelpers.core.hookenv.opened_ports')
116 @mock.patch('reactive.content_cache.service_start_or_reload')
117- def test_configure_nginx_sites_secrets(self, service_start_or_reload, opened_ports, close_port):
118+ @mock.patch('reactive.content_cache.update_logrotate')
119+ def test_configure_nginx_sites_secrets(self, logrotation, service_start_or_reload, opened_ports, close_port):
120 with open('tests/unit/files/config_test_secrets.txt', 'r', encoding='utf-8') as f:
121 secrets = f.read()
122 config = '''
123@@ -267,7 +273,10 @@ site1.local:
124 @mock.patch('charmhelpers.core.hookenv.opened_ports')
125 @mock.patch('shutil.disk_usage')
126 @mock.patch('reactive.content_cache.service_start_or_reload')
127- def test_configure_nginx_cache_config(self, service_start_or_reload, disk_usage, opened_ports, close_port):
128+ @mock.patch('reactive.content_cache.update_logrotate')
129+ def test_configure_nginx_cache_config(
130+ self, logrotation, service_start_or_reload, disk_usage, opened_ports, close_port
131+ ):
132 config = '''
133 site1.local:
134 locations:
135@@ -331,14 +340,23 @@ site1.local:
136 self.assertEqual(got, want)
137
138 @mock.patch('charms.reactive.clear_flag')
139- def test_configure_haproxy_no_sites(self, clear_flag):
140+ @mock.patch('reactive.content_cache.update_logrotate')
141+ def test_configure_haproxy_no_sites(self, logrotation, clear_flag):
142+ content_cache.configure_haproxy()
143+ status.blocked.assert_called()
144+ clear_flag.assert_called_once_with('content_cache.active')
145+
146+ status.reset_mock()
147+ clear_flag.reset_mock()
148+ self.mock_config.return_value = {'sites': 'site1:'}
149 content_cache.configure_haproxy()
150 status.blocked.assert_called()
151 clear_flag.assert_called_once_with('content_cache.active')
152
153 @freezegun.freeze_time("2019-03-22", tz_offset=0)
154 @mock.patch('reactive.content_cache.service_start_or_reload')
155- def test_configure_haproxy_sites(self, service_start_or_reload):
156+ @mock.patch('reactive.content_cache.update_logrotate')
157+ def test_configure_haproxy_sites(self, logrotation, service_start_or_reload):
158 with open('tests/unit/files/config_test_config.txt', 'r', encoding='utf-8') as f:
159 ngx_config = f.read()
160 self.mock_config.return_value = {'sites': ngx_config}
161@@ -365,6 +383,11 @@ site1.local:
162 got = f.read()
163 self.assertEqual(got, want)
164
165+ @mock.patch('charms.reactive.set_flag')
166+ def test_fire_stats_hook(self, set_flag):
167+ content_cache.fire_stats_hook()
168+ set_flag.assert_has_calls([mock.call('haproxy-statistics.available')])
169+
170 @freezegun.freeze_time("2019-03-22", tz_offset=0)
171 @mock.patch('charms.reactive.set_flag')
172 @mock.patch('charmhelpers.contrib.charmsupport.nrpe.get_nagios_hostname')
173@@ -559,11 +582,50 @@ site1.local:
174 ]
175 nrpe_instance_mock.add_check.assert_has_calls(want, any_order=True)
176
177+ want = [
178+ mock.call(
179+ shortname='site_site9_local_privatecontent_listen',
180+ description='site9.local site listen check',
181+ check_cmd='/usr/lib/nagios/plugins/check_http -I 127.0.0.1 -H site9.local -p 80 -j HEAD'
182+ ' -u /private/content --expect="401 Unauthorized"',
183+ ),
184+ mock.call(
185+ shortname='site_site9_local_privatecontent_cache',
186+ description='site9.local cache check',
187+ check_cmd='/usr/lib/nagios/plugins/check_http -I 127.0.0.1 -H site9.local -p 6088 -j HEAD'
188+ ' -u /private/content --expect="401 Unauthorized"',
189+ ),
190+ ]
191+ nrpe_instance_mock.add_check.assert_has_calls(want, any_order=True)
192+
193 nrpe_instance_mock.write.assert_called()
194
195 want = [mock.call('nagios-nrpe.configured')]
196 set_flag.assert_has_calls(want, any_order=True)
197
198+ @mock.patch('charms.reactive.set_flag')
199+ @mock.patch('charmhelpers.contrib.charmsupport.nrpe.get_nagios_hostname')
200+ @mock.patch('charmhelpers.contrib.charmsupport.nrpe.NRPE')
201+ def test_check_haproxy_alerts(self, nrpe, get_nagios_hostname, set_flag):
202+ get_nagios_hostname.return_value = 'some-host.local'
203+ nrpe_instance_mock = nrpe(get_nagios_hostname(), primary=True)
204+
205+ content_cache.check_haproxy_alerts()
206+
207+ want = [
208+ mock.call(
209+ shortname='haproxy_telegraf_metrics',
210+ description='Verify haproxy metrics are visible via telegraf subordinate',
211+ check_cmd='/usr/lib/nagios/plugins/check_http -I 127.0.0.1 -p 9103 -u /metrics -r "haproxy_rate"'
212+ )
213+ ]
214+ nrpe_instance_mock.add_check.assert_has_calls(want, any_order=True)
215+
216+ nrpe_instance_mock.write.assert_called()
217+
218+ want = [mock.call('nagios-nrpe-telegraf.configured')]
219+ set_flag.assert_has_calls(want, any_order=True)
220+
221 def test_sites_from_config(self):
222 config_yaml = '''
223 site1.local:
224@@ -697,6 +759,16 @@ site1.local:
225 want = {'site1.local': {'locations': {'/': {'signed-url-hmac-key': 'Maiqu7ohmeiSh6ooroa0'}}}}
226 self.assertEqual(content_cache.interpolate_secrets(config, secrets), want)
227
228+ # Secrets exists, but none the for site we want
229+ config = {
230+ 'site2.local': {
231+ 'locations': {
232+ '/': {'origin-headers': [{'X-Origin-Key': '${secret}'}], 'signed-url-hmac-key': '${secret}'}
233+ }
234+ }
235+ }
236+ self.assertEqual(content_cache.interpolate_secrets(config, secrets), config)
237+
238 @mock.patch('charms.reactive.set_flag')
239 @mock.patch('subprocess.call')
240 def test_configure_sysctl(self, call, set_flag):
241@@ -747,6 +819,13 @@ site1.local:
242 self.assertEqual(got, want)
243
244 @mock.patch('charmhelpers.core.host.write_file')
245+ def test_update_logrotate(self, write_file):
246+ content_cache.update_logrotate('nginx', '30', dateext=True, owner='somedude', group='somegroup', perms=444)
247+ write_file.assert_called_once_with(
248+ content=None, group='somegroup', owner='somedude', path='/etc/logrotate.d/nginx', perms=444
249+ )
250+
251+ @mock.patch('charmhelpers.core.host.write_file')
252 def test_copy_file_ownership(self, write_file):
253 source = os.path.join(self.charm_dir, 'tests/unit/files/test_file.txt')
254 dest = os.path.join(self.tmpdir, os.path.basename(source))
255@@ -764,7 +843,8 @@ site1.local:
256 @mock.patch('charmhelpers.core.hookenv.open_port')
257 @mock.patch('charmhelpers.core.hookenv.opened_ports')
258 @mock.patch('reactive.content_cache.service_start_or_reload')
259- def test_configure_nginx_metrics_sites(self, service_start_or_reload, opened_ports, open_port):
260+ @mock.patch('reactive.content_cache.update_logrotate')
261+ def test_configure_nginx_metrics_sites(self, logrotation, service_start_or_reload, opened_ports, open_port):
262 """Test configuration of Nginx sites with enable_prometheus_metrics activated."""
263 with open('tests/unit/files/config_test_basic_config.txt', 'r', encoding='utf-8') as f:
264 ngx_config = f.read()
265@@ -835,8 +915,9 @@ site1.local:
266 @mock.patch('charmhelpers.core.host.pwgen')
267 @mock.patch('lib.haproxy.HAProxyConf')
268 @mock.patch('reactive.content_cache.service_start_or_reload')
269+ @mock.patch('reactive.content_cache.update_logrotate')
270 def test_configure_haproxy_ports_management(
271- self, service_start_or_reload, haproxyconf, pwgen, opened_ports, open_port, close_port
272+ self, logrotation, service_start_or_reload, haproxyconf, pwgen, opened_ports, open_port, close_port
273 ):
274 with open('tests/unit/files/config_test_basic_config.txt', 'r', encoding='utf-8') as f:
275 ngx_config = f.read()

Subscribers

People subscribed via source and target branches