[ML] Tests for get groups endpoint (#133625)

* [ML] Tests for get groups endpoint

* adding extra calendar

* removing delete index pattern
This commit is contained in:
James Gowdy 2022-06-07 16:58:11 +01:00 committed by GitHub
parent 1c8f6ffb2f
commit e10f85f0d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 121 additions and 8 deletions

View file

@ -0,0 +1,12 @@
/*
* 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.
*/
export interface Group {
id: string;
jobIds: string[];
calendarIds: string[];
}

View file

@ -37,7 +37,7 @@ export const JobFilterBar: FC<JobFilterBarProps> = ({ queryText, setFilters }) =
const loadGroups = useCallback(async () => {
try {
const response = await mlApiServices.jobs.groups();
return response.map((g: any) => ({
return response.map((g) => ({
value: g.id,
view: (
<div className="group-item">

View file

@ -20,6 +20,7 @@ import type {
import type { JobMessage } from '../../../../common/types/audit_message';
import type { JobAction } from '../../../../common/constants/job_actions';
import type { AggFieldNamePair, RuntimeMappings } from '../../../../common/types/fields';
import type { Group } from '../../../../common/types/groups';
import type { ExistingJobsAndGroups } from '../job_service';
import type {
CategorizationAnalyzer,
@ -83,7 +84,7 @@ export const jobsApiProvider = (httpService: HttpService) => ({
},
groups() {
return httpService.http<any>({
return httpService.http<Group[]>({
path: `${ML_BASE_PATH}/jobs/groups`,
method: 'GET',
});

View file

@ -7,14 +7,9 @@
import { CalendarManager } from '../calendar';
import { GLOBAL_CALENDAR } from '../../../common/constants/calendars';
import type { Group } from '../../../common/types/groups';
import type { MlClient } from '../../lib/ml_client';
export interface Group {
id: string;
jobIds: string[];
calendarIds: string[];
}
export interface Results {
[id: string]: {
success: boolean;

View file

@ -0,0 +1,104 @@
/*
* 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 type { Group } from '@kbn/ml-plugin/common/types/groups';
import { FtrProviderContext } from '../../../ftr_provider_context';
import { COMMON_REQUEST_HEADERS } from '../../../../functional/services/ml/common_api';
import { USER } from '../../../../functional/services/ml/security_common';
import { MULTI_METRIC_JOB_CONFIG, SINGLE_METRIC_JOB_CONFIG } from './common_jobs';
export default ({ getService }: FtrProviderContext) => {
const esArchiver = getService('esArchiver');
const supertest = getService('supertestWithoutAuth');
const ml = getService('ml');
const testSetupJobConfigs = [SINGLE_METRIC_JOB_CONFIG, MULTI_METRIC_JOB_CONFIG];
const testCalendarsConfigs = [
{
calendar_id: `test_get_cal_1`,
job_ids: ['multi-metric'],
description: `Test calendar 1`,
},
{
calendar_id: `test_get_cal_2`,
job_ids: [MULTI_METRIC_JOB_CONFIG.job_id, 'multi-metric'],
description: `Test calendar 2`,
},
{
calendar_id: `test_get_cal_3`,
job_ids: ['brand-new-group'],
description: `Test calendar 3`,
},
];
async function runGetGroupsRequest(user: USER, expectedResponsecode: number): Promise<Group[]> {
const { body, status } = await supertest
.get('/api/ml/jobs/groups')
.auth(user, ml.securityCommon.getPasswordForUser(user))
.set(COMMON_REQUEST_HEADERS);
ml.api.assertResponseStatusCode(expectedResponsecode, status, body);
return body;
}
const expectedGroups = [
{
id: 'automated',
jobIds: [MULTI_METRIC_JOB_CONFIG.job_id, SINGLE_METRIC_JOB_CONFIG.job_id],
calendarIds: [],
},
{
id: 'brand-new-group',
jobIds: [],
calendarIds: ['test_get_cal_3'],
},
{
id: 'farequote',
jobIds: [MULTI_METRIC_JOB_CONFIG.job_id, SINGLE_METRIC_JOB_CONFIG.job_id],
calendarIds: [],
},
{
id: 'multi-metric',
jobIds: [MULTI_METRIC_JOB_CONFIG.job_id],
calendarIds: ['test_get_cal_1', 'test_get_cal_2'],
},
{
id: 'single-metric',
jobIds: [SINGLE_METRIC_JOB_CONFIG.job_id],
calendarIds: [],
},
];
describe('get groups', function () {
before(async () => {
await esArchiver.loadIfNeeded('x-pack/test/functional/es_archives/ml/farequote');
await ml.testResources.setKibanaTimeZoneToUTC();
for (const job of testSetupJobConfigs) {
await ml.api.createAnomalyDetectionJob(job);
}
for (const cal of testCalendarsConfigs) {
await ml.api.createCalendar(cal.calendar_id, cal);
}
});
after(async () => {
await ml.api.cleanMlIndices();
});
it('returns expected list of groups', async () => {
const groups = await runGetGroupsRequest(USER.ML_VIEWER, 200);
expect(groups).to.eql(
expectedGroups,
`response groups list should equal the expected groups ${JSON.stringify(expectedGroups)})`
);
});
});
};

View file

@ -22,5 +22,6 @@ export default function ({ loadTestFile }: FtrProviderContext) {
loadTestFile(require.resolve('./force_start_datafeeds_spaces'));
loadTestFile(require.resolve('./stop_datafeeds'));
loadTestFile(require.resolve('./stop_datafeeds_spaces'));
loadTestFile(require.resolve('./get_groups'));
});
}