Skip to content

Commit

Permalink
test(test_core.py): Add tests for agent reuse behavior and platform-s…
Browse files Browse the repository at this point in the history
…pecific SSH agent startup

Signed-off-by: longhao <hal.long@outlook.com>
  • Loading branch information
loonghao committed Dec 28, 2024
1 parent 3d399b3 commit 222c845
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
44 changes: 39 additions & 5 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,43 +103,77 @@ def test_start_ssh_agent_reuse(ssh_manager, mocker):
mock_load_agent = mocker.patch.object(ssh_manager, "_load_agent_info")
mock_verify_key = mocker.patch.object(ssh_manager, "_verify_loaded_key")
mock_run_command = mocker.patch.object(ssh_manager, "run_command")
mock_add_key = mocker.patch.object(ssh_manager, "_add_ssh_key")
mock_logger = mocker.patch("persistent_ssh_agent.core.logger")

# Mock run_command to return a successful result with proper stdout
class MockResult:
returncode = 0
stdout = "SSH_AUTH_SOCK=/tmp/ssh-XXX/agent.123; export SSH_AUTH_SOCK;\nSSH_AGENT_PID=123; export SSH_AGENT_PID;"
mock_run_command.return_value = MockResult()

# Mock successful key operations
mock_add_key.return_value = True

# Test case 1: Successfully reuse existing agent
mock_load_agent.return_value = True
mock_verify_key.return_value = True
identity_file = "~/.ssh/id_rsa"

assert ssh_manager._start_ssh_agent(identity_file) is True
mock_logger.debug.assert_called_with("Using existing agent with loaded key: %s", identity_file)
mock_run_command.assert_not_called()

# Test case 2: Found agent but key not loaded
mock_verify_key.return_value = False
mock_logger.debug.reset_mock() # Reset mock to clear previous calls

assert ssh_manager._start_ssh_agent(identity_file) is True
mock_logger.debug.assert_called_with("Existing agent found but key not loaded")
mock_logger.debug.assert_has_calls([
mocker.call("Existing agent found but key not loaded"),
mocker.call("Saved agent info to: %s", ssh_manager._agent_info_file),
mocker.call("Adding key to agent: %s", identity_file)
], any_order=False)

# Test case 3: No valid agent found
mock_load_agent.return_value = False
mock_logger.debug.reset_mock()

assert ssh_manager._start_ssh_agent(identity_file) is True
mock_logger.debug.assert_called_with("No valid existing agent found")
mock_logger.debug.assert_has_calls([
mocker.call("No valid existing agent found"),
mocker.call("Saved agent info to: %s", ssh_manager._agent_info_file),
mocker.call("Adding key to agent: %s", identity_file)
], any_order=False)

# Test case 4: Agent reuse disabled
ssh_manager._reuse_agent = False
mock_logger.debug.reset_mock()

assert ssh_manager._start_ssh_agent(identity_file) is True
mock_logger.debug.assert_called_with("Agent reuse disabled, starting new agent")
mock_logger.debug.assert_has_calls([
mocker.call("Agent reuse disabled, starting new agent"),
mocker.call("Saved agent info to: %s", ssh_manager._agent_info_file),
mocker.call("Adding key to agent: %s", identity_file)
], any_order=False)


def test_start_ssh_agent_platform_specific(ssh_manager, mocker):
"""Test platform-specific SSH agent startup."""
mock_run_command = mocker.patch.object(ssh_manager, "run_command")
mock_os = mocker.patch("persistent_ssh_agent.core.os")
mock_verify_key = mocker.patch.object(ssh_manager, "_verify_loaded_key")
mock_add_key = mocker.patch.object(ssh_manager, "_add_ssh_key")

# Mock run_command to return a successful result
# Mock run_command to return a successful result with proper stdout
class MockResult:
returncode = 0
stdout = "SSH_AUTH_SOCK=/tmp/ssh-XXX/agent.123; export SSH_AUTH_SOCK;\nSSH_AGENT_PID=123; export SSH_AGENT_PID;"
mock_run_command.return_value = MockResult()

# Mock successful key operations
mock_verify_key.return_value = False # Key not loaded initially
mock_add_key.return_value = True # Key added successfully

# Test Windows (nt) platform
mock_os.name = "nt"
ssh_manager._reuse_agent = False # Disable reuse to test agent startup
Expand Down
16 changes: 13 additions & 3 deletions tests/test_core_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,8 @@ def test_error_handling_edge_cases(ssh_manager):
with patch.object(ssh_manager, "run_command") as mock_run, \
patch("builtins.open", mock_open()) as mock_file, \
patch("os.path.exists") as mock_exists, \
patch("json.load") as mock_load:
patch("json.load") as mock_load, \
patch("os.name", "nt"): # Mock Windows platform for consistent testing

# Test file read error with permission denied
mock_exists.return_value = True
Expand All @@ -773,14 +774,23 @@ def test_error_handling_edge_cases(ssh_manager):
"timestamp": time.time(),
"SSH_AUTH_SOCK": "test_sock",
"SSH_AGENT_PID": "test_pid",
"platform": "wrong_platform" # Use wrong platform to trigger error
"platform": "nt" # Match the mocked Windows platform
}
mock_run.return_value = subprocess.CompletedProcess(
args=[], returncode=0, stdout=b"", stderr=b""
args=[], returncode=2, stdout=b"", stderr=b"" # Return code 2 indicates agent not running
)
with patch.dict(os.environ, {}, clear=True):
assert not ssh_manager._load_agent_info()

# Test platform mismatch error
mock_load.return_value = {
"timestamp": time.time(),
"SSH_AUTH_SOCK": "test_sock",
"SSH_AGENT_PID": "test_pid",
"platform": "posix" # Mismatch with Windows platform
}
assert not ssh_manager._load_agent_info()


def test_ssh_config_advanced_parsing(ssh_manager):
"""Test advanced SSH config parsing scenarios."""
Expand Down

0 comments on commit 222c845

Please sign in to comment.