2024-11-20 11:09:28 -06:00
|
|
|
# -*- 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
|
2025-08-31 12:52:36 -05:00
|
|
|
mod.cache_host_key(c, "example.com")
|
|
|
|
c.run.assert_called_once_with("ssh example.com whoami", warn=True)
|
2024-11-20 11:09:28 -06:00
|
|
|
|
|
|
|
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"
|
2025-08-31 12:52:36 -05:00
|
|
|
mod.cache_host_key(c, "example.com")
|
|
|
|
c.run.assert_called_once_with("ssh example.com whoami", warn=True)
|
2024-11-20 11:09:28 -06:00
|
|
|
|
|
|
|
def test_root_cache_key(self):
|
|
|
|
c = MagicMock()
|
|
|
|
|
|
|
|
# first command fails; second command caches host key
|
|
|
|
c.run.return_value.failed = True
|
2025-08-31 12:52:36 -05:00
|
|
|
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
|
|
|
|
)
|
2024-11-20 11:09:28 -06:00
|
|
|
|
|
|
|
def test_user_already_cached(self):
|
|
|
|
c = MagicMock()
|
|
|
|
|
|
|
|
# assume the first command runs okay
|
|
|
|
c.sudo.return_value.failed = False
|
2025-08-31 12:52:36 -05:00
|
|
|
mod.cache_host_key(c, "example.com", user="foo")
|
|
|
|
c.sudo.assert_called_once_with("ssh example.com whoami", user="foo", warn=True)
|
2024-11-20 11:09:28 -06:00
|
|
|
|
|
|
|
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"
|
2025-08-31 12:52:36 -05:00
|
|
|
mod.cache_host_key(c, "example.com", user="foo")
|
|
|
|
c.sudo.assert_called_once_with("ssh example.com whoami", user="foo", warn=True)
|
2024-11-20 11:09:28 -06:00
|
|
|
|
|
|
|
def test_user_cache_key(self):
|
|
|
|
c = MagicMock()
|
|
|
|
|
|
|
|
# first command fails; second command caches host key
|
|
|
|
c.sudo.return_value.failed = True
|
2025-08-31 12:52:36 -05:00
|
|
|
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
|
|
|
|
)
|