[Fix] Update test scripts, all test can pass before this commit
This commit is contained in:
@ -23,6 +23,8 @@ handler.setFormatter(colorlog.ColoredFormatter(
|
|||||||
logger = colorlog.getLogger(__name__)
|
logger = colorlog.getLogger(__name__)
|
||||||
logger.setLevel(logging.INFO)
|
logger.setLevel(logging.INFO)
|
||||||
logger.addHandler(handler)
|
logger.addHandler(handler)
|
||||||
|
|
||||||
|
|
||||||
class ServiceFactory:
|
class ServiceFactory:
|
||||||
"""Factory for creating Service instances based on tag.
|
"""Factory for creating Service instances based on tag.
|
||||||
|
|
||||||
@ -183,6 +185,7 @@ class Manager:
|
|||||||
"""
|
"""
|
||||||
services = self.list_services()
|
services = self.list_services()
|
||||||
if index < 0 or index >= len(services):
|
if index < 0 or index >= len(services):
|
||||||
|
logger.error(f"Invalid service index: {index}")
|
||||||
raise IndexError(f"Invalid service index: {index}")
|
raise IndexError(f"Invalid service index: {index}")
|
||||||
|
|
||||||
service = services[index]
|
service = services[index]
|
||||||
|
@ -165,20 +165,21 @@ class Service:
|
|||||||
"path": self.path
|
"path": self.path
|
||||||
}
|
}
|
||||||
|
|
||||||
def service_operation(self, operation_getter: Callable[[], Optional[int]] = get_operation) -> None:
|
def service_operation(self) -> None:
|
||||||
"""Perform service operation based on user input.
|
"""Perform service operation based on user input.
|
||||||
|
|
||||||
Args:
|
|
||||||
operation_getter: Function to get operation choice
|
|
||||||
"""
|
"""
|
||||||
operation = operation_getter()
|
operation = get_operation()
|
||||||
if operation is None:
|
if operation is None:
|
||||||
logger.info("Operation cancelled by user")
|
logger.info("Operation cancelled by user")
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Generate and execute command
|
# Generate and execute command
|
||||||
command = self.strategy.generate_command(operation, self.name, self.path)
|
if self.path is not None:
|
||||||
|
command = self.strategy.generate_command(operation, self.name, self.path)
|
||||||
|
else:
|
||||||
|
command = self.strategy.generate_command(operation, self.name)
|
||||||
logger.info(f"Executing: {' '.join(command)}")
|
logger.info(f"Executing: {' '.join(command)}")
|
||||||
self.strategy.execute(command)
|
self.strategy.execute(command)
|
||||||
logger.info(f"Service {self.name} operation completed")
|
logger.info(f"Service {self.name} operation completed")
|
||||||
|
@ -141,7 +141,8 @@ class TestManager(unittest.TestCase):
|
|||||||
with self.assertRaises(IndexError):
|
with self.assertRaises(IndexError):
|
||||||
manager.execute_service_operation(0)
|
manager.execute_service_operation(0)
|
||||||
|
|
||||||
mock_logger.error.assert_called()
|
# 验证错误日志记录
|
||||||
|
mock_logger.error.assert_called_with("Invalid service index: 0")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -142,5 +142,21 @@ class TestServiceOperation(unittest.TestCase):
|
|||||||
mock_info.assert_any_call("Executing: sudo systemctl stop nginx")
|
mock_info.assert_any_call("Executing: sudo systemctl stop nginx")
|
||||||
mock_info.assert_any_call("Service nginx operation completed")
|
mock_info.assert_any_call("Service nginx operation completed")
|
||||||
|
|
||||||
|
@patch("src.services.DockerServiceStrategy.execute")
|
||||||
|
@patch("src.services.DockerServiceStrategy.generate_command", return_value=["docker", "compose", "down"])
|
||||||
|
@patch("src.services.get_operation", return_value=0)
|
||||||
|
@patch("src.services.logger.info")
|
||||||
|
def test_docker_service_operation(self, mock_info, mock_get_operation, mock_generate, mock_execute):
|
||||||
|
"""测试Docker服务操作流程"""
|
||||||
|
service = Service(tag="docker", name="homepage", path="/path/to/docker")
|
||||||
|
service.service_operation()
|
||||||
|
|
||||||
|
# 验证调用
|
||||||
|
mock_get_operation.assert_called_once()
|
||||||
|
mock_generate.assert_called_with(0, "homepage", "/path/to/docker")
|
||||||
|
mock_execute.assert_called_with(["docker", "compose", "down"])
|
||||||
|
mock_info.assert_any_call("Executing: docker compose down")
|
||||||
|
mock_info.assert_any_call("Service homepage operation completed")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
Reference in New Issue
Block a user