Пример #1
0
 def test_unchanged_does_nothing(self, m_subp, m_update_ssh_config):
     """If 'unchanged', then no updates to config and no restart."""
     setpass.handle_ssh_pwauth("unchanged",
                               service_cmd=["systemctl"],
                               service_name="myssh")
     m_update_ssh_config.assert_not_called()
     m_subp.assert_not_called()
Пример #2
0
 def test_service_as_service_cmd(self, m_subp, m_update_ssh_config):
     """If systemctl in service cmd: systemctl restart name."""
     setpass.handle_ssh_pwauth(True,
                               service_cmd=["service"],
                               service_name="myssh")
     self.assertEqual(mock.call(["service", "myssh", "restart"]),
                      m_subp.call_args)
Пример #3
0
    def test_no_restart_when_service_is_not_running(
        self,
        m_subp,
        m_update_ssh_config,
        uses_systemd,
        raised_error,
        warning_log,
        debug_logs,
        update_ssh_call_count,
        caplog,
    ):
        """Write config but don't restart SSH service when not running."""
        cloud = get_cloud("ubuntu")
        cloud.distro.manage_service = mock.Mock(side_effect=raised_error)
        cloud.distro.uses_systemd = mock.Mock(return_value=uses_systemd)

        setpass.handle_ssh_pwauth(True, cloud.distro)
        logs_by_level = {logging.WARNING: [], logging.DEBUG: []}
        for _, level, msg in caplog.record_tuples:
            logs_by_level[level].append(msg)
        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")
        ] == cloud.distro.manage_service.call_args_list
        assert m_update_ssh_config.call_count == update_ssh_call_count
        assert m_subp.call_count == 0
        assert cloud.distro.uses_systemd.call_count == 1
Пример #4
0
 def test_systemctl_as_service_cmd(self, m_subp, m_update_ssh_config):
     """If systemctl in service cmd: systemctl restart name."""
     cloud = self.tmp_cloud(distro="ubuntu")
     cloud.distro.init_cmd = ["systemctl"]
     setpass.handle_ssh_pwauth(True, cloud.distro)
     m_subp.assert_called_with(["systemctl", "restart", "ssh"],
                               capture=True)
Пример #5
0
 def test_unknown_value_logs_warning(
     self, m_subp, uses_systemd, cmd, caplog
 ):
     cloud = get_cloud("ubuntu")
     with mock.patch.object(
         cloud.distro, "uses_systemd", return_value=uses_systemd
     ):
         setpass.handle_ssh_pwauth("floo", cloud.distro)
     assert "Unrecognized value: ssh_pwauth=floo" in caplog.text
     assert [mock.call(cmd, capture=True)] == m_subp.call_args_list
Пример #6
0
 def test_valid_change_values(self, m_subp):
     """If value is a valid changen value, then update should be called."""
     upname = MODPATH + "update_ssh_config"
     optname = "PasswordAuthentication"
     for value in util.FALSE_STRINGS + util.TRUE_STRINGS:
         optval = "yes" if value in util.TRUE_STRINGS else "no"
         with mock.patch(upname, return_value=False) as m_update:
             setpass.handle_ssh_pwauth(value)
             m_update.assert_called_with({optname: optval})
     m_subp.assert_not_called()
Пример #7
0
 def test_unchanged_value_does_nothing(
     self, m_subp, update_ssh_config, mock_uses_systemd
 ):
     """If 'unchanged', then no updates to config and no restart."""
     update_ssh_config.assert_not_called()
     cloud = get_cloud("ubuntu")
     setpass.handle_ssh_pwauth("unchanged", cloud.distro)
     assert [
         mock.call(["systemctl", "status", "ssh"], capture=True)
     ] == m_subp.call_args_list
Пример #8
0
 def test_valid_change_values(self, m_subp):
     """If value is a valid changen value, then update should be called."""
     upname = MODPATH + "update_ssh_config"
     optname = "PasswordAuthentication"
     for value in util.FALSE_STRINGS + util.TRUE_STRINGS:
         optval = "yes" if value in util.TRUE_STRINGS else "no"
         with mock.patch(upname, return_value=False) as m_update:
             setpass.handle_ssh_pwauth(value)
             m_update.assert_called_with({optname: optval})
     m_subp.assert_not_called()
Пример #9
0
 def test_valid_value_changes_updates_ssh(self, m_subp, mock_uses_systemd):
     """If value is a valid changed value, then update will be called."""
     cloud = get_cloud("ubuntu")
     upname = MODPATH + "update_ssh_config"
     optname = "PasswordAuthentication"
     for n, value in enumerate(util.FALSE_STRINGS + util.TRUE_STRINGS, 1):
         optval = "yes" if value in util.TRUE_STRINGS else "no"
         with mock.patch(upname, return_value=False) as m_update:
             setpass.handle_ssh_pwauth(value, cloud.distro)
             assert (
                 mock.call({optname: optval}) == m_update.call_args_list[-1]
             )
             assert m_subp.call_count == n
Пример #10
0
 def test_restart_ssh_only_when_changes_made_and_ssh_installed(
     self,
     m_subp,
     update_ssh_config,
     uses_systemd,
     ssh_updated,
     cmd,
     expected_log,
     caplog,
 ):
     update_ssh_config.return_value = ssh_updated
     cloud = get_cloud("ubuntu")
     with mock.patch.object(
         cloud.distro, "uses_systemd", return_value=uses_systemd
     ):
         setpass.handle_ssh_pwauth(True, cloud.distro)
     if ssh_updated:
         m_subp.assert_called_with(cmd, capture=True)
     else:
         assert [mock.call(cmd, capture=True)] == m_subp.call_args_list
     assert expected_log in "\n".join(
         r.msg for r in caplog.records if r.levelname == "DEBUG"
     )
Пример #11
0
 def test_unknown_value_logs_warning(self, m_subp):
     setpass.handle_ssh_pwauth("floo")
     self.assertIn("Unrecognized value: ssh_pwauth=floo",
                   self.logs.getvalue())
     m_subp.assert_not_called()
Пример #12
0
 def test_not_restarted_if_not_updated(self, m_subp, m_update_ssh_config):
     """If config is not updated, then no system restart should be done."""
     setpass.handle_ssh_pwauth(True)
     m_subp.assert_not_called()
     self.assertIn("No need to restart ssh", self.logs.getvalue())
Пример #13
0
 def test_unknown_value_logs_warning(self, m_subp):
     setpass.handle_ssh_pwauth("floo")
     self.assertIn("Unrecognized value: ssh_pwauth=floo",
                   self.logs.getvalue())
     m_subp.assert_not_called()
Пример #14
0
 def test_unchanged_does_nothing(self, m_subp, m_update_ssh_config):
     """If 'unchanged', then no updates to config and no restart."""
     setpass.handle_ssh_pwauth(
         "unchanged", service_cmd=["systemctl"], service_name="myssh")
     m_update_ssh_config.assert_not_called()
     m_subp.assert_not_called()
Пример #15
0
 def test_not_restarted_if_not_updated(self, m_subp, m_update_ssh_config):
     """If config is not updated, then no system restart should be done."""
     setpass.handle_ssh_pwauth(True)
     m_subp.assert_not_called()
     self.assertIn("No need to restart ssh", self.logs.getvalue())
Пример #16
0
 def test_not_restarted_if_not_updated(self, m_subp, m_update_ssh_config):
     """If config is not updated, then no system restart should be done."""
     cloud = self.tmp_cloud(distro="ubuntu")
     setpass.handle_ssh_pwauth(True, cloud.distro)
     m_subp.assert_not_called()
     self.assertIn("No need to restart SSH", self.logs.getvalue())
Пример #17
0
 def test_unknown_value_logs_warning(self, m_subp):
     cloud = self.tmp_cloud(distro="ubuntu")
     setpass.handle_ssh_pwauth("floo", cloud.distro)
     self.assertIn("Unrecognized value: ssh_pwauth=floo",
                   self.logs.getvalue())
     m_subp.assert_not_called()
Пример #18
0
 def test_unchanged_does_nothing(self, m_subp, m_update_ssh_config):
     """If 'unchanged', then no updates to config and no restart."""
     cloud = self.tmp_cloud(distro="ubuntu")
     setpass.handle_ssh_pwauth("unchanged", cloud.distro)
     m_update_ssh_config.assert_not_called()
     m_subp.assert_not_called()
Пример #19
0
 def test_service_as_service_cmd(self, m_subp, m_update_ssh_config):
     """If systemctl in service cmd: systemctl restart name."""
     setpass.handle_ssh_pwauth(
         True, service_cmd=["service"], service_name="myssh")
     self.assertEqual(mock.call(["service", "myssh", "restart"]),
                      m_subp.call_args)