diff --git a/__tests__/adapters/app/cron.test.ts b/__tests__/adapters/app/cron.test.ts index d937906c..42d2bbaf 100644 --- a/__tests__/adapters/app/cron.test.ts +++ b/__tests__/adapters/app/cron.test.ts @@ -104,6 +104,25 @@ describe('runScheduledJobs', () => { expect(lines.at(-1)).toMatchObject({ level: 'info', message: 'scheduled jobs ran for 4 repositories, 2 failed' }) }) + it('reports a repository whose handler throws a non-Error as its string form', async () => { + const { lines, sink } = recordingSink() + const report = await runScheduledJobs({ + settings, + tokens: staticTokens(), + installations: listing(...targets), + sink, + dispatch: async (context) => { + if (context.repo.repo === 'docs') { + // eslint-disable-next-line no-throw-literal + throw { code: 'ECONNRESET' } + } + }, + }) + + expect(report).toEqual({ repositories: 4, failed: ['globex/docs'] }) + expect(lines).toContainEqual({ level: 'error', event: 'schedule', repo: 'globex/docs', message: '[object Object]' }) + }) + it('reports a repository whose token cannot be minted', async () => { const report = await runScheduledJobs({ settings, diff --git a/__tests__/adapters/app/platform.test.ts b/__tests__/adapters/app/platform.test.ts index 2596a1b6..32d02090 100644 --- a/__tests__/adapters/app/platform.test.ts +++ b/__tests__/adapters/app/platform.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { describe, expect, it, vi } from 'vitest' import { memoryDeliveryStore } from '../../../src/adapters/app/dedupe' import { appPlatform, contextFromDelivery } from '../../../src/adapters/app/platform' @@ -70,6 +70,26 @@ describe('appPlatform', () => { it('refuses a payload without an installation', () => { expect(() => appPlatform(settings, staticTokens(), { ...delivery, payload: structuredClone(issueCommentEvent) }, () => {})).toThrow(/no installation/) }) + + it('defers in place by default: waits the whole delay, then continues', async () => { + vi.useFakeTimers() + try { + const { platform } = appPlatform(settings, staticTokens(), delivery, () => {}) + let outcome: 'continue' | 'deferred' | undefined + const waiting = platform.defer(5_000, { kind: 'require-matching-label', context: platform.context() }).then((result) => { + outcome = result + }) + + await vi.advanceTimersByTimeAsync(4_999) + expect(outcome).toBeUndefined() + await vi.advanceTimersByTimeAsync(1) + await waiting + expect(outcome).toBe('continue') + } + finally { + vi.useRealTimers() + } + }) }) describe('contextFromDelivery', () => { diff --git a/__tests__/adapters/app/webhookNonErrorThrow.test.ts b/__tests__/adapters/app/webhookNonErrorThrow.test.ts new file mode 100644 index 00000000..0ea75c26 --- /dev/null +++ b/__tests__/adapters/app/webhookNonErrorThrow.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, it, vi } from 'vitest' + +import { receiveWebhook } from '../../../src/adapters/app/webhook' +import issueCommentEvent from '../../fixtures/issues/issueCommentEvent.json' +import { receiver, recordingSink, signedRequest, withInstallation } from './testApp' + +// appPlatform's own guards throw Error instances; this pins the arm that reports a +// thrown non-Error so the sink never sees "[object Object]" or an empty message +vi.mock('../../../src/adapters/app/platform', () => ({ + appPlatform: () => { + // eslint-disable-next-line no-throw-literal + throw 'not an error object' + }, +})) + +describe('receiveWebhook when building the platform throws a non-Error', () => { + it('answers 400 and logs the thrown value as the message', async () => { + const { lines, sink } = recordingSink() + const r = receiver({ sink }) + + const { response, handled } = await receiveWebhook(r, await signedRequest('issue_comment', withInstallation(issueCommentEvent))) + + expect(response.status).toBe(400) + expect(await response.text()).toBe('unusable delivery') + expect(handled).toBeUndefined() + expect(r.dispatched).toEqual([]) + expect(lines).toEqual([{ level: 'warning', delivery: 'delivery-1', event: 'issue_comment', message: 'not an error object' }]) + }) +}) diff --git a/__tests__/server/bundle.test.ts b/__tests__/server/bundle.test.ts new file mode 100644 index 00000000..4cdb5e8e --- /dev/null +++ b/__tests__/server/bundle.test.ts @@ -0,0 +1,112 @@ +import type { ChildProcess } from 'node:child_process' +import { execFileSync, spawn } from 'node:child_process' +import { generateKeyPairSync } from 'node:crypto' +import fs from 'node:fs' +import { createRequire, isBuiltin } from 'node:module' +import os from 'node:os' +import path from 'node:path' +import process from 'node:process' +import { sign } from '@octokit/webhooks-methods' +import { afterAll, beforeAll, describe, expect, it } from 'vitest' + +// The Dockerfile ships `ncc build src/server/main.ts -o dist-server` and runs it on an image +// that never installs node_modules. dist-server/ is not committed, so this builds the same +// bundle into a directory outside the repository (where nothing resolves a leaked require) +// and boots it there, the way the container does. +const ncc = createRequire(import.meta.url).resolve('@vercel/ncc/dist/ncc/cli.js') +const root = path.resolve(__dirname, '../..') +const secret = 'bundle-secret' +const { privateKey } = generateKeyPairSync('rsa', { modulusLength: 2048, privateKeyEncoding: { type: 'pkcs1', format: 'pem' }, publicKeyEncoding: { type: 'pkcs1', format: 'pem' } }) + +let out: string +let child: ChildProcess | undefined +let port: number +const stdout: string[] = [] + +async function signedPost(eventName: string, body: string, signature?: string): Promise { + return fetch(`http://127.0.0.1:${port}/webhook`, { + method: 'POST', + headers: { + 'content-type': 'application/json', + 'x-github-delivery': 'delivery-1', + 'x-github-event': eventName, + ...(signature === undefined ? {} : { 'x-hub-signature-256': signature }), + }, + body, + }) +} + +describe('dist-server/index.js, built and run outside the repository', () => { + beforeAll(async () => { + out = fs.mkdtempSync(path.join(os.tmpdir(), 'prow-server-bundle-')) + execFileSync(process.execPath, [ncc, 'build', 'src/server/main.ts', '-o', out, '--quiet'], { cwd: root, stdio: 'inherit' }) + + port = 18000 + Math.floor(Math.random() * 1000) + child = spawn(process.execPath, ['index.js'], { + cwd: out, + env: { + PATH: process.env.PATH ?? '', + GITHUB_APP_ID: '4242', + GITHUB_APP_PRIVATE_KEY: privateKey, + GITHUB_WEBHOOK_SECRET: secret, + PORT: String(port), + }, + stdio: ['ignore', 'pipe', 'pipe'], + }) + const stderr: string[] = [] + child.stderr!.on('data', chunk => stderr.push(String(chunk))) + await new Promise((resolve, reject) => { + child!.stdout!.on('data', (chunk) => { + stdout.push(String(chunk)) + if (stdout.join('').includes(`listening on :${port}`)) { + resolve() + } + }) + child!.on('exit', code => reject(new Error(`bundle exited with ${code} before listening\n${stderr.join('')}`))) + }) + }, 120_000) + + afterAll(() => { + child?.kill() + fs.rmSync(out, { recursive: true, force: true }) + }) + + it('requires only node built-in modules', () => { + const source = fs.readFileSync(path.join(out, 'index.js'), 'utf8') + const specifiers = [...source.matchAll(/\brequire\((["'])([^"']+)\1\)/g)].map(match => match[2]) + + expect(specifiers.length).toBeGreaterThan(0) + expect(specifiers.filter(specifier => !isBuiltin(specifier))).toEqual([]) + }) + + it('announces the port as a json line on stdout', () => { + const lines = stdout.join('').trim().split('\n').map(line => JSON.parse(line)) + + expect(lines).toContainEqual(expect.objectContaining({ level: 'info', message: `listening on :${port}` })) + expect(lines.every(line => typeof line.time === 'string')).toBe(true) + }) + + it('serves /healthz', async () => { + const response = await fetch(`http://127.0.0.1:${port}/healthz`) + + expect(response.status).toBe(200) + expect(await response.text()).toBe('ok') + }) + + it('answers a signed ping with pong', async () => { + const body = JSON.stringify({ zen: 'bundled' }) + + const response = await signedPost('ping', body, await sign(secret, body)) + + expect(response.status).toBe(200) + expect(await response.text()).toBe('pong') + }) + + it('rejects an unsigned delivery with 401 and logs the rejection', async () => { + const response = await signedPost('ping', JSON.stringify({ zen: 'unsigned' })) + + expect(response.status).toBe(401) + expect(await response.text()).toBe('bad signature') + await expect.poll(() => stdout.join('')).toContain('"message":"rejected: bad signature"') + }) +}) diff --git a/__tests__/server/cronInterval.test.ts b/__tests__/server/cronInterval.test.ts new file mode 100644 index 00000000..1326e33f --- /dev/null +++ b/__tests__/server/cronInterval.test.ts @@ -0,0 +1,98 @@ +import { generateKeyPairSync } from 'node:crypto' +import { http, passthrough } from 'msw' +import { setupServer } from 'msw/node' +import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest' + +import { globalPlatformScope, installPlatformScope } from '../../src/platform' +import { main } from '../../src/server/node' +import * as utils from '../testUtils' + +const api = 'https://ghes.example/api/v3' +const { privateKey } = generateKeyPairSync('rsa', { modulusLength: 2048, privateKeyEncoding: { type: 'pkcs1', format: 'pem' }, publicKeyEncoding: { type: 'pkcs1', format: 'pem' } }) +const env = { GITHUB_APP_ID: '1', GITHUB_APP_PRIVATE_KEY: privateKey, GITHUB_WEBHOOK_SECRET: 's', GITHUB_API_URL: api } + +const server = setupServer() +utils.failTestOnUnhandledRequests(server) + +function stdoutLines(write: { mock: { calls: unknown[][] } }): Record[] { + return write.mock.calls.map(([chunk]) => JSON.parse(String(chunk)) as Record) +} + +describe('main() and PROW_CRON_INTERVAL', () => { + beforeAll(() => server.listen(utils.failOnUnhandledRequest)) + afterEach(() => server.resetHandlers()) + afterAll(() => { + server.close() + installPlatformScope(globalPlatformScope) + }) + + it('logs a tick whose scheduled run rejects and keeps serving and ticking', async () => { + vi.useFakeTimers() + const write = vi.spyOn(process.stdout, 'write').mockImplementation(() => true) + const port = 20000 + Math.floor(Math.random() * 1000) + server.use(http.all(`http://127.0.0.1:${port}/*`, () => passthrough())) + const listening = main({ ...env, PORT: String(port), PROW_CRON_INTERVAL: '1' }) + try { + let polls = 0 + let settled!: () => void + let polled = new Promise((resolve) => { + settled = resolve + }) + server.use(http.get(`${api}/app/installations`, () => { + polls++ + settled() + return new Response('{"message":"boom"}', { status: 500, headers: { 'Content-Type': 'application/json' } }) + })) + + await vi.advanceTimersByTimeAsync(60_000) + await polled + await vi.waitFor(() => expect(stdoutLines(write)).toContainEqual(expect.objectContaining({ level: 'error', event: 'schedule' }))) + + const logged = stdoutLines(write).find(line => line.level === 'error')! + expect(logged.message).toContain('boom') + expect(logged).not.toHaveProperty('repo') + + await listening.ready + const health = await fetch(`http://127.0.0.1:${port}/healthz`) + expect(health.status).toBe(200) + + polled = new Promise((resolve) => { + settled = resolve + }) + await vi.advanceTimersByTimeAsync(60_000) + await polled + await vi.waitFor(() => expect(stdoutLines(write).filter(line => line.level === 'error')).toHaveLength(2)) + expect(polls).toBe(2) + } + finally { + listening.close() + vi.useRealTimers() + write.mockRestore() + } + }) + + it('does not schedule anything when PROW_CRON_INTERVAL is unset', async () => { + vi.useFakeTimers() + const write = vi.spyOn(process.stdout, 'write').mockImplementation(() => true) + const port = 20000 + Math.floor(Math.random() * 1000) + server.use(http.all(`http://127.0.0.1:${port}/*`, () => passthrough())) + let polls = 0 + server.use(http.get(`${api}/app/installations`, () => { + polls++ + return new Response('[]', { headers: { 'Content-Type': 'application/json' } }) + })) + const listening = main({ ...env, PORT: String(port) }) + try { + await listening.ready + await vi.advanceTimersByTimeAsync(24 * 60 * 60_000) + + expect(polls).toBe(0) + expect(stdoutLines(write)).toEqual([expect.objectContaining({ level: 'info', message: `listening on :${port}` })]) + } + finally { + listening.close() + vi.useRealTimers() + write.mockRestore() + } + }) +}) diff --git a/__tests__/server/cronIntervalNonErrorRejection.test.ts b/__tests__/server/cronIntervalNonErrorRejection.test.ts new file mode 100644 index 00000000..4b4d1349 --- /dev/null +++ b/__tests__/server/cronIntervalNonErrorRejection.test.ts @@ -0,0 +1,40 @@ +import { generateKeyPairSync } from 'node:crypto' +import { afterEach, describe, expect, it, vi } from 'vitest' + +import { runScheduledJobs } from '../../src/adapters/app/cron' +import { main } from '../../src/server/node' + +// runScheduledJobs only ever rejects with what the installation lister throws, and +// octokit throws Errors; the module is mocked to reach the String(error) arm. +vi.mock('../../src/adapters/app/cron', async (importOriginal) => { + const actual = await importOriginal() + return { ...actual, runScheduledJobs: vi.fn() } +}) + +const { privateKey } = generateKeyPairSync('rsa', { modulusLength: 2048, privateKeyEncoding: { type: 'pkcs1', format: 'pem' }, publicKeyEncoding: { type: 'pkcs1', format: 'pem' } }) + +describe('main() when a scheduled tick rejects with a non-Error', () => { + afterEach(() => { + vi.useRealTimers() + vi.restoreAllMocks() + }) + + it('logs the rejected value as its string form', async () => { + vi.useFakeTimers() + const write = vi.spyOn(process.stdout, 'write').mockImplementation(() => true) + // eslint-disable-next-line prefer-promise-reject-errors + vi.mocked(runScheduledJobs).mockImplementation(() => Promise.reject({ code: 'ETIMEDOUT' })) + const port = 21000 + Math.floor(Math.random() * 1000) + const listening = main({ GITHUB_APP_ID: '1', GITHUB_APP_PRIVATE_KEY: privateKey, GITHUB_WEBHOOK_SECRET: 's', PORT: String(port), PROW_CRON_INTERVAL: '2' }) + try { + await vi.advanceTimersByTimeAsync(120_000) + + const lines = write.mock.calls.map(([chunk]) => JSON.parse(String(chunk)) as Record) + expect(runScheduledJobs).toHaveBeenCalledTimes(1) + expect(lines).toContainEqual(expect.objectContaining({ level: 'error', event: 'schedule', message: '[object Object]' })) + } + finally { + listening.close() + } + }) +}) diff --git a/__tests__/server/main.test.ts b/__tests__/server/main.test.ts new file mode 100644 index 00000000..55df574a --- /dev/null +++ b/__tests__/server/main.test.ts @@ -0,0 +1,41 @@ +import process from 'node:process' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +import { main, runCronOnce } from '../../src/server/node' + +vi.mock('../../src/server/node', () => ({ + main: vi.fn(() => ({ ready: Promise.resolve(), close: () => {} })), + runCronOnce: vi.fn(async () => 3), +})) + +describe('server entry point', () => { + const argv = process.argv + const exitCode = process.exitCode + + beforeEach(() => vi.resetModules()) + afterEach(() => { + process.argv = argv + process.exitCode = exitCode + }) + + it('starts the server once, from process.env, when loaded', async () => { + process.argv = [argv[0]!, argv[1]!] + + await import('../../src/server/main') + + expect(main).toHaveBeenCalledTimes(1) + expect(main).toHaveBeenCalledWith() + expect(runCronOnce).not.toHaveBeenCalled() + }) + + it('`cron` runs the scheduled jobs once, from process.env, and exits with their status', async () => { + process.argv = [argv[0]!, argv[1]!, 'cron'] + + await import('../../src/server/main') + await vi.waitFor(() => expect(process.exitCode).toBe(3)) + + expect(runCronOnce).toHaveBeenCalledTimes(1) + expect(runCronOnce).toHaveBeenCalledWith() + expect(main).not.toHaveBeenCalled() + }) +}) diff --git a/__tests__/server/node.test.ts b/__tests__/server/node.test.ts new file mode 100644 index 00000000..2c4a013d --- /dev/null +++ b/__tests__/server/node.test.ts @@ -0,0 +1,44 @@ +import { describe, expect, it } from 'vitest' + +import { buildApp, buildReceiver } from '../../src/server/node' +import { signedRequest, withInstallation } from '../adapters/app/testApp' +import issueCommentEvent from '../fixtures/issues/issueCommentEvent.json' + +const secret = 'node-secret' +const env = { GITHUB_APP_ID: '1', GITHUB_APP_PRIVATE_KEY: 'not-a-real-key', GITHUB_WEBHOOK_SECRET: secret } + +describe('buildReceiver', () => { + it('backgrounds the handler by default: the delivery is answered 202 while its dispatch is still running', async () => { + let release!: () => void + const released = new Promise((resolve) => { + release = resolve + }) + let finish!: () => void + const finished = new Promise((resolve) => { + finish = resolve + }) + let dispatched = 0 + const receiver = buildReceiver(env) + receiver.sink = () => {} + receiver.tokens = { installationToken: async () => 'token' } + receiver.dispatch = async () => { + await released + dispatched += 1 + finish() + } + + const response = await buildApp(receiver).fetch(await signedRequest('issue_comment', withInstallation(issueCommentEvent), { secret })) + + expect(response.status).toBe(202) + expect(dispatched).toBe(0) + release() + await finished + expect(dispatched).toBe(1) + }) + + it('does not throw when the default background is handed settled work', () => { + const receiver = buildReceiver(env) + + expect(() => receiver.background(Promise.resolve())).not.toThrow() + }) +}) diff --git a/__tests__/server/portDefault.test.ts b/__tests__/server/portDefault.test.ts new file mode 100644 index 00000000..56d05d44 --- /dev/null +++ b/__tests__/server/portDefault.test.ts @@ -0,0 +1,32 @@ +import { generateKeyPairSync } from 'node:crypto' +import { serve } from '@hono/node-server' +import { describe, expect, it, vi } from 'vitest' + +import { main } from '../../src/server/node' + +// serve is mocked so the default port is observed without binding :8080 on the test host +vi.mock('@hono/node-server', () => ({ + serve: vi.fn((_options: unknown, listening: () => void) => { + listening() + return { close: vi.fn() } + }), +})) + +const { privateKey } = generateKeyPairSync('rsa', { modulusLength: 2048, privateKeyEncoding: { type: 'pkcs1', format: 'pem' }, publicKeyEncoding: { type: 'pkcs1', format: 'pem' } }) + +describe('main() without PORT', () => { + it('listens on 8080', async () => { + const write = vi.spyOn(process.stdout, 'write').mockImplementation(() => true) + const listening = main({ GITHUB_APP_ID: '1', GITHUB_APP_PRIVATE_KEY: privateKey, GITHUB_WEBHOOK_SECRET: 's' }) + try { + await listening.ready + + expect(serve).toHaveBeenCalledWith(expect.objectContaining({ port: 8080 }), expect.any(Function)) + expect(write.mock.calls.map(([chunk]) => JSON.parse(String(chunk)))).toEqual([expect.objectContaining({ message: 'listening on :8080' })]) + } + finally { + listening.close() + write.mockRestore() + } + }) +}) diff --git a/vitest.config.mjs b/vitest.config.mjs index 52c1bf50..6f7934c1 100644 --- a/vitest.config.mjs +++ b/vitest.config.mjs @@ -24,10 +24,14 @@ export default defineConfig({ // Regression floor set just under what main reaches (100 / 99.7 / 100 / 100 // at 1062ce7) so a drop of about one point fails build-test. thresholds: { - lines: 99, - branches: 99, - functions: 99, - statements: 99, + 'lines': 99, + 'branches': 99, + 'functions': 99, + 'statements': 99, + // vitest.e2e-coverage.config.mjs leaves the App adapter and its server out of + // the bundle run because the unit suite holds them at 100%; make that a gate. + 'src/adapters/app/**': { lines: 100, branches: 100, functions: 100, statements: 100 }, + 'src/server/**': { lines: 100, branches: 100, functions: 100, statements: 100 }, }, }, },