Skip to content

Commit

Permalink
[8.6] [kbn-journeys] add optional beforeSteps hook (elastic#151717) (e…
Browse files Browse the repository at this point in the history
…lastic#151917)

# Backport

This will backport the following commits from `main` to `8.6`:
- [[kbn-journeys] add optional beforeSteps hook
(elastic#151717)](elastic#151717)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Dzmitry
Lemechko","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-02-22T17:53:00Z","message":"[kbn-journeys]
add optional beforeSteps hook (elastic#151717)\n\n## Summary\r\n\r\nRelated to
elastic#151613\r\n\r\nThere might be cases when we need to add extra wait for
Kibana plugin to\r\nbe ready before starting loading test data with
`esArchiver` /\r\n`kbnArchiver`.\r\n\r\nCurrently journey lifecycle
looks like this:\r\n\r\n- `onSetup` wrapped with mocha `before` hook\r\n
- in parallel \r\n - `setupBrowserAndPage` (including EBT tracker
setup)\r\n - load ES data / Kibana saved objects\r\n - `setupApm`\r\n-
steps execution (each step wrapped with mocha `it` function)\r\n-
`onTeardown` wrapped with mocha `after` hook\r\n -
`tearDownBrowserAndPage` (including closing EBT tracker)\r\n -
`teardownApm`\r\n - load ES data / Kibana saved
objects\r\n\r\nbeforeSteps hook purpose is to make sure Kibana/ES state
is ready for\r\njourney execution and not load test data, since it won't
be unloaded\r\nduring `after` hook:\r\n\r\n- `onSetup` wrapped with
mocha `before` hook\r\n - `setupBrowserAndPage` (including EBT tracker
setup)\r\n- run beforeSteps hook -> prepare Kibana/ES for data ingestion
/ steps\r\nexecution\r\n - load ES data / Kibana saved objects\r\n -
`setupApm`\r\n\r\nHow to use:\r\n\r\n```\r\nexport const journey = new
Journey({\r\n beforeSteps: async ({ kibanaServer, retry }) => {\r\n
retry.try(async () => {\r\n const response = await
kibanaServer.request({\r\n path:
'/internal/cloud_security_posture/status?check=init',\r\n method:
'GET',\r\n });\r\n return response.status === 200;\r\n });\r\n },\r\n
esArchives: [...],\r\n kbnArchives: [...],\r\n})\r\n .step({...})\r\n
.step({...})\r\n```","sha":"6c33644b5324df412acd71d2a6433e93c6e17904","branchLabelMapping":{"^v8.8.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","wg:performance","v8.7.0","v8.8.0","v8.6.3"],"number":151717,"url":"https://github.com/elastic/kibana/pull/151717","mergeCommit":{"message":"[kbn-journeys]
add optional beforeSteps hook (elastic#151717)\n\n## Summary\r\n\r\nRelated to
elastic#151613\r\n\r\nThere might be cases when we need to add extra wait for
Kibana plugin to\r\nbe ready before starting loading test data with
`esArchiver` /\r\n`kbnArchiver`.\r\n\r\nCurrently journey lifecycle
looks like this:\r\n\r\n- `onSetup` wrapped with mocha `before` hook\r\n
- in parallel \r\n - `setupBrowserAndPage` (including EBT tracker
setup)\r\n - load ES data / Kibana saved objects\r\n - `setupApm`\r\n-
steps execution (each step wrapped with mocha `it` function)\r\n-
`onTeardown` wrapped with mocha `after` hook\r\n -
`tearDownBrowserAndPage` (including closing EBT tracker)\r\n -
`teardownApm`\r\n - load ES data / Kibana saved
objects\r\n\r\nbeforeSteps hook purpose is to make sure Kibana/ES state
is ready for\r\njourney execution and not load test data, since it won't
be unloaded\r\nduring `after` hook:\r\n\r\n- `onSetup` wrapped with
mocha `before` hook\r\n - `setupBrowserAndPage` (including EBT tracker
setup)\r\n- run beforeSteps hook -> prepare Kibana/ES for data ingestion
/ steps\r\nexecution\r\n - load ES data / Kibana saved objects\r\n -
`setupApm`\r\n\r\nHow to use:\r\n\r\n```\r\nexport const journey = new
Journey({\r\n beforeSteps: async ({ kibanaServer, retry }) => {\r\n
retry.try(async () => {\r\n const response = await
kibanaServer.request({\r\n path:
'/internal/cloud_security_posture/status?check=init',\r\n method:
'GET',\r\n });\r\n return response.status === 200;\r\n });\r\n },\r\n
esArchives: [...],\r\n kbnArchives: [...],\r\n})\r\n .step({...})\r\n
.step({...})\r\n```","sha":"6c33644b5324df412acd71d2a6433e93c6e17904"}},"sourceBranch":"main","suggestedTargetBranches":["8.7","8.6"],"targetPullRequestStates":[{"branch":"8.7","label":"v8.7.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.8.0","labelRegex":"^v8.8.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/151717","number":151717,"mergeCommit":{"message":"[kbn-journeys]
add optional beforeSteps hook (elastic#151717)\n\n## Summary\r\n\r\nRelated to
elastic#151613\r\n\r\nThere might be cases when we need to add extra wait for
Kibana plugin to\r\nbe ready before starting loading test data with
`esArchiver` /\r\n`kbnArchiver`.\r\n\r\nCurrently journey lifecycle
looks like this:\r\n\r\n- `onSetup` wrapped with mocha `before` hook\r\n
- in parallel \r\n - `setupBrowserAndPage` (including EBT tracker
setup)\r\n - load ES data / Kibana saved objects\r\n - `setupApm`\r\n-
steps execution (each step wrapped with mocha `it` function)\r\n-
`onTeardown` wrapped with mocha `after` hook\r\n -
`tearDownBrowserAndPage` (including closing EBT tracker)\r\n -
`teardownApm`\r\n - load ES data / Kibana saved
objects\r\n\r\nbeforeSteps hook purpose is to make sure Kibana/ES state
is ready for\r\njourney execution and not load test data, since it won't
be unloaded\r\nduring `after` hook:\r\n\r\n- `onSetup` wrapped with
mocha `before` hook\r\n - `setupBrowserAndPage` (including EBT tracker
setup)\r\n- run beforeSteps hook -> prepare Kibana/ES for data ingestion
/ steps\r\nexecution\r\n - load ES data / Kibana saved objects\r\n -
`setupApm`\r\n\r\nHow to use:\r\n\r\n```\r\nexport const journey = new
Journey({\r\n beforeSteps: async ({ kibanaServer, retry }) => {\r\n
retry.try(async () => {\r\n const response = await
kibanaServer.request({\r\n path:
'/internal/cloud_security_posture/status?check=init',\r\n method:
'GET',\r\n });\r\n return response.status === 200;\r\n });\r\n },\r\n
esArchives: [...],\r\n kbnArchives: [...],\r\n})\r\n .step({...})\r\n
.step({...})\r\n```","sha":"6c33644b5324df412acd71d2a6433e93c6e17904"}},{"branch":"8.6","label":"v8.6.3","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Dzmitry Lemechko <[email protected]>
  • Loading branch information
kibanamachine and dmlemeshko authored Feb 22, 2023
1 parent cd1a7b7 commit fd4ef5f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
22 changes: 20 additions & 2 deletions packages/kbn-journeys/journey/journey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -28,6 +33,8 @@ export interface BaseStepCtx {
inputDelays: InputDelays;
kbnUrl: KibanaUrl;
kibanaServer: KibanaServer;
es: Es;
retry: RetryService;
}

export type AnyStep = Step<{}>;
Expand Down Expand Up @@ -96,7 +103,16 @@ export class Journey<CtxExt extends object> {

/**
* 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
Expand All @@ -119,6 +135,8 @@ export class Journey<CtxExt extends object> {
getService('config'),
getService('esArchiver'),
getService('kibanaServer'),
getService('es'),
getService('retry'),
new Auth(getService('config'), getService('log'), getService('kibanaServer')),
this.config
).initMochaSuite(this.#steps);
Expand Down
13 changes: 13 additions & 0 deletions packages/kbn-journeys/journey/journey_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export interface JourneyConfigOptions<CtxExt> {
* 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<void>;
}

export class JourneyConfig<CtxExt extends object> {
Expand Down Expand Up @@ -161,4 +166,12 @@ export class JourneyConfig<CtxExt extends object> {
...ext(ctx),
};
}

async getBeforeStepsFn(ctx: BaseStepCtx & CtxExt) {
if (this.#opts.beforeSteps) {
await this.#opts.beforeSteps(ctx);
} else {
new Promise<void>((resolve) => resolve());
}
}
}
12 changes: 10 additions & 2 deletions packages/kbn-journeys/journey/journey_ftr_harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<any>
) {
Expand Down Expand Up @@ -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);
}),
Expand Down Expand Up @@ -365,6 +371,8 @@ export class JourneyFtrHarness {
)
),
kibanaServer: this.kibanaServer,
es: this.es,
retry: this.retry,
});

return this.#_ctx;
Expand Down

0 comments on commit fd4ef5f

Please sign in to comment.