Skip to content

Commit

Permalink
[APM] Simplify cleanup of alerts in API tests (elastic#170111)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv authored Oct 31, 2023
1 parent 508f309 commit bf64c22
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 134 deletions.
23 changes: 10 additions & 13 deletions x-pack/test/apm_api_integration/configs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,33 @@ const apmDebugLogger = {
appenders: ['console'],
};

const kibanaConfig = {
'xpack.apm.forceSyntheticSource': 'true',
'logging.loggers': [apmDebugLogger],
'server.publicBaseUrl': 'http://mockedPublicBaseUrl',
};

const apmFtrConfigs = {
basic: {
license: 'basic' as const,
kibanaConfig: {
'xpack.apm.forceSyntheticSource': 'true',
'logging.loggers': [apmDebugLogger],
'server.publicBaseUrl': 'http://mockedPublicBaseUrl',
},
kibanaConfig,
},
trial: {
license: 'trial' as const,
kibanaConfig: {
'xpack.apm.forceSyntheticSource': 'true',
'logging.loggers': [apmDebugLogger],
},
kibanaConfig,
},
rules: {
license: 'trial' as const,
kibanaConfig: {
...kibanaConfig,
'xpack.ruleRegistry.write.enabled': 'true',
'xpack.apm.forceSyntheticSource': 'true',
'logging.loggers': [apmDebugLogger],
},
},
cloud: {
license: 'basic' as const,
kibanaConfig: {
...kibanaConfig,
'xpack.apm.agent.migrations.enabled': 'true',
'xpack.apm.forceSyntheticSource': 'true',
'logging.loggers': [apmDebugLogger],
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import { range } from 'lodash';
import { ML_ANOMALY_SEVERITY } from '@kbn/ml-anomaly-utils/anomaly_severity';
import { FtrProviderContext } from '../../common/ftr_provider_context';
import { createAndRunApmMlJobs } from '../../common/utils/create_and_run_apm_ml_jobs';
import { createApmRule, deleteApmRules } from './helpers/alerting_api_helper';
import { createApmRule } from './helpers/alerting_api_helper';
import { waitForActiveRule } from './helpers/wait_for_active_rule';
import { cleanupRuleAndAlertState } from './helpers/cleanup_rule_and_alert_state';

export default function ApiTest({ getService }: FtrProviderContext) {
const registry = getService('registry');
Expand Down Expand Up @@ -69,14 +70,9 @@ export default function ApiTest({ getService }: FtrProviderContext) {
});

async function cleanup() {
try {
await synthtraceEsClient.clean();
await deleteApmRules(supertest);
await ml.cleanMlIndices();
logger.info('Completed cleaned up');
} catch (e) {
logger.info('Could not cleanup', e);
}
await synthtraceEsClient.clean();
await cleanupRuleAndAlertState({ es, supertest, logger });
await ml.cleanMlIndices();
}

describe('with ml jobs', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ import { omit } from 'lodash';
import { FtrProviderContext } from '../../common/ftr_provider_context';
import {
createApmRule,
deleteRuleById,
deleteAlertsByRuleId,
fetchServiceInventoryAlertCounts,
fetchServiceTabAlertCount,
ApmAlertFields,
createIndexConnector,
deleteActionConnector,
getIndexAction,
} from './helpers/alerting_api_helper';
import { cleanupAllState } from './helpers/cleanup_state';
import { cleanupRuleAndAlertState } from './helpers/cleanup_rule_and_alert_state';
import { waitForAlertsForRule } from './helpers/wait_for_alerts_for_rule';
import { waitForIndexConnectorResults } from './helpers/wait_for_index_connector_results';
import { waitForActiveRule } from './helpers/wait_for_active_rule';
Expand Down Expand Up @@ -55,8 +52,6 @@ export default function ApiTest({ getService }: FtrProviderContext) {
};

before(async () => {
cleanupAllState({ es, supertest });

const opbeansJava = apm
.service({ name: 'opbeans-java', environment: 'production', agentName: 'java' })
.instance('instance');
Expand Down Expand Up @@ -134,13 +129,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {
});

after(async () => {
try {
await deleteActionConnector({ supertest, es, actionId });
await deleteRuleById({ supertest, ruleId });
await deleteAlertsByRuleId({ es, ruleId });
} catch (e) {
logger.info('Could not delete rule or action connector', e);
}
await cleanupRuleAndAlertState({ es, supertest, logger });
});

it('checks if rule is active', async () => {
Expand Down Expand Up @@ -285,12 +274,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {
});

after(async () => {
try {
await deleteRuleById({ supertest, ruleId });
await deleteAlertsByRuleId({ es, ruleId });
} catch (e) {
logger.info('Could not delete rule', e);
}
await cleanupRuleAndAlertState({ es, supertest, logger });
});

it('produces one alert for the opbeans-php service', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { Client } from '@elastic/elasticsearch';
import { ToolingLog } from '@kbn/tooling-log';
import type { SuperTest, Test } from 'supertest';
import {
clearKibanaApmEventLog,
Expand All @@ -14,12 +15,14 @@ import {
deleteActionConnectorIndex,
} from './alerting_api_helper';

export async function cleanupAllState({
export async function cleanupRuleAndAlertState({
es,
supertest,
logger,
}: {
es: Client;
supertest: SuperTest<Test>;
logger: ToolingLog;
}) {
try {
await Promise.all([
Expand All @@ -29,7 +32,6 @@ export async function cleanupAllState({
await clearKibanaApmEventLog(es),
]);
} catch (e) {
// eslint-disable-next-line no-console
console.error(`An error occured while cleaning up the state: ${e}`);
logger.error(`An error occured while cleaning up the state: ${e}`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ import {
createApmRule,
fetchServiceInventoryAlertCounts,
fetchServiceTabAlertCount,
deleteAlertsByRuleId,
deleteRuleById,
clearKibanaApmEventLog,
ApmAlertFields,
createIndexConnector,
getIndexAction,
deleteActionConnector,
} from './helpers/alerting_api_helper';
import { cleanupAllState } from './helpers/cleanup_state';
import { cleanupRuleAndAlertState } from './helpers/cleanup_rule_and_alert_state';
import { waitForAlertsForRule } from './helpers/wait_for_alerts_for_rule';
import { waitForActiveRule } from './helpers/wait_for_active_rule';
import { waitForIndexConnectorResults } from './helpers/wait_for_index_connector_results';
Expand All @@ -49,8 +45,6 @@ export default function ApiTest({ getService }: FtrProviderContext) {

registry.when('transaction duration alert', { config: 'basic', archives: [] }, () => {
before(async () => {
cleanupAllState({ es, supertest });

const opbeansJava = apm
.service({ name: 'opbeans-java', environment: 'production', agentName: 'java' })
.instance('instance');
Expand All @@ -77,12 +71,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {
});

after(async () => {
try {
await synthtraceEsClient.clean();
await clearKibanaApmEventLog(es);
} catch (e) {
logger.info('Could not clear apm event log', e);
}
await synthtraceEsClient.clean();
});

describe('create rule for opbeans-java without kql filter', () => {
Expand Down Expand Up @@ -111,13 +100,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {
});

after(async () => {
try {
await deleteActionConnector({ supertest, es, actionId });
await deleteRuleById({ supertest, ruleId });
await deleteAlertsByRuleId({ es, ruleId });
} catch (e) {
logger.info('Could not delete rule or action connector', e);
}
await cleanupRuleAndAlertState({ es, supertest, logger });
});

it('checks if rule is active', async () => {
Expand Down Expand Up @@ -229,12 +212,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {
});

after(async () => {
try {
await deleteAlertsByRuleId({ es, ruleId });
await deleteRuleById({ supertest, ruleId });
} catch (e) {
logger.info('Could not delete rule or action connector', e);
}
await cleanupRuleAndAlertState({ es, supertest, logger });
});

it('checks if rule is active', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ import {
createApmRule,
fetchServiceInventoryAlertCounts,
fetchServiceTabAlertCount,
deleteAlertsByRuleId,
clearKibanaApmEventLog,
deleteRuleById,
ApmAlertFields,
getIndexAction,
createIndexConnector,
deleteActionConnector,
} from './helpers/alerting_api_helper';
import { cleanupAllState } from './helpers/cleanup_state';
import { cleanupRuleAndAlertState } from './helpers/cleanup_rule_and_alert_state';
import { waitForAlertsForRule } from './helpers/wait_for_alerts_for_rule';
import { waitForActiveRule } from './helpers/wait_for_active_rule';
import { waitForIndexConnectorResults } from './helpers/wait_for_index_connector_results';
Expand All @@ -38,8 +34,6 @@ export default function ApiTest({ getService }: FtrProviderContext) {

registry.when('transaction error rate alert', { config: 'basic', archives: [] }, () => {
before(async () => {
cleanupAllState({ es, supertest });

const opbeansJava = apm
.service({ name: 'opbeans-java', environment: 'production', agentName: 'java' })
.instance('instance');
Expand Down Expand Up @@ -76,12 +70,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {
});

after(async () => {
try {
await synthtraceEsClient.clean();
await clearKibanaApmEventLog(es);
} catch (e) {
logger.info('Could not clean up apm event log', e);
}
await synthtraceEsClient.clean();
});

describe('create rule without kql query', () => {
Expand Down Expand Up @@ -121,13 +110,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {
});

after(async () => {
try {
await deleteActionConnector({ supertest, es, actionId });
await deleteRuleById({ supertest, ruleId });
await deleteAlertsByRuleId({ es, ruleId });
} catch (e) {
logger.info('Could not delete rule or action connector', e);
}
await cleanupRuleAndAlertState({ es, supertest, logger });
});

it('checks if rule is active', async () => {
Expand Down Expand Up @@ -250,12 +233,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {
});

after(async () => {
try {
await deleteRuleById({ supertest, ruleId });
await deleteAlertsByRuleId({ es, ruleId });
} catch (e) {
logger.info('Could not delete rule', e);
}
await cleanupRuleAndAlertState({ es, supertest, logger });
});

it('indexes alert document with all group-by fields', async () => {
Expand Down
Loading

0 comments on commit bf64c22

Please sign in to comment.