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
43 changes: 37 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:

permissions:
contents: read
actions: write

jobs:
test-typescript:
Expand Down Expand Up @@ -48,6 +49,22 @@ jobs:
name: GitHub Actions Test
runs-on: ubuntu-latest

env:
DURATION: 10s

services:
postgres:
image: postgres:16
env:
POSTGRES_USER: stroppy
POSTGRES_PASSWORD: stroppy
POSTGRES_DB: stroppy_test
ports:
- 5432:5432
options: >-
--health-cmd="pg_isready -U stroppy -d stroppy_test"
--health-interval=10s --health-timeout=5s --health-retries=5

steps:
- name: Checkout
id: checkout
Expand All @@ -56,12 +73,26 @@ jobs:
- name: Test Local Action
id: test-action
uses: ./
continue-on-error: true
with:
preset: simple
driver-url: 'postgres://localhost:5432/testdb'
preset: tpcc
driver-url: 'postgres://stroppy:stroppy@localhost:5432/stroppy_test'

- name: Verify action ran
- name: Verify outputs
env:
EXIT_CODE: ${{ steps.test-action.outputs.exit-code }}
RESULTS_FILE: ${{ steps.test-action.outputs.results-file }}
ARTIFACT_ID: ${{ steps.test-action.outputs.artifact-id }}
run: |
echo "Exit code output: ${{ steps.test-action.outputs.exit-code }}"
echo "Action outcome: ${{ steps.test-action.outcome }}"
echo "Exit code: ${EXIT_CODE}"
echo "Results file: ${RESULTS_FILE}"
echo "Artifact ID: ${ARTIFACT_ID}"

if [ "${EXIT_CODE}" != "0" ]; then
echo "::error::Benchmark exited with non-zero code"
exit 1
fi

if [ -z "${RESULTS_FILE}" ]; then
echo "::error::No results file produced"
exit 1
fi
12 changes: 12 additions & 0 deletions __fixtures__/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@ export const group = jest
return fn()
}
)

const summaryMock = {
addHeading: jest.fn().mockReturnThis(),
addTable: jest.fn().mockReturnThis(),
addRaw: jest.fn().mockReturnThis(),
addCodeBlock: jest.fn().mockReturnThis(),
addSeparator: jest.fn().mockReturnThis(),
addBreak: jest.fn().mockReturnThis(),
write: jest.fn<() => Promise<unknown>>().mockResolvedValue(undefined)
}

export const summary = summaryMock
104 changes: 98 additions & 6 deletions __tests__/results.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,73 @@ import {
DefaultArtifactClient,
mockUploadArtifact
} from '../__fixtures__/artifact.js'
import type { RunConfig } from '../src/run.js'

jest.unstable_mockModule('@actions/core', () => core)
jest.unstable_mockModule('@actions/artifact', () => ({ DefaultArtifactClient }))

const { collectResults } = await import('../src/results.js')

const baseConfig: RunConfig = {
script: '',
sqlFile: '',
preset: 'tpcb',
driverUrl: 'postgres://user:pass@localhost/test',
scaleFactor: '',
duration: '10s',
vus: '2',
logLevel: 'info',
k6Args: ''
}

describe('results.ts', () => {
beforeEach(() => {
core.summary.addHeading.mockReturnValue(core.summary)
core.summary.addTable.mockReturnValue(core.summary)
core.summary.addRaw.mockReturnValue(core.summary)
core.summary.addCodeBlock.mockReturnValue(core.summary)
core.summary.addSeparator.mockReturnValue(core.summary)
core.summary.write.mockResolvedValue(undefined)
})

afterEach(() => {
jest.resetAllMocks()
})

it('sets exit-code output', async () => {
await collectResults({ exitCode: 0, resultsFile: '' }, 'stroppy-results')
await collectResults(
baseConfig,
{ exitCode: 0, resultsFile: '' },
'stroppy-results',
'v1.0.0'
)

expect(core.setOutput).toHaveBeenCalledWith('exit-code', '0')
})

it('warns when no results file', async () => {
await collectResults({ exitCode: 0, resultsFile: '' }, 'stroppy-results')
await collectResults(
baseConfig,
{ exitCode: 0, resultsFile: '' },
'stroppy-results',
'v1.0.0'
)

expect(core.warning).toHaveBeenCalledWith('No results file was produced')
expect(mockUploadArtifact).not.toHaveBeenCalled()
})

it('uploads artifact when results file exists', async () => {
const tmpFile = path.join(os.tmpdir(), 'test-results.json')
fs.writeFileSync(tmpFile, '{}')
fs.writeFileSync(tmpFile, '{"metrics": "ok"}')

mockUploadArtifact.mockResolvedValueOnce({ id: 42, size: 100 })

await collectResults(
baseConfig,
{ exitCode: 0, resultsFile: tmpFile },
'stroppy-results'
'stroppy-results',
'v1.0.0'
)

expect(core.setOutput).toHaveBeenCalledWith('results-file', tmpFile)
Expand All @@ -49,7 +83,6 @@ describe('results.ts', () => {
path.dirname(tmpFile)
)
expect(core.setOutput).toHaveBeenCalledWith('artifact-id', '42')
expect(core.notice).toHaveBeenCalled()

fs.unlinkSync(tmpFile)
})
Expand All @@ -61,8 +94,10 @@ describe('results.ts', () => {
mockUploadArtifact.mockRejectedValueOnce(new Error('upload failed'))

await collectResults(
baseConfig,
{ exitCode: 0, resultsFile: tmpFile },
'stroppy-results'
'stroppy-results',
'v1.0.0'
)

expect(core.warning).toHaveBeenCalledWith(
Expand All @@ -72,4 +107,61 @@ describe('results.ts', () => {

fs.unlinkSync(tmpFile)
})

it('writes job summary with parsed metrics tables', async () => {
const tmpFile = path.join(os.tmpdir(), 'test-summary.json')
const results = {
metrics: {
iterations: { count: 44302, rate: 2946.44 },
iteration_duration: {
avg: 22.38,
min: 0.28,
med: 1.81,
max: 9612.75,
'p(90)': 13.66,
'p(95)': 32.26
},
vus: { value: 54, min: 0, max: 99 },
insert_error_rate: { passes: 0, fails: 5, value: 0 }
}
}
fs.writeFileSync(tmpFile, JSON.stringify(results))

mockUploadArtifact.mockResolvedValueOnce({ id: 7, size: 50 })

await collectResults(
baseConfig,
{ exitCode: 0, resultsFile: tmpFile },
'stroppy-results',
'v2.3.0'
)

expect(core.summary.addHeading).toHaveBeenCalledWith(
'Stroppy Benchmark Results',
2
)
// Config table + throughput + latency + other = 4 addTable calls
expect(core.summary.addTable).toHaveBeenCalledTimes(4)
expect(core.summary.addHeading).toHaveBeenCalledWith('Throughput', 3)
expect(core.summary.addHeading).toHaveBeenCalledWith('Latency (ms)', 3)
expect(core.summary.addHeading).toHaveBeenCalledWith('Other', 3)
expect(core.summary.write).toHaveBeenCalled()

fs.unlinkSync(tmpFile)
})

it('masks password in driver URL summary', async () => {
await collectResults(
baseConfig,
{ exitCode: 1, resultsFile: '' },
'stroppy-results',
'v1.0.0'
)

const tableCall = core.summary.addTable.mock.calls[0][0] as string[][]
const urlRow = tableCall.find((r) => r[0] === 'Driver URL')
expect(urlRow).toBeDefined()
expect(urlRow![1]).not.toContain('pass')
expect(urlRow![1]).toContain('***')
})
})
37 changes: 22 additions & 15 deletions __tests__/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,34 @@ describe('run.ts', () => {
})

describe('buildK6Args', () => {
it('builds args with all options', () => {
const args = buildK6Args('10', '30m', '--no-summary', '/tmp/results.json')
it('builds args with extra k6 args', () => {
const args = buildK6Args('--no-summary', '/tmp/results.json')
expect(args).toEqual([
'--vus',
'10',
'--duration',
'30m',
'--out',
'json=/tmp/results.json',
'--summary-mode',
'full',
'--summary-export',
'/tmp/results.json',
'--no-summary'
])
})

it('builds args with only results file', () => {
const args = buildK6Args('', '', '', '/tmp/results.json')
expect(args).toEqual(['--out', 'json=/tmp/results.json'])
const args = buildK6Args('', '/tmp/results.json')
expect(args).toEqual([
'--summary-mode',
'full',
'--summary-export',
'/tmp/results.json'
])
})

it('splits k6Args by whitespace', () => {
const args = buildK6Args('', '', '--tag env=ci --quiet', '/tmp/r.json')
const args = buildK6Args('--tag env=ci --quiet', '/tmp/r.json')
expect(args).toEqual([
'--out',
'json=/tmp/r.json',
'--summary-mode',
'full',
'--summary-export',
'/tmp/r.json',
'--tag',
'env=ci',
'--quiet'
Expand All @@ -72,8 +77,9 @@ describe('run.ts', () => {

expect(env.DRIVER_URL).toBe('postgres://localhost/test')
expect(env.LOG_LEVEL).toBe('info')
expect(env.LOG_MODE).toBe('ci')
expect(env.LOG_MODE).toBe('development')
expect(env.SCALE_FACTOR).toBeUndefined()
expect(env.VUS).toBeUndefined()
})

it('sets optional env vars when provided', () => {
Expand All @@ -84,13 +90,14 @@ describe('run.ts', () => {
driverUrl: 'postgres://localhost/test',
scaleFactor: '10',
duration: '1h',
vus: '',
vus: '4',
logLevel: 'debug',
k6Args: ''
})

expect(env.SCALE_FACTOR).toBe('10')
expect(env.DURATION).toBe('1h')
expect(env.VUS).toBe('4')
})
})

Expand Down
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading