Skip to content
Merged
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
15 changes: 12 additions & 3 deletions cloudinit/config/cc_set_passwords.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,25 @@ def handle_ssh_pwauth(pw_auth, distro: Distro):
distro.manage_service("status", service)
except subp.ProcessExecutionError as e:
uses_systemd = distro.uses_systemd()
if uses_systemd and e.exit_code == 3:
if not uses_systemd:
LOG.debug(
"Writing config 'ssh_pwauth: %s'. SSH service '%s'"
" will not be restarted because it is not running or not"
" available.",
pw_auth,
service,
)
restart_ssh = False
elif e.exit_code == 3:
# Service is not running. Write ssh config.
LOG.warning(
LOG.debug(
"Writing config 'ssh_pwauth: %s'. SSH service '%s'"
" will not be restarted because it is stopped.",
pw_auth,
service,
)
restart_ssh = False
elif uses_systemd and e.exit_code == 4:
elif e.exit_code == 4:
# Service status is unknown
LOG.warning(
"Ignoring config 'ssh_pwauth: %s'."
Expand Down
54 changes: 36 additions & 18 deletions tests/unittests/config/test_cc_set_passwords.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_valid_value_changes_updates_ssh(self, m_subp, mock_uses_systemd):
"uses_systemd",
"raised_error",
"warning_log",
"debug_log",
"debug_logs",
"update_ssh_call_count",
],
(
Expand All @@ -141,9 +141,12 @@ def test_valid_value_changes_updates_ssh(self, m_subp, mock_uses_systemd):
subp.ProcessExecutionError(
stderr="Service is not running.", exit_code=3
),
"Writing config 'ssh_pwauth: True'. SSH service"
" 'ssh' will not be restarted because it is stopped.",
"Not restarting SSH service: service is stopped.",
None,
[
"Writing config 'ssh_pwauth: True'. SSH service"
" 'ssh' will not be restarted because it is stopped.",
"Not restarting SSH service: service is stopped.",
],
1,
),
(
Expand All @@ -153,7 +156,7 @@ def test_valid_value_changes_updates_ssh(self, m_subp, mock_uses_systemd):
),
"Ignoring config 'ssh_pwauth: True'. SSH service 'ssh' is"
" not installed.",
None,
[],
0,
),
(
Expand All @@ -163,38 +166,50 @@ def test_valid_value_changes_updates_ssh(self, m_subp, mock_uses_systemd):
),
"Ignoring config 'ssh_pwauth: True'. SSH service 'ssh'"
" is not available. Error: ",
None,
[],
0,
),
(
False,
subp.ProcessExecutionError(
stderr="Service is not available.", exit_code=25
),
"Ignoring config 'ssh_pwauth: True'. SSH service 'ssh'"
" is not available. Error: ",
None,
0,
[
"Writing config 'ssh_pwauth: True'. SSH service"
" 'ssh' will not be restarted because it is not running"
" or not available.",
"Not restarting SSH service: service is stopped.",
],
1,
),
(
False,
subp.ProcessExecutionError(
stderr="Service is not available.", exit_code=3
),
"Ignoring config 'ssh_pwauth: True'. SSH service 'ssh'"
" is not available. Error: ",
None,
0,
[
"Writing config 'ssh_pwauth: True'. SSH service"
" 'ssh' will not be restarted because it is not running"
" or not available.",
"Not restarting SSH service: service is stopped.",
],
1,
),
(
False,
subp.ProcessExecutionError(
stderr="Service is not available.", exit_code=4
),
"Ignoring config 'ssh_pwauth: True'. SSH service 'ssh'"
" is not available. Error: ",
None,
0,
[
"Writing config 'ssh_pwauth: True'. SSH service"
" 'ssh' will not be restarted because it is not running"
" or not available.",
"Not restarting SSH service: service is stopped.",
],
1,
),
),
)
Expand All @@ -207,7 +222,7 @@ def test_no_restart_when_service_is_not_running(
uses_systemd,
raised_error,
warning_log,
debug_log,
debug_logs,
update_ssh_call_count,
caplog,
):
Expand All @@ -220,8 +235,11 @@ def test_no_restart_when_service_is_not_running(
logs_by_level = {logging.WARNING: [], logging.DEBUG: []}
for _, level, msg in caplog.record_tuples:
logs_by_level[level].append(msg)
assert warning_log in "\n".join(logs_by_level[logging.WARNING])
if debug_log:
if warning_log:
assert warning_log in "\n".join(
logs_by_level[logging.WARNING]
), logs_by_level
for debug_log in debug_logs:
assert debug_log in logs_by_level[logging.DEBUG]
assert [
mock.call("status", "ssh")
Expand Down