Merge ~hloeung/content-cache-charm:cleanup into content-cache-charm:master
- Git
- lp:~hloeung/content-cache-charm
- cleanup
- Merge into 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) |
Related bugs: |
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
Description of the change
To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : | # |
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 057ebc0d4095dbe
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | diff --git a/tests/unit/test_content_cache.py b/tests/unit/test_content_cache.py | |||
2 | index 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 | 57 | self.addCleanup(patcher.stop) | 57 | self.addCleanup(patcher.stop) |
7 | 58 | self.mock_log.return_value = '' | 58 | self.mock_log.return_value = '' |
8 | 59 | 59 | ||
9 | 60 | patcher = mock.patch('charmhelpers.core.host.pwgen') | ||
10 | 61 | self.mock_pwgen = patcher.start() | ||
11 | 62 | self.addCleanup(patcher.stop) | ||
12 | 63 | self.mock_pwgen.return_value = 'biometricsarenotsecret' | ||
13 | 64 | |||
14 | 65 | patcher = mock.patch('charmhelpers.core.hookenv.open_port') | ||
15 | 66 | self.mock_open_port = patcher.start() | ||
16 | 67 | self.addCleanup(patcher.stop) | ||
17 | 68 | |||
18 | 69 | patcher = mock.patch('charmhelpers.core.hookenv.close_port') | ||
19 | 70 | self.mock_close_port = patcher.start() | ||
20 | 71 | self.addCleanup(patcher.stop) | ||
21 | 72 | |||
22 | 60 | patcher = mock.patch('multiprocessing.cpu_count') | 73 | patcher = mock.patch('multiprocessing.cpu_count') |
23 | 61 | self.mock_cpu_count = patcher.start() | 74 | self.mock_cpu_count = patcher.start() |
24 | 62 | self.addCleanup(patcher.stop) | 75 | self.addCleanup(patcher.stop) |
25 | @@ -234,11 +247,10 @@ class TestCharm(unittest.TestCase): | |||
26 | 234 | got = f.read() | 247 | got = f.read() |
27 | 235 | self.assertEqual(got, want) | 248 | self.assertEqual(got, want) |
28 | 236 | 249 | ||
29 | 237 | @mock.patch('charmhelpers.core.hookenv.close_port') | ||
30 | 238 | @mock.patch('charmhelpers.core.hookenv.opened_ports') | 250 | @mock.patch('charmhelpers.core.hookenv.opened_ports') |
31 | 239 | @mock.patch('reactive.content_cache.service_start_or_reload') | 251 | @mock.patch('reactive.content_cache.service_start_or_reload') |
32 | 240 | @mock.patch('reactive.content_cache.update_logrotate') | 252 | @mock.patch('reactive.content_cache.update_logrotate') |
34 | 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): |
35 | 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: |
36 | 243 | secrets = f.read() | 255 | secrets = f.read() |
37 | 244 | config = ''' | 256 | config = ''' |
38 | @@ -281,14 +293,11 @@ site1.local: | |||
39 | 281 | got = f.read() | 293 | got = f.read() |
40 | 282 | self.assertEqual(got, want) | 294 | self.assertEqual(got, want) |
41 | 283 | 295 | ||
42 | 284 | @mock.patch('charmhelpers.core.hookenv.close_port') | ||
43 | 285 | @mock.patch('charmhelpers.core.hookenv.opened_ports') | 296 | @mock.patch('charmhelpers.core.hookenv.opened_ports') |
44 | 286 | @mock.patch('shutil.disk_usage') | 297 | @mock.patch('shutil.disk_usage') |
45 | 287 | @mock.patch('reactive.content_cache.service_start_or_reload') | 298 | @mock.patch('reactive.content_cache.service_start_or_reload') |
46 | 288 | @mock.patch('reactive.content_cache.update_logrotate') | 299 | @mock.patch('reactive.content_cache.update_logrotate') |
50 | 289 | def test_configure_nginx_cache_config( | 300 | def test_configure_nginx_cache_config(self, logrotation, service_start_or_reload, disk_usage, opened_ports): |
48 | 290 | self, logrotation, service_start_or_reload, disk_usage, opened_ports, close_port | ||
49 | 291 | ): | ||
51 | 292 | config = ''' | 301 | config = ''' |
52 | 293 | site1.local: | 302 | site1.local: |
53 | 294 | locations: | 303 | locations: |
54 | @@ -373,27 +382,24 @@ site1.local: | |||
55 | 373 | clear_flag.assert_called_once_with('content_cache.active') | 382 | clear_flag.assert_called_once_with('content_cache.active') |
56 | 374 | 383 | ||
57 | 375 | @freezegun.freeze_time("2019-03-22", tz_offset=0) | 384 | @freezegun.freeze_time("2019-03-22", tz_offset=0) |
58 | 385 | @mock.patch('charmhelpers.core.hookenv.opened_ports') | ||
59 | 376 | @mock.patch('charms.reactive.set_flag') | 386 | @mock.patch('charms.reactive.set_flag') |
60 | 377 | @mock.patch('reactive.content_cache.update_logrotate') | 387 | @mock.patch('reactive.content_cache.update_logrotate') |
62 | 378 | def test_configure_haproxy_sites(self, logrotation, set_flag): | 388 | def test_configure_haproxy_sites(self, logrotation, set_flag, opened_ports): |
63 | 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: |
64 | 380 | config = f.read() | 390 | config = f.read() |
65 | 381 | self.mock_config.return_value = {'max_connections': 8192, 'sites': config} | 391 | self.mock_config.return_value = {'max_connections': 8192, 'sites': config} |
66 | 382 | 392 | ||
67 | 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: |
68 | 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') |
73 | 385 | with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"), mock.patch( | 395 | opened_ports.return_value = ['443/tcp'] |
74 | 386 | 'charmhelpers.core.hookenv.opened_ports', return_value=["443/tcp"] | 396 | content_cache.configure_haproxy() |
71 | 387 | ), mock.patch('charmhelpers.core.hookenv.open_port'), mock.patch('charmhelpers.core.hookenv.close_port'): | ||
72 | 388 | content_cache.configure_haproxy() | ||
75 | 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')]) |
76 | 390 | 398 | ||
77 | 391 | # Again, this time should be no change so no need to restart HAProxy | 399 | # Again, this time should be no change so no need to restart HAProxy |
78 | 392 | set_flag.reset_mock() | 400 | set_flag.reset_mock() |
83 | 393 | with mock.patch('charmhelpers.core.hookenv.opened_ports', return_value=["443/tcp"]), mock.patch( | 401 | opened_ports.return_value = ['443/tcp'] |
84 | 394 | 'charmhelpers.core.hookenv.open_port' | 402 | content_cache.configure_haproxy() |
81 | 395 | ), mock.patch('charmhelpers.core.hookenv.close_port'): | ||
82 | 396 | content_cache.configure_haproxy() | ||
85 | 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) |
86 | 398 | 404 | ||
87 | 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: |
88 | @@ -413,10 +419,8 @@ site1.local: | |||
89 | 413 | self.mock_config.return_value = {'max_connections': 8192, 'sites': config} | 419 | self.mock_config.return_value = {'max_connections': 8192, 'sites': config} |
90 | 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: |
91 | 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') |
96 | 416 | with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"), mock.patch( | 422 | opened_ports.return_value = ['443/tcp'] |
97 | 417 | 'charmhelpers.core.hookenv.opened_ports', return_value=["443/tcp"] | 423 | content_cache.configure_haproxy() |
94 | 418 | ), mock.patch('charmhelpers.core.hookenv.open_port'), mock.patch('charmhelpers.core.hookenv.close_port'): | ||
95 | 419 | content_cache.configure_haproxy() | ||
98 | 420 | 424 | ||
99 | 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: |
100 | 422 | want = f.read() | 426 | want = f.read() |
101 | @@ -425,9 +429,10 @@ site1.local: | |||
102 | 425 | self.assertEqual(got, want) | 429 | self.assertEqual(got, want) |
103 | 426 | 430 | ||
104 | 427 | @freezegun.freeze_time("2019-03-22", tz_offset=0) | 431 | @freezegun.freeze_time("2019-03-22", tz_offset=0) |
105 | 432 | @mock.patch('charmhelpers.core.hookenv.opened_ports') | ||
106 | 428 | @mock.patch('charms.reactive.set_flag') | 433 | @mock.patch('charms.reactive.set_flag') |
107 | 429 | @mock.patch('reactive.content_cache.update_logrotate') | 434 | @mock.patch('reactive.content_cache.update_logrotate') |
109 | 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): |
110 | 431 | config = ''' | 436 | config = ''' |
111 | 432 | site1.local: | 437 | site1.local: |
112 | 433 | locations: | 438 | locations: |
113 | @@ -440,10 +445,8 @@ site1.local: | |||
114 | 440 | self.mock_config.return_value = {'max_connections': 8192, 'sites': config} | 445 | self.mock_config.return_value = {'max_connections': 8192, 'sites': config} |
115 | 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: |
116 | 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') |
121 | 443 | with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"), mock.patch( | 448 | opened_ports.return_value = ['443/tcp'] |
122 | 444 | 'charmhelpers.core.hookenv.opened_ports', return_value=["443/tcp"] | 449 | content_cache.configure_haproxy() |
119 | 445 | ), mock.patch('charmhelpers.core.hookenv.open_port'), mock.patch('charmhelpers.core.hookenv.close_port'): | ||
120 | 446 | content_cache.configure_haproxy() | ||
123 | 447 | 450 | ||
124 | 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: |
125 | 449 | want = f.read() | 452 | want = f.read() |
126 | @@ -452,19 +455,18 @@ site1.local: | |||
127 | 452 | self.assertEqual(got, want) | 455 | self.assertEqual(got, want) |
128 | 453 | 456 | ||
129 | 454 | @freezegun.freeze_time("2019-03-22", tz_offset=0) | 457 | @freezegun.freeze_time("2019-03-22", tz_offset=0) |
130 | 458 | @mock.patch('charmhelpers.core.hookenv.opened_ports') | ||
131 | 455 | @mock.patch('charms.reactive.set_flag') | 459 | @mock.patch('charms.reactive.set_flag') |
132 | 456 | @mock.patch('reactive.content_cache.update_logrotate') | 460 | @mock.patch('reactive.content_cache.update_logrotate') |
134 | 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): |
135 | 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: |
136 | 459 | ngx_config = f.read() | 463 | ngx_config = f.read() |
137 | 460 | self.mock_config.return_value = {'max_connections': 0, 'sites': ngx_config} | 464 | self.mock_config.return_value = {'max_connections': 0, 'sites': ngx_config} |
138 | 461 | 465 | ||
139 | 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: |
140 | 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') |
145 | 464 | with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"), mock.patch( | 468 | opened_ports.return_value = ['443/tcp'] |
146 | 465 | 'charmhelpers.core.hookenv.opened_ports', return_value=["443/tcp"] | 469 | content_cache.configure_haproxy() |
143 | 466 | ), mock.patch('charmhelpers.core.hookenv.open_port'), mock.patch('charmhelpers.core.hookenv.close_port'): | ||
144 | 467 | content_cache.configure_haproxy() | ||
147 | 468 | 470 | ||
148 | 469 | with open( | 471 | with open( |
149 | 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' |
150 | @@ -475,9 +477,10 @@ site1.local: | |||
151 | 475 | self.assertEqual(got, want) | 477 | self.assertEqual(got, want) |
152 | 476 | 478 | ||
153 | 477 | @freezegun.freeze_time("2019-03-22", tz_offset=0) | 479 | @freezegun.freeze_time("2019-03-22", tz_offset=0) |
154 | 480 | @mock.patch('charmhelpers.core.hookenv.opened_ports') | ||
155 | 478 | @mock.patch('charms.reactive.set_flag') | 481 | @mock.patch('charms.reactive.set_flag') |
156 | 479 | @mock.patch('reactive.content_cache.update_logrotate') | 482 | @mock.patch('reactive.content_cache.update_logrotate') |
158 | 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): |
159 | 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: |
160 | 482 | ngx_config = f.read() | 485 | ngx_config = f.read() |
161 | 483 | self.mock_config.return_value = { | 486 | self.mock_config.return_value = { |
162 | @@ -489,10 +492,8 @@ site1.local: | |||
163 | 489 | 492 | ||
164 | 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: |
165 | 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') |
170 | 492 | with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"), mock.patch( | 495 | opened_ports.return_value = ['443/tcp'] |
171 | 493 | 'charmhelpers.core.hookenv.opened_ports', return_value=["443/tcp"] | 496 | content_cache.configure_haproxy() |
168 | 494 | ), mock.patch('charmhelpers.core.hookenv.open_port'), mock.patch('charmhelpers.core.hookenv.close_port'): | ||
169 | 495 | content_cache.configure_haproxy() | ||
172 | 496 | 497 | ||
173 | 497 | with open( | 498 | with open( |
174 | 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', |
This merge proposal is being monitored by mergebot. Change the status to Approved to merge.