diff --git a/packages/integrations/src/truenas/test/truenas-integration.spec.ts b/packages/integrations/src/truenas/test/truenas-integration.spec.ts index 10c184535..237b41e3f 100644 --- a/packages/integrations/src/truenas/test/truenas-integration.spec.ts +++ b/packages/integrations/src/truenas/test/truenas-integration.spec.ts @@ -277,6 +277,19 @@ describe("TrueNasIntegration", () => { expect(result.fileSystem).toMatchObject(physicalFallbackFileSystem); }); + test("falls back to physical pool space when the dataset response fails schema validation", async () => { + ws.setResponder((method) => + method === "pool.dataset.query" ? { unexpected: "not an array" } : happyResponder(method), + ); + const integration = createIntegration(credentials); + + const result = await integration.getSystemInfoAsync(); + + expect(result.fileSystem).toMatchObject(physicalFallbackFileSystem); + // A malformed payload must not take the rest of the system info down with it. + expect(result).toMatchObject({ cpuTemp: 45, memAvailableInBytes: 6_000_000_000, uptime: 3600 }); + }); + test("falls back to physical pool space when the dataset query is rejected", async () => { ws.setResponder((method) => method === "pool.dataset.query" diff --git a/packages/integrations/src/truenas/truenas-integration.ts b/packages/integrations/src/truenas/truenas-integration.ts index a19c9a558..ffb2225d9 100644 --- a/packages/integrations/src/truenas/truenas-integration.ts +++ b/packages/integrations/src/truenas/truenas-integration.ts @@ -171,15 +171,15 @@ export class TrueNasIntegration extends Integration implements ISystemHealthMoni * A pool is left out when its usable space is unavailable, so the caller keeps reporting the pool with * its physical values instead of dropping it, which would also hide its health status: * - request rejected → method or `extra.properties` unsupported, or the API key lacks the dataset role + * - response rejected → the payload does not match the expected shape * - dataset missing → the pool's root dataset was not returned by the query * - property absent → the dataset does not report `used` / `available`, e.g. while locked */ private async getUsableSpaceByPoolAsync(poolNames: string[]) { const usableSpaceByPool = new Map(); - let datasets: unknown; try { - datasets = await this.requestAsync("pool.dataset.query", [ + const datasets = await this.requestAsync("pool.dataset.query", [ [["id", "in", poolNames]], { extra: { @@ -187,21 +187,19 @@ export class TrueNasIntegration extends Integration implements ISystemHealthMoni }, }, ]); + + for (const dataset of await poolDatasetSchema.parseAsync(datasets)) { + const used = dataset.used?.parsed; + const available = dataset.available?.parsed; + if (typeof used !== "number" || typeof available !== "number") continue; + + usableSpaceByPool.set(dataset.id, { used, available }); + } } catch (error) { logger.warn("Could not retrieve root dataset space, continuing with physical pool space", { url: this.integration.url, error: describeRequestError(error), }); - return usableSpaceByPool; - } - - const parsedDatasets = await poolDatasetSchema.parseAsync(datasets); - for (const dataset of parsedDatasets) { - const used = dataset.used?.parsed; - const available = dataset.available?.parsed; - if (typeof used !== "number" || typeof available !== "number") continue; - - usableSpaceByPool.set(dataset.id, { used, available }); } return usableSpaceByPool;