Files
mesh-catalog/modules/grafana/test/manifest.test.ts
T

55 lines
2.7 KiB
TypeScript

// grafana runs without a data source, so its influxdb-api is optional (novox/hq ADR 0330, issue 166):
// a mesh without influxdb can assign grafana, and influxdb is never installed for it. While absent, the
// data source and its credential are not delivered, and grafana mounts their directories empty.
import { test } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
type Requirement = string | { name: string; optional?: boolean };
type Resource = { id: string; type: string; path?: string; content?: string; template?: string; volumes?: string[]; "only-when-bound"?: string };
const manifest = JSON.parse(readFileSync(new URL("../module.json", import.meta.url), "utf8")) as {
requires: Requirement[];
resources: Resource[];
build: { artifacts: { name: string; kind: string; from?: string }[] };
};
// A file's text is its template's, in the module's files/ (novox/hq ADR 0349 §2).
const text = (r: Resource): string => {
assert.ok(r.template, `${r.id} names no template`);
return readFileSync(new URL(`../${r.template}`, import.meta.url), "utf8");
};
test("no file carries its text inline: each names its template, and the build packs files/ into one archive", () => {
for (const r of manifest.resources) {
assert.equal(r.content, undefined, `${r.id} keeps its text inline`);
if (r.type === "file") text(r);
}
const packs = manifest.build.artifacts.filter((a) => a.kind === "archive" && a.from === "files");
assert.deepEqual(packs.map((a) => a.name), ["files"]);
});
const optional = (name: string) =>
manifest.requires.some((r) => typeof r !== "string" && r.name === name && r.optional === true);
test("influxdb-api is optional, and route and oidc-client stay hard", () => {
assert.ok(optional("influxdb-api"));
assert.ok(manifest.requires.includes("route"));
assert.ok(manifest.requires.includes("oidc-client"));
});
test("every file naming influxdb-api is delivered only while it is bound", () => {
for (const r of manifest.resources) {
if (r.type === "file" && /\$\{(bound:influxdb-api:|secret:influxdb-api\})/.test(text(r))) {
assert.equal(r["only-when-bound"], "influxdb-api", `${r.id} names influxdb-api and is delivered while it is absent`);
}
}
});
test("the container mounts the data source's directories, never a file that may be absent", () => {
// A bind mount of a missing file makes a directory in its place, which the file could then never be written over.
const server = manifest.resources.find((r) => r.id === "server");
for (const v of server?.volumes ?? []) {
assert.ok(!/influxdb.*\.(yaml|secret)|influxdb-api:/.test(v.split(":")[0]), `${v} mounts an only-when-bound file`);
}
});