forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Api tests for ml/results/anomaly_search (elastic#142454)
* [ML] Api tests for ml/results/anomaly_search * changes based on review
- Loading branch information
1 parent
f656de8
commit 3d1b703
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
x-pack/test/api_integration/apis/ml/results/get_anomaly_search.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,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`); | ||
}); | ||
}); | ||
}; |
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