diff --git a/cloudinit/config/cc_set_passwords.py b/cloudinit/config/cc_set_passwords.py index d8f6a1db315..3c8b378bf91 100644 --- a/cloudinit/config/cc_set_passwords.py +++ b/cloudinit/config/cc_set_passwords.py @@ -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'." diff --git a/tests/unittests/config/test_cc_set_passwords.py b/tests/unittests/config/test_cc_set_passwords.py index 758241f7ca4..ac7abadb46e 100644 --- a/tests/unittests/config/test_cc_set_passwords.py +++ b/tests/unittests/config/test_cc_set_passwords.py @@ -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", ], ( @@ -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, ), ( @@ -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, ), ( @@ -163,7 +166,7 @@ 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, ), ( @@ -171,30 +174,42 @@ def test_valid_value_changes_updates_ssh(self, m_subp, mock_uses_systemd): 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, ), ), ) @@ -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, ): @@ -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")