Merge lp:~hloeung/ubuntu-repository-cache/only-reload-apache2-when-needed into lp:ubuntu-repository-cache

Proposed by Haw Loeung
Status: Merged
Approved by: Haw Loeung
Approved revision: 338
Merged at revision: 316
Proposed branch: lp:~hloeung/ubuntu-repository-cache/only-reload-apache2-when-needed
Merge into: lp:ubuntu-repository-cache
Diff against target: 544 lines (+278/-87)
4 files modified
.bzrignore (+1/-0)
.coveragerc (+2/-0)
lib/ubuntu_repository_cache/apache.py (+111/-45)
tests/unit/test_apache.py (+164/-42)
To merge this branch: bzr merge lp:~hloeung/ubuntu-repository-cache/only-reload-apache2-when-needed
Reviewer Review Type Date Requested Status
Joel Sing (community) +1 Approve
Canonical IS Reviewers Pending
Review via email: mp+398325@code.launchpad.net

Commit message

Only reload/graceful apache2 when required - LP:1916084

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.

333. By Haw Loeung

Ignore .juju-persistent-config used with config.save()

Revision history for this message
Joel Sing (jsing) wrote :

Generally looks good, but see comments inline.

334. By Haw Loeung

Don't call apache.start() in service.unpause()

335. By Haw Loeung

Merge upstream changes

336. By Haw Loeung

Reworked configure_remoteip() and enable module on install

337. By Haw Loeung

Fixed based on review

Revision history for this message
Haw Loeung (hloeung) :
338. By Haw Loeung

Fixed to use one set of config keys to retrieve values and check config.changed() as per review

Revision history for this message
Haw Loeung (hloeung) wrote :

> Generally looks good, but see comments inline.

Okay, all addressed. Thanks for the review.

Revision history for this message
Haw Loeung (hloeung) :
Revision history for this message
Joel Sing (jsing) wrote :

LGTM

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

Change successfully merged at revision 316

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file '.bzrignore'
--- .bzrignore 2020-05-07 05:29:31 +0000
+++ .bzrignore 2021-02-22 22:51:46 +0000
@@ -1,6 +1,7 @@
1*.pyc1*.pyc
2*.swp2*.swp
3*~3*~
4.juju-persistent-config
4.unit-state.db5.unit-state.db
5lib/test.py6lib/test.py
6.venv7.venv
78
=== added file '.coveragerc'
--- .coveragerc 1970-01-01 00:00:00 +0000
+++ .coveragerc 2021-02-22 22:51:46 +0000
@@ -0,0 +1,2 @@
1[run]
2omit = lib/ubuntu_repository_cache/tests/*
03
=== modified file 'lib/ubuntu_repository_cache/apache.py'
--- lib/ubuntu_repository_cache/apache.py 2021-02-22 03:11:12 +0000
+++ lib/ubuntu_repository_cache/apache.py 2021-02-22 22:51:46 +0000
@@ -41,6 +41,8 @@
41 config = hookenv.config()41 config = hookenv.config()
42 if config['enable_healthcheck']:42 if config['enable_healthcheck']:
43 subprocess.check_call(['a2enmod', 'cgid'])43 subprocess.check_call(['a2enmod', 'cgid'])
44 if config['remoteip_logging']:
45 subprocess.check_call(['a2enmod', 'remoteip'])
4446
45 start()47 start()
4648
@@ -123,20 +125,37 @@
123 return config125 return config
124126
125127
126def create_mpm_workerfile():128def create_mpm_workerfile(config=None):
127 '''Create the multi-processing module (MPM) configuration'''129 '''Create the multi-processing module (MPM) configuration'''
128130
129 config = hookenv.config()131 reload_required = False
130 mpm_context = {132
131 'startservers': config['apache2_mpm_startservers'],133 if not config:
132 'minsparethreads': config['apache2_mpm_minsparethreads'],134 config = hookenv.config()
133 'maxsparethreads': config['apache2_mpm_maxsparethreads'],135
134 'threadlimit': config['apache2_mpm_threadlimit'],136 mpm_configs = [
135 'threadsperchild': config['apache2_mpm_threadsperchild'],137 'maxconnectionsperchild',
136 'serverlimit': config['apache2_mpm_serverlimit'],138 'maxrequestworkers',
137 'maxrequestworkers': config['apache2_mpm_maxrequestworkers'],139 'maxsparethreads',
138 'maxconnectionsperchild': config['apache2_mpm_maxconnectionsperchild'],140 'minsparethreads',
139 }141 'serverlimit',
142 'startservers',
143 'threadlimit',
144 'threadsperchild',
145 ]
146 mpm_context = {}
147 for k in mpm_configs:
148 cfkey = 'apache2_mpm_{}'.format(k)
149 mpm_context[k] = config[cfkey]
150 if config.changed(cfkey):
151 reload_required = True
152
153 # We'll only proceed when any one of the MPM configs have changed.
154 if not reload_required:
155 return reload_required
156
157 reload_required = True
158
140 mpm_context = tune_mpm_configs(mpm_context)159 mpm_context = tune_mpm_configs(mpm_context)
141 conf_mpm_worker = '/etc/apache2/conf-available/000mpm-worker.conf'160 conf_mpm_worker = '/etc/apache2/conf-available/000mpm-worker.conf'
142 templating.render('apache2/mpm_worker.template', conf_mpm_worker, mpm_context)161 templating.render('apache2/mpm_worker.template', conf_mpm_worker, mpm_context)
@@ -148,24 +167,44 @@
148 os.unlink(mods_mpm_worker)167 os.unlink(mods_mpm_worker)
149 os.symlink(conf_mpm_worker, mods_mpm_worker)168 os.symlink(conf_mpm_worker, mods_mpm_worker)
150169
151170 return reload_required
152def create_security():171
172
173def create_security(config=None):
153 '''Create the security configuration'''174 '''Create the security configuration'''
154175
155 config = hookenv.config()176 reload_required = False
156 security_context = {177
157 'server_tokens': config['apache2_server_tokens'],178 if not config:
158 'server_signature': config['apache2_server_signature'],179 config = hookenv.config()
159 'trace_enabled': config['apache2_trace_enabled'],180
160 }181 security_configs = ['server_signature', 'server_tokens', 'trace_enabled']
182 security_context = {}
183 for k in security_configs:
184 cfkey = 'apache2_{}'.format(k)
185 security_context[k] = config[cfkey]
186 if config.changed(cfkey):
187 reload_required = True
188
189 # We'll only proceed when any one of the MPM configs have changed.
190 if not reload_required:
191 return reload_required
192
193 reload_required = True
194
161 templating.render('apache2/security.template', '/etc/apache2/conf-available/security.conf', security_context)195 templating.render('apache2/security.template', '/etc/apache2/conf-available/security.conf', security_context)
162 subprocess.check_call(['a2enconf', 'security'])196 subprocess.check_call(['a2enconf', 'security'])
163197 return reload_required
164198
165def create_metadata_site():199
200def create_metadata_site(config=None, apache2_conf_path='/etc/apache2'):
166 '''Create the site config for serving (or proxying) repository metadata'''201 '''Create the site config for serving (or proxying) repository metadata'''
167202
168 config = hookenv.config()203 reload_required = False
204
205 if not config:
206 config = hookenv.config()
207
169 apache_context = {}208 apache_context = {}
170 apache_context['DocumentRoot'] = unitdata.kv().get('apache-root')209 apache_context['DocumentRoot'] = unitdata.kv().get('apache-root')
171 apache_context['Sync_Host'] = config['sync-host']210 apache_context['Sync_Host'] = config['sync-host']
@@ -188,38 +227,62 @@
188 apache_context['MetadataProxy'] = metaproxy_conf227 apache_context['MetadataProxy'] = metaproxy_conf
189 apache_context['Enable_Healthcheck'] = config['enable_healthcheck']228 apache_context['Enable_Healthcheck'] = config['enable_healthcheck']
190229
191 templating.render(230 apache_config_file = os.path.join(apache2_conf_path, 'sites-available', 'archive_ubuntu_com.conf')
192 'apache2/archive_ubuntu_com.conf', '/etc/apache2/sites-available/archive_ubuntu_com.conf', apache_context231 cur_conf = ''
193 )232 if os.path.exists(apache_config_file):
194 subprocess.call(['a2ensite', 'archive_ubuntu_com'])233 with open(apache_config_file) as f:
195 start()234 cur_conf = f.read()
235 new_conf = templating.render('apache2/archive_ubuntu_com.conf', apache_config_file, apache_context)
236
237 if new_conf != cur_conf:
238 reload_required = True
239 if not os.path.exists(os.path.join(apache2_conf_path, 'sites-enabled', 'archive_ubuntu_com.conf')):
240 subprocess.call(['a2ensite', 'archive_ubuntu_com'])
241
242 return reload_required
196243
197244
198@util.run_as_user('root')245@util.run_as_user('root')
199def configure_remoteip_logging():246def configure_remoteip_logging(config=None, apache2_conf_path='/etc/apache2'):
200 config = hookenv.config()247 reload_required = False
248
249 if not config:
250 config = hookenv.config()
251
252 if config['remoteip_logging']:
253 apache_config_file = os.path.join(apache2_conf_path, 'conf-available', 'remoteip.conf')
254 cur_conf = ''
255 if os.path.exists(apache_config_file):
256 with open(apache_config_file) as f:
257 cur_conf = f.read()
258 new_conf = templating.render('apache2/remoteip.conf', apache_config_file, {})
259
260 if new_conf != cur_conf:
261 reload_required = True
262 if not os.path.exists(os.path.join(apache2_conf_path, 'conf-enabled', 'remoteip.conf')):
263 subprocess.check_call(['a2enconf', 'remoteip'])
264
201 if config.changed('remoteip_logging'):265 if config.changed('remoteip_logging'):
202 LOG('Apache remoteip_logging changed')266 LOG('Apache remoteip_logging changed')
203 if config['remoteip_logging'] is True:267 if config['remoteip_logging']:
204 subprocess.check_call(['a2enmod', 'remoteip'])268 subprocess.check_call(['a2enmod', 'remoteip'])
205 templating.render('apache2/remoteip.conf', '/etc/apache2/conf-available/remoteip.conf', {})
206 subprocess.check_call(['a2enconf', 'remoteip'])
207 LOG('Enabled remoteip logging')269 LOG('Enabled remoteip logging')
208 else:270 else:
209 subprocess.check_call(['a2dismod', 'remoteip'])271 subprocess.check_call(['a2dismod', 'remoteip'])
210 if os.path.exists('/etc/apache2/conf-enabled/remoteip.conf'):272 if os.path.exists('/etc/apache2/conf-enabled/remoteip.conf'):
211 subprocess.check_call(['a2disconf', 'remoteip'])273 subprocess.check_call(['a2disconf', 'remoteip'])
212 LOG('Disabled remoteip logging')274 LOG('Disabled remoteip logging')
275 reload_required = True
213276
214 start()277 return reload_required
215278
216279
217def configure_health_check(config=None):280def configure_health_check(config=None):
281 reload_required = False
282
218 if not config:283 if not config:
219 config = hookenv.config()284 config = hookenv.config()
220285
221 restart_required = False
222
223 if config.changed('enable_healthcheck'):286 if config.changed('enable_healthcheck'):
224 LOG('Apache enable_healthcheck changed')287 LOG('Apache enable_healthcheck changed')
225 if config['enable_healthcheck']:288 if config['enable_healthcheck']:
@@ -231,7 +294,7 @@
231294
232 subprocess.check_call([cmd, 'cgid'])295 subprocess.check_call([cmd, 'cgid'])
233 LOG('{} health check endpoint'.format(action))296 LOG('{} health check endpoint'.format(action))
234 restart_required = True297 reload_required = True
235298
236 subprocess.call(['touch', HEALTH_CHECK_LOG])299 subprocess.call(['touch', HEALTH_CHECK_LOG])
237 subprocess.call(['chown', HEALTH_CHECK_LOG_OWNER, HEALTH_CHECK_LOG])300 subprocess.call(['chown', HEALTH_CHECK_LOG_OWNER, HEALTH_CHECK_LOG])
@@ -244,16 +307,19 @@
244 content = f.read()307 content = f.read()
245 host.write_file(path=healthcheck_script, content=content, perms=0o755)308 host.write_file(path=healthcheck_script, content=content, perms=0o755)
246309
247 return restart_required310 return reload_required
248311
249312
250@util.run_as_user('root')313@util.run_as_user('root')
251def render_configs():314def render_configs():
252 '''Render the configuration templates for apache'''315 '''Render the configuration templates for apache'''
253316
317 reload_required = False
318
254 LOG('Rendering apache2 configuration templates')319 LOG('Rendering apache2 configuration templates')
255 config = hookenv.config()320 config = hookenv.config()
256 if config.changed('apache2_mpm_type'):321 if config.changed('apache2_mpm_type'):
322 reload_required = True
257 if config['apache2_mpm_type'] == 'worker':323 if config['apache2_mpm_type'] == 'worker':
258 LOG('Using apache2-mpm-worker')324 LOG('Using apache2-mpm-worker')
259 subprocess.check_call(['a2dismod', 'mpm_prefork'])325 subprocess.check_call(['a2dismod', 'mpm_prefork'])
@@ -264,13 +330,13 @@
264 subprocess.check_call(['a2dismod', 'mpm_worker'])330 subprocess.check_call(['a2dismod', 'mpm_worker'])
265 subprocess.check_call(['a2enmod', 'mpm_prefork'])331 subprocess.check_call(['a2enmod', 'mpm_prefork'])
266332
267 create_metadata_site()333 reload_required = create_metadata_site() or reload_required
268 create_mpm_workerfile()334 reload_required = create_mpm_workerfile() or reload_required
269 create_security()335 reload_required = create_security() or reload_required
270 configure_remoteip_logging()336 reload_required = configure_remoteip_logging() or reload_required
271 configure_health_check()337 reload_required = configure_health_check() or reload_required
272338
273 start()339 start(start_only=(not reload_required))
274340
275341
276def update_checks(nrpe_config):342def update_checks(nrpe_config):
277343
=== modified file 'tests/unit/test_apache.py'
--- tests/unit/test_apache.py 2021-02-22 02:59:25 +0000
+++ tests/unit/test_apache.py 2021-02-22 22:51:46 +0000
@@ -43,11 +43,6 @@
43 self.addCleanup(patcher.stop)43 self.addCleanup(patcher.stop)
44 self.mock_local_unit.return_value = 'mock-ubuntu-repository-cache/0'44 self.mock_local_unit.return_value = 'mock-ubuntu-repository-cache/0'
4545
46 patcher = mock.patch('charmhelpers.core.hookenv.config')
47 self.mock_config = patcher.start()
48 self.addCleanup(patcher.stop)
49 self.mock_config.return_value = {'nagios_context': 'juju'}
50
51 patcher = mock.patch('charmhelpers.core.host.log')46 patcher = mock.patch('charmhelpers.core.host.log')
52 self.mock_log = patcher.start()47 self.mock_log = patcher.start()
53 self.addCleanup(patcher.stop)48 self.addCleanup(patcher.stop)
@@ -65,46 +60,53 @@
65 @mock.patch('subprocess.check_call')60 @mock.patch('subprocess.check_call')
66 def test_configure_health_check(self, check_call, call, seteuid, setegid, chown, write_file):61 def test_configure_health_check(self, check_call, call, seteuid, setegid, chown, write_file):
67 config = hookenv.Config({'enable_healthcheck': True})62 config = hookenv.Config({'enable_healthcheck': True})
68 self.assertEqual(apache.configure_health_check(config), True)63 self.assertTrue(apache.configure_health_check(config))
69 check_call.assert_called_with(['a2enmod', 'cgid'])64 check_call.assert_called_with(['a2enmod', 'cgid'])
7065
71 check_call.reset_mock()66 check_call.reset_mock()
72 config = hookenv.Config({'enable_healthcheck': False})67 config = hookenv.Config({'enable_healthcheck': False})
73 self.assertEqual(apache.configure_health_check(config), True)68 self.assertTrue(apache.configure_health_check(config))
74 check_call.assert_called_with(['a2dismod', 'cgid'])69 check_call.assert_called_with(['a2dismod', 'cgid'])
7570
71 # No change
76 check_call.reset_mock()72 check_call.reset_mock()
77 config = hookenv.Config({'enable_healthcheck': True})73 config = hookenv.Config({'enable_healthcheck': True})
78 config.save()74 config.save()
79 config = hookenv.Config({'enable_healthcheck': True})75 config = hookenv.Config({'enable_healthcheck': True})
80 self.assertEqual(apache.configure_health_check(config), False)76 self.assertFalse(apache.configure_health_check(config))
8177
82 @mock.patch('charmhelpers.core.templating.render')
83 @mock.patch('lib.ubuntu_repository_cache.apache.start')78 @mock.patch('lib.ubuntu_repository_cache.apache.start')
84 @mock.patch('lib.ubuntu_repository_cache.util.get_failover_host')79 @mock.patch('lib.ubuntu_repository_cache.util.get_failover_host')
80 @mock.patch('os.chown')
81 @mock.patch('os.fchown')
85 @mock.patch('subprocess.call')82 @mock.patch('subprocess.call')
86 def test_create_metadata_site(self, call, get_failover_host, start, render):83 def test_create_metadata_site(self, call, fchown, chown, get_failover_host, start):
87 get_failover_host.return_value = '10.1.1.1'84 get_failover_host.return_value = '10.1.1.1'
88 self.mock_config.return_value = {85 config = hookenv.Config(
89 'display-host': 'archive.ubuntu.com',
90 'enable_healthcheck': True,
91 'path-base': 'ubuntu',
92 'sync-host': 'us.archive.ubuntu.com',
93 }
94 apache.create_metadata_site()
95 render.assert_called_with(
96 'apache2/archive_ubuntu_com.conf',
97 '/etc/apache2/sites-available/archive_ubuntu_com.conf',
98 {86 {
99 'DocumentRoot': os.path.join(self.tmpdir, 'apache'),87 'display-host': 'archive.ubuntu.com',
100 'Sync_Host': 'us.archive.ubuntu.com',88 'enable_healthcheck': True,
101 'Display_Host': 'archive.ubuntu.com',89 'path-base': 'ubuntu',
102 'Path_Base': 'ubuntu',90 'sync-host': 'us.archive.ubuntu.com',
103 'MetadataProxy': '# No failover configured',91 }
104 'Enable_Healthcheck': True,
105 },
106 )92 )
93 self.assertTrue(apache.create_metadata_site(config, apache2_conf_path=self.tmpdir))
107 call.assert_called_with(['a2ensite', 'archive_ubuntu_com'])94 call.assert_called_with(['a2ensite', 'archive_ubuntu_com'])
95 chown.assert_called()
96
97 # No change, no need to reload
98 call.reset_mock()
99 config.save()
100 config = hookenv.Config(
101 {
102 'display-host': 'archive.ubuntu.com',
103 'enable_healthcheck': True,
104 'path-base': 'ubuntu',
105 'sync-host': 'us.archive.ubuntu.com',
106 }
107 )
108 self.assertFalse(apache.create_metadata_site(config, apache2_conf_path=self.tmpdir))
109 call.assert_not_called()
108110
109 @mock.patch('charmhelpers.core.templating.render')111 @mock.patch('charmhelpers.core.templating.render')
110 @mock.patch('lib.ubuntu_repository_cache.apache.start')112 @mock.patch('lib.ubuntu_repository_cache.apache.start')
@@ -113,12 +115,14 @@
113 @mock.patch('os.mknod')115 @mock.patch('os.mknod')
114 @mock.patch('subprocess.call')116 @mock.patch('subprocess.call')
115 def test_create_metadata_site_in_failover(self, call, mknod, in_failover, get_failover_host, start, render):117 def test_create_metadata_site_in_failover(self, call, mknod, in_failover, get_failover_host, start, render):
116 self.mock_config.return_value = {118 config = hookenv.Config(
117 'display-host': 'archive.ubuntu.com',119 {
118 'enable_healthcheck': True,120 'display-host': 'archive.ubuntu.com',
119 'path-base': 'ubuntu',121 'enable_healthcheck': True,
120 'sync-host': 'us.archive.ubuntu.com',122 'path-base': 'ubuntu',
121 }123 'sync-host': 'us.archive.ubuntu.com',
124 }
125 )
122 get_failover_host.return_value = '10.1.1.1'126 get_failover_host.return_value = '10.1.1.1'
123 in_failover.return_value = True127 in_failover.return_value = True
124128
@@ -127,7 +131,7 @@
127 os.mkdir(os.path.join(self.tmpdir, 'apache'))131 os.mkdir(os.path.join(self.tmpdir, 'apache'))
128 os.mkdir(health_check_path)132 os.mkdir(health_check_path)
129133
130 apache.create_metadata_site()134 apache.create_metadata_site(config)
131 mknod.assert_called_with(disabled_flag)135 mknod.assert_called_with(disabled_flag)
132 render.assert_called_with(136 render.assert_called_with(
133 'apache2/archive_ubuntu_com.conf',137 'apache2/archive_ubuntu_com.conf',
@@ -148,7 +152,7 @@
148 # If the disabled flag exists, no need to create it.152 # If the disabled flag exists, no need to create it.
149 mknod.reset_mock()153 mknod.reset_mock()
150 open(disabled_flag, 'a').close()154 open(disabled_flag, 'a').close()
151 apache.create_metadata_site()155 apache.create_metadata_site(config)
152 mknod.assert_not_called()156 mknod.assert_not_called()
153157
154 @mock.patch('charmhelpers.core.templating.render')158 @mock.patch('charmhelpers.core.templating.render')
@@ -157,19 +161,21 @@
157 @mock.patch('os.unlink')161 @mock.patch('os.unlink')
158 @mock.patch('subprocess.call')162 @mock.patch('subprocess.call')
159 def test_create_metadata_site_no_failover(self, call, unlink, get_failover_host, start, render):163 def test_create_metadata_site_no_failover(self, call, unlink, get_failover_host, start, render):
160 self.mock_config.return_value = {164 config = hookenv.Config(
161 'display-host': 'archive.ubuntu.com',165 {
162 'enable_healthcheck': True,166 'display-host': 'archive.ubuntu.com',
163 'path-base': 'ubuntu',167 'enable_healthcheck': True,
164 'sync-host': 'us.archive.ubuntu.com',168 'path-base': 'ubuntu',
165 }169 'sync-host': 'us.archive.ubuntu.com',
170 }
171 )
166172
167 health_check_path = os.path.join(self.tmpdir, 'apache', 'health-check')173 health_check_path = os.path.join(self.tmpdir, 'apache', 'health-check')
168 disabled_flag = os.path.join(health_check_path, 'health-check-disabled.flag')174 disabled_flag = os.path.join(health_check_path, 'health-check-disabled.flag')
169 os.mkdir(os.path.join(self.tmpdir, 'apache'))175 os.mkdir(os.path.join(self.tmpdir, 'apache'))
170 os.mkdir(health_check_path)176 os.mkdir(health_check_path)
171 os.mknod(disabled_flag)177 os.mknod(disabled_flag)
172 apache.create_metadata_site()178 apache.create_metadata_site(config)
173 unlink.assert_called_with(disabled_flag)179 unlink.assert_called_with(disabled_flag)
174180
175 @mock.patch('charmhelpers.core.host.service_start')181 @mock.patch('charmhelpers.core.host.service_start')
@@ -204,3 +210,119 @@
204 apache.start(start_only=True)210 apache.start(start_only=True)
205 service_reload.assert_not_called()211 service_reload.assert_not_called()
206 service_start.assert_called_with('apache2')212 service_start.assert_called_with('apache2')
213
214 @mock.patch('charmhelpers.core.templating.render')
215 @mock.patch('os.symlink')
216 @mock.patch('subprocess.check_call')
217 def test_create_mpm_workerfile(self, check_call, symlink, render):
218 config = hookenv.Config(
219 {
220 'apache2_mpm_maxconnectionsperchild': 10000,
221 'apache2_mpm_maxrequestworkers': 0,
222 'apache2_mpm_maxsparethreads': 0,
223 'apache2_mpm_minsparethreads': 0,
224 'apache2_mpm_serverlimit': 0,
225 'apache2_mpm_startservers': 0,
226 'apache2_mpm_threadlimit': 0,
227 'apache2_mpm_threadsperchild': 0,
228 }
229 )
230 self.assertTrue(apache.create_mpm_workerfile(config))
231 render.assert_called_with(
232 'apache2/mpm_worker.template',
233 '/etc/apache2/conf-available/000mpm-worker.conf',
234 {
235 'maxconnectionsperchild': 10000,
236 'maxrequestworkers': 512,
237 'maxsparethreads': 512,
238 'minsparethreads': 256,
239 'serverlimit': 1,
240 'startservers': 1,
241 'threadlimit': 512,
242 'threadsperchild': 512,
243 },
244 )
245 check_call.assert_called_with(['a2enconf', '000mpm-worker'])
246 symlink.assert_called_with(
247 '/etc/apache2/conf-available/000mpm-worker.conf', '/etc/apache2/mods-available/mpm_worker.conf'
248 )
249
250 # No change, no need to reload
251 render.reset_mock()
252 check_call.reset_mock()
253 symlink.reset_mock()
254 config.save()
255 config = hookenv.Config(
256 {
257 'apache2_mpm_maxconnectionsperchild': 10000,
258 'apache2_mpm_maxrequestworkers': 0,
259 'apache2_mpm_maxsparethreads': 0,
260 'apache2_mpm_minsparethreads': 0,
261 'apache2_mpm_serverlimit': 0,
262 'apache2_mpm_startservers': 0,
263 'apache2_mpm_threadlimit': 0,
264 'apache2_mpm_threadsperchild': 0,
265 }
266 )
267 self.assertFalse(apache.create_mpm_workerfile(config))
268
269 @mock.patch('charmhelpers.core.templating.render')
270 @mock.patch('subprocess.check_call')
271 def test_create_security(self, check_call, render):
272 config = hookenv.Config(
273 {
274 'apache2_server_tokens': 'OS',
275 'apache2_server_signature': 'On',
276 'apache2_trace_enabled': 'Off',
277 }
278 )
279 self.assertTrue(apache.create_security(config))
280 render.assert_called_with(
281 'apache2/security.template',
282 '/etc/apache2/conf-available/security.conf',
283 {
284 'server_tokens': 'OS',
285 'server_signature': 'On',
286 'trace_enabled': 'Off',
287 },
288 )
289 check_call.assert_called_with(['a2enconf', 'security'])
290
291 # No change, no need to reload
292 render.reset_mock()
293 check_call.reset_mock()
294 config.save()
295 config = hookenv.Config(
296 {
297 'apache2_server_tokens': 'OS',
298 'apache2_server_signature': 'On',
299 'apache2_trace_enabled': 'Off',
300 }
301 )
302 self.assertFalse(apache.create_security(config))
303
304 @mock.patch('os.chown')
305 @mock.patch('os.fchown')
306 # For @util.run_as_user()
307 @mock.patch('os.setegid')
308 @mock.patch('os.seteuid')
309 @mock.patch('subprocess.check_call')
310 def test_configure_remoteip_logging(self, check_call, seteuid, setegid, fchown, chown):
311 config = hookenv.Config({'remoteip_logging': True})
312 self.assertTrue(apache.configure_remoteip_logging(config, apache2_conf_path=self.tmpdir))
313 check_call.assert_called_with(['a2enmod', 'remoteip'])
314 with open(os.path.join(self.tmpdir, 'conf-available', 'remoteip.conf'), 'r') as f:
315 got = f.read()
316 with open('templates/apache2/remoteip.conf', 'r') as f:
317 want = f.read().strip()
318 self.assertEqual(want, got)
319
320 # No change
321 config.save()
322 config = hookenv.Config({'remoteip_logging': True})
323 self.assertFalse(apache.configure_remoteip_logging(config, apache2_conf_path=self.tmpdir))
324
325 check_call.reset_mock()
326 config = hookenv.Config({'remoteip_logging': False})
327 self.assertTrue(apache.configure_remoteip_logging(config, apache2_conf_path=self.tmpdir))
328 check_call.assert_called_with(['a2dismod', 'remoteip'])

Subscribers

People subscribed via source and target branches