2026-07-14 16:30:36 +08:00
|
|
|
import sys
|
|
|
|
|
import tempfile
|
|
|
|
|
import unittest
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
|
|
from deploy_tool.service_console import main
|
2026-10-09 09:12:14 +08:00
|
|
|
import deploy_tool.service_console as service_console_module
|
2026-07-14 16:30:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceConsoleTests(unittest.TestCase):
|
|
|
|
|
def test_tees_service_output_to_log_and_removes_pid_file(self):
|
|
|
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
|
|
|
root = Path(temp)
|
|
|
|
|
log_path = root / "service.log"
|
|
|
|
|
pid_path = root / "service.pid"
|
|
|
|
|
argv = [
|
|
|
|
|
"service_console.py",
|
|
|
|
|
"--log",
|
|
|
|
|
str(log_path),
|
|
|
|
|
"--pid-file",
|
|
|
|
|
str(pid_path),
|
|
|
|
|
"--",
|
|
|
|
|
sys.executable,
|
|
|
|
|
"-c",
|
|
|
|
|
"print('service-ready')",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
with patch.object(sys, "argv", argv):
|
|
|
|
|
exit_code = main()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(exit_code, 0)
|
|
|
|
|
self.assertIn("service-ready", log_path.read_text(encoding="utf-8"))
|
|
|
|
|
self.assertFalse(pid_path.exists())
|
|
|
|
|
|
2026-10-09 09:12:14 +08:00
|
|
|
def test_invalid_console_output_does_not_stop_service_or_log_capture(self):
|
|
|
|
|
class InvalidConsole:
|
|
|
|
|
def write(self, _value):
|
|
|
|
|
raise OSError(22, "Invalid argument")
|
|
|
|
|
|
|
|
|
|
def flush(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
|
|
|
root = Path(temp)
|
|
|
|
|
log_path = root / "service.log"
|
|
|
|
|
pid_path = root / "service.pid"
|
|
|
|
|
argv = [
|
|
|
|
|
"service_console.py",
|
|
|
|
|
"--log",
|
|
|
|
|
str(log_path),
|
|
|
|
|
"--pid-file",
|
|
|
|
|
str(pid_path),
|
|
|
|
|
"--",
|
|
|
|
|
sys.executable,
|
|
|
|
|
"-c",
|
|
|
|
|
"print('java-service-ready')",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
with (
|
|
|
|
|
patch.object(sys, "argv", argv),
|
|
|
|
|
patch.object(service_console_module, "_open_console_output", return_value=(InvalidConsole(), False)),
|
|
|
|
|
):
|
|
|
|
|
exit_code = main()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(exit_code, 0)
|
|
|
|
|
self.assertIn("java-service-ready", log_path.read_text(encoding="utf-8"))
|
|
|
|
|
self.assertFalse(pid_path.exists())
|
|
|
|
|
|
2026-10-10 16:35:29 +08:00
|
|
|
def test_missing_service_executable_is_logged_without_unhandled_exception(self):
|
|
|
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
|
|
|
root = Path(temp)
|
|
|
|
|
log_path = root / "service.log"
|
|
|
|
|
pid_path = root / "service.pid"
|
|
|
|
|
missing_executable = root / "missing-java.exe"
|
|
|
|
|
argv = [
|
|
|
|
|
"service_console.py",
|
|
|
|
|
"--log",
|
|
|
|
|
str(log_path),
|
|
|
|
|
"--pid-file",
|
|
|
|
|
str(pid_path),
|
|
|
|
|
"--",
|
|
|
|
|
str(missing_executable),
|
|
|
|
|
"-version",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
with patch.object(sys, "argv", argv):
|
|
|
|
|
exit_code = main()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(exit_code, 1)
|
|
|
|
|
self.assertIn("无法启动服务命令", log_path.read_text(encoding="utf-8"))
|
|
|
|
|
self.assertFalse(pid_path.exists())
|
|
|
|
|
|
2026-07-14 16:30:36 +08:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|