Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions __tests__/adapters/app/cron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 21 additions & 1 deletion __tests__/adapters/app/platform.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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', () => {
Expand Down
29 changes: 29 additions & 0 deletions __tests__/adapters/app/webhookNonErrorThrow.test.ts
Original file line number Diff line number Diff line change
@@ -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' }])
})
})
112 changes: 112 additions & 0 deletions __tests__/server/bundle.test.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
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<void>((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"')
})
})
98 changes: 98 additions & 0 deletions __tests__/server/cronInterval.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>[] {
return write.mock.calls.map(([chunk]) => JSON.parse(String(chunk)) as Record<string, unknown>)
}

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<void>((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<void>((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()
}
})
})
40 changes: 40 additions & 0 deletions __tests__/server/cronIntervalNonErrorRejection.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof import('../../src/adapters/app/cron')>()
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<string, unknown>)
expect(runScheduledJobs).toHaveBeenCalledTimes(1)
expect(lines).toContainEqual(expect.objectContaining({ level: 'error', event: 'schedule', message: '[object Object]' }))
}
finally {
listening.close()
}
})
})
41 changes: 41 additions & 0 deletions __tests__/server/main.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})
Loading
Loading