mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
[8.6] [ML] Api Integration tests: adds tests for 'api/ml/results/datafeed_results_chart' (#145619) (#145933)
# Backport This will backport the following commits from `main` to `8.6`: - [[ML] Api Integration tests: adds tests for 'api/ml/results/datafeed_results_chart' (#145619)](https://github.com/elastic/kibana/pull/145619) <!--- Backport version: 8.9.7 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Melissa Alvarez","email":"melissa.alvarez@elastic.co"},"sourceCommit":{"committedDate":"2022-11-21T20:19:07Z","message":"[ML] Api Integration tests: adds tests for 'api/ml/results/datafeed_results_chart' (#145619)\n\n## Summary\r\n\r\nRelated meta issue https://github.com/elastic/kibana/issues/142456\r\n\r\nThis PR adds api integration tests for the\r\n`api/ml/results/datafeed_results_chart` endpoint\r\n\r\n\r\n### Checklist\r\n\r\nDelete any items that are not applicable to this PR.\r\n\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios\r\n\r\nCo-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>","sha":"a74bfee93f174e9cde586c27746053f71d899b55","branchLabelMapping":{"^v8.7.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":[":ml","release_note:skip","test-api-integration","v8.6.0","v8.7.0"],"number":145619,"url":"https://github.com/elastic/kibana/pull/145619","mergeCommit":{"message":"[ML] Api Integration tests: adds tests for 'api/ml/results/datafeed_results_chart' (#145619)\n\n## Summary\r\n\r\nRelated meta issue https://github.com/elastic/kibana/issues/142456\r\n\r\nThis PR adds api integration tests for the\r\n`api/ml/results/datafeed_results_chart` endpoint\r\n\r\n\r\n### Checklist\r\n\r\nDelete any items that are not applicable to this PR.\r\n\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios\r\n\r\nCo-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>","sha":"a74bfee93f174e9cde586c27746053f71d899b55"}},"sourceBranch":"main","suggestedTargetBranches":["8.6"],"targetPullRequestStates":[{"branch":"8.6","label":"v8.6.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.7.0","labelRegex":"^v8.7.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/145619","number":145619,"mergeCommit":{"message":"[ML] Api Integration tests: adds tests for 'api/ml/results/datafeed_results_chart' (#145619)\n\n## Summary\r\n\r\nRelated meta issue https://github.com/elastic/kibana/issues/142456\r\n\r\nThis PR adds api integration tests for the\r\n`api/ml/results/datafeed_results_chart` endpoint\r\n\r\n\r\n### Checklist\r\n\r\nDelete any items that are not applicable to this PR.\r\n\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios\r\n\r\nCo-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>","sha":"a74bfee93f174e9cde586c27746053f71d899b55"}}]}] BACKPORT--> Co-authored-by: Melissa Alvarez <melissa.alvarez@elastic.co>
This commit is contained in:
parent
33fe536a4e
commit
17e2fecfa2
2 changed files with 127 additions and 0 deletions
|
@ -0,0 +1,126 @@
|
|||
/*
|
||||
* 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 expect from '@kbn/expect';
|
||||
import { Datafeed, Job } from '@kbn/ml-plugin/common/types/anomaly_detection_jobs';
|
||||
import { ANNOTATION_TYPE } from '@kbn/ml-plugin/common/constants/annotations';
|
||||
import { FtrProviderContext } from '../../../ftr_provider_context';
|
||||
import { USER } from '../../../../functional/services/ml/security_common';
|
||||
import { COMMON_REQUEST_HEADERS } from '../../../../functional/services/ml/common_api';
|
||||
|
||||
export default ({ getService }: FtrProviderContext) => {
|
||||
const esArchiver = getService('esArchiver');
|
||||
const supertest = getService('supertestWithoutAuth');
|
||||
const ml = getService('ml');
|
||||
|
||||
// @ts-expect-error not full interface
|
||||
const JOB_CONFIG: Job = {
|
||||
job_id: `fq_multi_1_ae`,
|
||||
description:
|
||||
'mean/min/max(responsetime) partition=airline on farequote dataset with 1h bucket span',
|
||||
groups: ['farequote', 'automated', 'multi-metric'],
|
||||
analysis_config: {
|
||||
bucket_span: '1h',
|
||||
influencers: ['airline'],
|
||||
detectors: [
|
||||
{ function: 'mean', field_name: 'responsetime', partition_field_name: 'airline' },
|
||||
{ function: 'min', field_name: 'responsetime', partition_field_name: 'airline' },
|
||||
{ function: 'max', field_name: 'responsetime', partition_field_name: 'airline' },
|
||||
],
|
||||
},
|
||||
data_description: { time_field: '@timestamp' },
|
||||
analysis_limits: { model_memory_limit: '20mb' },
|
||||
model_plot_config: { enabled: true },
|
||||
};
|
||||
|
||||
// @ts-expect-error not full interface
|
||||
const DATAFEED_CONFIG: Datafeed = {
|
||||
datafeed_id: 'datafeed-fq_multi_1_ae',
|
||||
indices: ['ft_farequote'],
|
||||
job_id: 'fq_multi_1_ae',
|
||||
query: { bool: { must: [{ match_all: {} }] } },
|
||||
};
|
||||
|
||||
async function createMockJobs() {
|
||||
await ml.api.createAndRunAnomalyDetectionLookbackJob(JOB_CONFIG, DATAFEED_CONFIG);
|
||||
await ml.api.indexAnnotation({
|
||||
timestamp: 1454950800000,
|
||||
end_timestamp: 1454950860000,
|
||||
annotation: 'Test annotation',
|
||||
job_id: JOB_CONFIG.job_id,
|
||||
type: ANNOTATION_TYPE.ANNOTATION,
|
||||
event: 'user',
|
||||
detector_index: 1,
|
||||
partition_field_name: 'airline',
|
||||
partition_field_value: 'AAL',
|
||||
});
|
||||
}
|
||||
|
||||
const requestBody = {
|
||||
jobId: JOB_CONFIG.job_id,
|
||||
start: 1454889600000, // February 8, 2016 12:00:00 AM GMT
|
||||
end: 1454976000000, // February 9, 2016 12:00:00 AM GMT
|
||||
};
|
||||
|
||||
describe('GetDatafeedResultsChart', () => {
|
||||
before(async () => {
|
||||
await esArchiver.loadIfNeeded('x-pack/test/functional/es_archives/ml/farequote');
|
||||
await ml.testResources.setKibanaTimeZoneToUTC();
|
||||
await createMockJobs();
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await ml.api.cleanMlIndices();
|
||||
});
|
||||
|
||||
it('it should fetch datafeed chart data', async () => {
|
||||
const { body, status } = await supertest
|
||||
.post(`/api/ml/results/datafeed_results_chart`)
|
||||
.auth(USER.ML_VIEWER, ml.securityCommon.getPasswordForUser(USER.ML_VIEWER))
|
||||
.set(COMMON_REQUEST_HEADERS)
|
||||
.send(requestBody);
|
||||
ml.api.assertResponseStatusCode(200, status, body);
|
||||
|
||||
expect(body.bucketResults.length).to.eql(24);
|
||||
expect(body.datafeedResults.length).to.eql(24);
|
||||
expect(body.annotationResultsRect.length).to.eql(1);
|
||||
expect(body.annotationResultsLine.length).to.eql(0);
|
||||
});
|
||||
|
||||
it('should validate request body', async () => {
|
||||
const incompleteRequestBody = {
|
||||
// MISSING JOB ID
|
||||
start: 1454889600000, // February 8, 2016 12:00:00 AM GMT
|
||||
end: 1454976000000, // February 9, 2016 12:00:00 AM GMT
|
||||
};
|
||||
|
||||
const { body, status } = await supertest
|
||||
.post(`/api/ml/results/datafeed_results_chart`)
|
||||
.auth(USER.ML_VIEWER, ml.securityCommon.getPasswordForUser(USER.ML_VIEWER))
|
||||
.set(COMMON_REQUEST_HEADERS)
|
||||
.send(incompleteRequestBody);
|
||||
ml.api.assertResponseStatusCode(400, status, body);
|
||||
|
||||
expect(body.error).to.eql('Bad Request');
|
||||
expect(body.message).to.eql(
|
||||
'[request body.jobId]: expected value of type [string] but got [undefined]'
|
||||
);
|
||||
});
|
||||
|
||||
it('it should not allow fetching of datafeed chart data without required permissions', async () => {
|
||||
const { body, status } = await supertest
|
||||
.post(`/api/ml/results/datafeed_results_chart`)
|
||||
.auth(USER.ML_UNAUTHORIZED, ml.securityCommon.getPasswordForUser(USER.ML_UNAUTHORIZED))
|
||||
.set(COMMON_REQUEST_HEADERS)
|
||||
.send(requestBody);
|
||||
ml.api.assertResponseStatusCode(403, status, body);
|
||||
|
||||
expect(body.error).to.eql('Forbidden');
|
||||
expect(body.message).to.eql('Forbidden');
|
||||
});
|
||||
});
|
||||
};
|
|
@ -17,5 +17,6 @@ export default function ({ loadTestFile }: FtrProviderContext) {
|
|||
loadTestFile(require.resolve('./max_anomaly_score'));
|
||||
loadTestFile(require.resolve('./get_partition_fields_values'));
|
||||
loadTestFile(require.resolve('./get_anomaly_search'));
|
||||
loadTestFile(require.resolve('./get_datafeed_results_chart'));
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue