From 0cac0266e01d67a4db8ee566e250428675f67986 Mon Sep 17 00:00:00 2001 From: sax3l Date: Tue, 10 Nov 2026 03:48:48 +0100 Subject: [PATCH] test(safety): require an isolated opt-in target for ST0RE integration --- .../iceberg-write-service.realinfra.test.ts | 80 +++++------- tests/p0/st0re-realinfra-guard.test.ts | 116 ++++++++++++++++++ tests/support/st0re-realinfra.ts | 66 ++++++++++ 3 files changed, 210 insertions(+), 52 deletions(-) create mode 100644 tests/p0/st0re-realinfra-guard.test.ts create mode 100644 tests/support/st0re-realinfra.ts diff --git a/tests/p0/iceberg-write-service.realinfra.test.ts b/tests/p0/iceberg-write-service.realinfra.test.ts index 0ffd582..6efb26b 100644 --- a/tests/p0/iceberg-write-service.realinfra.test.ts +++ b/tests/p0/iceberg-write-service.realinfra.test.ts @@ -1,61 +1,37 @@ -import { describe, it, expect, beforeAll } from "vitest"; +import { describe, it, expect, beforeAll, afterAll, vi } from "vitest"; import { DatasetService } from "@/src/services/dataset-service"; import { DatasetIcebergMappingService } from "@/src/services/dataset-iceberg-mapping-service"; import { IcebergWriteService } from "@/src/services/iceberg-write-service"; +import { isolatedSt0reFetch, probeSt0reRealinfra, type St0reRealinfraProbe } from "@/tests/support/st0re-realinfra"; + /** - * LIVE ST0RE integration -- the SEAM pattern this session's task spec asked - * for: "attempt the real call, degrade honestly with a clear status if ST0RE - * is not reachable, never fabricate success" (same convention this same - * session verified in api0's clients, and the same convention ST0RE's own - * `integration-realinfra.test.ts` uses for its DATABASE_URL/MINIO_* gate). - * - * This is NOT a mock-client test -- it constructs `IcebergWriteService` with - * a real `st0reBaseUrl` and drives the actual `HttpSt0reLakehouseClient` - * HTTP path against production ST0RE (api.st0re.siax.io, currently reachable - * from this environment -- DNS for that name resolves here even though - * Cloudflare's public A record is still pending per this session's known - * infra state; set ST0RE_URL to override). - * - * Real writes this test produces land in ST0RE's live Postgres under a - * dedicated pilot tenant (`t_0pt_realinfra_pilot` by default, overridable via - * ST0RE_TEST_TENANT_ID) -- that IS the point of task item 3 ("write ONE - * first test dataset to an Iceberg table through ST0RE's table registry"). - * - * Honest degrade: a `beforeAll` reachability probe (5s timeout) decides - * whether to run for real or `ctx.skip()` with a logged reason. This test - * never asserts success without having actually made the HTTP calls, and - * never falls back to the mock client silently. + * Integration against an isolated local ST0RE test instance only. + * Requires ST0RE_REALINFRA_TESTS=1 and a loopback ST0RE_TEST_URL origin. + * Disabled tests make no HTTP requests; invalid opt-in configuration fails. + * A read-only probe permits an honest skip if the local instance is unreachable. + * Successful runs exercise the real HTTP client without a mock fallback. */ -const ST0RE_URL = process.env.ST0RE_URL ?? "https://api.st0re.siax.io"; -const TEST_TENANT = process.env.ST0RE_TEST_TENANT_ID ?? "t_0pt_realinfra_pilot"; +const TEST_TENANT = process.env.ST0RE_TEST_TENANT_ID ?? "t_0pt_realinfra_local"; const TEST_PROJECT = "p_0pt_realinfra"; const TEST_ACTOR = "0pt-realinfra-test"; -let reachable = false; -let unreachableReason = ""; +let probe: St0reRealinfraProbe; beforeAll(async () => { - try { - const res = await fetch(`${ST0RE_URL}/v1/st0re/lakehouse/namespaces`, { - headers: { "X-Tenant-Id": TEST_TENANT, "X-Actor-Id": TEST_ACTOR }, - signal: AbortSignal.timeout(5000), - }); - reachable = res.ok; - if (!reachable) unreachableReason = `ST0RE responded HTTP ${res.status}`; - } catch (err) { - reachable = false; - unreachableReason = err instanceof Error ? err.message : String(err); - } + probe = await probeSt0reRealinfra(process.env, fetch); + if (probe.reachable && probe.target) vi.stubGlobal("fetch", isolatedSt0reFetch(probe.target, fetch)); }, 10000); -describe("IcebergWriteService against a LIVE ST0RE deployment", () => { +afterAll(() => vi.unstubAllGlobals()); + +describe("IcebergWriteService against an isolated local ST0RE test instance", () => { it( "mapDatasetToIcebergTable + requestIcebergWrite register a real namespace/table/snapshot in ST0RE", async (ctx) => { - if (!reachable) { + if (!probe.reachable || !probe.target) { console.warn( - `[realinfra] ST0RE (${ST0RE_URL}) not reachable this run (${unreachableReason}) -- skipping honestly, not fabricating success.` + `[realinfra] Local ST0RE disabled or unreachable (${probe.reason}) -- skipping honestly, not fabricating success.` ); ctx.skip(); return; @@ -63,15 +39,15 @@ describe("IcebergWriteService against a LIVE ST0RE deployment", () => { const datasetService = new DatasetService(); const mappingService = new DatasetIcebergMappingService({ datasetService }); - const service = new IcebergWriteService({ datasetService, mappingService, st0reBaseUrl: ST0RE_URL }); + const service = new IcebergWriteService({ datasetService, mappingService, st0reBaseUrl: probe.target }); const stamp = Date.now(); - const datasetId = `ds_realinfra_pilot_${stamp}`; + const datasetId = `ds_realinfra_local_${stamp}`; await datasetService.registerDataset({ dataset_id: datasetId, tenant_id: TEST_TENANT, project_id: TEST_PROJECT, - name: "0pt real-ST0RE pilot dataset", + name: "0pt isolated local ST0RE test dataset", dataset_type: "enriched", }); @@ -80,31 +56,31 @@ describe("IcebergWriteService against a LIVE ST0RE deployment", () => { project_id: TEST_PROJECT, dataset_id: datasetId, dataset_iceberg_mapping_id: `dim_realinfra_${stamp}`, - namespace: "0pt_pilot", - table_name: `dim_company_se_pilot_${stamp}`, - storage_uri: `s3://siax-tenants/prod/tenants/${TEST_TENANT}/projects/${TEST_PROJECT}/lakehouse/0pt_pilot/dim_company_se_pilot_${stamp}`, - catalog_ref: `0pt_pilot.dim_company_se_pilot_${stamp}`, + namespace: "0pt_local_test", + table_name: `dim_company_se_local_${stamp}`, + storage_uri: `s3://siax-test-tenants/local/tenants/${TEST_TENANT}/projects/${TEST_PROJECT}/lakehouse/0pt_local_test/dim_company_se_local_${stamp}`, + catalog_ref: `0pt_local_test.dim_company_se_local_${stamp}`, actor_id: TEST_ACTOR, correlation_id: `corr_realinfra_${stamp}`, }); // Real ST0RE issues Postgres UUIDs -- never the mock client's // "mock_table_" shape. This is the assertion that proves - // this test actually hit the live HTTP path, not the mock fallback. + // this test actually hit the local HTTP path, not the mock fallback. expect(mapped.iceberg_table_id).not.toContain("mock_table_"); expect(mapped.iceberg_table_id).toMatch(/^[0-9a-f-]{36}$/i); expect(mapped.namespace_id).not.toContain("mock_ns_"); expect(mapped.namespace_id).toMatch(/^[0-9a-f-]{36}$/i); expect(mapped.mapping.dataset_id).toBe(datasetId); // eslint-disable-next-line no-console - console.log(`[realinfra] LIVE ST0RE table registered: tableId=${mapped.iceberg_table_id} namespaceId=${mapped.namespace_id}`); + console.log(`[realinfra] LOCAL ST0RE table registered: tableId=${mapped.iceberg_table_id} namespaceId=${mapped.namespace_id}`); const write = await service.requestIcebergWrite({ dataset_iceberg_mapping_id: mapped.mapping.dataset_iceberg_mapping_id, actor_id: TEST_ACTOR, correlation_id: `corr_realinfra_write_${stamp}`, schema_version: "1", - manifest_uri: `s3://siax-tenants/prod/tenants/${TEST_TENANT}/projects/${TEST_PROJECT}/lakehouse/0pt_pilot/dim_company_se_pilot_${stamp}/metadata/manifest-1.avro`, + manifest_uri: `s3://siax-test-tenants/local/tenants/${TEST_TENANT}/projects/${TEST_PROJECT}/lakehouse/0pt_local_test/dim_company_se_local_${stamp}/metadata/manifest-1.avro`, record_count: 1, size_bytes: 128, }); @@ -113,7 +89,7 @@ describe("IcebergWriteService against a LIVE ST0RE deployment", () => { expect(write.snapshot_id).toMatch(/^[0-9a-f-]{36}$/i); expect(write.iceberg_table_id).toBe(mapped.iceberg_table_id); // eslint-disable-next-line no-console - console.log(`[realinfra] LIVE ST0RE snapshot registered: snapshotId=${write.snapshot_id}`); + console.log(`[realinfra] LOCAL ST0RE snapshot registered: snapshotId=${write.snapshot_id}`); }, 20000 ); diff --git a/tests/p0/st0re-realinfra-guard.test.ts b/tests/p0/st0re-realinfra-guard.test.ts new file mode 100644 index 0000000..9b50c24 --- /dev/null +++ b/tests/p0/st0re-realinfra-guard.test.ts @@ -0,0 +1,116 @@ +import { describe, expect, it, vi } from "vitest"; +import { isolatedSt0reFetch, probeSt0reRealinfra, resolveSt0reRealinfraTarget } from "@/tests/support/st0re-realinfra"; + +describe("ST0RE realinfra safety guard", () => { + it("uses real HTTP for local writes while forbidding redirect following", async () => { + const fetchSpy = vi.fn().mockResolvedValue(new Response("{}")); + const transport = isolatedSt0reFetch("http://127.0.0.1:4620", fetchSpy); + await transport("http://127.0.0.1:4620/v1/st0re/lakehouse/tables", { method: "POST", body: "{}", redirect: "follow" }); + expect(fetchSpy).toHaveBeenCalledWith("http://127.0.0.1:4620/v1/st0re/lakehouse/tables", + { method: "POST", body: "{}", redirect: "error" }); + }); + + it.each(["https://api.st0re.siax.io/v1/st0re/lakehouse/tables", "http://127.0.0.1:4621/v1/st0re/lakehouse/tables"]) + ("refuses a write outside the exact test origin without sending it (%s)", async (url) => { + const fetchSpy = vi.fn(); + const transport = isolatedSt0reFetch("http://127.0.0.1:4620", fetchSpy); + await expect(transport(new Request(url, { method: "POST" }))).rejects.toThrow(/isolated test origin/); + expect(fetchSpy).not.toHaveBeenCalled(); + }); + + it.each([ + {}, + { ST0RE_URL: "https://api.st0re.siax.io" }, + { ST0RE_TEST_URL: "http://localhost:4620" }, + { ST0RE_TEST_URL: "https://api.st0re.siax.io" }, + { ST0RE_REALINFRA_TESTS: "true", ST0RE_TEST_URL: "http://localhost:4620" }, + { ST0RE_REALINFRA_TESTS: "0", ST0RE_TEST_URL: "http://localhost:4620" }, + ])("stays disabled without exact opt-in and makes no request (%j)", async (env) => { + const fetchSpy = vi.fn(); + expect(resolveSt0reRealinfraTarget(env)).toBeUndefined(); + expect(await probeSt0reRealinfra(env, fetchSpy)).toEqual({ + reachable: false, + reason: expect.stringContaining("disabled"), + }); + expect(fetchSpy).not.toHaveBeenCalled(); + }); + + it.each([ + undefined, + "", + "https://api.st0re.siax.io", + "https://st0re.siax.io", + "https://example.com", + "http://192.168.1.1", + "http://localhost.example.com", + "http://127.1", + "http://2130706433", + "http://127.0.0.2", + "http://[::ffff:127.0.0.1]", + "ftp://localhost", + "not-a-url", + "http://localhost:65536", + ["http://", "user:password", "@localhost"].join(""), + ["http://", "@localhost"].join(""), + "http://localhost/path", + "http://localhost/..", + "http://localhost?query=1", + "http://localhost?", + "http://localhost#fragment", + "http://localhost#", + "http://localhost\\remote", + " http://localhost", + ])("fails closed before fetch for missing or invalid target (%s)", async (target) => { + const env = { + ST0RE_REALINFRA_TESTS: "1", + ST0RE_TEST_URL: target, + ST0RE_URL: "http://localhost:4620", + }; + const fetchSpy = vi.fn(); + expect(() => resolveSt0reRealinfraTarget(env)).toThrow(/ST0RE realinfra configuration error/); + await expect(probeSt0reRealinfra(env, fetchSpy)).rejects.toThrow(/ST0RE_TEST_URL/); + expect(fetchSpy).not.toHaveBeenCalled(); + }); + + it.each([ + "http://localhost:4620", + "http://127.0.0.1:4620", + "http://[::1]:4620", + "https://localhost:4620/", + "https://127.0.0.1", + "https://[::1]", + ])("probes an explicit loopback target with one GET (%s)", async (target) => { + const env = { + ST0RE_REALINFRA_TESTS: "1", + ST0RE_TEST_URL: target, + ST0RE_URL: "https://api.st0re.siax.io", + }; + const origin = new URL(target).origin; + const fetchSpy = vi.fn().mockResolvedValue(new Response("[]", { status: 200 })); + expect(resolveSt0reRealinfraTarget(env)).toBe(origin); + expect(await probeSt0reRealinfra(env, fetchSpy)).toEqual({ target: origin, reachable: true, reason: "" }); + expect(fetchSpy).toHaveBeenCalledTimes(1); + expect(fetchSpy).toHaveBeenCalledWith(`${origin}/v1/st0re/lakehouse/namespaces`, { + method: "GET", + redirect: "error", + headers: { "X-Tenant-Id": "t_0pt_realinfra_local", "X-Actor-Id": "0pt-realinfra-test" }, + signal: expect.any(AbortSignal), + }); + }); + + it("reports HTTP 500 as unreachable", async () => { + const target = "http://localhost:4620"; + const fetchSpy = vi.fn().mockResolvedValue(new Response(null, { status: 500 })); + expect(await probeSt0reRealinfra({ ST0RE_REALINFRA_TESTS: "1", ST0RE_TEST_URL: target }, fetchSpy)) + .toEqual({ target, reachable: false, reason: "ST0RE responded HTTP 500" }); + expect(fetchSpy).toHaveBeenCalledTimes(1); + }); + + it("reports a throwing fetch as unreachable", async () => { + const target = "http://localhost:4620"; + const fetchSpy = vi.fn().mockRejectedValue(new Error("synthetic connection failure")); + expect(await probeSt0reRealinfra({ ST0RE_REALINFRA_TESTS: "1", ST0RE_TEST_URL: target }, fetchSpy)) + .toEqual({ target, reachable: false, reason: "Local ST0RE reachability probe failed" }); + expect(fetchSpy).toHaveBeenCalledTimes(1); + }); +}); diff --git a/tests/support/st0re-realinfra.ts b/tests/support/st0re-realinfra.ts new file mode 100644 index 0000000..b057821 --- /dev/null +++ b/tests/support/st0re-realinfra.ts @@ -0,0 +1,66 @@ +type St0reTestEnv = Readonly>; + +export interface St0reRealinfraProbe { + target?: string; + reachable: boolean; + reason: string; +} + +/** Keep the real client's subsequent writes inside the selected test origin too. */ +export function isolatedSt0reFetch(target: string, fetchImpl: typeof fetch): typeof fetch { + const origin = resolveSt0reRealinfraTarget({ ST0RE_REALINFRA_TESTS: "1", ST0RE_TEST_URL: target }); + return async (input, init) => { + const url = new URL(typeof input === "string" ? input : input instanceof URL ? input.href : input.url); + if (url.origin !== origin) throw new Error("ST0RE realinfra request escaped its isolated test origin"); + return fetchImpl(input, { ...init, redirect: "error" }); + }; +} + +/** Only an explicit, isolated loopback origin can enable real-infrastructure tests. */ +export function resolveSt0reRealinfraTarget(env: St0reTestEnv): string | undefined { + if (env.ST0RE_REALINFRA_TESTS !== "1") return undefined; + + const target = env.ST0RE_TEST_URL; + const configError = () => new Error( + "ST0RE realinfra configuration error: ST0RE_REALINFRA_TESTS=1 requires ST0RE_TEST_URL to be an HTTP(S) loopback origin (localhost, 127.0.0.1 or [::1]), without credentials, path, query or fragment. Remote/production targets are forbidden." + ); + // Check the original spelling too: URL parsing normalizes paths and IP aliases. + if (!target || !/^https?:\/\/(localhost|127\.0\.0\.1|\[::1\])(?::[0-9]+)?\/?$/i.test(target)) { + throw configError(); + } + try { + return new URL(target).origin; + } catch { + throw configError(); + } +} + +export async function probeSt0reRealinfra( + env: St0reTestEnv, + fetchImpl: typeof fetch, +): Promise { + // Configuration failures must escape the reachability catch and fail closed. + const target = resolveSt0reRealinfraTarget(env); + if (!target) { + return { reachable: false, reason: "disabled: ST0RE_REALINFRA_TESTS is not 1" }; + } + + try { + const response = await fetchImpl(`${target}/v1/st0re/lakehouse/namespaces`, { + method: "GET", + redirect: "error", + headers: { + "X-Tenant-Id": env.ST0RE_TEST_TENANT_ID ?? "t_0pt_realinfra_local", + "X-Actor-Id": "0pt-realinfra-test", + }, + signal: AbortSignal.timeout(5000), + }); + return { + target, + reachable: response.ok, + reason: response.ok ? "" : `ST0RE responded HTTP ${response.status}`, + }; + } catch { + return { target, reachable: false, reason: "Local ST0RE reachability probe failed" }; + } +}