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
1diff --git a/tests/unit/test_content_cache.py b/tests/unit/test_content_cache.py
2index d9a8b68..7050e96 100644
3--- a/tests/unit/test_content_cache.py
4+++ b/tests/unit/test_content_cache.py
5@@ -57,6 +57,19 @@ class TestCharm(unittest.TestCase):
6 self.addCleanup(patcher.stop)
7 self.mock_log.return_value = ''
8
9+ patcher = mock.patch('charmhelpers.core.host.pwgen')
10+ self.mock_pwgen = patcher.start()
11+ self.addCleanup(patcher.stop)
12+ self.mock_pwgen.return_value = 'biometricsarenotsecret'
13+
14+ patcher = mock.patch('charmhelpers.core.hookenv.open_port')
15+ self.mock_open_port = patcher.start()
16+ self.addCleanup(patcher.stop)
17+
18+ patcher = mock.patch('charmhelpers.core.hookenv.close_port')
19+ self.mock_close_port = patcher.start()
20+ self.addCleanup(patcher.stop)
21+
22 patcher = mock.patch('multiprocessing.cpu_count')
23 self.mock_cpu_count = patcher.start()
24 self.addCleanup(patcher.stop)
25@@ -234,11 +247,10 @@ class TestCharm(unittest.TestCase):
26 got = f.read()
27 self.assertEqual(got, want)
28
29- @mock.patch('charmhelpers.core.hookenv.close_port')
30 @mock.patch('charmhelpers.core.hookenv.opened_ports')
31 @mock.patch('reactive.content_cache.service_start_or_reload')
32 @mock.patch('reactive.content_cache.update_logrotate')
33- def test_configure_nginx_sites_secrets(self, logrotation, service_start_or_reload, opened_ports, close_port):
34+ def test_configure_nginx_sites_secrets(self, logrotation, service_start_or_reload, opened_ports):
35 with open('tests/unit/files/config_test_secrets.txt', 'r', encoding='utf-8') as f:
36 secrets = f.read()
37 config = '''
38@@ -281,14 +293,11 @@ site1.local:
39 got = f.read()
40 self.assertEqual(got, want)
41
42- @mock.patch('charmhelpers.core.hookenv.close_port')
43 @mock.patch('charmhelpers.core.hookenv.opened_ports')
44 @mock.patch('shutil.disk_usage')
45 @mock.patch('reactive.content_cache.service_start_or_reload')
46 @mock.patch('reactive.content_cache.update_logrotate')
47- def test_configure_nginx_cache_config(
48- self, logrotation, service_start_or_reload, disk_usage, opened_ports, close_port
49- ):
50+ def test_configure_nginx_cache_config(self, logrotation, service_start_or_reload, disk_usage, opened_ports):
51 config = '''
52 site1.local:
53 locations:
54@@ -373,27 +382,24 @@ site1.local:
55 clear_flag.assert_called_once_with('content_cache.active')
56
57 @freezegun.freeze_time("2019-03-22", tz_offset=0)
58+ @mock.patch('charmhelpers.core.hookenv.opened_ports')
59 @mock.patch('charms.reactive.set_flag')
60 @mock.patch('reactive.content_cache.update_logrotate')
61- def test_configure_haproxy_sites(self, logrotation, set_flag):
62+ def test_configure_haproxy_sites(self, logrotation, set_flag, opened_ports):
63 with open('tests/unit/files/config_test_config.txt', 'r', encoding='utf-8') as f:
64 config = f.read()
65 self.mock_config.return_value = {'max_connections': 8192, 'sites': config}
66
67 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:
68 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')
69- with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"), mock.patch(
70- 'charmhelpers.core.hookenv.opened_ports', return_value=["443/tcp"]
71- ), mock.patch('charmhelpers.core.hookenv.open_port'), mock.patch('charmhelpers.core.hookenv.close_port'):
72- content_cache.configure_haproxy()
73+ opened_ports.return_value = ['443/tcp']
74+ content_cache.configure_haproxy()
75 set_flag.assert_has_calls([mock.call('content_cache.haproxy.reload-required')])
76
77 # Again, this time should be no change so no need to restart HAProxy
78 set_flag.reset_mock()
79- with mock.patch('charmhelpers.core.hookenv.opened_ports', return_value=["443/tcp"]), mock.patch(
80- 'charmhelpers.core.hookenv.open_port'
81- ), mock.patch('charmhelpers.core.hookenv.close_port'):
82- content_cache.configure_haproxy()
83+ opened_ports.return_value = ['443/tcp']
84+ content_cache.configure_haproxy()
85 self.assertFalse(mock.call('content_cache.haproxy.reload-required') in set_flag.call_args_list)
86
87 with open('tests/unit/files/content_cache_rendered_haproxy_test_output.txt', 'r', encoding='utf-8') as f:
88@@ -413,10 +419,8 @@ site1.local:
89 self.mock_config.return_value = {'max_connections': 8192, 'sites': config}
90 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:
91 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')
92- with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"), mock.patch(
93- 'charmhelpers.core.hookenv.opened_ports', return_value=["443/tcp"]
94- ), mock.patch('charmhelpers.core.hookenv.open_port'), mock.patch('charmhelpers.core.hookenv.close_port'):
95- content_cache.configure_haproxy()
96+ opened_ports.return_value = ['443/tcp']
97+ content_cache.configure_haproxy()
98
99 with open('tests/unit/files/content_cache_rendered_haproxy_test_output2.txt', 'r', encoding='utf-8') as f:
100 want = f.read()
101@@ -425,9 +429,10 @@ site1.local:
102 self.assertEqual(got, want)
103
104 @freezegun.freeze_time("2019-03-22", tz_offset=0)
105+ @mock.patch('charmhelpers.core.hookenv.opened_ports')
106 @mock.patch('charms.reactive.set_flag')
107 @mock.patch('reactive.content_cache.update_logrotate')
108- def test_configure_haproxy_sites_no_extra_stanzas(self, logrotation, set_flag):
109+ def test_configure_haproxy_sites_no_extra_stanzas(self, logrotation, set_flag, opened_ports):
110 config = '''
111 site1.local:
112 locations:
113@@ -440,10 +445,8 @@ site1.local:
114 self.mock_config.return_value = {'max_connections': 8192, 'sites': config}
115 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:
116 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')
117- with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"), mock.patch(
118- 'charmhelpers.core.hookenv.opened_ports', return_value=["443/tcp"]
119- ), mock.patch('charmhelpers.core.hookenv.open_port'), mock.patch('charmhelpers.core.hookenv.close_port'):
120- content_cache.configure_haproxy()
121+ opened_ports.return_value = ['443/tcp']
122+ content_cache.configure_haproxy()
123
124 with open('tests/unit/files/content_cache_rendered_haproxy_test_output3.txt', 'r', encoding='utf-8') as f:
125 want = f.read()
126@@ -452,19 +455,18 @@ site1.local:
127 self.assertEqual(got, want)
128
129 @freezegun.freeze_time("2019-03-22", tz_offset=0)
130+ @mock.patch('charmhelpers.core.hookenv.opened_ports')
131 @mock.patch('charms.reactive.set_flag')
132 @mock.patch('reactive.content_cache.update_logrotate')
133- def test_configure_haproxy_sites_auto_maxconns(self, logrotation, set_flag):
134+ def test_configure_haproxy_sites_auto_maxconns(self, logrotation, set_flag, opened_ports):
135 with open('tests/unit/files/config_test_config.txt', 'r', encoding='utf-8') as f:
136 ngx_config = f.read()
137 self.mock_config.return_value = {'max_connections': 0, 'sites': ngx_config}
138
139 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:
140 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')
141- with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"), mock.patch(
142- 'charmhelpers.core.hookenv.opened_ports', return_value=["443/tcp"]
143- ), mock.patch('charmhelpers.core.hookenv.open_port'), mock.patch('charmhelpers.core.hookenv.close_port'):
144- content_cache.configure_haproxy()
145+ opened_ports.return_value = ['443/tcp']
146+ content_cache.configure_haproxy()
147
148 with open(
149 'tests/unit/files/content_cache_rendered_haproxy_test_output_auto_maxconns.txt', 'r', encoding='utf-8'
150@@ -475,9 +477,10 @@ site1.local:
151 self.assertEqual(got, want)
152
153 @freezegun.freeze_time("2019-03-22", tz_offset=0)
154+ @mock.patch('charmhelpers.core.hookenv.opened_ports')
155 @mock.patch('charms.reactive.set_flag')
156 @mock.patch('reactive.content_cache.update_logrotate')
157- def test_configure_haproxy_processes_and_threads(self, logrotation, set_flag):
158+ def test_configure_haproxy_processes_and_threads(self, logrotation, set_flag, opened_ports):
159 with open('tests/unit/files/config_test_config.txt', 'r', encoding='utf-8') as f:
160 ngx_config = f.read()
161 self.mock_config.return_value = {
162@@ -489,10 +492,8 @@ site1.local:
163
164 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:
165 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')
166- with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"), mock.patch(
167- 'charmhelpers.core.hookenv.opened_ports', return_value=["443/tcp"]
168- ), mock.patch('charmhelpers.core.hookenv.open_port'), mock.patch('charmhelpers.core.hookenv.close_port'):
169- content_cache.configure_haproxy()
170+ opened_ports.return_value = ['443/tcp']
171+ content_cache.configure_haproxy()
172
173 with open(
174 'tests/unit/files/content_cache_rendered_haproxy_test_output_processes_and_threads.txt',

Subscribers

People subscribed via source and target branches