conversation-session-r3-install-environment.test.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import { mkdir, writeFile } from "node:fs/promises";
  2. import { resolve } from "node:path";
  3. import { clearAppData } from "#e2e/helpers/desktop-app";
  4. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  5. import {
  6. clickLastEnabledSSHDialogButton,
  7. fillSSHPasswordSettings,
  8. openSSHRemoteDialog,
  9. readHostProcesses,
  10. readRemoteTabSnapshot,
  11. readSSHRuntimeConfig,
  12. runSSHCommand,
  13. selectSSHRemoteDirectory,
  14. waitForRemoteTab,
  15. waitForSSHDirectoryStep,
  16. type SSHConnectionConfig,
  17. type SSHRuntimeConfig,
  18. } from "#e2e/helpers/ssh-remote-p0";
  19. const CASE_MARKER = "E2E_R3_INSTALL_ENVIRONMENT";
  20. const artifactDir = resolve(
  21. process.env.ZCODE_E2E_ARTIFACT_DIR ?? ".e2e-artifacts/r3-install-environment",
  22. );
  23. // old-glibc-runtime 需要真实 glibc < 2.28 的远端镜像,不注入 getconf。服务器队列没有该档位,
  24. // 只能本机运行,构建与运行命令见 harness/remote/README.md「低版本 glibc 目标」。
  25. type InstallEnvironmentCase =
  26. | "noexec-tmp"
  27. | "no-tar"
  28. | "no-forwarding"
  29. | "old-glibc"
  30. | "old-glibc-runtime";
  31. const SUBCASES: InstallEnvironmentCase[] = [
  32. "noexec-tmp",
  33. "no-tar",
  34. "no-forwarding",
  35. "old-glibc",
  36. "old-glibc-runtime",
  37. ];
  38. const SUBCASE = (process.env.R3_INSTALL_ENV_CASE ?? "") as InstallEnvironmentCase;
  39. async function readRemoteFacts(config: SSHConnectionConfig) {
  40. const facts = await runSSHCommand(
  41. config,
  42. 'echo "tmp=$(mount | grep " /tmp " | head -n1)"; echo "tar=$(command -v tar || echo none)"; ' +
  43. 'echo "forwarding=$(grep -iE "^AllowTcpForwarding" /etc/ssh/sshd_config | tail -n1)"; ' +
  44. 'echo "current=$(test -f "$HOME/.zcode/server/current.json" && grep -o \'"archiveSha256": *"[0-9a-f]*"\' "$HOME/.zcode/server/current.json" || echo none)"; ' +
  45. 'echo "staging=$(ls -d "$HOME"/.zcode/server/staging* "$HOME"/.zcode/server/bootstrap* 2>/dev/null | tr "\\n" ",")"; ' +
  46. 'echo "serverRoot=$(ls -A "$HOME/.zcode/server" 2>/dev/null | tr "\\n" ",")"; ' +
  47. 'echo "libc=$(getconf GNU_LIBC_VERSION 2>/dev/null || echo none)"; ' +
  48. 'echo "tmpEntries=$(ls /tmp 2>/dev/null | tr "\\n" ",")"; echo "status=$(cat "$HOME/.zcode/server/run/status.json" 2>/dev/null | tr -d "\\n ")"',
  49. );
  50. return Object.fromEntries(
  51. facts.stdout
  52. .trim()
  53. .split("\n")
  54. .map((line) => {
  55. const index = line.indexOf("=");
  56. return [line.slice(0, index), line.slice(index + 1)];
  57. }),
  58. ) as Record<string, string>;
  59. }
  60. /** 沿现行项目优先流程完成首装、选目录和绑定;仅把真实向导错误作为失败结果。 */
  61. async function connectUntilErrorOrReady(config: SSHRuntimeConfig): Promise<{
  62. errorText: string;
  63. connectionLog: string;
  64. ready: boolean;
  65. }> {
  66. await openSSHRemoteDialog(CASE_MARKER);
  67. await fillSSHPasswordSettings(config);
  68. await clickLastEnabledSSHDialogButton(CASE_MARKER);
  69. try {
  70. // 原脚本等向导自行关闭,但首连成功会停在目录页,导致成功连接也耗尽 180 秒。
  71. const phase = await waitForSSHDirectoryStep(CASE_MARKER, 180_000);
  72. expect(phase).toBe("directory");
  73. await selectSSHRemoteDirectory(config.workspacePaths[0]!, CASE_MARKER);
  74. const project = await waitForRemoteTab({
  75. caseMarker: CASE_MARKER,
  76. expectedPath: config.workspacePaths[0]!,
  77. });
  78. expect(project.remoteSessionId).toBeTruthy();
  79. return { errorText: "", connectionLog: "", ready: true };
  80. } catch (error) {
  81. const failure = await browser.execute(() => {
  82. const panel = document.querySelector<HTMLElement>('[data-testid="ssh-error"]');
  83. if (!panel?.getClientRects().length) return null;
  84. return {
  85. errorText: panel.innerText.trim(),
  86. connectionLog:
  87. document.querySelector<HTMLElement>('[data-testid="ssh-dialog"]')?.innerText ?? "",
  88. ready: false,
  89. };
  90. });
  91. if (!failure) throw error;
  92. return failure;
  93. }
  94. }
  95. describe(`${CASE_MARKER}: 受限远端环境下的安装与连接(${SUBCASE})`, () => {
  96. after(async () => {
  97. await browser.electron.restoreAllMocks();
  98. await clearAppData();
  99. });
  100. it(`R3A-BLD-09/CON-06 ${SUBCASE}`, async function () {
  101. this.timeout(20 * 60_000);
  102. if (!SUBCASES.includes(SUBCASE)) {
  103. throw new Error(`${CASE_MARKER}: R3_INSTALL_ENV_CASE 必须为 ${SUBCASES.join("|")}`);
  104. }
  105. const config = readSSHRuntimeConfig(CASE_MARKER);
  106. const before = await readRemoteFacts(config);
  107. expect(before.current).toBe("none");
  108. await prepareV4ConversationE2E({ skipProvider: true });
  109. await browser.execute(() => {
  110. const state = window as Window & {
  111. __installEnvironmentLogs?: Array<{ message: string; level: string }>;
  112. zcode?: {
  113. onRemoteConnectionLog: (
  114. callback: (entry: { message: string; level: string }) => void,
  115. ) => () => void;
  116. };
  117. };
  118. state.__installEnvironmentLogs = [];
  119. if (!state.zcode) throw new Error("Missing desktop platform");
  120. state.zcode.onRemoteConnectionLog(({ message, level }) =>
  121. state.__installEnvironmentLogs!.push({ message, level }),
  122. );
  123. });
  124. const initialHosts = await readHostProcesses();
  125. expect(initialHosts).toHaveLength(1);
  126. const outcome = await connectUntilErrorOrReady(config);
  127. const connectionLogs = await browser.execute(
  128. () =>
  129. (
  130. window as Window & {
  131. __installEnvironmentLogs?: Array<{ message: string; level: string }>;
  132. }
  133. ).__installEnvironmentLogs ?? [],
  134. );
  135. const after = await readRemoteFacts(config);
  136. const hosts = await readHostProcesses();
  137. await mkdir(artifactDir, { recursive: true });
  138. await writeFile(
  139. resolve(artifactDir, `${SUBCASE}.json`),
  140. JSON.stringify({ subcase: SUBCASE, before, outcome, connectionLogs, after, hosts }, null, 2),
  141. );
  142. expect(hosts).toEqual(initialHosts);
  143. expect(outcome.errorText).not.toContain(config.password);
  144. if (SUBCASE === "noexec-tmp") {
  145. // /tmp 为受限 noexec tmpfs:archive 与 bootstrap 必须落在 Server root 私有 staging,安装照常成功。
  146. expect(before.tmp).toContain("noexec");
  147. if (!outcome.ready)
  148. throw new Error(`${CASE_MARKER}: noexec-tmp 安装失败: ${outcome.errorText}`);
  149. expect(after.current).toMatch(/archiveSha256/u);
  150. expect(after.status).toContain('"state":"ready"');
  151. expect(after.tmpEntries).not.toContain("zcode-server");
  152. return;
  153. }
  154. if (SUBCASE === "no-tar") {
  155. // tar 缺失应在上传前 preflight 早失败:不上传 74MB、不留 staging、不写 current。
  156. expect(before.tar).toBe("none");
  157. expect(outcome.ready).toBe(false);
  158. // 主面板按当前合同给出环境分类,缺失工具的原始原因在展开的连接日志中。
  159. expect(outcome.errorText).toMatch(/环境|environment/iu);
  160. expect(outcome.connectionLog.toLowerCase()).toContain("tar");
  161. expect(outcome.connectionLog).not.toMatch(/Uploading|上传/u);
  162. expect(after.current).toBe("none");
  163. return;
  164. }
  165. if (SUBCASE === "old-glibc") {
  166. // 仅注入 getconf 的低版本报告,真实 runtime 仍可运行:警告不得阻断首装、握手和项目绑定。
  167. expect(before.libc).toBe("glibc 2.17");
  168. expect(connectionLogs).toContainEqual(
  169. expect.objectContaining({
  170. level: "warn",
  171. message: expect.stringMatching(/glibc 2\.17.*glibc 2\.28.*may fail.*Continuing/u),
  172. }),
  173. );
  174. expect(outcome.errorText).toBe("");
  175. expect(outcome.ready).toBe(true);
  176. expect(after.current).toMatch(/archiveSha256/u);
  177. expect(after.status).toContain('"state":"ready"');
  178. return;
  179. }
  180. if (SUBCASE === "old-glibc-runtime") {
  181. // 真实低版本 glibc:随包 Node 在解压后的 update 命令里动态链接失败,安装按原错误通道失败。
  182. // ZCT-2115996335380615168:连接日志须遮住路径,但保留动态链接器给出的缺失符号。
  183. expect(before.libc).toMatch(/^glibc 2\.(?:1\d|2[0-7])$/u);
  184. expect(outcome.ready).toBe(false);
  185. expect(connectionLogs).toContainEqual(
  186. expect.objectContaining({
  187. level: "warn",
  188. message: expect.stringMatching(/glibc 2\.\d+.*glibc 2\.28.*may fail.*Continuing/u),
  189. }),
  190. );
  191. const errorLogs = connectionLogs.filter((entry) => entry.level === "error");
  192. expect(errorLogs.map((entry) => entry.message).join("\n")).toMatch(
  193. /\[redacted-path\]: version `GLIBC_2\.28' not found \(required by \[redacted-path\]\)/u,
  194. );
  195. for (const entry of connectionLogs) {
  196. expect(entry.message).not.toMatch(/\/home\/dev|\.bootstrap-/u);
  197. }
  198. expect(after.current).toBe("none");
  199. return;
  200. }
  201. // 主规格与 Transport Adapter 明确支持 direct-tcpip 被禁后回退 proxy-stdio。
  202. // 任意非空错误不能算通过;必须证明安装、握手、目录 RPC 和项目绑定都成功。
  203. expect((before.forwarding ?? "").toLowerCase()).toContain("allowtcpforwarding no");
  204. expect(outcome.errorText).toBe("");
  205. expect(outcome.ready).toBe(true);
  206. expect(after.current).toMatch(/archiveSha256/u);
  207. expect(after.status).toContain('"state":"ready"');
  208. expect(
  209. (await readRemoteTabSnapshot(undefined, config.workspacePaths[0])).remoteSessionId,
  210. ).toBeTruthy();
  211. });
  212. });