remoteAssetCacheWindowsRename.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { createHash } from "node:crypto";
  2. import * as realFsPromises from "node:fs/promises";
  3. import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises";
  4. import { tmpdir } from "node:os";
  5. import { join, normalize } from "node:path";
  6. import { fileURLToPath, pathToFileURL } from "node:url";
  7. import { afterEach, describe, expect, it, vi } from "vitest";
  8. import { ZCODE_VERSION } from "@zcode/shared";
  9. import { createTarGzArchive } from "@zcode/server/remote/localTarGz.js";
  10. function remoteAssetRenameEperm(): NodeJS.ErrnoException {
  11. const error = new Error("operation not permitted, rename") as NodeJS.ErrnoException;
  12. error.code = "EPERM";
  13. error.errno = -4048;
  14. error.syscall = "rename";
  15. return error;
  16. }
  17. async function writeComponentArchive(options: {
  18. sourceRoot: string;
  19. cdnRoot: string;
  20. platformArch: string;
  21. id: string;
  22. version: string;
  23. mount: string;
  24. artifactPath: string;
  25. }): Promise<string> {
  26. const archivePath = join(options.cdnRoot, options.artifactPath);
  27. const sourcePath = join(options.sourceRoot, ...options.mount.split("/"));
  28. const entries = await readdir(sourcePath);
  29. await createTarGzArchive(
  30. archivePath,
  31. entries.map((entry) => ({
  32. sourcePath: join(sourcePath, entry),
  33. archivePath: entry,
  34. })),
  35. );
  36. return createHash("sha256").update(await readFile(archivePath)).digest("hex");
  37. }
  38. describe("remote asset cache Windows directory commit", () => {
  39. afterEach(async () => {
  40. vi.doUnmock("node:fs/promises");
  41. vi.unstubAllGlobals();
  42. vi.resetModules();
  43. });
  44. it("retries transient EPERM while committing a staged release component directory", async () => {
  45. const rootDir = await mkdtemp(join(tmpdir(), "zcode-remote-cache-rename-"));
  46. const sourceRoot = join(rootDir, "source");
  47. const cdnRoot = join(rootDir, "cdn", ZCODE_VERSION);
  48. const remoteCacheDir = join(rootDir, "remote-cache");
  49. const platformArch = "linux-x64";
  50. const failedRenameTargets: string[] = [];
  51. try {
  52. await mkdir(join(sourceRoot, "server"), { recursive: true });
  53. await mkdir(join(sourceRoot, "node", platformArch), { recursive: true });
  54. await mkdir(join(sourceRoot, "glm", platformArch), { recursive: true });
  55. await writeFile(join(sourceRoot, "server", "zcode-server.cjs"), "server", "utf8");
  56. await writeFile(join(sourceRoot, "node", platformArch, "node"), "node", "utf8");
  57. await writeFile(join(sourceRoot, "glm", platformArch, "zcode-agent"), "glm", "utf8");
  58. const components = [
  59. {
  60. id: "server-bundle",
  61. version: `${ZCODE_VERSION}-server-bundle`,
  62. artifactPath: `components/${platformArch}/server-bundle.tar.gz`,
  63. mount: "server",
  64. },
  65. {
  66. id: "node-runtime",
  67. version: "24.10.0-node-runtime",
  68. artifactPath: `components/${platformArch}/node-runtime.tar.gz`,
  69. mount: `node/${platformArch}`,
  70. },
  71. {
  72. id: "glm",
  73. version: "3.1.2-glm",
  74. artifactPath: `components/${platformArch}/glm.tar.gz`,
  75. mount: `glm/${platformArch}`,
  76. },
  77. ];
  78. const manifestComponents = [];
  79. for (const component of components) {
  80. manifestComponents.push({
  81. ...component,
  82. sha256: await writeComponentArchive({
  83. sourceRoot,
  84. cdnRoot,
  85. platformArch,
  86. ...component,
  87. }),
  88. });
  89. }
  90. const glmSha256 = manifestComponents.find(
  91. (component) => component.id === "glm",
  92. )?.sha256;
  93. if (!glmSha256) {
  94. throw new Error("missing GLM component SHA");
  95. }
  96. await writeFile(
  97. join(cdnRoot, `manifest-${platformArch}.json`),
  98. JSON.stringify(
  99. {
  100. schemaVersion: 1,
  101. appVersion: ZCODE_VERSION,
  102. platformArch,
  103. components: manifestComponents,
  104. },
  105. null,
  106. 2,
  107. ),
  108. "utf8",
  109. );
  110. vi.doMock("node:fs/promises", async () => {
  111. const actual = await vi.importActual<typeof realFsPromises>("node:fs/promises");
  112. let failedGlmReleaseCommit = false;
  113. return {
  114. ...actual,
  115. rename: vi.fn(async (from: string, to: string) => {
  116. const normalizedTo = normalize(to);
  117. if (
  118. !failedGlmReleaseCommit &&
  119. normalizedTo.endsWith(normalize(join("glm", platformArch)))
  120. ) {
  121. failedGlmReleaseCommit = true;
  122. failedRenameTargets.push(normalizedTo);
  123. throw remoteAssetRenameEperm();
  124. }
  125. return actual.rename(from, to);
  126. }),
  127. };
  128. });
  129. vi.stubGlobal(
  130. "fetch",
  131. vi.fn(async (input: RequestInfo | URL) => {
  132. const url = String(input);
  133. if (!url.startsWith("file:")) {
  134. return new Response(null, { status: 404 });
  135. }
  136. try {
  137. const body = await readFile(fileURLToPath(url));
  138. return new Response(body, {
  139. status: 200,
  140. headers: { "content-length": String(body.byteLength) },
  141. });
  142. } catch {
  143. return new Response(null, { status: 404 });
  144. }
  145. }),
  146. );
  147. const { ensureRemoteReleaseDirFromCdn } = await import(
  148. "@zcode/server/remote/remoteAssetCache.js"
  149. );
  150. const releaseDir = await ensureRemoteReleaseDirFromCdn(
  151. {
  152. remoteCdnBaseUrl: pathToFileURL(join(rootDir, "cdn")).href,
  153. remoteCacheDir,
  154. version: ZCODE_VERSION,
  155. platformArch,
  156. },
  157. { log: () => undefined, logWarn: () => undefined },
  158. );
  159. expect(releaseDir).toBe(
  160. join(
  161. remoteCacheDir,
  162. "releases",
  163. ZCODE_VERSION,
  164. platformArch,
  165. "glm-content",
  166. glmSha256,
  167. ),
  168. );
  169. expect(failedRenameTargets).toHaveLength(1);
  170. await expect(
  171. readFile(join(releaseDir, "glm", platformArch, "zcode-agent"), "utf8"),
  172. ).resolves.toBe("glm");
  173. } finally {
  174. await rm(rootDir, { force: true, recursive: true });
  175. }
  176. });
  177. });