Skip to content

Commit

Permalink
[ResponseOps] Flaky test on MKI: Alerting APIs Summary actions should…
Browse files Browse the repository at this point in the history
… schedule actions for summary of alerts on a custom interval (#170268)

Resolves #169204

## Summary

Updates wait for queries to filter on the `ruleId`. To verify the
changes used a loop to run all the alerting tests 25x.
I also noticed we were using `action` instead of `connector`, so I
cleaned up the code.


### To verify

- Follow the [docs](https://docs.elastic.dev/serverless/create-project)
to create a serverless qa project using the image in this PR
- Run the following to verify that the tests pass

```
TEST_CLOUD=1 NODE_TLS_REJECT_UNAUTHORIZED=0 TEST_ES_URL="https://elastic:PASSWORD@ES_URL:443" TEST_KIBANA_URL="https://elastic:PASSWORD@KBN_URL" node  --no-warnings scripts/functional_test_runner --config x-pack/test_serverless/api_integration/test_suites/observability/common_configs/config.group1.ts
```
  • Loading branch information
doakalexi authored Nov 2, 2023
1 parent 2aa8e6d commit 99a0adf
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,33 @@ import { runRule } from './alerting_api_helper';
export async function waitForDocumentInIndex({
esClient,
indexName,
ruleId,
num = 1,
}: {
esClient: Client;
indexName: string;
ruleId: string;
num?: number;
}): Promise<SearchResponse> {
return await pRetry(
async () => {
const response = await esClient.search({ index: indexName, sort: 'date:desc' });
const response = await esClient.search({
index: indexName,
sort: 'date:desc',
body: {
query: {
bool: {
must: [
{
term: {
'ruleId.keyword': ruleId,
},
},
],
},
},
},
});
if (response.hits.hits.length < num) {
throw new Error(`Only found ${response.hits.hits.length} / ${num} documents`);
}
Expand All @@ -38,11 +56,28 @@ export async function waitForDocumentInIndex({
export async function getDocumentsInIndex({
esClient,
indexName,
ruleId,
}: {
esClient: Client;
indexName: string;
ruleId: string;
}): Promise<SearchResponse> {
return await esClient.search({ index: indexName });
return await esClient.search({
index: indexName,
body: {
query: {
bool: {
must: [
{
term: {
'ruleId.keyword': ruleId,
},
},
],
},
},
},
});
}

export async function createIndex({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ export default function ({ getService }: FtrProviderContext) {
this.tags(['failsOnMKI']);
const RULE_TYPE_ID = '.es-query';
const ALERT_ACTION_INDEX = 'alert-action-es-query';
let actionId: string;
let connectorId: string;
let ruleId: string;

afterEach(async () => {
await supertest
.delete(`/api/actions/connector/${actionId}`)
.delete(`/api/actions/connector/${connectorId}`)
.set('kbn-xsrf', 'foo')
.set('x-elastic-internal-origin', 'foo');
await supertest
Expand All @@ -64,12 +64,12 @@ export default function ({ getService }: FtrProviderContext) {
it('should schedule task, run rule and schedule actions when appropriate', async () => {
const testStart = new Date();

const createdAction = await createIndexConnector({
const createdConnector = await createIndexConnector({
supertest,
name: 'Index Connector: Alerting API test',
indexName: ALERT_ACTION_INDEX,
});
actionId = createdAction.id;
connectorId = createdConnector.id;

const createdRule = await createEsQueryRule({
supertest,
Expand All @@ -89,7 +89,7 @@ export default function ({ getService }: FtrProviderContext) {
actions: [
{
group: 'query matched',
id: actionId,
id: connectorId,
params: {
documents: [
{
Expand Down Expand Up @@ -120,6 +120,7 @@ export default function ({ getService }: FtrProviderContext) {
const resp = await waitForDocumentInIndex({
esClient,
indexName: ALERT_ACTION_INDEX,
ruleId,
});
expect(resp.hits.hits.length).to.be(1);

Expand Down Expand Up @@ -157,12 +158,12 @@ export default function ({ getService }: FtrProviderContext) {
it('should pass updated rule params to executor', async () => {
const testStart = new Date();

const createdAction = await createIndexConnector({
const createdConnector = await createIndexConnector({
supertest,
name: 'Index Connector: Alerting API test',
indexName: ALERT_ACTION_INDEX,
});
actionId = createdAction.id;
connectorId = createdConnector.id;

const createdRule = await createEsQueryRule({
supertest,
Expand All @@ -182,7 +183,7 @@ export default function ({ getService }: FtrProviderContext) {
actions: [
{
group: 'query matched',
id: actionId,
id: connectorId,
params: {
documents: [
{
Expand Down Expand Up @@ -213,6 +214,7 @@ export default function ({ getService }: FtrProviderContext) {
const resp = await waitForDocumentInIndex({
esClient,
indexName: ALERT_ACTION_INDEX,
ruleId,
});
expect(resp.hits.hits.length).to.be(1);

Expand Down Expand Up @@ -253,6 +255,7 @@ export default function ({ getService }: FtrProviderContext) {
const resp2 = await waitForDocumentInIndex({
esClient,
indexName: ALERT_ACTION_INDEX,
ruleId,
num: 2,
});
expect(resp2.hits.hits.length).to.be(2);
Expand All @@ -276,11 +279,11 @@ export default function ({ getService }: FtrProviderContext) {
const testStart = new Date();

// Should fail
const createdAction = await createSlackConnector({
const createdConnector = await createSlackConnector({
supertest,
name: 'Slack Connector: Alerting API test',
});
actionId = createdAction.id;
connectorId = createdConnector.id;

const createdRule = await createEsQueryRule({
supertest,
Expand All @@ -300,7 +303,7 @@ export default function ({ getService }: FtrProviderContext) {
actions: [
{
group: 'query matched',
id: actionId,
id: connectorId,
params: {
message: `message: {{rule.id}}`,
},
Expand All @@ -327,12 +330,12 @@ export default function ({ getService }: FtrProviderContext) {
it('should throttle alerts when appropriate', async () => {
const testStart = new Date();

const createdAction = await createIndexConnector({
const createdConnector = await createIndexConnector({
supertest,
name: 'Index Connector: Alerting API test',
indexName: ALERT_ACTION_INDEX,
});
actionId = createdAction.id;
connectorId = createdConnector.id;

const createdRule = await createEsQueryRule({
supertest,
Expand All @@ -354,7 +357,7 @@ export default function ({ getService }: FtrProviderContext) {
actions: [
{
group: 'query matched',
id: actionId,
id: connectorId,
params: {
documents: [
{
Expand Down Expand Up @@ -394,19 +397,20 @@ export default function ({ getService }: FtrProviderContext) {
const resp = await waitForDocumentInIndex({
esClient,
indexName: ALERT_ACTION_INDEX,
ruleId,
});
expect(resp.hits.hits.length).to.be(1);
});

it('should throttle alerts with throttled action when appropriate', async () => {
const testStart = new Date();

const createdAction = await createIndexConnector({
const createdConnector = await createIndexConnector({
supertest,
name: 'Index Connector: Alerting API test',
indexName: ALERT_ACTION_INDEX,
});
actionId = createdAction.id;
connectorId = createdConnector.id;

const createdRule = await createEsQueryRule({
supertest,
Expand All @@ -427,7 +431,7 @@ export default function ({ getService }: FtrProviderContext) {
actions: [
{
group: 'query matched',
id: actionId,
id: connectorId,
params: {
documents: [
{
Expand Down Expand Up @@ -472,19 +476,20 @@ export default function ({ getService }: FtrProviderContext) {
const resp = await waitForDocumentInIndex({
esClient,
indexName: ALERT_ACTION_INDEX,
ruleId,
});
expect(resp.hits.hits.length).to.be(1);
});

it('should reset throttle window when not firing and should not throttle when changing groups', async () => {
const testStart = new Date();

const createdAction = await createIndexConnector({
const createdConnector = await createIndexConnector({
supertest,
name: 'Index Connector: Alerting API test',
indexName: ALERT_ACTION_INDEX,
});
actionId = createdAction.id;
connectorId = createdConnector.id;

const createdRule = await createEsQueryRule({
supertest,
Expand All @@ -505,7 +510,7 @@ export default function ({ getService }: FtrProviderContext) {
actions: [
{
group: 'query matched',
id: actionId,
id: connectorId,
params: {
documents: [
{
Expand All @@ -530,7 +535,7 @@ export default function ({ getService }: FtrProviderContext) {
},
{
group: 'recovered',
id: actionId,
id: connectorId,
params: {
documents: [
{
Expand Down Expand Up @@ -561,6 +566,7 @@ export default function ({ getService }: FtrProviderContext) {
const resp = await waitForDocumentInIndex({
esClient,
indexName: ALERT_ACTION_INDEX,
ruleId,
});
expect(resp.hits.hits.length).to.be(1);

Expand Down Expand Up @@ -616,6 +622,7 @@ export default function ({ getService }: FtrProviderContext) {
const resp2 = await waitForDocumentInIndex({
esClient,
indexName: ALERT_ACTION_INDEX,
ruleId,
num: 2,
});
expect(resp2.hits.hits.length).to.be(2);
Expand All @@ -625,12 +632,12 @@ export default function ({ getService }: FtrProviderContext) {
const testStart = new Date();
await createIndex({ esClient, indexName: ALERT_ACTION_INDEX });

const createdAction = await createIndexConnector({
const createdConnector = await createIndexConnector({
supertest,
name: 'Index Connector: Alerting API test',
indexName: ALERT_ACTION_INDEX,
});
actionId = createdAction.id;
connectorId = createdConnector.id;

const createdRule = await createEsQueryRule({
supertest,
Expand All @@ -651,7 +658,7 @@ export default function ({ getService }: FtrProviderContext) {
actions: [
{
group: 'query matched',
id: actionId,
id: connectorId,
params: {
documents: [
{
Expand Down Expand Up @@ -707,6 +714,7 @@ export default function ({ getService }: FtrProviderContext) {
const resp2 = await getDocumentsInIndex({
esClient,
indexName: ALERT_ACTION_INDEX,
ruleId,
});
expect(resp2.hits.hits.length).to.be(0);
});
Expand All @@ -715,12 +723,12 @@ export default function ({ getService }: FtrProviderContext) {
const testStart = new Date();
await createIndex({ esClient, indexName: ALERT_ACTION_INDEX });

const createdAction = await createIndexConnector({
const createdConnector = await createIndexConnector({
supertest,
name: 'Index Connector: Alerting API test',
indexName: ALERT_ACTION_INDEX,
});
actionId = createdAction.id;
connectorId = createdConnector.id;

const createdRule = await createEsQueryRule({
supertest,
Expand All @@ -741,7 +749,7 @@ export default function ({ getService }: FtrProviderContext) {
actions: [
{
group: 'query matched',
id: actionId,
id: connectorId,
params: {
documents: [
{
Expand Down Expand Up @@ -798,17 +806,18 @@ export default function ({ getService }: FtrProviderContext) {
const resp2 = await getDocumentsInIndex({
esClient,
indexName: ALERT_ACTION_INDEX,
ruleId,
});
expect(resp2.hits.hits.length).to.be(0);
});

it(`should unmute all instances when unmuting an alert`, async () => {
const createdAction = await createIndexConnector({
const createdConnector = await createIndexConnector({
supertest,
name: 'Index Connector: Alerting API test',
indexName: ALERT_ACTION_INDEX,
});
actionId = createdAction.id;
connectorId = createdConnector.id;

const createdRule = await createEsQueryRule({
supertest,
Expand All @@ -829,7 +838,7 @@ export default function ({ getService }: FtrProviderContext) {
actions: [
{
group: 'query matched',
id: actionId,
id: connectorId,
params: {
documents: [
{
Expand Down Expand Up @@ -881,6 +890,7 @@ export default function ({ getService }: FtrProviderContext) {
const resp = await waitForDocumentInIndex({
esClient,
indexName: ALERT_ACTION_INDEX,
ruleId,
});
expect(resp.hits.hits.length).to.be(1);
});
Expand Down
Loading

0 comments on commit 99a0adf

Please sign in to comment.