Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: recording verification rule #128

Merged
merged 9 commits into from
Sep 6, 2024
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
1 change: 1 addition & 0 deletions forge.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const config: ForgeConfig = {
extraResource: [
'./resources/json_output.py',
'./resources/group_snippet.js',
'./resources/checks_snippet.js',
'./resources/' + getPlatform() + '/' + getArch(),
],
osxSign: {
Expand Down
14 changes: 14 additions & 0 deletions resources/checks_snippet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function handleSummary(data) {
const checks = []
data.root_group.checks.forEach((check) => {
checks.push(check)
})
data.root_group.groups.forEach((group) => {
group.checks.forEach((check) => {
checks.push(check)
})
})
return {
stdout: JSON.stringify(checks),
}
}
39 changes: 38 additions & 1 deletion src/codegen/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CorrelationStateMap, TestRule } from '@/types/rules'
import { generateSequentialInt } from '@/rules/utils'
import { ProxyData } from '@/types'
import { correlationRecording } from '@/test/fixtures/correlationRecording'
import { checksRecording } from '@/test/fixtures/checksRecording'
import { ThinkTime } from '@/types/testOptions'

describe('Code generation', () => {
Expand All @@ -25,7 +26,7 @@ describe('Code generation', () => {
describe('generateScript', () => {
it('should generate script', () => {
const expectedResult = `
import { group, sleep } from 'k6'
import { group, sleep, check } from 'k6'
import http from 'k6/http'

export const options = {}
Expand Down Expand Up @@ -198,6 +199,42 @@ describe('Code generation', () => {
).replace(/\s/g, '')
).toBe(expectedResult.replace(/\s/g, ''))
})

it('should generate checks', () => {
const rules: TestRule[] = [
{
type: 'recording-verification',
id: '1',
},
]
const correlationStateMap: CorrelationStateMap = {}
const sequentialIdGenerator = generateSequentialInt()
const thinkTime: ThinkTime = {
sleepType: 'iterations',
timing: {
type: 'fixed',
value: 1,
},
}

const expectedResult = `
params = { headers: {}, cookies: {} }
url = http.url\`http://test.k6.io/api/v1/foo\`
resp = http.request('POST', url, null, params)
check(resp,{'RecordingVerificationRule:statusmatchesrecording':(r)=>r.status===200,})

`

expect(
generateRequestSnippets(
checksRecording,
rules,
correlationStateMap,
sequentialIdGenerator,
thinkTime
).replace(/\s/g, '')
).toBe(expectedResult.replace(/\s/g, ''))
})
})

describe('generateGroupSnippet', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/constants/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ export const ALL_EXPORTS = {
export const REQUIRED_IMPORTS: ImportModule[] = [
{
path: 'k6',
imports: { type: 'named', imports: [{ name: 'group' }, { name: 'sleep' }] },
imports: {
type: 'named',
imports: [{ name: 'group' }, { name: 'sleep' }, { name: 'check' }],
},
},
{ path: 'k6/http', default: { name: 'http' } },
]
3 changes: 2 additions & 1 deletion src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ export const launchProxy = (
const stdoutReader = readline.createInterface(proxy.stdout)

stdoutReader.on('line', (data) => {
console.log(`stdout: ${data}`)
// console.log(`stdout: ${data}`)

if (data === 'Proxy Started~') {
console.log(data)
onReady?.()
return
}
Expand Down
3 changes: 3 additions & 0 deletions src/rules/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CorrelationStateMap, TestRule } from '@/types/rules'
import { exhaustive } from '../utils/typescript'
import { applyCustomCodeRule } from './customCode'
import { applyCorrelationRule } from './correlation'
import { applyRecordingVerificationRule } from './verification'

export function applyRule(
requestSnippetSchema: RequestSnippetSchema,
Expand All @@ -24,6 +25,8 @@ export function applyRule(
case 'parameterization':
case 'verification':
return requestSnippetSchema
case 'recording-verification':
return applyRecordingVerificationRule(requestSnippetSchema)
default:
return exhaustive(rule)
}
Expand Down
3 changes: 3 additions & 0 deletions src/rules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export function matchFilter(
const { filter } = rule
return new RegExp(filter.path).test(request.url)
}
case 'recording-verification':
// NOTE: no filtering yet on recording verification
return true
default:
return exhaustive(rule)
}
Expand Down
21 changes: 21 additions & 0 deletions src/rules/verification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { RequestSnippetSchema } from '@/types'

export function applyRecordingVerificationRule(
requestSnippetSchema: RequestSnippetSchema
): RequestSnippetSchema {
const response = requestSnippetSchema.data.response

if (!response) {
return requestSnippetSchema
}

const verificationSnippet = `
check(resp, {
'Recording Verification Rule: status matches recording': (r) => r.status === ${response.statusCode},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The checks are now grouped under a single name.

This still respects groups differences, in case you have two groups, it will be two checks

image

})
`
return {
...requestSnippetSchema,
after: [...requestSnippetSchema['after'], verificationSnippet],
}
}
18 changes: 17 additions & 1 deletion src/schemas/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,23 @@ export const CustomCodeSelectorSchema = z.object({
snippet: z.string(),
})

export const StatusCodeSelectorSchema = z.object({
type: z.literal('status-code'),
})

export const SelectorSchema = z.discriminatedUnion('type', [
BeginEndSelectorSchema,
RegexSelectorSchema,
JsonSelectorSchema,
])

export const VerificationRuleSelectorSchema = z.discriminatedUnion('type', [
BeginEndSelectorSchema,
RegexSelectorSchema,
JsonSelectorSchema,
StatusCodeSelectorSchema,
])

export const CorrelationExtractorSchema = z.object({
filter: FilterSchema,
selector: SelectorSchema,
Expand Down Expand Up @@ -90,7 +101,7 @@ export const CorrelationRuleSchema = RuleBaseSchema.extend({
export const VerificationRuleSchema = RuleBaseSchema.extend({
type: z.literal('verification'),
filter: FilterSchema,
selector: SelectorSchema,
selector: VerificationRuleSelectorSchema,
value: z.discriminatedUnion('type', [
VariableValueSchema,
ArrayValueSchema,
Expand All @@ -99,6 +110,10 @@ export const VerificationRuleSchema = RuleBaseSchema.extend({
]),
})

export const RecordingVerificationRuleSchema = RuleBaseSchema.extend({
type: z.literal('recording-verification'),
})

export const CustomCodeRuleSchema = RuleBaseSchema.extend({
type: z.literal('customCode'),
filter: FilterSchema,
Expand All @@ -111,4 +126,5 @@ export const TestRuleSchema = z.discriminatedUnion('type', [
CorrelationRuleSchema,
VerificationRuleSchema,
CustomCodeRuleSchema,
RecordingVerificationRuleSchema,
])
42 changes: 26 additions & 16 deletions src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { spawn, ChildProcessWithoutNullStreams } from 'node:child_process'
import { readFile, writeFile } from 'fs/promises'
import path from 'path'
import readline from 'readline/promises'
import { K6Log } from './types'
import { K6Check, K6Log } from './types'
import { getArch, getPlatform } from './utils/electron'

export type K6Process = ChildProcessWithoutNullStreams
Expand Down Expand Up @@ -65,7 +65,6 @@ export const runScript = async (
'--iterations=1',
'--insecure-skip-tls-verify',
'--log-format=json',
'--no-summary',
'--quiet',
],
{
Expand All @@ -75,14 +74,16 @@ export const runScript = async (

// we use a reader to read entire lines from stderr instead of buffered data
const stderrReader = readline.createInterface(k6.stderr)
const stdoutReader = readline.createInterface(k6.stdout)

k6.stdout.on('data', (data) => {
console.error(`stdout: ${data}`)
stdoutReader.on('line', (data) => {
console.log(`stdout: ${data}`)

const checkData: K6Check[] = JSON.parse(data)
browserWindow.webContents.send('script:check', checkData)
})

stderrReader.on('line', (data) => {
console.log(`stderr: ${data}`)

const logData: K6Log = JSON.parse(data)
browserWindow.webContents.send('script:log', logData)
})
Expand All @@ -96,12 +97,25 @@ export const runScript = async (
}

const enhanceScript = async (scriptPath: string) => {
const groupSnippet = await getGroupSnippet()
const groupSnippet = await getJsSnippet('group_snippet.js')
const checksSnippet = await getJsSnippet('checks_snippet.js')
const scriptContent = await readFile(scriptPath, { encoding: 'utf-8' })
const scriptLines = scriptContent.split('\n')
const httpImportIndex = scriptLines.findIndex((line) =>
line.includes('k6/http')
)
const handleSummaryIndex = scriptLines.findIndex(
(line) =>
// NOTE: if the custom handle summary is commented out we can still insert our snippet
// this check should be improved
line.includes('export function handleSummary(') && !line.includes('//')
)

// NOTE: checks works only if the user doesn't define a custom summary handler
// if no custom handleSummary is defined we add our version to retrieve checks
if (handleSummaryIndex === -1) {
scriptLines.push(checksSnippet)
}
Comment on lines +107 to +118
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only add the checks retrieval logic via handleSummary if there is no custom summary defined by the user since we retrieve that information via summary


if (httpImportIndex !== -1) {
scriptLines.splice(httpImportIndex + 1, 0, groupSnippet)
Expand All @@ -112,19 +126,15 @@ const enhanceScript = async (scriptPath: string) => {
}
}

const getGroupSnippet = async () => {
let groupSnippetPath: string
const getJsSnippet = async (snippetName: string) => {
let jsSnippetPath: string

// if we are in dev server we take resources directly, otherwise look in the app resources folder.
if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
groupSnippetPath = path.join(
app.getAppPath(),
'resources',
'group_snippet.js'
)
jsSnippetPath = path.join(app.getAppPath(), 'resources', snippetName)
} else {
groupSnippetPath = path.join(process.resourcesPath, 'group_snippet.js')
jsSnippetPath = path.join(process.resourcesPath, snippetName)
}

return readFile(groupSnippetPath, { encoding: 'utf-8' })
return readFile(jsSnippetPath, { encoding: 'utf-8' })
}
33 changes: 33 additions & 0 deletions src/test/fixtures/checksRecording.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ProxyData } from '@/types'

export const checksRecording: ProxyData[] = [
{
id: '1',
request: {
method: 'POST',
url: 'http://test.k6.io/api/v1/foo',
headers: [],
cookies: [],
query: [],
scheme: 'http',
host: 'localhost:3000',
content: '',
path: '/api/v1/foo',
timestampStart: 0,
timestampEnd: 0,
contentLength: 0,
httpVersion: '1.1',
},
response: {
statusCode: 200,
path: '/api/v1/foo',
reason: 'OK',
httpVersion: '1.1',
headers: [['Content-Type', 'application/json']],
cookies: [],
content: JSON.stringify({ user_id: '444' }),
contentLength: 0,
timestampStart: 0,
},
},
]
8 changes: 8 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ export interface K6Log {
error?: string
}

export interface K6Check {
id: string
name: string
path: string
passes: number
fails: number
}

export type GroupedProxyData = Record<string, ProxyData[]>

export interface RequestSnippetSchema {
Expand Down
10 changes: 10 additions & 0 deletions src/types/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import {
JsonSelectorSchema,
ParameterizationRuleSchema,
RecordedValueSchema,
RecordingVerificationRuleSchema,
RegexSelectorSchema,
RuleBaseSchema,
SelectorSchema,
StatusCodeSelectorSchema,
TestRuleSchema,
VariableValueSchema,
VerificationRuleSchema,
VerificationRuleSelectorSchema,
} from '@/schemas/rules'

interface CorrelationState {
Expand All @@ -39,13 +42,20 @@ export type Filter = z.infer<typeof FilterSchema>
export type BeginEndSelector = z.infer<typeof BeginEndSelectorSchema>
export type RegexSelector = z.infer<typeof RegexSelectorSchema>
export type JsonSelector = z.infer<typeof JsonSelectorSchema>
export type StatusCodeSelector = z.infer<typeof StatusCodeSelectorSchema>
export type CustomCodeSelector = z.infer<typeof CustomCodeSelectorSchema>
export type Selector = z.infer<typeof SelectorSchema>
export type VerificationRuleSelector = z.infer<
typeof VerificationRuleSelectorSchema
>
export type CorrelationExtractor = z.infer<typeof CorrelationExtractorSchema>
export type CorrelationReplacer = z.infer<typeof CorrelationReplacerSchema>
export type RuleBase = z.infer<typeof RuleBaseSchema>
export type ParameterizationRule = z.infer<typeof ParameterizationRuleSchema>
export type CorrelationRule = z.infer<typeof CorrelationRuleSchema>
export type VerificationRule = z.infer<typeof VerificationRuleSchema>
export type CustomCodeRule = z.infer<typeof CustomCodeRuleSchema>
export type RecordingVerificationRule = z.infer<
typeof RecordingVerificationRuleSchema
>
export type TestRule = z.infer<typeof TestRuleSchema>
3 changes: 2 additions & 1 deletion src/utils/generator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GeneratorFileData } from '@/types/generator'
import { RampingStage } from '@/types/testOptions'
import { createEmptyRule } from './rules'

export function createNewGeneratorFile(recordingPath = ''): GeneratorFileData {
return {
Expand All @@ -21,7 +22,7 @@ export function createNewGeneratorFile(recordingPath = ''): GeneratorFileData {
testData: {
variables: [],
},
rules: [],
rules: [createEmptyRule('recording-verification')],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this rule will be present on every newly created generator as a default that can be removed

allowlist: [],
includeStaticAssets: false,
}
Expand Down
Loading
Loading