import sys
import logging
from unittest import mock
import paramiko
import pytest
from tools.multisystem_performance.src.load_check import connect_test, host_test_body, communication_test_body
sys.path.append("..")
logging.info('开始: test_load_check.py')
class FakeSSHClient:
"""
创建一个虚拟的 paramiko.SSHClient 类
"""
def set_missing_host_key_policy(self, policy):
pass
def connect(self, hostname, port, username, password):
pass
def close(self):
pass
@pytest.fixture
def fake_ssh_client():
return FakeSSHClient()
def test_connect_test_successful(fake_ssh_client):
"""
测试 connect_test 函数的成功情况
fake_ssh_client: 虚拟的 paramiko.SSHClient 类
"""
logging.info(f'开始:test_connect_test_successful')
pc1 = {'ip': '192.168.1.1', 'user': 'user1'}
pc2 = {'ip': '192.168.1.2', 'user': 'user2'}
with mock.patch('getpass.getpass', return_value='password'):
with mock.patch('paramiko.SSHClient', return_value=fake_ssh_client):
with mock.patch.object(fake_ssh_client, 'connect'):
result = connect_test(pc1, pc2)
logging.info(f'result = {result}')
assert result == 1
def test_connect_test_failure(fake_ssh_client):
"""
测试 connect_test 函数的认证失败情况
fake_ssh_client: 虚拟的 paramiko.SSHClient 类
"""
logging.info(f'开始:test_connect_test_failure')
pc1 = {'ip': '192.168.1.1', 'user': 'user1'}
pc2 = {'ip': '192.168.1.2', 'user': 'user2'}
with mock.patch('getpass.getpass', return_value='wrong_password'):
with mock.patch('paramiko.SSHClient', return_value=fake_ssh_client):
with mock.patch.object(fake_ssh_client, 'connect', side_effect=paramiko.AuthenticationException):
result = connect_test(pc1, pc2)
logging.info(f'result = {result}')
assert result == 1
@pytest.fixture
def mock_input(monkeypatch):
"""
使用 monkeypatch 来模拟 input 函数
"""
input_values = []
def mock_input_generator(prompt):
if input_values:
return input_values.pop(0)
else:
raise ValueError("Not enough input values provided")
monkeypatch.setattr('builtins.input', mock_input_generator)
def test_host_test_body_missing_config():
"""
针对 host_test 读取配置文件时缺少配置的情况进行测试
"""
data = {}
with pytest.raises(KeyError, match="host_test"):
host_test_body(data)
data = {"host_test": {"pc1": {}, "pc2": {}}}
with pytest.raises(KeyError, match="local"):
host_test_body(data)
data = {"host_test": {"local": {"user": "testuser"}}}
with pytest.raises(KeyError, match="ip"):
host_test_body(data)
data = {"host_test": {"local": {"ip": "127.0.0.1", "user": "testuser"}}}
with pytest.raises(KeyError, match="pc1"):
host_test_body(data)
def test_communication_test_body_missing_config():
"""
针对 communication_test 读取配置文件时缺少配置的情况进行测试
"""
data = {}
with pytest.raises(KeyError, match='communication_test'):
communication_test_body(data)
data = {"communication_test": {"remote": {"ip": "", "user": "remoteuser"}}}
with pytest.raises(KeyError, match='local'):
communication_test_body(data)
data = {"communication_test": {"local": {"user": "localuser"}, "remote": {"ip": "", "user": "remoteuser"}}}
with pytest.raises(KeyError, match='ip'):
communication_test_body(data)
data = {"communication_test": {"local": {"ip": "192.168.1.1", "user": "localuser"}, }}
with pytest.raises(KeyError, match='remote'):
communication_test_body(data)