From fd4ef5f2fbfce82f7d9419faf4c3f99dae692f75 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 22 Feb 2023 14:01:29 -0500 Subject: [PATCH] [8.6] [kbn-journeys] add optional beforeSteps hook (#151717) (#151917) # Backport This will backport the following commits from `main` to `8.6`: - [[kbn-journeys] add optional beforeSteps hook (#151717)](https://github.com/elastic/kibana/pull/151717) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Dzmitry Lemechko --- packages/kbn-journeys/journey/journey.ts | 22 +++++++++++++++++-- .../kbn-journeys/journey/journey_config.ts | 13 +++++++++++ .../journey/journey_ftr_harness.ts | 12 ++++++++-- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/packages/kbn-journeys/journey/journey.ts b/packages/kbn-journeys/journey/journey.ts index 537587fef3872..7bc14e6bebe14 100644 --- a/packages/kbn-journeys/journey/journey.ts +++ b/packages/kbn-journeys/journey/journey.ts @@ -12,7 +12,12 @@ import { Page } from 'playwright'; import callsites from 'callsites'; import { ToolingLog } from '@kbn/tooling-log'; import { FtrConfigProvider } from '@kbn/test'; -import { FtrProviderContext, KibanaServer } from '@kbn/ftr-common-functional-services'; +import { + FtrProviderContext, + KibanaServer, + Es, + RetryService, +} from '@kbn/ftr-common-functional-services'; import { Auth } from '../services/auth'; import { InputDelays } from '../services/input_delays'; @@ -28,6 +33,8 @@ export interface BaseStepCtx { inputDelays: InputDelays; kbnUrl: KibanaUrl; kibanaServer: KibanaServer; + es: Es; + retry: RetryService; } export type AnyStep = Step<{}>; @@ -96,7 +103,16 @@ export class Journey { /** * Define a step of this Journey. Steps are only separated from each other - * to aid in reading/debuging the journey and reading it's logging output. + * to aid in reading/debugging the journey and reading it's logging output. + * These services might be helpful: + * + * page: methods to interact with a single tab in a browser + * + * kbnUrl: get the URL for a Kibana plugin + * + * kibanaServer: basic Kibana server client + * + * es: basic Elasticsearch client * * If a journey fails, a failure report will be created with a screenshot * at the point of failure as well as a screenshot at the end of every @@ -119,6 +135,8 @@ export class Journey { getService('config'), getService('esArchiver'), getService('kibanaServer'), + getService('es'), + getService('retry'), new Auth(getService('config'), getService('log'), getService('kibanaServer')), this.config ).initMochaSuite(this.#steps); diff --git a/packages/kbn-journeys/journey/journey_config.ts b/packages/kbn-journeys/journey/journey_config.ts index 7343dc5fac5be..e4a61776a0eac 100644 --- a/packages/kbn-journeys/journey/journey_config.ts +++ b/packages/kbn-journeys/journey/journey_config.ts @@ -96,6 +96,11 @@ export interface JourneyConfigOptions { * be merged with the default context provided to each step function. */ extendContext?: (ctx: BaseStepCtx) => CtxExt; + /** + * Use this to define actions that will be executed after Kibana & ES were started, + * but before archives are loaded. APM traces are not collected for this hook. + */ + beforeSteps?: (ctx: BaseStepCtx & CtxExt) => Promise; } export class JourneyConfig { @@ -161,4 +166,12 @@ export class JourneyConfig { ...ext(ctx), }; } + + async getBeforeStepsFn(ctx: BaseStepCtx & CtxExt) { + if (this.#opts.beforeSteps) { + await this.#opts.beforeSteps(ctx); + } else { + new Promise((resolve) => resolve()); + } + } } diff --git a/packages/kbn-journeys/journey/journey_ftr_harness.ts b/packages/kbn-journeys/journey/journey_ftr_harness.ts index 7b9d8cc0266fc..12590f05c44d6 100644 --- a/packages/kbn-journeys/journey/journey_ftr_harness.ts +++ b/packages/kbn-journeys/journey/journey_ftr_harness.ts @@ -16,7 +16,7 @@ import playwright, { ChromiumBrowser, Page, BrowserContext, CDPSession, Request import { asyncMap, asyncForEach } from '@kbn/std'; import { ToolingLog } from '@kbn/tooling-log'; import { Config } from '@kbn/test'; -import { EsArchiver, KibanaServer } from '@kbn/ftr-common-functional-services'; +import { EsArchiver, KibanaServer, Es, RetryService } from '@kbn/ftr-common-functional-services'; import { Auth } from '../services/auth'; import { getInputDelays } from '../services/input_delays'; @@ -34,6 +34,8 @@ export class JourneyFtrHarness { private readonly config: Config, private readonly esArchiver: EsArchiver, private readonly kibanaServer: KibanaServer, + private readonly es: Es, + private readonly retry: RetryService, private readonly auth: Auth, private readonly journeyConfig: JourneyConfig ) { @@ -117,8 +119,12 @@ export class JourneyFtrHarness { } private async onSetup() { + // We start browser and init page in the first place + await this.setupBrowserAndPage(); + // We allow opt-in beforeSteps hook to manage Kibana/ES state + await this.journeyConfig.getBeforeStepsFn(this.getCtx()); + // Loading test data await Promise.all([ - this.setupBrowserAndPage(), asyncForEach(this.journeyConfig.getEsArchives(), async (esArchive) => { await this.esArchiver.load(esArchive); }), @@ -365,6 +371,8 @@ export class JourneyFtrHarness { ) ), kibanaServer: this.kibanaServer, + es: this.es, + retry: this.retry, }); return this.#_ctx;