Skip to content

Commit

Permalink
Merge pull request #882 from dosaboy/bug/2058505
Browse files Browse the repository at this point in the history
Skip service_resume if already enabled
  • Loading branch information
ajkavanagh authored Mar 28, 2024
2 parents 511bc03 + a334500 commit 58ab965
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
7 changes: 5 additions & 2 deletions charmhelpers/core/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,11 @@ def service_resume(service_name, init_dir="/etc/init",
upstart_file = os.path.join(init_dir, "{}.conf".format(service_name))
sysv_file = os.path.join(initd_dir, service_name)
if init_is_systemd(service_name=service_name):
service('unmask', service_name)
service('enable', service_name)
if service('is-enabled', service_name):
log('service {} already enabled'.format(service_name), level=DEBUG)
else:
service('unmask', service_name)
service('enable', service_name)
elif os.path.exists(upstart_file):
override_path = os.path.join(
init_dir, '{}.override'.format(service_name))
Expand Down
27 changes: 27 additions & 0 deletions tests/core/test_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,18 @@ def test_resumes_a_stopped_systemd_unit(self, service, systemd,
service_name = 'foo-service'
service_running.return_value = False
systemd.return_value = True

def fake_service(action, name):
if action == 'is-enabled':
return False

if action == 'start':
return True

service.side_effect = fake_service
self.assertTrue(host.service_resume(service_name))
service.assert_has_calls([
call('is-enabled', service_name),
call('unmask', service_name),
# Ensures a package starts up if disabled but not masked,
# per lp:1692178
Expand Down Expand Up @@ -455,6 +465,23 @@ def test_resume_a_running_sysv_service(self, service, check_call,
self.assertFalse(service.called)
check_call.assert_called_with(["update-rc.d", service_name, "enable"])

@patch.object(host, 'service_running')
@patch.object(host, 'init_is_systemd')
@patch.object(host, 'service')
def test_resume_an_enabled_systemd_service(self, service, systemd,
service_running):
service_name = 'foo-service'
systemd.return_value = True
service_running.return_value = True

def fake_service(action, name):
if action == 'is-enabled':
return True

service.side_effect = fake_service
self.assertTrue(host.service_resume(service_name))
service.assert_has_calls([call('is-enabled', service_name)])

@patch.object(host, 'service_running')
@patch.object(host, 'init_is_systemd')
@patch.object(host, 'service')
Expand Down

0 comments on commit 58ab965

Please sign in to comment.