-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AO][SERVERLESS] Create serverless integration tests for the Threshol…
…d rule #161458 (#161569) ## Summary It fixes #161458 by adding API integration tests for the Threshold rule, with many scenarios (file per scenario), and each scenario has a complete life-cycle ### The scenario life-cycle - Generating data using the `fake_host` dataset from the high-card - Create a DataView based on the generated data - Create the rule and wait to be active - Get the fired alert and matches its value - Clean up ### The covered scenarios - Avg. percentage, fires alert - Avg. percentage, fires alert with no data - Custom equation on bytes filed, fires alert - Doc count, fires alert - Group by two fields, fires alert. ---------
- Loading branch information
Showing
14 changed files
with
1,252 additions
and
2 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
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
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
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
69 changes: 69 additions & 0 deletions
69
.../test_serverless/api_integration/test_suites/observability/helpers/alerting_api_helper.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,69 @@ | ||
/* | ||
* 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 { MetricThresholdParams } from '@kbn/infra-plugin/common/alerting/metrics'; | ||
import { ThresholdParams } from '@kbn/observability-plugin/common/threshold_rule/types'; | ||
import type { SuperTest, Test } from 'supertest'; | ||
|
||
export async function createIndexConnector({ | ||
supertest, | ||
name, | ||
indexName, | ||
}: { | ||
supertest: SuperTest<Test>; | ||
name: string; | ||
indexName: string; | ||
}) { | ||
const { body } = await supertest | ||
.post(`/api/actions/connector`) | ||
.set('kbn-xsrf', 'foo') | ||
.send({ | ||
name, | ||
config: { | ||
index: indexName, | ||
refresh: true, | ||
}, | ||
connector_type_id: '.index', | ||
}); | ||
return body.id as string; | ||
} | ||
|
||
export async function createRule({ | ||
supertest, | ||
name, | ||
ruleTypeId, | ||
params, | ||
actions = [], | ||
tags = [], | ||
schedule, | ||
consumer, | ||
}: { | ||
supertest: SuperTest<Test>; | ||
ruleTypeId: string; | ||
name: string; | ||
params: MetricThresholdParams | ThresholdParams; | ||
actions?: any[]; | ||
tags?: any[]; | ||
schedule?: { interval: string }; | ||
consumer: string; | ||
}) { | ||
const { body } = await supertest | ||
.post(`/api/alerting/rule`) | ||
.set('kbn-xsrf', 'foo') | ||
.send({ | ||
params, | ||
consumer, | ||
schedule: schedule || { | ||
interval: '5m', | ||
}, | ||
tags, | ||
name, | ||
rule_type_id: ruleTypeId, | ||
actions, | ||
}); | ||
return body; | ||
} |
87 changes: 87 additions & 0 deletions
87
...serverless/api_integration/test_suites/observability/helpers/alerting_wait_for_helpers.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,87 @@ | ||
/* | ||
* 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 pRetry from 'p-retry'; | ||
|
||
import type SuperTest from 'supertest'; | ||
import type { Client } from '@elastic/elasticsearch'; | ||
import type { | ||
AggregationsAggregate, | ||
SearchResponse, | ||
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; | ||
|
||
export async function waitForRuleStatus({ | ||
id, | ||
expectedStatus, | ||
supertest, | ||
}: { | ||
id: string; | ||
expectedStatus: string; | ||
supertest: SuperTest.SuperTest<SuperTest.Test>; | ||
}): Promise<Record<string, any>> { | ||
return pRetry( | ||
async () => { | ||
const response = await supertest.get(`/api/alerting/rule/${id}`); | ||
const { execution_status: executionStatus } = response.body || {}; | ||
const { status } = executionStatus || {}; | ||
if (status !== expectedStatus) { | ||
throw new Error(`waitForStatus(${expectedStatus}): got ${status}`); | ||
} | ||
return executionStatus; | ||
}, | ||
{ retries: 10 } | ||
); | ||
} | ||
|
||
export async function waitForDocumentInIndex<T>({ | ||
esClient, | ||
indexName, | ||
}: { | ||
esClient: Client; | ||
indexName: string; | ||
}): Promise<SearchResponse<T, Record<string, AggregationsAggregate>>> { | ||
return pRetry( | ||
async () => { | ||
const response = await esClient.search<T>({ index: indexName }); | ||
if (response.hits.hits.length === 0) { | ||
throw new Error('No hits found'); | ||
} | ||
return response; | ||
}, | ||
{ retries: 10 } | ||
); | ||
} | ||
|
||
export async function waitForAlertInIndex<T>({ | ||
esClient, | ||
indexName, | ||
ruleId, | ||
}: { | ||
esClient: Client; | ||
indexName: string; | ||
ruleId: string; | ||
}): Promise<SearchResponse<T, Record<string, AggregationsAggregate>>> { | ||
return pRetry( | ||
async () => { | ||
const response = await esClient.search<T>({ | ||
index: indexName, | ||
body: { | ||
query: { | ||
term: { | ||
'kibana.alert.rule.uuid': ruleId, | ||
}, | ||
}, | ||
}, | ||
}); | ||
if (response.hits.hits.length === 0) { | ||
throw new Error('No hits found'); | ||
} | ||
return response; | ||
}, | ||
{ retries: 10 } | ||
); | ||
} |
59 changes: 59 additions & 0 deletions
59
x-pack/test_serverless/api_integration/test_suites/observability/helpers/data_view.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,59 @@ | ||
/* | ||
* 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 { SuperTest, Test } from 'supertest'; | ||
|
||
export const createDataView = async ({ | ||
supertest, | ||
id, | ||
name, | ||
title, | ||
}: { | ||
supertest: SuperTest<Test>; | ||
id: string; | ||
name: string; | ||
title: string; | ||
}) => { | ||
const { body } = await supertest | ||
.post(`/api/content_management/rpc/create`) | ||
.set('kbn-xsrf', 'foo') | ||
.send({ | ||
contentTypeId: 'index-pattern', | ||
data: { | ||
fieldAttrs: '{}', | ||
title, | ||
timeFieldName: '@timestamp', | ||
sourceFilters: '[]', | ||
fields: '[]', | ||
fieldFormatMap: '{}', | ||
typeMeta: '{}', | ||
runtimeFieldMap: '{}', | ||
name, | ||
}, | ||
options: { id }, | ||
version: 1, | ||
}); | ||
return body; | ||
}; | ||
export const deleteDataView = async ({ | ||
supertest, | ||
id, | ||
}: { | ||
supertest: SuperTest<Test>; | ||
id: string; | ||
}) => { | ||
const { body } = await supertest | ||
.post(`/api/content_management/rpc/delete`) | ||
.set('kbn-xsrf', 'foo') | ||
.send({ | ||
contentTypeId: 'index-pattern', | ||
id, | ||
options: { force: true }, | ||
version: 1, | ||
}); | ||
return body; | ||
}; |
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
Oops, something went wrong.