wuttamess/tests/test_ssh.py
Lance Edgar 147d2fd871 fix: format all code with black
and from now on should not deviate from that...
2025-08-31 12:52:36 -05:00

65 lines
2.3 KiB
Python

# -*- coding: utf-8; -*-
from unittest import TestCase
from unittest.mock import MagicMock, call
from wuttamess import ssh as mod
class TestCacheHostKey(TestCase):
def test_root_already_cached(self):
c = MagicMock()
# assume the first command runs okay
c.run.return_value.failed = False
mod.cache_host_key(c, "example.com")
c.run.assert_called_once_with("ssh example.com whoami", warn=True)
def test_root_commands_not_allowed(self):
c = MagicMock()
# assume the first command fails b/c "disallowed"
c.run.return_value.failed = True
c.run.return_value.stderr = "Disallowed command"
mod.cache_host_key(c, "example.com")
c.run.assert_called_once_with("ssh example.com whoami", warn=True)
def test_root_cache_key(self):
c = MagicMock()
# first command fails; second command caches host key
c.run.return_value.failed = True
mod.cache_host_key(c, "example.com")
c.run.assert_has_calls([call("ssh example.com whoami", warn=True)])
c.run.assert_called_with(
"ssh -o StrictHostKeyChecking=no example.com whoami", warn=True
)
def test_user_already_cached(self):
c = MagicMock()
# assume the first command runs okay
c.sudo.return_value.failed = False
mod.cache_host_key(c, "example.com", user="foo")
c.sudo.assert_called_once_with("ssh example.com whoami", user="foo", warn=True)
def test_user_commands_not_allowed(self):
c = MagicMock()
# assume the first command fails b/c "disallowed"
c.sudo.return_value.failed = True
c.sudo.return_value.stderr = "Disallowed command"
mod.cache_host_key(c, "example.com", user="foo")
c.sudo.assert_called_once_with("ssh example.com whoami", user="foo", warn=True)
def test_user_cache_key(self):
c = MagicMock()
# first command fails; second command caches host key
c.sudo.return_value.failed = True
mod.cache_host_key(c, "example.com", user="foo")
c.sudo.assert_has_calls([call("ssh example.com whoami", user="foo", warn=True)])
c.sudo.assert_called_with(
"ssh -o StrictHostKeyChecking=no example.com whoami", user="foo", warn=True
)