fix: close record repair and verification gaps

This commit is contained in:
bryanpedini committed 2026-11-19 15:46:12 +01:00
1 parent 9251c85fd8
commit adff599e08
27 files changed
+787 -153

No files matched your search

+65 -13
View File
@@ -9,7 +9,7 @@ import { waitForZone, waitForZoneGone } from '../harness/dig';
import { runnerAddress, SESSION_COOKIE } from '../harness/env';
import { bulkDeleteBody, expectCheckErrorRowsCarryRaw, expectZoneCreateWarnings, importApplyBody, UNKNOWN_API_OPERATION, ZONE_CREATE_DELEGATIONS_IN } from '../harness/decisions';
import { READ_OPERATIONS } from '../harness/ledger';
import type { AuditRow, RecordRow } from '../harness/mysql';
import type { AuditRow } from '../harness/mysql';
import { newZoneSerial, uiValue, utcDay } from '../harness/records';
import type { RecordInput, Ui } from '../harness/ui';
import { expectServed } from '../harness/served';
@@ -297,26 +297,31 @@ test.describe('core flow', () => {
test('5a. bulk deletion removes the selected records in one transaction', async ({ world }) => {
await adminUi.openZone(zone);
const recs: Rec[] = [1, 2, 3].map((i) => ({ name: `bulk${i}`, type: 'TXT', ttl: 300, data: { text: `bulk ${i}` } }));
const remaining: Rec = { name: 'bulk4', type: 'TXT', ttl: 300, data: { text: 'x'.repeat(256) } };
for (const r of recs) {
const res = await adminApi.post(`${zp(zone)}/records`, { name: r.name, type: r.type, ttl: r.ttl, data: r.data });
expect(res.status, res.text).toBe(201);
}
const added = await adminApi.post(`${zp(zone)}/records`, remaining);
expect(added.status, added.text).toBe(201);
const survivor = added.json.record;
await adminUi.openZone(zone);
const rows = await Promise.all(recs.map((r) => storedRow(world, zone, r)));
const change = await begin(world, zone);
const mark = await world.db.auditMaxId();
const res = await adminUi.bulkDelete(recs.map((r) => ({ name: r.name, type: r.type })), { expectStatus: 200 });
expect(res.json).toEqual(bulkDeleteBody(3));
expect(res.json.warnings).toContainEqual(expect.objectContaining({ code: 'txt_split' }));
await adminUi.expectToast('TXT is served in strings of at most 255 bytes');
await expectBumped(world, zone, change, 'bulk delete');
for (const r of recs) await threeWayGone(world, adminUi, zone, r, null, `bulk deleted ${r.name}`);
const audit = findAudit(await auditSince(world, mark), 'record_bulk_delete', 200);
expect(audit.zone).toBe(zone);
await expectAuditChange(world, zone, audit, change, 'bulk delete');
const survivor = await adminApi.post(`${zp(zone)}/records`, { name: 'bulk4', type: 'TXT', ttl: 300, data: { text: 'bulk 4' } });
const stale = await adminApi.post(`${zp(zone)}/records/delete`, { records: [{ id: survivor.json.record.id, etag: survivor.json.record.etag }, { id: rows[0].id, etag: '"0000"' }] });
const stale = await adminApi.post(`${zp(zone)}/records/delete`, { records: [{ id: survivor.id, etag: survivor.etag }, { id: rows[0].id, etag: '"0000"' }] });
expect(stale.status, 'a stale or missing ETag fails the whole bulk delete').toBe(412);
await storedRow(world, zone, { name: 'bulk4', type: 'TXT', ttl: 300, data: { text: 'bulk 4' } });
const done = await adminApi.post(`${zp(zone)}/records/delete`, { records: [{ id: survivor.json.record.id, etag: survivor.json.record.etag }] });
await storedRow(world, zone, remaining);
const done = await adminApi.post(`${zp(zone)}/records/delete`, { records: [{ id: survivor.id, etag: survivor.etag }] });
expect(done.status).toBe(200);
});
@@ -373,7 +378,9 @@ test.describe('core flow', () => {
const applied = await adminUi.importApply();
expect(applied.status).toBe(200);
expect(applied.json).toEqual(importApplyBody(3, 1, 0));
expect(applied.json.warnings).toContainEqual(expect.objectContaining({ message: 'TXT strings were joined and the plugin will re-split them at 255 bytes' }));
await expect(adminUi.page).toHaveURL(new RegExp(`/zones/${zone.replace(/\./g, '\\.')}$`));
await adminUi.expectToast('TXT strings were joined and the plugin will re-split them at 255 bytes');
await expectBumped(world, zone, change, 'merge import');
const imported: Rec[] = [
{ name: 'imp1', type: 'A', ttl: 600, data: { ip: '192.0.2.40' } },
@@ -426,7 +433,9 @@ test.describe('core flow', () => {
expect(created.status, created.text).toBe(201);
await adminApi.post(`${zp(zoneC)}/records`, { name: 'ns1', type: 'A', ttl: 3600, data: { ip: '192.0.2.1' } });
await waitForZone(world.g, zoneC, { recorder: world.recorder });
const soa = (await world.db.records(zoneC)).find((r) => r.record_type === 'SOA') as RecordRow;
const original = await begin(world, zoneC);
const soa = original.rows.find((r) => r.record_type === 'SOA')!;
const duplicateSOA = JSON.stringify({ ...JSON.parse(soa.content!), ns: 'duplicate.example.net.' });
const id = async (name: string, type: string, ttl: number | null, content: string | null, z = zoneC) => world.db.insertRaw({ zone: z, name, ttl, content, record_type: type });
const broken: Record<string, number> = {
ttl_null: await id('tnull', 'A', null, '{"ip":"192.0.2.60"}'),
@@ -438,12 +447,12 @@ test.describe('core flow', () => {
name_not_fqdn: await id('nfq', 'CNAME', 3600, '{"host":"target.example.net"}'),
mx_priority_key: await id('mxp', 'MX', 3600, '{"host":"mail.example.net.","priority":10}'),
soa_not_apex: await id('soasub', 'SOA', 3600, soa.content),
soa_multiple: await id('', 'SOA', 3600, soa.content),
soa_multiple: await id('', 'SOA', 3600, duplicateSOA),
cname_apex: await id('', 'CNAME', 3600, '{"host":"target.example.net."}'),
cname_conflict: await id('both', 'CNAME', 3600, '{"host":"target.example.net."}'),
pack_failed: await id('cut', 'NS', 3600, '{"host":"ns.example.net"}'),
};
await id('both', 'A', 3600, '{"ip":"192.0.2.63"}');
const conflictAddress = await id('both', 'A', 3600, '{"ip":"192.0.2.63"}');
const codes = Object.keys(broken);
const brokenRows = await world.db.records(zoneC);
for (const [name, type, rcode] of [
@@ -464,6 +473,8 @@ test.describe('core flow', () => {
zone_case: await id('upper', 'A', 3600, '{"ip":"192.0.2.65"}', upper),
};
await id('', 'NS', 3600, `{"host":"ns1.${nosoa}"}`, nosoa);
await id('ns1', 'A', 3600, '{"ip":"192.0.2.67"}', nosoa);
const noSOARows = await world.db.records(nosoa);
const check = await adminApi.get(`${zp(zoneC)}/check`);
expect(check.status).toBe(200);
@@ -508,15 +519,31 @@ test.describe('core flow', () => {
expect(zonesTable.find((z) => z.zone === zoneC)?.problems).toMatch(/\d+ errors?/);
expect(zonesTable.find((z) => z.zone === zone)?.problems).not.toMatch(/error/);
await world.db.exec(`DELETE FROM ${world.db.t.records} WHERE id IN (?, ?)`, [broken.soa_not_apex, broken.soa_multiple]);
await world.db.deleteZoneRaw(nodot);
await world.db.deleteZoneRaw(upper);
await world.db.deleteZoneRaw(nosoa);
await adminUi.openZone(zoneC);
await adminUi.deleteRecord({ name: 'soasub', type: 'SOA' }, { expectStatus: 200 });
expect(await world.db.recordById(broken.soa_not_apex)).toBeUndefined();
await adminUi.openZone(zoneC);
expect((await adminUi.problems()).map((p) => p.code)).toEqual(expect.arrayContaining(['soa_multiple', 'cname_apex', 'cname_conflict']));
await adminUi.deleteRecord({ name: '@', type: 'SOA', value: duplicateSOA }, { expectStatus: 200 });
expect(await world.db.recordById(broken.soa_multiple)).toBeUndefined();
const soaSerial = await expectBumped(world, zoneC, original, 'repair leaves one managed SOA');
expect((await world.db.records(zoneC)).filter((row) => row.record_type === 'SOA')).toEqual([
{ ...soa, content: JSON.stringify({ ...JSON.parse(soa.content!), serial: soaSerial }) },
]);
await adminUi.openZone(zoneC);
await adminUi.expectRow({ name: '@', type: 'SOA' });
const beforeCNAMERepair = await begin(world, zoneC);
await adminUi.bulkDelete([{ name: 'both', type: 'CNAME' }, { name: '@', type: 'CNAME' }], { expectStatus: 200 });
expect(await world.db.recordById(broken.cname_apex)).toBeUndefined();
expect(await world.db.recordById(broken.cname_conflict)).toBeUndefined();
await adminUi.expectNoRow({ name: 'both', type: 'CNAME' });
await adminUi.expectNoRow({ name: '@', type: 'CNAME' });
const preservedAddress = await threeWay(world, adminUi, zoneC, { name: 'both', type: 'A', ttl: 3600, data: { ip: '192.0.2.63' } }, beforeCNAMERepair, 'repair conflicting CNAMEs');
expect(preservedAddress.id).toBe(conflictAddress);
const listed = (await adminApi.get(`${zp(zoneC)}/records`)).json.records as Array<{ id: number; name: string; type: string; data: unknown; raw: string | null; problems: Array<{ severity: string }> }>;
for (const r of listed.filter((x) => x.problems.length > 0 && x.data === null)) expect(r.raw === null || typeof r.raw === 'string').toBe(true);
const storedContent = new Map((await world.db.records(zoneC)).map((r) => [r.id, r.content]));
const storedContent = new Map((await world.db.records()).map((r) => [r.id, r.content]));
expectCheckErrorRowsCarryRaw(listed, (i) => storedContent.get(listed[i].id) ?? null);
const repair = async (what: string, match: { name: string; type: string }, inp: RecordInput & { selectType?: string }, rec: Rec) => {
const apiRow = listed.find((x) => x.name === match.name && x.type === match.type);
@@ -550,12 +577,37 @@ test.describe('core flow', () => {
await repair('name_not_fqdn', { name: 'nfq', type: 'CNAME' }, { name: 'nfq', type: 'CNAME', ttl: '3600', fields: { host: 'target.example.net' } }, { name: 'nfq', type: 'CNAME', ttl: 3600, data: { host: 'target.example.net.' } });
await repair('mx_priority_key', { name: 'mxp', type: 'MX' }, { name: 'mxp', type: 'MX', ttl: '3600', fields: { preference: '10', host: 'mail.example.net' } }, { name: 'mxp', type: 'MX', ttl: 3600, data: { host: 'mail.example.net.', preference: 10 } });
await repair('pack_failed', { name: 'cut', type: 'NS' }, { name: 'cut', type: 'NS', ttl: '3600', fields: { host: 'ns.example.net' } }, { name: 'cut', type: 'NS', ttl: 3600, data: { host: 'ns.example.net.' } });
for (const [name, code, ip] of [['nodot', 'zone_not_fqdn', '192.0.2.64'], ['upper', 'zone_case', '192.0.2.65']]) {
await repair(code, { name, type: 'A' }, { name, type: 'A', ttl: '3600', fields: { ip } }, { name, type: 'A', ttl: 3600, data: { ip } });
expect((await world.db.recordById(other[code]))?.zone).toBe(zoneC);
}
const after = await adminApi.get(`${zp(zoneC)}/check`);
expect((after.json.problems as Array<{ severity: string; code: string }>).filter((p) => p.severity === 'error')).toEqual([]);
expect((await adminUi.problems()).filter((p) => p.severity === 'error')).toEqual([]);
const cliAfter = await world.cli.check(zoneC);
expect(cliAfter.code).toBe(after.json.problems.length === 0 ? 0 : 3);
expect(cliAfter.stdout).not.toMatch(/\berror\b/);
for (const row of original.rows.filter((r) => r.id !== soa.id)) expect(await world.db.recordById(row.id)).toEqual(row);
await servedZone(world, zoneC);
await adminUi.openZone(nosoa);
expect((await adminUi.problems()).some((p) => p.code === 'soa_missing')).toBe(true);
const started = new Date();
const preview = await adminUi.importPreview(nosoa, `@ 3600 IN SOA ns1.${nosoa} hostmaster.${nosoa} 1980010100 3600 600 604800 300`, 'merge');
expect(preview.status, JSON.stringify(preview.json)).toBe(200);
expect(preview.json).toMatchObject({ added: [expect.objectContaining({ name: '', type: 'SOA', ttl: 3600 })], updated: [], deleted: [] });
const applied = await adminUi.importApply();
expect(applied.status).toBe(200);
expect(applied.json).toEqual(importApplyBody(1, 0, 0));
await adminUi.openZone(nosoa);
await waitForZone(world.g, nosoa, { recorder: world.recorder });
const restoredSerial = (await world.db.soaSerial(nosoa))!;
expect([utcDay(started), utcDay(new Date())]).toContain(restoredSerial);
await threeWay(world, adminUi, nosoa, { name: '', type: 'SOA', ttl: 3600, data: { ns: `ns1.${nosoa}`, MBox: `hostmaster.${nosoa}`, serial: restoredSerial, refresh: 3600, retry: 600, expire: 604800, minttl: 300 } }, null, 'merge-import restores missing SOA');
expect((await world.db.records(nosoa)).filter((row) => row.record_type !== 'SOA')).toEqual(noSOARows);
expect((await adminApi.get(`${zp(nosoa)}/check`)).json.problems).toEqual([]);
expect(await adminUi.problems()).toEqual([]);
await servedZone(world, nosoa);
});
test('noncanonical zones and selected SOA rows can be repaired through the UI', async ({ world }) => {