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

Proposed by Haw Loeung
Status: Merged
Approved by: Haw Loeung
Approved revision: c904c2e553842eecadea707f5c76041cd1d727f5
Merged at revision: 057ebc0d4095dbe754afeb858677c4f9001da8a7
Proposed branch: ~hloeung/content-cache-charm:cleanup
Merge into: content-cache-charm:master
Diff against target: 174 lines (+35/-34)
1 file modified
tests/unit/test_content_cache.py (+35/-34)
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Content Cache Charmers Pending
Review via email: mp+383043@code.launchpad.net

Commit message

Fixed unit test removing multiple 'with mock()' calls

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 :

Yer, that all looks better

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

Change successfully merged at revision 057ebc0d4095dbe754afeb858677c4f9001da8a7

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/tests/unit/test_content_cache.py b/tests/unit/test_content_cache.py
index d9a8b68..7050e96 100644
--- a/tests/unit/test_content_cache.py
+++ b/tests/unit/test_content_cache.py
@@ -57,6 +57,19 @@ class TestCharm(unittest.TestCase):
57 self.addCleanup(patcher.stop)57 self.addCleanup(patcher.stop)
58 self.mock_log.return_value = ''58 self.mock_log.return_value = ''
5959
60 patcher = mock.patch('charmhelpers.core.host.pwgen')
61 self.mock_pwgen = patcher.start()
62 self.addCleanup(patcher.stop)
63 self.mock_pwgen.return_value = 'biometricsarenotsecret'
64
65 patcher = mock.patch('charmhelpers.core.hookenv.open_port')
66 self.mock_open_port = patcher.start()
67 self.addCleanup(patcher.stop)
68
69 patcher = mock.patch('charmhelpers.core.hookenv.close_port')
70 self.mock_close_port = patcher.start()
71 self.addCleanup(patcher.stop)
72
60 patcher = mock.patch('multiprocessing.cpu_count')73 patcher = mock.patch('multiprocessing.cpu_count')
61 self.mock_cpu_count = patcher.start()74 self.mock_cpu_count = patcher.start()
62 self.addCleanup(patcher.stop)75 self.addCleanup(patcher.stop)
@@ -234,11 +247,10 @@ class TestCharm(unittest.TestCase):
234 got = f.read()247 got = f.read()
235 self.assertEqual(got, want)248 self.assertEqual(got, want)
236249
237 @mock.patch('charmhelpers.core.hookenv.close_port')
238 @mock.patch('charmhelpers.core.hookenv.opened_ports')250 @mock.patch('charmhelpers.core.hookenv.opened_ports')
239 @mock.patch('reactive.content_cache.service_start_or_reload')251 @mock.patch('reactive.content_cache.service_start_or_reload')
240 @mock.patch('reactive.content_cache.update_logrotate')252 @mock.patch('reactive.content_cache.update_logrotate')
241 def test_configure_nginx_sites_secrets(self, logrotation, service_start_or_reload, opened_ports, close_port):253 def test_configure_nginx_sites_secrets(self, logrotation, service_start_or_reload, opened_ports):
242 with open('tests/unit/files/config_test_secrets.txt', 'r', encoding='utf-8') as f:254 with open('tests/unit/files/config_test_secrets.txt', 'r', encoding='utf-8') as f:
243 secrets = f.read()255 secrets = f.read()
244 config = '''256 config = '''
@@ -281,14 +293,11 @@ site1.local:
281 got = f.read()293 got = f.read()
282 self.assertEqual(got, want)294 self.assertEqual(got, want)
283295
284 @mock.patch('charmhelpers.core.hookenv.close_port')
285 @mock.patch('charmhelpers.core.hookenv.opened_ports')296 @mock.patch('charmhelpers.core.hookenv.opened_ports')
286 @mock.patch('shutil.disk_usage')297 @mock.patch('shutil.disk_usage')
287 @mock.patch('reactive.content_cache.service_start_or_reload')298 @mock.patch('reactive.content_cache.service_start_or_reload')
288 @mock.patch('reactive.content_cache.update_logrotate')299 @mock.patch('reactive.content_cache.update_logrotate')
289 def test_configure_nginx_cache_config(300 def test_configure_nginx_cache_config(self, logrotation, service_start_or_reload, disk_usage, opened_ports):
290 self, logrotation, service_start_or_reload, disk_usage, opened_ports, close_port
291 ):
292 config = '''301 config = '''
293site1.local:302site1.local:
294 locations:303 locations:
@@ -373,27 +382,24 @@ site1.local:
373 clear_flag.assert_called_once_with('content_cache.active')382 clear_flag.assert_called_once_with('content_cache.active')
374383
375 @freezegun.freeze_time("2019-03-22", tz_offset=0)384 @freezegun.freeze_time("2019-03-22", tz_offset=0)
385 @mock.patch('charmhelpers.core.hookenv.opened_ports')
376 @mock.patch('charms.reactive.set_flag')386 @mock.patch('charms.reactive.set_flag')
377 @mock.patch('reactive.content_cache.update_logrotate')387 @mock.patch('reactive.content_cache.update_logrotate')
378 def test_configure_haproxy_sites(self, logrotation, set_flag):388 def test_configure_haproxy_sites(self, logrotation, set_flag, opened_ports):
379 with open('tests/unit/files/config_test_config.txt', 'r', encoding='utf-8') as f:389 with open('tests/unit/files/config_test_config.txt', 'r', encoding='utf-8') as f:
380 config = f.read()390 config = f.read()
381 self.mock_config.return_value = {'max_connections': 8192, 'sites': config}391 self.mock_config.return_value = {'max_connections': 8192, 'sites': config}
382392
383 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:393 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:
384 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')394 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')
385 with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"), mock.patch(395 opened_ports.return_value = ['443/tcp']
386 'charmhelpers.core.hookenv.opened_ports', return_value=["443/tcp"]396 content_cache.configure_haproxy()
387 ), mock.patch('charmhelpers.core.hookenv.open_port'), mock.patch('charmhelpers.core.hookenv.close_port'):
388 content_cache.configure_haproxy()
389 set_flag.assert_has_calls([mock.call('content_cache.haproxy.reload-required')])397 set_flag.assert_has_calls([mock.call('content_cache.haproxy.reload-required')])
390398
391 # Again, this time should be no change so no need to restart HAProxy399 # Again, this time should be no change so no need to restart HAProxy
392 set_flag.reset_mock()400 set_flag.reset_mock()
393 with mock.patch('charmhelpers.core.hookenv.opened_ports', return_value=["443/tcp"]), mock.patch(401 opened_ports.return_value = ['443/tcp']
394 'charmhelpers.core.hookenv.open_port'402 content_cache.configure_haproxy()
395 ), mock.patch('charmhelpers.core.hookenv.close_port'):
396 content_cache.configure_haproxy()
397 self.assertFalse(mock.call('content_cache.haproxy.reload-required') in set_flag.call_args_list)403 self.assertFalse(mock.call('content_cache.haproxy.reload-required') in set_flag.call_args_list)
398404
399 with open('tests/unit/files/content_cache_rendered_haproxy_test_output.txt', 'r', encoding='utf-8') as f:405 with open('tests/unit/files/content_cache_rendered_haproxy_test_output.txt', 'r', encoding='utf-8') as f:
@@ -413,10 +419,8 @@ site1.local:
413 self.mock_config.return_value = {'max_connections': 8192, 'sites': config}419 self.mock_config.return_value = {'max_connections': 8192, 'sites': config}
414 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:420 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:
415 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')421 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')
416 with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"), mock.patch(422 opened_ports.return_value = ['443/tcp']
417 'charmhelpers.core.hookenv.opened_ports', return_value=["443/tcp"]423 content_cache.configure_haproxy()
418 ), mock.patch('charmhelpers.core.hookenv.open_port'), mock.patch('charmhelpers.core.hookenv.close_port'):
419 content_cache.configure_haproxy()
420424
421 with open('tests/unit/files/content_cache_rendered_haproxy_test_output2.txt', 'r', encoding='utf-8') as f:425 with open('tests/unit/files/content_cache_rendered_haproxy_test_output2.txt', 'r', encoding='utf-8') as f:
422 want = f.read()426 want = f.read()
@@ -425,9 +429,10 @@ site1.local:
425 self.assertEqual(got, want)429 self.assertEqual(got, want)
426430
427 @freezegun.freeze_time("2019-03-22", tz_offset=0)431 @freezegun.freeze_time("2019-03-22", tz_offset=0)
432 @mock.patch('charmhelpers.core.hookenv.opened_ports')
428 @mock.patch('charms.reactive.set_flag')433 @mock.patch('charms.reactive.set_flag')
429 @mock.patch('reactive.content_cache.update_logrotate')434 @mock.patch('reactive.content_cache.update_logrotate')
430 def test_configure_haproxy_sites_no_extra_stanzas(self, logrotation, set_flag):435 def test_configure_haproxy_sites_no_extra_stanzas(self, logrotation, set_flag, opened_ports):
431 config = '''436 config = '''
432site1.local:437site1.local:
433 locations:438 locations:
@@ -440,10 +445,8 @@ site1.local:
440 self.mock_config.return_value = {'max_connections': 8192, 'sites': config}445 self.mock_config.return_value = {'max_connections': 8192, 'sites': config}
441 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:446 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:
442 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')447 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')
443 with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"), mock.patch(448 opened_ports.return_value = ['443/tcp']
444 'charmhelpers.core.hookenv.opened_ports', return_value=["443/tcp"]449 content_cache.configure_haproxy()
445 ), mock.patch('charmhelpers.core.hookenv.open_port'), mock.patch('charmhelpers.core.hookenv.close_port'):
446 content_cache.configure_haproxy()
447450
448 with open('tests/unit/files/content_cache_rendered_haproxy_test_output3.txt', 'r', encoding='utf-8') as f:451 with open('tests/unit/files/content_cache_rendered_haproxy_test_output3.txt', 'r', encoding='utf-8') as f:
449 want = f.read()452 want = f.read()
@@ -452,19 +455,18 @@ site1.local:
452 self.assertEqual(got, want)455 self.assertEqual(got, want)
453456
454 @freezegun.freeze_time("2019-03-22", tz_offset=0)457 @freezegun.freeze_time("2019-03-22", tz_offset=0)
458 @mock.patch('charmhelpers.core.hookenv.opened_ports')
455 @mock.patch('charms.reactive.set_flag')459 @mock.patch('charms.reactive.set_flag')
456 @mock.patch('reactive.content_cache.update_logrotate')460 @mock.patch('reactive.content_cache.update_logrotate')
457 def test_configure_haproxy_sites_auto_maxconns(self, logrotation, set_flag):461 def test_configure_haproxy_sites_auto_maxconns(self, logrotation, set_flag, opened_ports):
458 with open('tests/unit/files/config_test_config.txt', 'r', encoding='utf-8') as f:462 with open('tests/unit/files/config_test_config.txt', 'r', encoding='utf-8') as f:
459 ngx_config = f.read()463 ngx_config = f.read()
460 self.mock_config.return_value = {'max_connections': 0, 'sites': ngx_config}464 self.mock_config.return_value = {'max_connections': 0, 'sites': ngx_config}
461465
462 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:466 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:
463 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')467 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')
464 with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"), mock.patch(468 opened_ports.return_value = ['443/tcp']
465 'charmhelpers.core.hookenv.opened_ports', return_value=["443/tcp"]469 content_cache.configure_haproxy()
466 ), mock.patch('charmhelpers.core.hookenv.open_port'), mock.patch('charmhelpers.core.hookenv.close_port'):
467 content_cache.configure_haproxy()
468470
469 with open(471 with open(
470 'tests/unit/files/content_cache_rendered_haproxy_test_output_auto_maxconns.txt', 'r', encoding='utf-8'472 'tests/unit/files/content_cache_rendered_haproxy_test_output_auto_maxconns.txt', 'r', encoding='utf-8'
@@ -475,9 +477,10 @@ site1.local:
475 self.assertEqual(got, want)477 self.assertEqual(got, want)
476478
477 @freezegun.freeze_time("2019-03-22", tz_offset=0)479 @freezegun.freeze_time("2019-03-22", tz_offset=0)
480 @mock.patch('charmhelpers.core.hookenv.opened_ports')
478 @mock.patch('charms.reactive.set_flag')481 @mock.patch('charms.reactive.set_flag')
479 @mock.patch('reactive.content_cache.update_logrotate')482 @mock.patch('reactive.content_cache.update_logrotate')
480 def test_configure_haproxy_processes_and_threads(self, logrotation, set_flag):483 def test_configure_haproxy_processes_and_threads(self, logrotation, set_flag, opened_ports):
481 with open('tests/unit/files/config_test_config.txt', 'r', encoding='utf-8') as f:484 with open('tests/unit/files/config_test_config.txt', 'r', encoding='utf-8') as f:
482 ngx_config = f.read()485 ngx_config = f.read()
483 self.mock_config.return_value = {486 self.mock_config.return_value = {
@@ -489,10 +492,8 @@ site1.local:
489492
490 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:493 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:
491 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')494 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')
492 with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"), mock.patch(495 opened_ports.return_value = ['443/tcp']
493 'charmhelpers.core.hookenv.opened_ports', return_value=["443/tcp"]496 content_cache.configure_haproxy()
494 ), mock.patch('charmhelpers.core.hookenv.open_port'), mock.patch('charmhelpers.core.hookenv.close_port'):
495 content_cache.configure_haproxy()
496497
497 with open(498 with open(
498 'tests/unit/files/content_cache_rendered_haproxy_test_output_processes_and_threads.txt',499 'tests/unit/files/content_cache_rendered_haproxy_test_output_processes_and_threads.txt',

Subscribers

People subscribed via source and target branches