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: ability to disable a rule #375

Merged
merged 3 commits into from
Dec 10, 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
241 changes: 176 additions & 65 deletions src/codegen/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,12 @@ describe('Code generation', () => {
).toBe(expectedResult.replace(/\s/g, ''))
})

it('should replace correlated values', async () => {
describe('Correlation', () => {
const rules: TestRule[] = [
{
type: 'correlation',
id: '1',
enabled: true,
extractor: {
filter: { path: '/login' },
selector: {
Expand All @@ -174,6 +175,7 @@ describe('Code generation', () => {
{
type: 'correlation',
id: '1',
enabled: true,
extractor: {
filter: { path: '' },
selector: {
Expand All @@ -185,7 +187,8 @@ describe('Code generation', () => {
},
]

const expectedResult = await prettify(`
it('should replace correlated values', async () => {
const expectedResult = await prettify(`
let params
let resp
let match
Expand Down Expand Up @@ -236,16 +239,70 @@ describe('Code generation', () => {
sleep(1)
`)

expect(
await prettify(generateVUCode(correlationRecording, rules, thinkTime))
).toBe(expectedResult)
expect(
await prettify(generateVUCode(correlationRecording, rules, thinkTime))
).toBe(expectedResult)
})

it('should not generate correlation if rule is disabled', async () => {
const expectedResult = await prettify(`
let params
let resp
let match
let regex
let url
const correlation_vars = {}

group('one', function () {
params = {
headers: {}, cookies: {}
}

url = http.url\`http://test.k6.io/api/v1/foo\`
resp = http.request('POST', url, null, params)

params = {
headers: {}, cookies: {}
}

url = http.url\`http://test.k6.io/api/v1/login?project_id=555\`
resp = http.request('POST', url, null, params)
})

group('two', function () {
params = {
headers: {}, cookies: {}
}

url = http.url\`http://test.k6.io/api/v1/users/333\`
resp = http.request('GET', url, null, params)

params = {
headers: {}, cookies: {}
}

url = http.url\`http://test.k6.io/api/v1/users\`
resp = http.request('POST', url, \`${JSON.stringify({ user_id: '333' })}\`, params)
})

sleep(1)
`)

const disabledRules = rules.map((rule) => ({ ...rule, enabled: false }))
expect(
await prettify(
generateVUCode(correlationRecording, disabledRules, thinkTime)
)
).toBe(expectedResult)
})
})

it('should generate checks', () => {
const rules: TestRule[] = [
{
type: 'verification',
id: '1',
enabled: true,
filter: { path: '' },
value: {
type: 'recordedValue',
Expand All @@ -258,7 +315,6 @@ describe('Code generation', () => {
url = http.url\`http://test.k6.io/api/v1/foo\`
resp = http.request('POST', url, null, params)
check(resp,{'statusmatches200':(r)=>r.status===200,})

`

expect(
Expand All @@ -270,7 +326,7 @@ describe('Code generation', () => {
).toBe(expectedResult.replace(/\s/g, ''))
})

it('should replace paremeterization values', async () => {
describe('Parameterization', () => {
const recording = [
createProxyData({
id: '1',
Expand All @@ -297,64 +353,119 @@ describe('Code generation', () => {
}),
]

const rules: TestRule[] = [
jsonRule,
customCodeReplaceProjectId,
customCodeReplaceCsrf,
]

const expectedResult = await prettify(`
let params
let resp
let match
let regex
let url
const correlation_vars = {}

group('Default group', function () {
params = {
headers: {
'content-type': \`application/json\`
},
cookies: {}
}

url = http.url\`http://test.k6.io/api/v1/users\`
resp = http.request('POST', url, \`${JSON.stringify({ user_id: 'TEST_ID' })}\`, params)

params = {
headers: {},
cookies: {},
}

function getParameterizationValue1() {
const randomInteger = Math.floor(Math.random() * 100000)
return randomInteger
}
function getParameterizationValue2() {
return '123456'
}

url = http.url\`http://example.com/api/v1/users?project_id=\${getParameterizationValue1()}&csrf=\${getParameterizationValue2()}\`
resp = http.request('GET', url, null, params)


params = {
headers: {},
cookies: {},
}

url = http.url\`http://example.com/api/v1/users?project_id=\${getParameterizationValue1()}\`
resp = http.request('GET', url, null, params)
})

sleep(1)

`)

expect(await prettify(generateVUCode(recording, rules, thinkTime))).toBe(
expectedResult
)
it('should replace paremeterization values', async () => {
const rules: TestRule[] = [
jsonRule,
customCodeReplaceProjectId,
customCodeReplaceCsrf,
]

const expectedResult = await prettify(`
let params
let resp
let match
let regex
let url
const correlation_vars = {}

group('Default group', function () {
params = {
headers: {
'content-type': \`application/json\`
},
cookies: {}
}

url = http.url\`http://test.k6.io/api/v1/users\`
resp = http.request('POST', url, \`${JSON.stringify({ user_id: 'TEST_ID' })}\`, params)

params = {
headers: {},
cookies: {},
}

function getParameterizationValue1() {
const randomInteger = Math.floor(Math.random() * 100000)
return randomInteger
}
function getParameterizationValue2() {
return '123456'
}

url = http.url\`http://example.com/api/v1/users?project_id=\${getParameterizationValue1()}&csrf=\${getParameterizationValue2()}\`
resp = http.request('GET', url, null, params)


params = {
headers: {},
cookies: {},
}

url = http.url\`http://example.com/api/v1/users?project_id=\${getParameterizationValue1()}\`
resp = http.request('GET', url, null, params)
})

sleep(1)

`)

expect(
await prettify(generateVUCode(recording, rules, thinkTime))
).toBe(expectedResult)
})

it('should not replace paremeterization values if rule is disabled', async () => {
const disabledRules: TestRule[] = [
{ ...jsonRule, enabled: false },
{ ...customCodeReplaceProjectId, enabled: false },
{ ...customCodeReplaceCsrf, enabled: false },
]

const expectedResult = await prettify(`
let params
let resp
let match
let regex
let url
const correlation_vars = {}

group('Default group', function () {
params = {
headers: {
'content-type': \`application/json\`
},
cookies: {}
}

url = http.url\`http://test.k6.io/api/v1/users\`
resp = http.request('POST', url, \`${JSON.stringify({ user_id: '333' })}\`, params)

params = {
headers: {},
cookies: {},
}

url = http.url\`http://example.com/api/v1/users?project_id=123&csrf=321\`
resp = http.request('GET', url, null, params)


params = {
headers: {},
cookies: {},
}

url = http.url\`http://example.com/api/v1/users?project_id=123\`
resp = http.request('GET', url, null, params)
})

sleep(1)

`)

expect(
await prettify(generateVUCode(recording, disabledRules, thinkTime))
).toBe(expectedResult)
})
})
})

Expand Down
3 changes: 3 additions & 0 deletions src/rules/correlation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,7 @@ correlation_vars['correlation_1'] = resp.json().user_id`
const rule: CorrelationRule = {
type: 'correlation',
id: '1',
enabled: true,
extractor: {
filter: { path: '' },
selector: {
Expand Down Expand Up @@ -906,6 +907,7 @@ correlation_vars['correlation_1'] = resp.json().user_id`
const rule: CorrelationRule = {
type: 'correlation',
id: '1',
enabled: true,
extractor: {
filter: { path: '' },
selector: {
Expand Down Expand Up @@ -960,6 +962,7 @@ correlation_vars['correlation_1'] = resp.json().user_id`
const rule: CorrelationRule = {
type: 'correlation',
id: '1',
enabled: true,
extractor: {
filter: { path: '' },
selector: {
Expand Down
6 changes: 3 additions & 3 deletions src/rules/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ function createSequentialIdPool() {

export function applyRules(recording: ProxyData[], rules: TestRule[]) {
const idGenerator = createSequentialIdPool()
const ruleInstances = rules.map((rule) =>
createRuleInstance(rule, idGenerator(rule.type))
)
const ruleInstances = rules
.filter((rule) => rule.enabled)
.map((rule) => createRuleInstance(rule, idGenerator(rule.type)))

const requestSnippetSchemas = recording.map((data) =>
ruleInstances.reduce<RequestSnippetSchema>((acc, rule) => rule.apply(acc), {
Expand Down
1 change: 1 addition & 0 deletions src/schemas/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export const CorrelationReplacerSchema = z.object({

export const RuleBaseSchema = z.object({
id: z.string(),
enabled: z.boolean().default(true),
})

export const ParameterizationRuleSchema = RuleBaseSchema.extend({
Expand Down
5 changes: 5 additions & 0 deletions src/store/generator/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TestRule } from '@/types/rules'
export const rules: TestRule[] = [
{
id: '0',
enabled: true,
type: 'customCode',
filter: { path: '' },
snippet: 'console.log("Hello, world!")',
Expand All @@ -11,6 +12,7 @@ export const rules: TestRule[] = [
{
type: 'correlation',
id: '1',
enabled: true,
extractor: {
filter: { path: '' },
selector: {
Expand All @@ -24,6 +26,7 @@ export const rules: TestRule[] = [
{
type: 'correlation',
id: '3',
enabled: true,
extractor: {
filter: { path: '' },
selector: {
Expand All @@ -36,6 +39,7 @@ export const rules: TestRule[] = [
{
type: 'correlation',
id: '2',
enabled: true,
extractor: {
filter: { path: '' },
selector: {
Expand All @@ -49,6 +53,7 @@ export const rules: TestRule[] = [
{
type: 'correlation',
id: '4',
enabled: true,
extractor: {
filter: { path: 'api.k6.io/v3/account/me' },
selector: {
Expand Down
Loading
Loading