fix: SAC pushes RDP bundle from git via WinRM, no client git (0.20.14)
This commit is contained in:
@@ -1,42 +1,92 @@
|
||||
"""WinRM command builder and CLIXML error parsing tests."""
|
||||
"""WinRM command builder, bundle push and CLIXML error parsing tests."""
|
||||
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from app.services.winrm_connect import (
|
||||
RDP_BUNDLE_REQUIRED,
|
||||
RDP_REMOTE_STAGING,
|
||||
WinRmCmdResult,
|
||||
_clixml_to_plain,
|
||||
_rdp_update_powershell_script,
|
||||
_custom_deploy_body,
|
||||
_deploy_from_staging_body,
|
||||
_prepare_staging_body,
|
||||
_winrm_failure_detail,
|
||||
_write_file_chunk_body,
|
||||
run_winrm_rdp_monitor_update,
|
||||
)
|
||||
|
||||
|
||||
def test_rdp_update_default_script_clones_git_and_runs_local_deploy():
|
||||
script = _rdp_update_powershell_script("")
|
||||
assert "$ProgressPreference = 'SilentlyContinue'" in script
|
||||
assert "NETLOGON" not in script
|
||||
assert "_sac_git" in script
|
||||
assert "git.kalinamall.ru/PapaTramp/RDP-login-monitor.git" in script
|
||||
assert "-SourceShareRoot $repoDir" in script
|
||||
def test_prepare_staging_recreates_remote_dir():
|
||||
script = _prepare_staging_body(RDP_REMOTE_STAGING)
|
||||
assert "Remove-Item" in script
|
||||
assert "_sac_staging" in script
|
||||
|
||||
|
||||
def test_rdp_update_default_script_honors_sac_git_settings():
|
||||
script = _rdp_update_powershell_script(
|
||||
"",
|
||||
repo_url="https://git.example.com/org/rdp.git",
|
||||
git_branch="release",
|
||||
)
|
||||
assert "git.example.com/org/rdp.git" in script
|
||||
assert "$branch = 'release'" in script
|
||||
def test_deploy_from_staging_runs_local_bundle():
|
||||
script = _deploy_from_staging_body(RDP_REMOTE_STAGING)
|
||||
assert "-SourceShareRoot" in script
|
||||
assert "Deploy-LoginMonitor.ps1" in script
|
||||
|
||||
|
||||
def test_rdp_update_custom_script_uses_literal_path():
|
||||
script = _rdp_update_powershell_script(r"\\b26\NETLOGON\RDP-login-monitor\Deploy-LoginMonitor.ps1")
|
||||
assert r"\\b26\NETLOGON\RDP-login-monitor\Deploy-LoginMonitor.ps1" in script
|
||||
def test_write_file_chunk_body_supports_append():
|
||||
first = _write_file_chunk_body(r"C:\temp\a.ps1", "YQ==", append=False)
|
||||
second = _write_file_chunk_body(r"C:\temp\a.ps1", "Yg==", append=True)
|
||||
assert "WriteAllBytes" in first
|
||||
assert "Append" in second
|
||||
|
||||
|
||||
def test_custom_deploy_script_uses_literal_path():
|
||||
script = _custom_deploy_body(r"C:\tmp\Deploy-LoginMonitor.ps1")
|
||||
assert r"C:\tmp\Deploy-LoginMonitor.ps1" in script
|
||||
assert "Test-Path -LiteralPath" in script
|
||||
|
||||
|
||||
def test_rdp_update_custom_script_escapes_single_quotes():
|
||||
script = _rdp_update_powershell_script(r"C:\Users\O'Brien\Deploy-LoginMonitor.ps1")
|
||||
def test_custom_deploy_script_escapes_single_quotes():
|
||||
script = _custom_deploy_body(r"C:\Users\O'Brien\Deploy-LoginMonitor.ps1")
|
||||
assert "O''Brien" in script
|
||||
|
||||
|
||||
def test_run_winrm_rdp_monitor_update_pushes_bundle_from_sac_git(tmp_path: Path):
|
||||
repo = tmp_path / "repo"
|
||||
repo.mkdir()
|
||||
for name in RDP_BUNDLE_REQUIRED:
|
||||
(repo / name).write_text(f"content-{name}", encoding="utf-8")
|
||||
(repo / "Sac-Client.ps1").write_text("Sac client", encoding="utf-8")
|
||||
|
||||
calls: list[str] = []
|
||||
|
||||
def fake_run_ps(*, script: str, **kwargs) -> WinRmCmdResult:
|
||||
calls.append(script)
|
||||
if "Remove-Item" in script:
|
||||
return WinRmCmdResult(ok=True, message="staging ok", target="pc", stdout="Staging ready")
|
||||
if "Deploy-LoginMonitor.ps1 missing" in script or "& $deploy" in script:
|
||||
return WinRmCmdResult(
|
||||
ok=True,
|
||||
message="WinRM OK (pc), exit 0",
|
||||
target="pc",
|
||||
stdout="deployed 1.2.3",
|
||||
exit_code=0,
|
||||
)
|
||||
return WinRmCmdResult(ok=True, message="chunk ok", target="pc", exit_code=0)
|
||||
|
||||
with (
|
||||
patch("app.services.winrm_connect._fetch_rdp_bundle_dir", return_value=repo),
|
||||
patch("app.services.winrm_connect.run_winrm_ps", side_effect=fake_run_ps),
|
||||
):
|
||||
result = run_winrm_rdp_monitor_update(
|
||||
target="pc",
|
||||
user="B26\\admin",
|
||||
password="pw",
|
||||
repo_url="https://git.example.com/RDP-login-monitor.git",
|
||||
)
|
||||
|
||||
assert result.ok is True
|
||||
assert "SAC pushed" in result.stdout
|
||||
assert any("WriteAllBytes" in call or "Append" in call for call in calls)
|
||||
assert "git.exe" not in "\n".join(calls)
|
||||
|
||||
|
||||
def test_winrm_failure_detail_extracts_message_from_clixml():
|
||||
clixml = (
|
||||
"#< CLIXML\n"
|
||||
|
||||
Reference in New Issue
Block a user