fsFaultInjection.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. createFsFaultInjector,
  4. createFsFaultInjectorFromEnv,
  5. parseFsFaultRulesFromEnvValue,
  6. } from "../src/fs/fsFaultInjection.js";
  7. describe("fsFaultInjection", () => {
  8. it("matches by operation and normalized path, then throws a Node-style errno error", () => {
  9. const injector = createFsFaultInjector([
  10. {
  11. code: "ENOSPC",
  12. id: "D01-session-history-enospc",
  13. operations: ["writeFile"],
  14. pathIncludes: "\\.zcode\\",
  15. },
  16. ]);
  17. expect(() =>
  18. injector.maybeThrow({
  19. operation: "rename",
  20. path: "/tmp/demo/.zcode/session.json",
  21. }),
  22. ).not.toThrow();
  23. expect(() =>
  24. injector.maybeThrow({
  25. operation: "writeFile",
  26. path: "/tmp/demo/.zcode/session.json",
  27. }),
  28. ).toThrow(
  29. expect.objectContaining({
  30. code: "ENOSPC",
  31. path: "/tmp/demo/.zcode/session.json",
  32. syscall: "writeFile",
  33. zcodeFsFaultId: "D01-session-history-enospc",
  34. }),
  35. );
  36. expect(injector.getHits()).toMatchObject([
  37. {
  38. code: "ENOSPC",
  39. id: "D01-session-history-enospc",
  40. matchIndex: 1,
  41. operation: "writeFile",
  42. path: "/tmp/demo/.zcode/session.json",
  43. },
  44. ]);
  45. });
  46. it("defaults to one match so one injected write does not poison unrelated later writes", () => {
  47. const injector = createFsFaultInjector([
  48. {
  49. code: "EACCES",
  50. id: "D03-provider-settings-eacces",
  51. operations: ["writeFile"],
  52. pathEndsWith: "/provider_config.json",
  53. },
  54. ]);
  55. expect(() =>
  56. injector.maybeThrow({
  57. operation: "writeFile",
  58. path: "/tmp/home/.zcode/v2/provider_config.json",
  59. }),
  60. ).toThrow(expect.objectContaining({ code: "EACCES" }));
  61. expect(() =>
  62. injector.maybeThrow({
  63. operation: "writeFile",
  64. path: "/tmp/home/.zcode/v2/provider_config.json",
  65. }),
  66. ).not.toThrow();
  67. expect(injector.getHits()).toHaveLength(1);
  68. });
  69. it("supports unlimited matches when maxMatches is zero", () => {
  70. const injector = createFsFaultInjector([
  71. {
  72. code: "ENOSPC",
  73. id: "D02-workspace-write-enospc",
  74. maxMatches: 0,
  75. operations: ["rename"],
  76. pathRegex: "/workspace/.+\\.tmp$",
  77. },
  78. ]);
  79. for (let index = 0; index < 3; index += 1) {
  80. expect(() =>
  81. injector.maybeThrow({
  82. operation: "rename",
  83. path: `/tmp/workspace/file-${index}.tmp`,
  84. }),
  85. ).toThrow(expect.objectContaining({ code: "ENOSPC" }));
  86. }
  87. expect(injector.getHits().map((hit) => hit.matchIndex)).toEqual([1, 2, 3]);
  88. });
  89. it("keeps env-based injection disabled outside test mode unless explicitly allowed", () => {
  90. const rawRules = JSON.stringify([
  91. {
  92. code: "ENOSPC",
  93. id: "D01-session-history-enospc",
  94. },
  95. ]);
  96. const productionInjector = createFsFaultInjectorFromEnv({
  97. NODE_ENV: "production",
  98. ZCODE_E2E_FS_FAULTS: rawRules,
  99. ZCODE_ENV: "production",
  100. });
  101. expect(productionInjector.isEnabled()).toBe(false);
  102. const testInjector = createFsFaultInjectorFromEnv({
  103. NODE_ENV: "production",
  104. ZCODE_E2E_FS_FAULTS: rawRules,
  105. ZCODE_ENV: "test",
  106. });
  107. expect(testInjector.isEnabled()).toBe(true);
  108. const explicitlyAllowedInjector = createFsFaultInjectorFromEnv({
  109. NODE_ENV: "production",
  110. ZCODE_E2E_FS_FAULTS: rawRules,
  111. ZCODE_E2E_FS_FAULTS_ALLOW: "1",
  112. ZCODE_ENV: "production",
  113. });
  114. expect(explicitlyAllowedInjector.isEnabled()).toBe(true);
  115. });
  116. it("rejects malformed env rules early as infra errors", () => {
  117. expect(() => parseFsFaultRulesFromEnvValue("{")).toThrow(/Invalid ZCODE_E2E_FS_FAULTS/);
  118. expect(() => parseFsFaultRulesFromEnvValue("{}")).toThrow(/expected a JSON array/);
  119. expect(() =>
  120. createFsFaultInjector([
  121. {
  122. code: "ENOSPC",
  123. id: "bad-operation",
  124. operations: ["not-a-real-operation" as "writeFile"],
  125. },
  126. ]),
  127. ).toThrow(/unsupported operation/);
  128. });
  129. });