-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
# Backport This will backport the following commits from `main` to `8.7`: - [[ML] update_trained_models_spaces api tests (#150901)](#150901) <!--- Backport version: 8.9.7 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"James Gowdy","email":"jgowdy@elastic.co"},"sourceCommit":{"committedDate":"2023-02-10T16:53:10Z","message":"[ML] update_trained_models_spaces api tests (#150901)\n\nAdds tests for the `update_trained_models_spaces` endpoint","sha":"648786e5329ebe6a397fa423f47a4d0b6fbe08cd","branchLabelMapping":{"^v8.8.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["non-issue",":ml","release_note:skip","v8.7.0","v8.8.0"],"number":150901,"url":"#150901 update_trained_models_spaces api tests (#150901)\n\nAdds tests for the `update_trained_models_spaces` endpoint","sha":"648786e5329ebe6a397fa423f47a4d0b6fbe08cd"}},"sourceBranch":"main","suggestedTargetBranches":["8.7"],"targetPullRequestStates":[{"branch":"8.7","label":"v8.7.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.8.0","labelRegex":"^v8.8.0$","isSourceBranch":true,"state":"MERGED","url":"#150901 update_trained_models_spaces api tests (#150901)\n\nAdds tests for the `update_trained_models_spaces` endpoint","sha":"648786e5329ebe6a397fa423f47a4d0b6fbe08cd"}}]}] BACKPORT--> Co-authored-by: James Gowdy <jgowdy@elastic.co>
- Loading branch information
1 parent
99d4935
commit da60218
Showing
3 changed files
with
103 additions
and
4 deletions.
There are no files selected for viewing
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
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
98 changes: 98 additions & 0 deletions
98
x-pack/test/api_integration/apis/ml/saved_objects/update_trained_model_spaces.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,98 @@ | ||
/* | ||
* 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 ml = getService('ml'); | ||
const spacesService = getService('spaces'); | ||
const supertest = getService('supertestWithoutAuth'); | ||
|
||
const trainedModelId = 'trained_model'; | ||
const idSpace1 = 'space1'; | ||
const idSpace2 = 'space2'; | ||
const defaultSpaceId = 'default'; | ||
|
||
async function runRequest( | ||
requestBody: { | ||
modelIds: string[]; | ||
spacesToAdd: string[]; | ||
spacesToRemove: string[]; | ||
}, | ||
expectedStatusCode: number, | ||
user: USER | ||
) { | ||
const { body, status } = await supertest | ||
.post(`/api/ml/saved_objects/update_trained_models_spaces`) | ||
.auth(user, ml.securityCommon.getPasswordForUser(user)) | ||
.set(COMMON_REQUEST_HEADERS) | ||
.send(requestBody); | ||
ml.api.assertResponseStatusCode(expectedStatusCode, status, body); | ||
|
||
return body; | ||
} | ||
|
||
describe('POST saved_objects/update_trained_models_spaces', () => { | ||
before(async () => { | ||
await spacesService.create({ id: idSpace1, name: 'space_one', disabledFeatures: [] }); | ||
await spacesService.create({ id: idSpace2, name: 'space_two', disabledFeatures: [] }); | ||
|
||
await ml.testResources.setKibanaTimeZoneToUTC(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
// Create trained model | ||
const trainedModelConfig = ml.api.createTestTrainedModelConfig(trainedModelId, 'regression'); | ||
await ml.api.createTrainedModel(trainedModelId, trainedModelConfig.body); | ||
}); | ||
|
||
afterEach(async () => { | ||
await ml.api.cleanMlIndices(); | ||
await ml.testResources.cleanMLSavedObjects(); | ||
}); | ||
|
||
after(async () => { | ||
await spacesService.delete(idSpace1); | ||
await spacesService.delete(idSpace2); | ||
}); | ||
|
||
it('should assign trained model to space for user with access to that space', async () => { | ||
await ml.api.assertTrainedModelSpaces(trainedModelId, [defaultSpaceId]); | ||
const body = await runRequest( | ||
{ | ||
modelIds: [trainedModelId], | ||
spacesToAdd: [idSpace1], | ||
spacesToRemove: [defaultSpaceId], | ||
}, | ||
200, | ||
USER.ML_POWERUSER_SPACE1 | ||
); | ||
|
||
expect(body).to.eql({ [trainedModelId]: { type: 'trained-model', success: true } }); | ||
await ml.api.assertTrainedModelSpaces(trainedModelId, [idSpace1]); | ||
}); | ||
|
||
it('should fail to update trained model spaces for space the user has no access to', async () => { | ||
await ml.api.assertTrainedModelSpaces(trainedModelId, [defaultSpaceId]); | ||
const body = await runRequest( | ||
{ | ||
modelIds: [trainedModelId], | ||
spacesToAdd: [idSpace2], | ||
spacesToRemove: [], | ||
}, | ||
200, | ||
USER.ML_POWERUSER_SPACE1 | ||
); | ||
|
||
expect(body[trainedModelId]).to.have.property('success', false); | ||
await ml.api.assertTrainedModelSpaces(trainedModelId, [defaultSpaceId]); | ||
}); | ||
}); | ||
}; |