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

fix(cli): allow beforeafter validation schema to run engines without flow #2396

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
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({
Copy link
Contributor Author

@bernardobridge bernardobridge Jan 5, 2024

Choose a reason for hiding this comment

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

Previously the schema would look at config.engines to determine this. However, this is more complicated than just looking at the engine field within before/after itself:

  • If it doesn't have it, it's http (default), so it needs flow.
  • If it has it, and if it's socketio, ws or http, it needs flow. Otherwise it does not require flow (e.g. custom engine or playwright).

Looking at config.engines doesn't actually make sense, since you could be putting a config there for an engine you're using in your scenario, but not in before/after.

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}"?`
);
}
Comment on lines +496 to +501
Copy link
Contributor Author

Choose a reason for hiding this comment

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

similar logic to #2101 for consistency

const hookScenario = engine.createScenario(script[hook], ee);
const hookContext = createContext(script, contextVars, {
scenario: script[hook]
Expand Down
Loading