179 lines
8.8 KiB
Python
179 lines
8.8 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
|
|
DEV_UP_PATH = Path(__file__).with_name("dev-up.ps1")
|
|
|
|
|
|
class DevUpDatabaseReadinessTest(unittest.TestCase):
|
|
"""Docker 없이 PowerShell 테스트 더블로 polling 동작을 검증한다."""
|
|
|
|
@classmethod
|
|
def setUpClass(cls) -> None:
|
|
cls.source = DEV_UP_PATH.read_text(encoding="utf-8")
|
|
cls.powershell = shutil.which("powershell.exe")
|
|
assert cls.powershell is not None, (
|
|
"이 검증은 Windows PowerShell 5.1을 필수 사전조건으로 한다."
|
|
)
|
|
|
|
def function_source(self, name: str) -> str:
|
|
match = re.search(rf"^function {re.escape(name)}\b", self.source, re.MULTILINE)
|
|
self.assertIsNotNone(match, name)
|
|
assert match is not None
|
|
body_start = self.source.index("{", match.start())
|
|
depth = 0
|
|
for index in range(body_start, len(self.source)):
|
|
if self.source[index] == "{":
|
|
depth += 1
|
|
elif self.source[index] == "}":
|
|
depth -= 1
|
|
if depth == 0:
|
|
return self.source[match.start() : index + 1]
|
|
self.fail(f"unterminated PowerShell function: {name}")
|
|
|
|
def run_powershell(self, script: str) -> subprocess.CompletedProcess[str]:
|
|
assert self.powershell is not None
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
harness = Path(directory) / "dev-up-readiness-harness.ps1"
|
|
prelude = (
|
|
"[Console]::OutputEncoding = [Text.UTF8Encoding]::new($false)\n"
|
|
"$OutputEncoding = [Text.UTF8Encoding]::new($false)\n"
|
|
)
|
|
harness.write_text(prelude + script, encoding="utf-8-sig")
|
|
return subprocess.run(
|
|
[self.powershell, "-NoProfile", "-File", str(harness)],
|
|
check=False,
|
|
capture_output=True,
|
|
text=True,
|
|
encoding="utf-8",
|
|
)
|
|
|
|
def test_tcp_readiness_retries_then_succeeds_with_real_database_name(self) -> None:
|
|
wait = self.function_source("Wait-DevDbReady")
|
|
completed = self.run_powershell(
|
|
"\n".join(
|
|
(
|
|
"$DevDbContainerName = 'vignette-dev-db'",
|
|
"$script:probeCalls = 0",
|
|
"$script:probeArguments = ''",
|
|
"function Invoke-DockerProbe {",
|
|
" $script:probeCalls += 1",
|
|
" $script:probeArguments = $args -join ' '",
|
|
" if ($script:probeCalls -lt 3) { $global:LASTEXITCODE = 1 } else { $global:LASTEXITCODE = 0 }",
|
|
"}",
|
|
"function Start-Sleep { param([int]$Seconds, [int]$Milliseconds) }",
|
|
wait,
|
|
"if (-not (Wait-DevDbReady 'owner' 'vignette' 3)) { throw 'expected readiness success' }",
|
|
"if ($script:probeCalls -ne 3) { throw \"probe calls: $script:probeCalls\" }",
|
|
"if ($script:probeArguments -ne 'exec vignette-dev-db pg_isready -h 127.0.0.1 -p 5432 -U owner -d vignette') { throw \"unexpected probe: $script:probeArguments\" }",
|
|
)
|
|
)
|
|
)
|
|
self.assertEqual(0, completed.returncode, completed.stderr)
|
|
|
|
def test_tcp_readiness_times_out_without_false_success(self) -> None:
|
|
wait = self.function_source("Wait-DevDbReady")
|
|
completed = self.run_powershell(
|
|
"\n".join(
|
|
(
|
|
"$DevDbContainerName = 'vignette-dev-db'",
|
|
"$script:probeCalls = 0",
|
|
"function Invoke-DockerProbe { $script:probeCalls += 1; $global:LASTEXITCODE = 1 }",
|
|
"function Start-Sleep { param([int]$Seconds, [int]$Milliseconds) }",
|
|
wait,
|
|
"if (Wait-DevDbReady 'owner' 'vignette' 2) { throw 'timeout was reported as success' }",
|
|
"if ($script:probeCalls -ne 2) { throw \"probe calls: $script:probeCalls\" }",
|
|
)
|
|
)
|
|
)
|
|
self.assertEqual(0, completed.returncode, completed.stderr)
|
|
|
|
def test_http_200_with_db_false_is_rejected(self) -> None:
|
|
wait = self.function_source("Wait-ApiDbHealth")
|
|
completed = self.run_powershell(
|
|
"\n".join(
|
|
(
|
|
"$script:clock = [System.Collections.Generic.Queue[datetime]]::new()",
|
|
"$script:clock.Enqueue([datetime]'2026-01-01T00:00:00Z')",
|
|
"$script:clock.Enqueue([datetime]'2026-01-01T00:00:00Z')",
|
|
"$script:clock.Enqueue([datetime]'2026-01-01T00:01:00Z')",
|
|
"function Get-Date { return $script:clock.Dequeue() }",
|
|
"function Invoke-WebRequest { return [pscustomobject]@{ StatusCode = 200; Content = '{\"db\":false}' } }",
|
|
"function Start-Sleep { param([int]$Seconds, [int]$Milliseconds) }",
|
|
wait,
|
|
"if (Wait-ApiDbHealth 'http://127.0.0.1:8000/health' 40) { throw 'db=false was accepted' }",
|
|
)
|
|
)
|
|
)
|
|
self.assertEqual(0, completed.returncode, completed.stderr)
|
|
|
|
def test_http_200_with_string_true_is_rejected(self) -> None:
|
|
wait = self.function_source("Wait-ApiDbHealth")
|
|
completed = self.run_powershell(
|
|
"\n".join(
|
|
(
|
|
"$script:clock = [System.Collections.Generic.Queue[datetime]]::new()",
|
|
"$script:clock.Enqueue([datetime]'2026-01-01T00:00:00Z')",
|
|
"$script:clock.Enqueue([datetime]'2026-01-01T00:00:00Z')",
|
|
"$script:clock.Enqueue([datetime]'2026-01-01T00:01:00Z')",
|
|
"function Get-Date { return $script:clock.Dequeue() }",
|
|
"function Invoke-WebRequest { return [pscustomobject]@{ StatusCode = 200; Content = '{\"db\":\"true\"}' } }",
|
|
"function Start-Sleep { param([int]$Seconds, [int]$Milliseconds) }",
|
|
wait,
|
|
"if (Wait-ApiDbHealth 'http://127.0.0.1:8000/health' 40) { throw 'string db=true was accepted' }",
|
|
)
|
|
)
|
|
)
|
|
self.assertEqual(0, completed.returncode, completed.stderr)
|
|
|
|
def test_http_200_with_db_false_retries_until_boolean_true(self) -> None:
|
|
wait = self.function_source("Wait-ApiDbHealth")
|
|
completed = self.run_powershell(
|
|
"\n".join(
|
|
(
|
|
"$script:clock = [System.Collections.Generic.Queue[datetime]]::new()",
|
|
"$script:clock.Enqueue([datetime]'2026-01-01T00:00:00Z')",
|
|
"$script:clock.Enqueue([datetime]'2026-01-01T00:00:00Z')",
|
|
"$script:clock.Enqueue([datetime]'2026-01-01T00:00:01Z')",
|
|
"$script:requestCalls = 0",
|
|
"function Get-Date { return $script:clock.Dequeue() }",
|
|
"function Invoke-WebRequest {",
|
|
" $script:requestCalls += 1",
|
|
" if ($script:requestCalls -eq 1) { return [pscustomobject]@{ StatusCode = 200; Content = '{\"db\":false}' } }",
|
|
" return [pscustomobject]@{ StatusCode = 200; Content = '{\"db\":true}' }",
|
|
"}",
|
|
"function Start-Sleep { param([int]$Seconds, [int]$Milliseconds) }",
|
|
wait,
|
|
"if (-not (Wait-ApiDbHealth 'http://127.0.0.1:8000/health' 40)) { throw 'db=false to db=true retry did not succeed' }",
|
|
"if ($script:requestCalls -ne 2) { throw \"request calls: $script:requestCalls\" }",
|
|
)
|
|
)
|
|
)
|
|
self.assertEqual(0, completed.returncode, completed.stderr)
|
|
|
|
def test_degraded_mode_skips_managed_database_health_requirement(self) -> None:
|
|
confirm = self.function_source("Confirm-ManagedApiDbHealth")
|
|
completed = self.run_powershell(
|
|
"\n".join(
|
|
(
|
|
"$script:apiDbHealthCalls = 0",
|
|
"function Wait-ApiDbHealth { $script:apiDbHealthCalls += 1; return $false }",
|
|
confirm,
|
|
"if (-not (Confirm-ManagedApiDbHealth $false 'http://127.0.0.1:8000/health')) { throw 'degraded mode was rejected' }",
|
|
"if ($script:apiDbHealthCalls -ne 0) { throw 'degraded mode called managed DB health check' }",
|
|
"if (Confirm-ManagedApiDbHealth $true 'http://127.0.0.1:8000/health') { throw 'managed DB health failure was accepted' }",
|
|
"if ($script:apiDbHealthCalls -ne 1) { throw \"managed DB health calls: $script:apiDbHealthCalls\" }",
|
|
)
|
|
)
|
|
)
|
|
self.assertEqual(0, completed.returncode, completed.stderr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|