273 lines
12 KiB
Python
273 lines
12 KiB
Python
|
|
"""Execute recovery helpers against temporary files and mocked native commands.
|
||
|
|
|
||
|
|
No MySQL installation or Windows service is used by these tests.
|
||
|
|
"""
|
||
|
|
|
||
|
|
import json
|
||
|
|
import shutil
|
||
|
|
import subprocess
|
||
|
|
import tempfile
|
||
|
|
import unittest
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
from deploy_tool.installer import build_mysql_service_start_script
|
||
|
|
from deploy_tool.mysql_recovery import MYSQL_RECOVERY_HELPERS
|
||
|
|
|
||
|
|
|
||
|
|
POWERSHELL = shutil.which("powershell.exe")
|
||
|
|
|
||
|
|
|
||
|
|
def ps_literal(value):
|
||
|
|
return "'" + str(value).replace("'", "''") + "'"
|
||
|
|
|
||
|
|
|
||
|
|
@unittest.skipUnless(POWERSHELL, "Windows PowerShell is required")
|
||
|
|
class MySqlRecoveryTests(unittest.TestCase):
|
||
|
|
def setUp(self):
|
||
|
|
self.temporary = tempfile.TemporaryDirectory(prefix="autodeploy-recovery-test-")
|
||
|
|
self.addCleanup(self.temporary.cleanup)
|
||
|
|
self.root = Path(self.temporary.name)
|
||
|
|
self.data = self.root / "实际数据目录"
|
||
|
|
self.data.mkdir()
|
||
|
|
self.ini = self.root / "自定义配置.ini"
|
||
|
|
self.server = self.root / "Server Files" / "bin" / "mysqld.exe"
|
||
|
|
self.config = (
|
||
|
|
"# Preserve this comment\r\n"
|
||
|
|
"[client]\r\n"
|
||
|
|
"log-error=client-only.err\r\n"
|
||
|
|
"port=3307\r\n"
|
||
|
|
"[mysqld]\r\n"
|
||
|
|
f'datadir="{self.data.as_posix()}"\r\n'
|
||
|
|
'log_bin="马航宇-bin"\r\n'
|
||
|
|
'log-bin-index="乱码?-bin.index"\r\n'
|
||
|
|
'log-error="马航宇.err"\r\n'
|
||
|
|
"max_connections=151\r\n"
|
||
|
|
)
|
||
|
|
self.ini.write_bytes(self.config.encode("utf-8"))
|
||
|
|
|
||
|
|
def run_harness(self, body, *, state="Stopped", process_id=0, exit_code=0,
|
||
|
|
create_dictionary=True):
|
||
|
|
result_path = self.root / "result.json"
|
||
|
|
script_path = self.root / "exercise-recovery.ps1"
|
||
|
|
command_line = f'"{self.server}" --defaults-file="{self.ini}" MySQL80'
|
||
|
|
mock = r'''
|
||
|
|
$script:commandCalls = @()
|
||
|
|
function Invoke-MySqlRecoveryCommand([string]$Executable, [string[]]$Arguments) {
|
||
|
|
$script:commandCalls += @{ Executable = $Executable; Arguments = @($Arguments) }
|
||
|
|
if ($script:createDictionary -and $script:mockExitCode -eq 0) {
|
||
|
|
[IO.File]::WriteAllText((Join-Path $script:testData 'mysql.ibd'), 'test dictionary')
|
||
|
|
}
|
||
|
|
return $script:mockExitCode
|
||
|
|
}
|
||
|
|
'''
|
||
|
|
script = (
|
||
|
|
"$ErrorActionPreference = 'Stop'\n"
|
||
|
|
+ MYSQL_RECOVERY_HELPERS
|
||
|
|
+ f"\n$script:testData = {ps_literal(self.data)}\n"
|
||
|
|
+ f"$script:mockExitCode = {exit_code}\n"
|
||
|
|
+ "$script:createDictionary = "
|
||
|
|
+ ("$true" if create_dictionary else "$false")
|
||
|
|
+ "\n"
|
||
|
|
+ mock
|
||
|
|
+ "$service = [pscustomobject]@{ Name = 'MySQL80'; "
|
||
|
|
+ f"PathName = {ps_literal(command_line)}; "
|
||
|
|
+ f"State = {ps_literal(state)}; ProcessId = {process_id} }}\n"
|
||
|
|
+ "$layout = Get-MySqlServiceLayout $service\n"
|
||
|
|
+ body
|
||
|
|
+ f"\n$json = $result | ConvertTo-Json -Depth 10 -Compress\n"
|
||
|
|
+ f"[IO.File]::WriteAllText({ps_literal(result_path)}, $json, "
|
||
|
|
+ "(New-Object Text.UTF8Encoding($false)))\n"
|
||
|
|
)
|
||
|
|
script_path.write_text(script, encoding="utf-8-sig")
|
||
|
|
completed = subprocess.run(
|
||
|
|
[POWERSHELL, "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass",
|
||
|
|
"-File", str(script_path)],
|
||
|
|
capture_output=True,
|
||
|
|
timeout=20,
|
||
|
|
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
|
||
|
|
)
|
||
|
|
self.assertEqual(
|
||
|
|
completed.returncode, 0,
|
||
|
|
(completed.stdout + completed.stderr).decode("utf-8", errors="replace"),
|
||
|
|
)
|
||
|
|
self.assertTrue(result_path.exists(), "PowerShell harness produced no result")
|
||
|
|
return json.loads(result_path.read_text(encoding="utf-8-sig"))
|
||
|
|
|
||
|
|
def test_resolves_actual_service_executable_config_and_data_paths(self):
|
||
|
|
result = self.run_harness("$result = $layout")
|
||
|
|
self.assertEqual(Path(result["Server"]), self.server)
|
||
|
|
self.assertEqual(Path(result["Ini"]), self.ini)
|
||
|
|
self.assertEqual(Path(result["Data"]), self.data)
|
||
|
|
|
||
|
|
def test_empty_data_recovery_backs_up_and_normalizes_chinese_log_names(self):
|
||
|
|
original = self.ini.read_bytes()
|
||
|
|
result = self.run_harness("""
|
||
|
|
$initialized = Initialize-EmptyMySqlData $service $layout
|
||
|
|
$result = @{ Initialized = $initialized; Calls = @($script:commandCalls) }
|
||
|
|
""")
|
||
|
|
self.assertTrue(result["Initialized"])
|
||
|
|
backups = list(self.root.glob(self.ini.name + ".autodeploy-*.bak"))
|
||
|
|
self.assertEqual(len(backups), 1)
|
||
|
|
self.assertEqual(backups[0].read_bytes(), original)
|
||
|
|
updated = self.ini.read_text(encoding="utf-8")
|
||
|
|
self.assertIn("log-bin=mysql-bin", updated)
|
||
|
|
self.assertIn("log-bin-index=mysql-bin.index", updated)
|
||
|
|
self.assertIn("log-error=mysql-error.err", updated)
|
||
|
|
self.assertNotIn("马航宇-bin", updated)
|
||
|
|
self.assertNotIn("乱码?-bin.index", updated)
|
||
|
|
self.assertIn("# Preserve this comment", updated)
|
||
|
|
self.assertIn("log-error=client-only.err", updated)
|
||
|
|
self.assertIn("max_connections=151", updated)
|
||
|
|
self.assertIn(self.data.as_posix(), updated)
|
||
|
|
self.assertIn(b"\r\n", self.ini.read_bytes())
|
||
|
|
self.assertEqual(len(result["Calls"]), 1)
|
||
|
|
call = result["Calls"][0]
|
||
|
|
self.assertEqual(Path(call["Executable"]), self.server)
|
||
|
|
self.assertEqual(call["Arguments"][0], "--defaults-file=" + str(self.ini))
|
||
|
|
self.assertIn("--initialize-insecure", call["Arguments"])
|
||
|
|
self.assertIn("--skip-log-bin", call["Arguments"])
|
||
|
|
|
||
|
|
def test_recovery_preserves_utf8_bom_and_utf16_configuration(self):
|
||
|
|
for encoding, preamble in (("utf-8-sig", b"\xef\xbb\xbf"),
|
||
|
|
("utf-16", b"\xff\xfe")):
|
||
|
|
with self.subTest(encoding=encoding):
|
||
|
|
self.ini.write_bytes(self.config.encode(encoding))
|
||
|
|
result = self.run_harness("""
|
||
|
|
$result = @{ Initialized = (Initialize-EmptyMySqlData $service $layout) }
|
||
|
|
""")
|
||
|
|
self.assertTrue(result["Initialized"])
|
||
|
|
content = self.ini.read_bytes()
|
||
|
|
self.assertTrue(content.startswith(preamble))
|
||
|
|
self.assertIn(self.data.as_posix(), content.decode(encoding))
|
||
|
|
(self.data / "mysql.ibd").unlink()
|
||
|
|
|
||
|
|
def test_existing_dictionary_or_user_data_blocks_all_initialization_changes(self):
|
||
|
|
for relative in ("mysql.ibd", "ibdata1", "business/orders.ibd", "mysql-bin.index"):
|
||
|
|
with self.subTest(existing=relative):
|
||
|
|
existing = self.data / relative
|
||
|
|
existing.parent.mkdir(exist_ok=True)
|
||
|
|
existing.write_bytes(b"existing data must be preserved")
|
||
|
|
original = self.ini.read_bytes()
|
||
|
|
result = self.run_harness("""
|
||
|
|
$initialized = Initialize-EmptyMySqlData $service $layout
|
||
|
|
$result = @{ Initialized = $initialized; Calls = @($script:commandCalls) }
|
||
|
|
""")
|
||
|
|
self.assertFalse(result["Initialized"])
|
||
|
|
self.assertEqual(result["Calls"], [])
|
||
|
|
self.assertEqual(self.ini.read_bytes(), original)
|
||
|
|
self.assertEqual(existing.read_bytes(), b"existing data must be preserved")
|
||
|
|
self.assertEqual(list(self.root.glob(self.ini.name + ".autodeploy-*.bak")), [])
|
||
|
|
existing.unlink()
|
||
|
|
if existing.parent != self.data:
|
||
|
|
existing.parent.rmdir()
|
||
|
|
|
||
|
|
def test_pending_service_blocks_configuration_changes_and_initialization(self):
|
||
|
|
original = self.ini.read_bytes()
|
||
|
|
result = self.run_harness("""
|
||
|
|
$failure = ''
|
||
|
|
try { Initialize-EmptyMySqlData $service $layout | Out-Null }
|
||
|
|
catch { $failure = $_.Exception.Message }
|
||
|
|
$result = @{ Failure = $failure; Calls = @($script:commandCalls) }
|
||
|
|
""", state="Start Pending", process_id=1234)
|
||
|
|
self.assertIn("fully stopped", result["Failure"])
|
||
|
|
self.assertEqual(result["Calls"], [])
|
||
|
|
self.assertEqual(self.ini.read_bytes(), original)
|
||
|
|
self.assertEqual(list(self.data.iterdir()), [])
|
||
|
|
self.assertEqual(list(self.root.glob(self.ini.name + ".autodeploy-*.bak")), [])
|
||
|
|
|
||
|
|
def test_nonzero_initialize_exit_is_surfaced(self):
|
||
|
|
result = self.run_harness("""
|
||
|
|
$failure = ''
|
||
|
|
try { Initialize-EmptyMySqlData $service $layout | Out-Null }
|
||
|
|
catch { $failure = $_.Exception.Message }
|
||
|
|
$result = @{ Failure = $failure; Calls = @($script:commandCalls) }
|
||
|
|
""", exit_code=7)
|
||
|
|
self.assertIn("exit code: 7", result["Failure"])
|
||
|
|
self.assertEqual(len(result["Calls"]), 1)
|
||
|
|
self.assertEqual(list(self.data.iterdir()), [])
|
||
|
|
self.assertEqual(len(list(self.root.glob(self.ini.name + ".autodeploy-*.bak"))), 1)
|
||
|
|
|
||
|
|
def test_initialize_success_without_dictionary_is_rejected(self):
|
||
|
|
result = self.run_harness("""
|
||
|
|
$failure = ''
|
||
|
|
try { Initialize-EmptyMySqlData $service $layout | Out-Null }
|
||
|
|
catch { $failure = $_.Exception.Message }
|
||
|
|
$result = @{ Failure = $failure; Calls = @($script:commandCalls) }
|
||
|
|
""", create_dictionary=False)
|
||
|
|
self.assertIn("mysql.ibd is missing", result["Failure"])
|
||
|
|
self.assertEqual(len(result["Calls"]), 1)
|
||
|
|
|
||
|
|
def run_generated_script(self, initialize_exit):
|
||
|
|
events_path = self.root / "generated-script-events.json"
|
||
|
|
command_line = f'"{self.server}" --defaults-file="{self.ini}" MySQL80'
|
||
|
|
mocks = (
|
||
|
|
f"\n$script:eventFile = {ps_literal(events_path)}\n"
|
||
|
|
f"$script:testData = {ps_literal(self.data)}\n"
|
||
|
|
f"$script:initExit = {initialize_exit}\n"
|
||
|
|
"$script:mockService = [pscustomobject]@{ Name = 'MySQL80'; "
|
||
|
|
f"PathName = {ps_literal(command_line)}; "
|
||
|
|
"State = 'Stopped'; ProcessId = 0; StartName = 'NT AUTHORITY\\NetworkService' }\n"
|
||
|
|
+ r'''
|
||
|
|
$script:events = @()
|
||
|
|
function Record-RecoveryEvent([string]$Name) {
|
||
|
|
$script:events += $Name
|
||
|
|
$json = ConvertTo-Json -InputObject @($script:events) -Compress
|
||
|
|
[IO.File]::WriteAllText($script:eventFile, $json, (New-Object Text.UTF8Encoding($false)))
|
||
|
|
}
|
||
|
|
function Get-CimInstance { return $script:mockService }
|
||
|
|
function Get-WinEvent {}
|
||
|
|
function Start-Sleep {}
|
||
|
|
function Start-Service {
|
||
|
|
Record-RecoveryEvent 'start'
|
||
|
|
$script:mockService.State = 'Running'
|
||
|
|
$script:mockService.ProcessId = 100
|
||
|
|
}
|
||
|
|
function Invoke-MySqlRecoveryCommand([string]$Executable, [string[]]$Arguments) {
|
||
|
|
if ($Arguments -contains '--initialize-insecure') {
|
||
|
|
Record-RecoveryEvent 'initialize'
|
||
|
|
if ($script:initExit -eq 0) {
|
||
|
|
[IO.File]::WriteAllText((Join-Path $script:testData 'mysql.ibd'), 'test dictionary')
|
||
|
|
}
|
||
|
|
return $script:initExit
|
||
|
|
}
|
||
|
|
if ($Executable -eq 'icacls.exe') { Record-RecoveryEvent 'acl'; return 0 }
|
||
|
|
if ($Arguments -contains '--validate-config') { Record-RecoveryEvent 'validate'; return 0 }
|
||
|
|
throw 'Unexpected native operation in test harness'
|
||
|
|
}
|
||
|
|
'''
|
||
|
|
)
|
||
|
|
generated = build_mysql_service_start_script("MySQL80", self.server)
|
||
|
|
# Replace only the helper prefix to override native/service commands before
|
||
|
|
# exercising the complete generated workflow, including its process exit.
|
||
|
|
self.assertTrue(generated.startswith(MYSQL_RECOVERY_HELPERS))
|
||
|
|
instrumented = (
|
||
|
|
MYSQL_RECOVERY_HELPERS + mocks + generated[len(MYSQL_RECOVERY_HELPERS):]
|
||
|
|
)
|
||
|
|
script_path = self.root / "generated-start-script.ps1"
|
||
|
|
script_path.write_text(instrumented, encoding="utf-8-sig")
|
||
|
|
completed = subprocess.run(
|
||
|
|
[POWERSHELL, "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass",
|
||
|
|
"-File", str(script_path)],
|
||
|
|
capture_output=True,
|
||
|
|
timeout=20,
|
||
|
|
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
|
||
|
|
)
|
||
|
|
self.assertTrue(events_path.exists(), (completed.stdout + completed.stderr))
|
||
|
|
events = json.loads(events_path.read_text(encoding="utf-8"))
|
||
|
|
return completed, events
|
||
|
|
|
||
|
|
def test_generated_script_never_starts_service_after_initialization_failure(self):
|
||
|
|
completed, events = self.run_generated_script(initialize_exit=7)
|
||
|
|
self.assertEqual(completed.returncode, 2)
|
||
|
|
self.assertEqual(events, ["initialize"])
|
||
|
|
self.assertIn(b"MySQL initialization failed, exit code: 7", completed.stdout)
|
||
|
|
|
||
|
|
def test_generated_script_initializes_before_granting_validating_and_starting(self):
|
||
|
|
completed, events = self.run_generated_script(initialize_exit=0)
|
||
|
|
self.assertEqual(completed.returncode, 0, completed.stdout + completed.stderr)
|
||
|
|
self.assertEqual(events, ["initialize", "acl", "validate", "start"])
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|