Skip to content

Commit

Permalink
Merge pull request #2396 from artilleryio/bernardobridge/art-1547-all…
Browse files Browse the repository at this point in the history
…ow-beforeafter-validate-script-schema-to-run-arbitrary

fix(cli): allow beforeafter validation schema to run engines without flow
  • Loading branch information
bernardobridge committed Jan 12, 2024
2 parents da20704 + 2d1a1f1 commit 21c8e17
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
8 changes: 6 additions & 2 deletions packages/artillery/lib/util/validate-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,13 @@ const scenarioItem = Joi.object({
});

const beforeAfterSchema = Joi.object({
flow: Joi.any().when('...config.engines', {
flow: Joi.when('engine', {
is: Joi.exist(),
then: Joi.array().items(Joi.any()).required(),
then: Joi.when('engine', {
is: Joi.valid('socketio', 'ws', 'http'),
then: Joi.array().items(flowItemSchema).required(),
otherwise: Joi.array().items(Joi.any())
}),
otherwise: Joi.array().items(flowItemSchema).required()
})
});
Expand Down
22 changes: 4 additions & 18 deletions packages/artillery/test/lib/validate-script.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,38 +110,24 @@ test('validate script', (t) => {
};

scriptWithCustomEngine.before = {
flow: [
{
get: {
url: '/'
},
data: '123'
}
]
engine: 'myengine'
};

t.equal(
validateScript(scriptWithCustomEngine),
undefined,
'it should not enforce validation for before sections when custom engines are configured'
'it should not require flow for before sections when custom engines are configured'
);
delete scriptWithCustomEngine.before;

scriptWithCustomEngine.after = {
flow: [
{
get: {
url: '/'
},
data: '123'
}
]
engine: 'myengine'
};

t.equal(
validateScript(scriptWithCustomEngine),
undefined,
'it should not enforce validation for after sections when custom engines are configured'
'it should not require flow for after sections when custom engines are configured'
);

t.end();
Expand Down
6 changes: 6 additions & 0 deletions packages/core/lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,12 @@ function handleScriptHook(hook, script, hookEvents, contextVars = {}) {

const name = script[hook].engine || 'http';
const engine = engines.find((e) => e.__name === name);

if (typeof engine === 'undefined') {
throw new Error(
`Failed to run ${hook} hook: unknown engine "${name}". Did you forget to include it in "config.engines.${name}"?`
);
}
const hookScenario = engine.createScenario(script[hook], ee);
const hookContext = createContext(script, contextVars, {
scenario: script[hook]
Expand Down

0 comments on commit 21c8e17

Please sign in to comment.