kibana/x-pack/test/functional_execution_context/test_utils.ts
Mikhail Shustov 4a2acad218
Functional tests for execution context propagation in alerting and task manager plugins (#111179)
* move execution context tests to x-pack folder

* add execution context tests for alerts and tasks

* cleanup tests

* remove obsolete test folder

* use ToolingLog instead of console.log
2021-09-14 11:52:33 +02:00

53 lines
1.6 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import Fs from 'fs/promises';
import Path from 'path';
import { isEqualWith } from 'lodash';
import type { Ecs, KibanaExecutionContext } from 'kibana/server';
import type { RetryService } from '../../../test/common/services/retry';
export const logFilePath = Path.resolve(__dirname, './kibana.log');
export const ANY = Symbol('any');
export function isExecutionContextLog(
record: string | undefined,
executionContext: KibanaExecutionContext
) {
if (!record) return false;
try {
const object = JSON.parse(record);
return isEqualWith(object, executionContext, function customizer(obj1: any, obj2: any) {
if (obj2 === ANY) return true;
});
} catch (e) {
return false;
}
}
// to avoid splitting log record containing \n symbol
const endOfLine = /(?<=})\s*\n/;
export async function assertLogContains({
description,
predicate,
retry,
}: {
description: string;
predicate: (record: Ecs) => boolean;
retry: RetryService;
}): Promise<void> {
// logs are written to disk asynchronously. I sacrificed performance to reduce flakiness.
await retry.waitFor(description, async () => {
const logsStr = await Fs.readFile(logFilePath, 'utf-8');
const normalizedRecords = logsStr
.split(endOfLine)
.filter(Boolean)
.map((s) => JSON.parse(s));
return normalizedRecords.some(predicate);
});
}