Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip service resume zed - squash #891

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 6 additions & 2 deletions charmhelpers/fetch/ubuntu.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,10 +945,14 @@ def _run_with_retries(cmd, max_retries=CMD_RETRY_COUNT, retry_exitcodes=(1,),
try:
result = subprocess.check_call(cmd, env=env, **kwargs)
except subprocess.CalledProcessError as e:
retry_count = retry_count + 1
result = e.returncode
if result not in retry_results:
# a non-retriable exitcode was produced
raise
retry_count += 1
if retry_count > max_retries:
# a retriable exitcode was produced more than {max_retries} times
raise
result = e.returncode
log(retry_message)
time.sleep(CMD_RETRY_DELAY)

Expand Down
10 changes: 6 additions & 4 deletions tests/contrib/openstack/test_deferred_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,13 @@ def test_get_service_start_time(self, check_output):
def test_check_restart_timestamps(self, get_service_start_time, log,
clear_deferred_restarts,
get_deferred_restarts):
request_time = '2021-02-02 10:19:55'
timestamp = datetime.datetime.strptime(
'Tue ' + request_time + ' UTC',
'%a %Y-%m-%d %H:%M:%S %Z').timestamp()
deferred_restarts = [
# 'Tue 2021-02-02 10:19:55 UTC'
deferred_events.ServiceEvent(
timestamp=1612261195.0,
timestamp=timestamp,
service='svcA',
reason='ReasonA',
action='restart')]
Expand All @@ -326,8 +329,7 @@ def test_check_restart_timestamps(self, get_service_start_time, log,
self.assertFalse(clear_deferred_restarts.called)
log.assert_called_once_with(
('Restart still required, svcA was started at 2021-02-02 10:10:55,'
' restart was requested after that at {}'.format(
datetime.datetime.fromtimestamp(1612261195.0))),
' restart was requested after that at {}'.format(request_time)),
level='DEBUG')

def test_set_deferred_hook(self):
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
21 changes: 15 additions & 6 deletions tests/core/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,47 +46,56 @@ def test_register_preserves_order(self):
@mock.patch.object(services.ServiceManager, 'reconfigure_services')
@mock.patch.object(services.ServiceManager, 'stop_services')
@mock.patch.object(hookenv, 'hook_name')
@mock.patch.object(hookenv, 'config')
def test_manage_stop(self, config, hook_name, stop_services, reconfigure_services):
@mock.patch.object(hookenv, '_run_atstart')
@mock.patch.object(hookenv, 'config', mock.Mock())
def test_manage_stop(self, _run_atstart, hook_name, stop_services, reconfigure_services):
manager = services.ServiceManager()
hook_name.return_value = 'stop'
manager.manage()
_run_atstart.assert_called_once_with()
stop_services.assert_called_once_with()
assert not reconfigure_services.called

@mock.patch.object(services.ServiceManager, 'provide_data')
@mock.patch.object(services.ServiceManager, 'reconfigure_services')
@mock.patch.object(services.ServiceManager, 'stop_services')
@mock.patch.object(hookenv, 'hook_name')
@mock.patch.object(hookenv, 'config')
def test_manage_other(self, config, hook_name, stop_services, reconfigure_services, provide_data):
@mock.patch.object(hookenv, '_run_atstart')
@mock.patch.object(hookenv, 'config', mock.Mock())
def test_manage_other(self, _run_atstart, hook_name, stop_services, reconfigure_services, provide_data):
manager = services.ServiceManager()
hook_name.return_value = 'config-changed'
manager.manage()
_run_atstart.assert_called_once_with()
assert not stop_services.called
reconfigure_services.assert_called_once_with()
provide_data.assert_called_once_with()

@mock.patch.object(hookenv, '_atstart', [])
def test_manage_calls_atstart(self):
cb = mock.MagicMock()
hookenv.atstart(cb)
manager = services.ServiceManager()
manager.manage()
self.assertTrue(cb.called)

def test_manage_calls_atexit(self):
@mock.patch.object(hookenv, '_run_atstart')
def test_manage_calls_atexit(self, _run_atstart):
cb = mock.MagicMock()
hookenv.atexit(cb)
manager = services.ServiceManager()
manager.manage()
_run_atstart.assert_called_once_with()
self.assertTrue(cb.called)

@mock.patch.object(hookenv, 'config')
def test_manage_config_not_saved(self, config):
@mock.patch.object(hookenv, '_run_atstart')
def test_manage_config_not_saved(self, _run_atstart, config):
config = config.return_value
config.implicit_save = False
manager = services.ServiceManager()
manager.manage()
_run_atstart.assert_called_once_with()
self.assertFalse(config.save.called)

@mock.patch.object(services.ServiceManager, 'save_ready')
Expand Down
Loading