mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
[APM] fixing rule anomaly API test (#129527)
* fixing api test * removing unused code * removing unused code * fix * fix
This commit is contained in:
parent
3d182b965e
commit
c5af4414e5
2 changed files with 80 additions and 44 deletions
|
@ -6,7 +6,7 @@
|
|||
*/
|
||||
|
||||
/* eslint-disable no-console */
|
||||
|
||||
const { times } = require('lodash');
|
||||
const yargs = require('yargs');
|
||||
const path = require('path');
|
||||
const childProcess = require('child_process');
|
||||
|
@ -44,6 +44,10 @@ const { argv } = yargs(process.argv.slice(2))
|
|||
type: 'boolean',
|
||||
description: 'Add --inspect-brk flag to the ftr for debugging',
|
||||
})
|
||||
.option('times', {
|
||||
type: 'number',
|
||||
description: 'Repeat the test n number of times',
|
||||
})
|
||||
.check((argv) => {
|
||||
const { inspect, runner } = argv;
|
||||
if (inspect && !runner) {
|
||||
|
@ -73,4 +77,27 @@ const cmd = `node ${inspectArg} ../../../../scripts/${ftrScript} ${grepArg} --co
|
|||
|
||||
console.log(`Running ${cmd}`);
|
||||
|
||||
childProcess.execSync(cmd, { cwd: path.join(__dirname), stdio: 'inherit' });
|
||||
function runTests() {
|
||||
childProcess.execSync(cmd, { cwd: path.join(__dirname), stdio: 'inherit' });
|
||||
}
|
||||
|
||||
if (argv.times) {
|
||||
const runCounter = { succeeded: 0, failed: 0, remaining: argv.times };
|
||||
let exitStatus = 0;
|
||||
times(argv.times, () => {
|
||||
try {
|
||||
runTests();
|
||||
runCounter.succeeded++;
|
||||
} catch (e) {
|
||||
exitStatus = 1;
|
||||
runCounter.failed++;
|
||||
}
|
||||
runCounter.remaining--;
|
||||
if (argv.times > 1) {
|
||||
console.log(runCounter);
|
||||
}
|
||||
});
|
||||
process.exit(exitStatus);
|
||||
} else {
|
||||
runTests();
|
||||
}
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
* 2.0; you may not use this file except in compliance with the Elastic License
|
||||
* 2.0.
|
||||
*/
|
||||
|
||||
import { apm, timerange } from '@elastic/apm-synthtrace';
|
||||
import datemath from '@elastic/datemath';
|
||||
import expect from '@kbn/expect';
|
||||
import { range } from 'lodash';
|
||||
import { AlertType } from '../../../../plugins/apm/common/alert_types';
|
||||
|
@ -15,21 +15,22 @@ import { waitForRuleStatus } from './wait_for_rule_status';
|
|||
|
||||
export default function ApiTest({ getService }: FtrProviderContext) {
|
||||
const registry = getService('registry');
|
||||
const synthtraceEsClient = getService('synthtraceEsClient');
|
||||
const ml = getService('ml');
|
||||
|
||||
const supertest = getService('supertest');
|
||||
const ml = getService('ml');
|
||||
const log = getService('log');
|
||||
|
||||
// FLAKY: https://github.com/elastic/kibana/issues/129224
|
||||
registry.when.skip(
|
||||
const synthtraceEsClient = getService('synthtraceEsClient');
|
||||
|
||||
registry.when(
|
||||
'fetching service anomalies with a trial license',
|
||||
{ config: 'trial', archives: ['apm_mappings_only_8.0.0'] },
|
||||
() => {
|
||||
const spikeStart = datemath.parse('now-2h')!.valueOf();
|
||||
const spikeEnd = datemath.parse('now')!.valueOf();
|
||||
const start = '2021-01-01T00:00:00.000Z';
|
||||
const end = '2021-01-08T00:15:00.000Z';
|
||||
|
||||
const start = datemath.parse('now-2w')!.valueOf();
|
||||
const end = datemath.parse('now')!.valueOf();
|
||||
const spikeStart = new Date('2021-01-07T23:15:00.000Z').getTime();
|
||||
const spikeEnd = new Date('2021-01-08T00:15:00.000Z').getTime();
|
||||
|
||||
const NORMAL_DURATION = 100;
|
||||
const NORMAL_RATE = 1;
|
||||
|
@ -37,7 +38,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {
|
|||
let ruleId: string | undefined;
|
||||
|
||||
before(async () => {
|
||||
const serviceA = apm.service('service-a', 'production', 'java').instance('a');
|
||||
const serviceA = apm.service('a', 'production', 'java').instance('a');
|
||||
|
||||
const events = timerange(new Date(start).getTime(), new Date(end).getTime())
|
||||
.interval('1m')
|
||||
|
@ -61,46 +62,54 @@ export default function ApiTest({ getService }: FtrProviderContext) {
|
|||
});
|
||||
|
||||
await synthtraceEsClient.index(events);
|
||||
await createAndRunApmMlJob({ environment: 'production', ml });
|
||||
const { body: createdRule } = await supertest
|
||||
.post(`/api/alerting/rule`)
|
||||
.set('kbn-xsrf', 'foo')
|
||||
.send({
|
||||
params: {
|
||||
environment: 'production',
|
||||
serviceName: 'service-a',
|
||||
transactionType: 'request',
|
||||
windowSize: 30,
|
||||
windowUnit: 'm',
|
||||
anomalySeverityType: 'warning',
|
||||
},
|
||||
consumer: 'apm',
|
||||
schedule: {
|
||||
interval: '1m',
|
||||
},
|
||||
tags: ['apm', 'service.name:service-a'],
|
||||
name: 'Latency anomaly | service-a',
|
||||
rule_type_id: AlertType.Anomaly,
|
||||
notify_when: 'onActiveAlert',
|
||||
actions: [],
|
||||
});
|
||||
ruleId = createdRule.id;
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await synthtraceEsClient.clean();
|
||||
await ml.cleanMlIndices();
|
||||
await supertest.delete(`/api/alerting/rule/${ruleId}`).set('kbn-xsrf', 'foo');
|
||||
});
|
||||
|
||||
it('checks if alert is active', async () => {
|
||||
const executionStatus = await waitForRuleStatus({
|
||||
id: ruleId,
|
||||
expectedStatus: 'active',
|
||||
supertest,
|
||||
log,
|
||||
describe('with ml jobs', () => {
|
||||
before(async () => {
|
||||
await createAndRunApmMlJob({ environment: 'production', ml });
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await ml.cleanMlIndices();
|
||||
});
|
||||
|
||||
it('checks if alert is active', async () => {
|
||||
const { body: createdRule } = await supertest
|
||||
.post(`/api/alerting/rule`)
|
||||
.set('kbn-xsrf', 'foo')
|
||||
.send({
|
||||
params: {
|
||||
environment: 'production',
|
||||
windowSize: 99,
|
||||
windowUnit: 'y',
|
||||
anomalySeverityType: 'warning',
|
||||
},
|
||||
consumer: 'apm',
|
||||
schedule: {
|
||||
interval: '1m',
|
||||
},
|
||||
tags: ['apm', 'service.name:service-a'],
|
||||
name: 'Latency anomaly | service-a',
|
||||
rule_type_id: AlertType.Anomaly,
|
||||
notify_when: 'onActiveAlert',
|
||||
actions: [],
|
||||
});
|
||||
|
||||
ruleId = createdRule.id;
|
||||
|
||||
const executionStatus = await waitForRuleStatus({
|
||||
id: ruleId,
|
||||
expectedStatus: 'active',
|
||||
supertest,
|
||||
log,
|
||||
});
|
||||
expect(executionStatus.status).to.be('active');
|
||||
});
|
||||
expect(executionStatus.status).to.be('active');
|
||||
});
|
||||
}
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue