Skip to content

Commit

Permalink
[ML] Api tests for ml/results/anomaly_search (elastic#142454)
Browse files Browse the repository at this point in the history
* [ML] Api tests for ml/results/anomaly_search

* changes based on review
  • Loading branch information
jgowdyelastic authored and WafaaNasr committed Oct 11, 2022
1 parent f656de8 commit 3d1b703
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
106 changes: 106 additions & 0 deletions x-pack/test/api_integration/apis/ml/results/get_anomaly_search.ts
Original file line number Diff line number Diff line change
@@ -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`);
});
});
};
1 change: 1 addition & 0 deletions x-pack/test/api_integration/apis/ml/results/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
}

0 comments on commit 3d1b703

Please sign in to comment.