From b140da16d63c27818c404fa732d1178dd7cc682d Mon Sep 17 00:00:00 2001 From: Bernardo Guerreiro Date: Thu, 4 Jan 2024 17:17:17 +0000 Subject: [PATCH 1/3] fix(cli): allow engines without flow in before/after schema --- packages/artillery/lib/util/validate-script.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/artillery/lib/util/validate-script.js b/packages/artillery/lib/util/validate-script.js index 3bf868c021..904ff5580b 100644 --- a/packages/artillery/lib/util/validate-script.js +++ b/packages/artillery/lib/util/validate-script.js @@ -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() }) }); From 653daabc0c0ecd4befda25945a0611d498a72cfa Mon Sep 17 00:00:00 2001 From: Bernardo Guerreiro Date: Thu, 4 Jan 2024 17:33:06 +0000 Subject: [PATCH 2/3] test(cli): fix validate-script before/after test to not require flow --- .../test/lib/validate-script.test.js | 22 ++++--------------- 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/packages/artillery/test/lib/validate-script.test.js b/packages/artillery/test/lib/validate-script.test.js index ad84562737..7cf29c2401 100644 --- a/packages/artillery/test/lib/validate-script.test.js +++ b/packages/artillery/test/lib/validate-script.test.js @@ -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(); From 4baa50bb4e60dd96f7bee0562c2be9ed6d36cf16 Mon Sep 17 00:00:00 2001 From: Bernardo Guerreiro Date: Thu, 4 Jan 2024 17:36:44 +0000 Subject: [PATCH 3/3] feat(core): error when unknown engine is used in before/after --- packages/core/lib/runner.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/core/lib/runner.js b/packages/core/lib/runner.js index eb291fdaab..9099b8087d 100644 --- a/packages/core/lib/runner.js +++ b/packages/core/lib/runner.js @@ -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]