Skip to content

Commit

Permalink
Merge branch 'main' of github.com:grafana/k6-studio into feat/generat…
Browse files Browse the repository at this point in the history
…or-schema-migration
  • Loading branch information
cristianoventura committed Dec 11, 2024
2 parents de96729 + 564aab9 commit 3871084
Show file tree
Hide file tree
Showing 15 changed files with 290 additions and 126 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "k6-studio",
"productName": "k6 Studio",
"version": "0.9.0",
"version": "0.10.0",
"description": "k6 Studio",
"repository": "github:grafana/k6-studio",
"main": ".vite/build/main.js",
Expand Down
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/generator/v0/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
1 change: 1 addition & 0 deletions src/schemas/generator/v1/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
Loading

0 comments on commit 3871084

Please sign in to comment.