[ML] Api tests for ml/results/anomaly_search (#142454)

* [ML] Api tests for ml/results/anomaly_search

* changes based on review
This commit is contained in:
James Gowdy 2022-10-03 13:28:43 +01:00 committed by GitHub
parent 4db7564209
commit bea4228569
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 107 additions and 0 deletions

View file

@ -0,0 +1,106 @@
/*
* 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 { 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 ml = getService('ml');
const spacesService = getService('spaces');
const supertest = getService('supertestWithoutAuth');
const adJobId = 'fq_single';
const idSpace1 = 'space1';
const idSpace2 = 'space2';
const jobQuery = {
size: 1,
body: {
query: {
bool: {
filter: [{ term: { job_id: adJobId } }],
},
},
},
};
async function runRequest(
requestBody: {
jobIds: string[];
query: any;
},
space: string,
expectedStatusCode: number,
user: USER
) {
const { body, status } = await supertest
.post(`/s/${space}/api/ml/results/anomaly_search`)
.auth(user, ml.securityCommon.getPasswordForUser(user))
.set(COMMON_REQUEST_HEADERS)
.send(requestBody);
ml.api.assertResponseStatusCode(expectedStatusCode, status, body);
return body;
}
describe('POST results/anomaly_search', () => {
before(async () => {
await ml.testResources.setKibanaTimeZoneToUTC();
await esArchiver.loadIfNeeded('x-pack/test/functional/es_archives/ml/farequote');
// create spaces
await spacesService.create({ id: idSpace1, name: 'space_one', disabledFeatures: [] });
await spacesService.create({ id: idSpace2, name: 'space_two', disabledFeatures: [] });
await ml.api.createAndRunAnomalyDetectionLookbackJob(
ml.commonConfig.getADFqSingleMetricJobConfig(adJobId),
ml.commonConfig.getADFqDatafeedConfig(adJobId),
idSpace1
);
await ml.api.assertJobSpaces(adJobId, 'anomaly-detector', [idSpace1]);
});
after(async () => {
await ml.api.cleanMlIndices();
await ml.testResources.cleanMLSavedObjects();
await spacesService.delete(idSpace1);
await spacesService.delete(idSpace2);
});
it('should see results in current space', async () => {
const body = await runRequest(
{
jobIds: [adJobId],
query: jobQuery,
},
idSpace1,
200,
USER.ML_POWERUSER
);
expect(body.hits.hits[0]._source.job_id).to.eql(
adJobId,
`Expected job_id to equal ${adJobId}`
);
});
it('should not see results in different space', async () => {
const body = await runRequest(
{
jobIds: [adJobId],
query: jobQuery,
},
idSpace2,
404,
USER.ML_POWERUSER
);
expect(body.message).to.eql(`${adJobId} missing`);
});
});
};

View file

@ -16,5 +16,6 @@ export default function ({ loadTestFile }: FtrProviderContext) {
loadTestFile(require.resolve('./get_category_examples'));
loadTestFile(require.resolve('./max_anomaly_score'));
loadTestFile(require.resolve('./get_partition_fields_values'));
loadTestFile(require.resolve('./get_anomaly_search'));
});
}