conversation-session-ssh-remote-install-killed.test.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { execFile } from "node:child_process";
  2. import { mkdir, writeFile } from "node:fs/promises";
  3. import { resolve } from "node:path";
  4. import { promisify } from "node:util";
  5. import { readLastConnectRemoteError } from "#e2e/helpers/connect-remote-error-log";
  6. import { clearAppData } from "#e2e/helpers/desktop-app";
  7. import {
  8. readRemoteContainerFile,
  9. readRemoteDockerContainer,
  10. writeRemoteContainerFile,
  11. } from "#e2e/helpers/remote-docker-target";
  12. import {
  13. clickLastEnabledSSHDialogButton,
  14. fillSSHPasswordSettings,
  15. openSSHRemoteDialog,
  16. readSSHRuntimeConfig,
  17. waitForSSHDirectoryStep,
  18. } from "#e2e/helpers/ssh-remote-p0";
  19. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  20. // 3.16.2 远端回归 R1 的真实 SSH 验证(本机 Docker runner):远端安装命令被信号杀死时,
  21. // SSH exec 必须报告失败退出码,Desktop 报安装失败;不能把信号退出记成 0,
  22. // 再在下游报出误导性的 "Remote command returned no JSON result"。
  23. const MARKER = "E2E_SSH_INSTALL_KILLED";
  24. const KILLER_SCRIPT = "/home/dev/.e2e-install-killer.sh";
  25. const KILLER_LOG = "/tmp/e2e-install-killer.log";
  26. const NO_JSON_MESSAGE = "Remote command returned no JSON result";
  27. const artifactDir = resolve(
  28. process.env.ZCODE_E2E_ARTIFACT_DIR ?? ".e2e-artifacts/ssh-install-killed",
  29. );
  30. const execFileAsync = promisify(execFile);
  31. describe(`${MARKER}: 远端安装命令被信号杀死`, () => {
  32. const facts: Record<string, unknown> = {};
  33. after(async () => {
  34. await mkdir(artifactDir, { recursive: true });
  35. await writeFile(resolve(artifactDir, "install-killed.json"), JSON.stringify(facts, null, 2));
  36. await browser.electron.restoreAllMocks();
  37. await clearAppData();
  38. });
  39. it("R1 安装 shell 被 SIGKILL 时 Desktop 报安装失败,而不是 no JSON result", async function () {
  40. this.timeout(15 * 60_000);
  41. const config = readSSHRuntimeConfig(MARKER);
  42. const container = readRemoteDockerContainer(MARKER);
  43. await prepareV4ConversationE2E({ skipProvider: true });
  44. // 只杀 sshd 直接拉起的安装 shell,让 sshd 向客户端发送 exit-signal。
  45. await writeRemoteContainerFile(
  46. container,
  47. KILLER_SCRIPT,
  48. [
  49. `end=$(( $(date +%s) + 600 ))`,
  50. `while [ "$(date +%s)" -lt "$end" ]; do`,
  51. ` for p in $(pgrep -f 'update --from-archive'); do`,
  52. ` pp=$(ps -o ppid= -p "$p" 2>/dev/null | tr -d ' ')`,
  53. ` [ -n "$pp" ] || continue`,
  54. ` if [ "$(ps -o comm= -p "$pp" 2>/dev/null)" = "sshd" ] && ps -o args= -p "$p" | grep -q 'tar -xzf'; then`,
  55. ` comm=$(ps -o comm= -p "$p" 2>/dev/null)`,
  56. ` kill -KILL "$p" && echo "$(date +%s%3N) killed pid=$p comm=$comm" >> ${KILLER_LOG}`,
  57. ` exit 0`,
  58. ` fi`,
  59. ` done`,
  60. ` sleep 0.02`,
  61. `done`,
  62. "",
  63. ].join("\n"),
  64. );
  65. await execFileAsync("docker", ["exec", "-d", "--user", "dev", container, "sh", KILLER_SCRIPT]);
  66. await openSSHRemoteDialog(MARKER);
  67. await fillSSHPasswordSettings(config);
  68. await clickLastEnabledSSHDialogButton(MARKER);
  69. let connectError: unknown;
  70. try {
  71. // 新容器会弹出安装确认,helper 自动确认后开始上传与远端安装。
  72. await waitForSSHDirectoryStep(MARKER);
  73. } catch (error) {
  74. connectError = error;
  75. }
  76. facts.connectError = connectError instanceof Error ? connectError.message : connectError;
  77. facts.killer = await readRemoteContainerFile(container, KILLER_LOG).catch(() => "");
  78. facts.rawError = await readLastConnectRemoteError();
  79. expect(facts.killer).toMatch(/killed pid=\d+/u);
  80. expect(connectError).toBeInstanceOf(Error);
  81. expect(facts.rawError).not.toBe(NO_JSON_MESSAGE);
  82. expect(facts.rawError).toMatch(/archive installation failed/u);
  83. });
  84. });