-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add execution context tests for alerts and tasks
- Loading branch information
Showing
10 changed files
with
712 additions
and
376 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
x-pack/test/functional_execution_context/fixtures/plugins/alerts/kibana.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"id": "alertsFixtures", | ||
"owner": { | ||
"name": "Core team", | ||
"githubTeam": "kibana-core" | ||
}, | ||
"version": "1.0.0", | ||
"kibanaVersion": "kibana", | ||
"configPath": ["xpack"], | ||
"requiredPlugins": ["taskManager", "features", "actions", "alerting"], | ||
"optionalPlugins": ["security", "spaces"], | ||
"server": true, | ||
"ui": false | ||
} |
13 changes: 13 additions & 0 deletions
13
x-pack/test/functional_execution_context/fixtures/plugins/alerts/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "alerts-fixtures", | ||
"version": "1.0.0", | ||
"kibana": { | ||
"version": "kibana", | ||
"templateVersion": "1.0.0" | ||
}, | ||
"scripts": { | ||
"kbn": "node ../../../../../../scripts/kbn.js", | ||
"build": "rm -rf './target' && ../../../../../../node_modules/.bin/tsc" | ||
}, | ||
"license": "Elastic License 2.0" | ||
} |
10 changes: 10 additions & 0 deletions
10
x-pack/test/functional_execution_context/fixtures/plugins/alerts/server/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* 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 { FixturePlugin } from './plugin'; | ||
|
||
export const plugin = () => new FixturePlugin(); |
88 changes: 88 additions & 0 deletions
88
x-pack/test/functional_execution_context/fixtures/plugins/alerts/server/plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* 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 { Plugin, CoreSetup } from 'kibana/server'; | ||
import { PluginSetupContract as AlertingPluginSetup } from '../../../../../../plugins/alerting/server/plugin'; | ||
import { EncryptedSavedObjectsPluginStart } from '../../../../../../plugins/encrypted_saved_objects/server'; | ||
import { PluginSetupContract as FeaturesPluginSetup } from '../../../../../../plugins/features/server'; | ||
import { SpacesPluginStart } from '../../../../../../plugins/spaces/server'; | ||
import { SecurityPluginStart } from '../../../../../../plugins/security/server'; | ||
|
||
export interface FixtureSetupDeps { | ||
features: FeaturesPluginSetup; | ||
alerting: AlertingPluginSetup; | ||
} | ||
|
||
export interface FixtureStartDeps { | ||
encryptedSavedObjects: EncryptedSavedObjectsPluginStart; | ||
security?: SecurityPluginStart; | ||
spaces?: SpacesPluginStart; | ||
} | ||
|
||
export class FixturePlugin implements Plugin<void, void, FixtureSetupDeps, FixtureStartDeps> { | ||
constructor() {} | ||
|
||
public setup(core: CoreSetup<FixtureStartDeps>, { features, alerting }: FixtureSetupDeps) { | ||
features.registerKibanaFeature({ | ||
id: 'alertsFixture', | ||
name: 'Alerts', | ||
app: ['alerts', 'kibana'], | ||
category: { id: 'foo', label: 'foo' }, | ||
alerting: ['test.longRunning'], | ||
privileges: { | ||
all: { | ||
app: ['alerts', 'kibana'], | ||
savedObject: { | ||
all: ['alert'], | ||
read: [], | ||
}, | ||
alerting: { | ||
rule: { | ||
all: ['test.longRunning'], | ||
}, | ||
}, | ||
ui: [], | ||
}, | ||
read: { | ||
app: ['alerts', 'kibana'], | ||
savedObject: { | ||
all: [], | ||
read: ['alert'], | ||
}, | ||
alerting: { | ||
rule: { | ||
read: ['test.longRunning'], | ||
}, | ||
}, | ||
ui: [], | ||
}, | ||
}, | ||
}); | ||
|
||
alerting.registerType({ | ||
id: 'test.longRunning', | ||
name: 'Test: Query Elasticsearch server', | ||
actionGroups: [ | ||
{ | ||
id: 'default', | ||
name: 'Default', | ||
}, | ||
], | ||
producer: 'alertsFixture', | ||
defaultActionGroupId: 'default', | ||
minimumLicenseRequired: 'basic', | ||
isExportable: true, | ||
async executor() { | ||
const [coreStart] = await core.getStartServices(); | ||
await coreStart.elasticsearch.client.asInternalUser.ping(); | ||
}, | ||
}); | ||
} | ||
|
||
public start() {} | ||
public stop() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* 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, | ||
path, | ||
}: { | ||
description: string; | ||
predicate: (record: Ecs) => boolean; | ||
path: string; | ||
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(path, 'utf-8'); | ||
const normalizedRecords = logsStr | ||
.split(endOfLine) | ||
.filter(Boolean) | ||
.map((s) => JSON.parse(s)); | ||
|
||
return normalizedRecords.some(predicate); | ||
}); | ||
} |
Oops, something went wrong.