Files
websitedev/scripts/check-ports.test.js
T
Do SikiandClaude Opus 5.5 63150ed115
CI Pipeline with Test Management / 🧪 Run Tests & Generate Reports (push) Canceled after 0s
Test Reporting & Gherkin Analysis / 🧪 Run Tests & Generate Reports (push) Canceled after 0s
CI Pipeline with Test Management / 🐳 Docker Integration Tests (push) Canceled after 0s
CI Pipeline with Test Management / 🏗️ Build Docker Image (push) Canceled after 0s
CI Pipeline with Test Management / 📊 Generate Test Summary (push) Canceled after 0s
Test Reporting & Gherkin Analysis / 📊 Analyze Test Coverage (push) Canceled after 0s
Test Reporting & Gherkin Analysis / 🔄 Sync with Linear (push) Canceled after 0s
Test Reporting & Gherkin Analysis / ⚡ Performance Monitoring (push) Canceled after 0s
feat(infra): port pre-check before every port definition (MITHOME-137)
scripts/check-ports.sh checks a port list or a compose file (locally or on a
remote host over SSH) before anything is built or started; a port counts as
owned only by the same compose project and service. deploy.sh runs it before
the build; tests are part of the pre-deploy suite. Found: the dev compose
clashes on llmdev (3001: step, 8081: mozdit staging).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 21:38:43 +02:00

117 lines
4.1 KiB
JavaScript

// Tests for scripts/check-ports.sh: a real listener on a random port is a
// conflict, a free port passes, a port held by the same compose project
// passes, and compose files are parsed without a Docker daemon.
const { test, before, after } = require('node:test')
const assert = require('node:assert/strict')
const net = require('node:net')
const fs = require('node:fs')
const os = require('node:os')
const path = require('node:path')
const { spawn } = require('node:child_process')
const SCRIPT = path.join(__dirname, 'check-ports.sh')
let server
let busyPort
let freePort
async function randomPort() {
const s = net.createServer()
await new Promise((resolve) => s.listen(0, '127.0.0.1', resolve))
const { port } = s.address()
await new Promise((resolve) => s.close(resolve))
return port
}
before(async () => {
server = net.createServer()
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve))
busyPort = server.address().port
freePort = await randomPort()
})
after(() => server.close())
function run(args, env = {}) {
return new Promise((resolve) => {
// No Docker in the test: the publisher lookup is stubbed per test.
const child = spawn('bash', [SCRIPT, ...args], {
env: { ...process.env, CHECK_PORTS_DOCKER_PS: 'true', ...env },
})
let out = ''
child.stdout.on('data', (d) => (out += d))
child.stderr.on('data', (d) => (out += d))
child.on('close', (code) => resolve({ code, out }))
})
}
test('passes for a free port', async () => {
const { code, out } = await run([String(freePort)])
assert.equal(code, 0, out)
assert.match(out, new RegExp(`${freePort}: szabad`))
})
test('fails fast for a port held by another process', async () => {
const { code, out } = await run([String(busyPort)])
assert.equal(code, 1, out)
assert.match(out, new RegExp(`${busyPort}: foglalt`))
assert.match(out, /portregiszterbe/)
})
test('accepts a port held by the same compose project', async () => {
const stub = `printf 'mozdit-app-staging\\twebsitedev\\n' #`
const { code, out } = await run(['--project', 'websitedev', String(busyPort)], { CHECK_PORTS_DOCKER_PS: stub })
assert.equal(code, 0, out)
assert.match(out, /websitedev projekt használja/)
})
test('rejects a port held by a container of another project', async () => {
const stub = `printf 'peakxi-dev-postgres-1\\tpeakxi-dev\\n' #`
const { code, out } = await run(['--project', 'websitedev', String(busyPort)], { CHECK_PORTS_DOCKER_PS: stub })
assert.equal(code, 1, out)
assert.match(out, /peakxi-dev-postgres-1/)
})
test('reads the published ports of a compose file without Docker', async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'check-ports-'))
const file = path.join(dir, 'docker-compose.yml')
fs.writeFileSync(
file,
[
'services:',
' app:',
' ports:',
` - "127.0.0.1:${freePort}:3000" # comment`,
' db:',
' ports:',
` - "\${DB_PORT:-${busyPort}}:5432"`,
' environment:',
' - POSTGRES_DB=mozdit_web',
].join('\n'),
)
// PATH without docker forces the plain parser.
const { code, out } = await run(['--compose', file], { PATH: '/usr/bin:/bin:/usr/sbin:/sbin' })
assert.equal(code, 1, out)
assert.match(out, new RegExp(`${freePort}: szabad`))
assert.match(out, new RegExp(`${busyPort}: foglalt`))
assert.doesNotMatch(out, /POSTGRES/)
})
test('rejects a port held by another service of the same project (compose mode)', async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'check-ports-'))
const file = path.join(dir, 'docker-compose.dev.yml')
fs.writeFileSync(file, ['services:', ' cms:', ' ports:', ` - "127.0.0.1:${busyPort}:8081"`].join('\n'))
const stub = `printf 'mozdit-app-staging\\twebsitedev\\tapp\\n' #`
const { code, out } = await run(['--project', 'websitedev', '--compose', file], {
PATH: '/usr/bin:/bin:/usr/sbin:/sbin',
CHECK_PORTS_DOCKER_PS: stub,
})
assert.equal(code, 1, out)
assert.match(out, /mozdit-app-staging/)
})
test('refuses to run without ports', async () => {
const { code } = await run([])
assert.equal(code, 2)
})