-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Data Frame Analytics: adds api integration tests for job creation (
#92101) * add create endpoint integration tests * update test suite name
- Loading branch information
1 parent
df8a8cb
commit c0de668
Showing
2 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
x-pack/test/api_integration/apis/ml/data_frame_analytics/create_job.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,143 @@ | ||
/* | ||
* 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 { DataFrameAnalyticsConfig } from '../../../../../plugins/ml/public/application/data_frame_analytics/common'; | ||
import { DeepPartial } from '../../../../../plugins/ml/common/types/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'); | ||
|
||
const jobId = `bm_${Date.now()}`; | ||
const generateDestinationIndex = (analyticsId: string) => `user-${analyticsId}`; | ||
const commonJobConfig = { | ||
source: { | ||
index: ['ft_bank_marketing'], | ||
query: { | ||
match_all: {}, | ||
}, | ||
}, | ||
analyzed_fields: { | ||
includes: [], | ||
excludes: [], | ||
}, | ||
model_memory_limit: '60mb', | ||
allow_lazy_start: false, // default value | ||
max_num_threads: 1, // default value | ||
}; | ||
|
||
const jobTypes = ['classification', 'regression', 'outlier_detection']; | ||
const jobAnalyses: any = { | ||
classification: { | ||
dependent_variable: 'y', | ||
training_percent: 20, | ||
}, | ||
regression: { | ||
dependent_variable: 'y', | ||
training_percent: 20, | ||
}, | ||
outlier_detection: { | ||
compute_feature_influence: true, | ||
standardization_enabled: true, | ||
}, | ||
}; | ||
|
||
const testJobConfigs: Array<{ | ||
jobId: string; | ||
jobType: string; | ||
config: DeepPartial<DataFrameAnalyticsConfig>; | ||
}> = ['Test classification job', 'Test regression job', 'Test outlier detection job'].map( | ||
(description, idx) => { | ||
const analyticsId = `${jobId}_${idx}`; | ||
const jobType = jobTypes[idx]; | ||
return { | ||
jobId: analyticsId, | ||
jobType, | ||
config: { | ||
description, | ||
dest: { | ||
index: generateDestinationIndex(analyticsId), | ||
results_field: 'ml', | ||
}, | ||
analysis: { [jobType]: jobAnalyses[jobType] }, | ||
...commonJobConfig, | ||
}, | ||
}; | ||
} | ||
); | ||
|
||
describe('PUT data_frame/analytics/{analyticsId}', () => { | ||
before(async () => { | ||
await esArchiver.loadIfNeeded('ml/bm_classification'); | ||
await ml.testResources.setKibanaTimeZoneToUTC(); | ||
}); | ||
|
||
after(async () => { | ||
await ml.api.cleanMlIndices(); | ||
}); | ||
|
||
describe('CreateDataFrameAnalytics', () => { | ||
testJobConfigs.forEach((testConfig) => { | ||
it(`should create ${testConfig.jobType} job with given config`, async () => { | ||
const analyticsId = `${testConfig.jobId}`; | ||
const requestBody = testConfig.config; | ||
|
||
const { body } = await supertest | ||
.put(`/api/ml/data_frame/analytics/${analyticsId}`) | ||
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER)) | ||
.set(COMMON_REQUEST_HEADERS) | ||
.send(requestBody) | ||
.expect(200); | ||
|
||
expect(body).not.to.be(undefined); | ||
|
||
expect(body.description).to.eql(requestBody.description); | ||
expect(body.allow_lazy_start).to.eql(requestBody.allow_lazy_start); | ||
expect(body.model_memory_limit).to.eql(requestBody.model_memory_limit); | ||
expect(body.max_num_threads).to.eql(requestBody.max_num_threads); | ||
|
||
expect(Object.keys(body.analysis)).to.eql(Object.keys(requestBody.analysis!)); | ||
}); | ||
}); | ||
|
||
it('should not allow analytics job creation for unauthorized user', async () => { | ||
const analyticsId = `${testJobConfigs[0].jobId}`; | ||
const requestBody = testJobConfigs[0].config; | ||
|
||
const { body } = await supertest | ||
.put(`/api/ml/data_frame/analytics/${analyticsId}`) | ||
.auth(USER.ML_UNAUTHORIZED, ml.securityCommon.getPasswordForUser(USER.ML_UNAUTHORIZED)) | ||
.set(COMMON_REQUEST_HEADERS) | ||
.send(requestBody) | ||
.expect(403); | ||
|
||
expect(body.error).to.eql('Forbidden'); | ||
expect(body.message).to.eql('Forbidden'); | ||
}); | ||
|
||
it('should not allow analytics job creation for the user with only view permission', async () => { | ||
const analyticsId = `${testJobConfigs[0].jobId}`; | ||
const requestBody = testJobConfigs[0].config; | ||
|
||
const { body } = await supertest | ||
.put(`/api/ml/data_frame/analytics/${analyticsId}`) | ||
.auth(USER.ML_VIEWER, ml.securityCommon.getPasswordForUser(USER.ML_VIEWER)) | ||
.set(COMMON_REQUEST_HEADERS) | ||
.send(requestBody) | ||
.expect(403); | ||
|
||
expect(body.error).to.eql('Forbidden'); | ||
expect(body.message).to.eql('Forbidden'); | ||
}); | ||
}); | ||
}); | ||
}; |
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