98 lines
3.3 KiB
JavaScript
98 lines
3.3 KiB
JavaScript
import { spawnSync } from 'node:child_process'
|
|
import { randomUUID } from 'node:crypto'
|
|
import assert from 'node:assert/strict'
|
|
|
|
const image = process.env.SYNCOVA_IMAGE || 'syncova-ticket:local'
|
|
const name = `syncova-image-check-${randomUUID()}`
|
|
const password = `Test-${randomUUID()}!`
|
|
const env = { ...process.env, ADMIN_EMAIL: 'image-check@example.invalid', ADMIN_PASSWORD: password }
|
|
function docker(args, required = true) {
|
|
const result = spawnSync('docker', args, { encoding: 'utf8', timeout: 60000, env })
|
|
if (required && result.status !== 0)
|
|
throw Error(
|
|
(result.stderr || result.error?.message || 'Docker fehlgeschlagen').replaceAll(
|
|
password,
|
|
'[redacted]',
|
|
),
|
|
)
|
|
return result
|
|
}
|
|
const configuration = JSON.parse(docker(['image', 'inspect', image]).stdout)[0].Config
|
|
assert.equal(configuration.User, 'node')
|
|
assert(
|
|
!configuration.Env.some((v) => /^(ADMIN_PASSWORD|REGISTRY_TOKEN|SMTP_ENCRYPTION_KEY)=.+/.test(v)),
|
|
'Keine Zugangsdaten im Image',
|
|
)
|
|
docker([
|
|
'run',
|
|
'--rm',
|
|
'--entrypoint',
|
|
'node',
|
|
image,
|
|
'--input-type=module',
|
|
'-e',
|
|
`
|
|
import assert from 'node:assert/strict'; import fs from 'node:fs';
|
|
assert.deepEqual(fs.readdirSync('/app/data'), [], 'Image muss ein leeres Datenverzeichnis enthalten');
|
|
for(const file of ['/app/.env','/app/.env.docker','/app/.git','/app/docs','/app/tests']) assert(!fs.existsSync(file),file+' darf nicht im Laufzeitimage liegen');
|
|
`,
|
|
])
|
|
try {
|
|
docker([
|
|
'run',
|
|
'--detach',
|
|
'--name',
|
|
name,
|
|
'--read-only',
|
|
'--tmpfs',
|
|
'/app/data:rw,noexec,nosuid,uid=1000,gid=1000,mode=0700',
|
|
'--tmpfs',
|
|
'/tmp:rw,noexec,nosuid',
|
|
'--env',
|
|
'ADMIN_EMAIL',
|
|
'--env',
|
|
'ADMIN_PASSWORD',
|
|
image,
|
|
])
|
|
let healthy = false
|
|
for (let attempt = 0; attempt < 30; attempt++) {
|
|
const result = docker(
|
|
[
|
|
'exec',
|
|
name,
|
|
'node',
|
|
'-e',
|
|
"fetch('http://127.0.0.1:3001/healthz').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))",
|
|
],
|
|
false,
|
|
)
|
|
if (result.status === 0) {
|
|
healthy = true
|
|
break
|
|
}
|
|
await new Promise((resolve) => setTimeout(resolve, 500))
|
|
}
|
|
assert(healthy, 'Produktionsimage muss starten und gesund werden')
|
|
docker([
|
|
'exec',
|
|
name,
|
|
'node',
|
|
'--input-type=module',
|
|
'-e',
|
|
`
|
|
import Database from 'better-sqlite3'; import assert from 'node:assert/strict';
|
|
const db=new Database('/app/data/syncova.db',{readonly:true});
|
|
for(const table of ['tickets','messages','attachments','notifications','outbox']) assert.equal(db.prepare('SELECT count(*) n FROM '+table).get().n,0,table+' muss leer sein');
|
|
const users=db.prepare('SELECT email FROM users').all(); assert.equal(users.length,1); assert.equal(users[0].email,process.env.ADMIN_EMAIL);
|
|
db.close();
|
|
const login=await fetch('http://127.0.0.1:3001/api/login',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({email:process.env.ADMIN_EMAIL,password:process.env.ADMIN_PASSWORD})});
|
|
assert.equal(login.status,200,'Bootstrap-Anmeldung muss funktionieren');
|
|
`,
|
|
])
|
|
console.log(
|
|
'Image geprüft: leeres Datenverzeichnis, keine lokalen Daten oder Zugangsdaten, frische Produktion ohne Demotickets/-benutzer, Bootstrap-Anmeldung erfolgreich.',
|
|
)
|
|
} finally {
|
|
docker(['rm', '--force', name], false)
|
|
}
|