Skip to content

Commit

Permalink
[8.7] [ML] update_trained_models_spaces api tests (#150901) (#150925)
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
kibanamachine and jgowdyelastic committed Feb 10, 2023
1 parent 99d4935 commit da60218
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 4 deletions.
1 change: 1 addition & 0 deletions x-pack/test/api_integration/apis/ml/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default function ({ loadTestFile }: FtrProviderContext) {
loadTestFile(require.resolve('./sync_jobs'));
loadTestFile(require.resolve('./sync_trained_models'));
loadTestFile(require.resolve('./update_jobs_spaces'));
loadTestFile(require.resolve('./update_trained_model_spaces'));
loadTestFile(require.resolve('./remove_from_current_space'));
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export default ({ getService }: FtrProviderContext) => {
});

it('should assign AD job to space for user with access to that space', async () => {
await ml.api.assertJobSpaces(adJobId, 'anomaly-detector', [defaultSpaceId]);
const jobType = 'anomaly-detector';
await ml.api.assertJobSpaces(adJobId, jobType, [defaultSpaceId]);
const body = await runRequest(
{
jobType,
Expand All @@ -88,8 +88,8 @@ export default ({ getService }: FtrProviderContext) => {
});

it('should assign DFA job to space for user with access to that space', async () => {
await ml.api.assertJobSpaces(dfaJobId, 'data-frame-analytics', [defaultSpaceId]);
const jobType = 'data-frame-analytics';
await ml.api.assertJobSpaces(dfaJobId, jobType, [defaultSpaceId]);
const body = await runRequest(
{
jobType,
Expand All @@ -106,8 +106,8 @@ export default ({ getService }: FtrProviderContext) => {
});

it('should fail to update AD job spaces for space the user has no access to', async () => {
await ml.api.assertJobSpaces(adJobId, 'anomaly-detector', [defaultSpaceId]);
const jobType = 'anomaly-detector';
await ml.api.assertJobSpaces(adJobId, jobType, [defaultSpaceId]);
const body = await runRequest(
{
jobType,
Expand All @@ -124,8 +124,8 @@ export default ({ getService }: FtrProviderContext) => {
});

it('should fail to update DFA job spaces for space the user has no access to', async () => {
await ml.api.assertJobSpaces(dfaJobId, 'data-frame-analytics', [defaultSpaceId]);
const jobType = 'data-frame-analytics';
await ml.api.assertJobSpaces(dfaJobId, jobType, [defaultSpaceId]);
const body = await runRequest(
{
jobType,
Expand Down
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]);
});
});
};

0 comments on commit da60218

Please sign in to comment.