iterate.com / cloud

git clone https://git.iterate.com/repos/cloud

cloud / internal-packages / installer / test / records-free.mjsmain at aacbc74

// records-free.mjs — the run's check of the names a stack needs (container/run.sh
// `records_free`, which says why), run in bash against stand-ins for Cloudflare's answers.
// Run: node test/records-free.mjs
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { test } from "node:test";

const MINE = "iterate-a-boundary";
const record = (type, name, content, proxied = true) => ({ type, name, content, proxied });
const route = (pattern, script) => ({ id: pattern, pattern, script });

/** `records_free` of run.sh, with the zone's DNS records and Workers routes. `{ code, out }`. */
function check({ records = [], routes = [] }) {
  const run = readFileSync(new URL("../container/run.sh", import.meta.url), "utf8");
  const fn = new RegExp("^records_free\\(\\) \\{\\n[\\s\\S]*?^\\}", "m").exec(run)[0];
  const dir = mkdtempSync(join(tmpdir(), "records-free-"));
  writeFileSync(join(dir, "records.json"), JSON.stringify(records));
  writeFileSync(join(dir, "routes.json"), JSON.stringify({ success: !!routes, result: routes }));
  writeFileSync(join(dir, "routes.code"), routes ? "200" : "500");
  const script = `
ITERATE_ZONE=a.com
ITERATE_ZONE_ID=z
ITERATE_NAME=a
cf_get() {
  case $1 in
    /zones/z/workers/routes) cat "${dir}/routes.json"; printf '\\n%s' "$(cat "${dir}/routes.code")" ;;
    /zones/z/dns_records\\?per_page=100\\&name=*)
      jq -c --arg n "\${1##*&name=}" '{ success: true, result: [.[] | select(.name == $n)] }' "${dir}/records.json"; printf '\\n200' ;;
    *) echo "another read: $1" >&2; exit 9 ;;
  esac
}
${fn}
records_free
`;
  const ran = spawnSync("bash", ["--norc", "-c", script], {
    encoding: "utf8",
    stdio: ["ignore", "pipe", "pipe"],
    env: { PATH: process.env.PATH },
  });
  assert.equal(ran.stderr, "");
  return { code: ran.status, out: ran.stdout };
}

test("free names, or the stack's own AAAA 100::, pass", () => {
  assert.deepEqual(check({}), { code: 0, out: "" });
  assert.deepEqual(check({ records: [record("AAAA", "a.com", "100::")] }), { code: 0, out: "" });
});

test("a record that points somewhere else stops the run and is named", () => {
  const { code, out } = check({ records: [record("CNAME", "*.a.com", "shop.example")] });
  assert.equal(code, 1);
  assert.match(
    out,
    /^Stopped: a\.com already has DNS records that point somewhere else: CNAME \*\.a\.com → shop\.example\./,
  );
});

test("a proxied placeholder under the stack's own route is the stack's name, not an origin", () => {
  const records = [record("A", "a.com", "192.0.2.1"), record("AAAA", "a.com", "100::")];
  const routes = [route("a.com/*", MINE), route("*.a.com/*", MINE)];
  assert.deepEqual(check({ records, routes }), { code: 0, out: "" });
});

test("the same placeholder stops the run when no route of the stack holds that name", () => {
  const records = [record("A", "a.com", "192.0.2.1")];
  for (const routes of [[], [route("a.com/*", "shop")], [route("*.a.com/*", MINE)]]) {
    const { code, out } = check({ records, routes });
    assert.equal(code, 1);
    assert.match(out, /A a\.com → 192\.0\.2\.1/);
  }
});

test("under the stack's own route, an unproxied record or a real address still stops the run", () => {
  const routes = [route("a.com/*", MINE)];
  for (const each of [
    record("A", "a.com", "192.0.2.1", false),
    record("A", "a.com", "203.0.113.7"),
  ]) {
    const { code, out } = check({ records: [each], routes });
    assert.equal(code, 1, JSON.stringify(each));
    assert.match(out, /^Stopped: /);
  }
});

test("a routes read that does not answer 200 changes nothing: the placeholder stops the run", () => {
  const { code } = check({ records: [record("A", "a.com", "192.0.2.1")], routes: null });
  assert.equal(code, 1);
});