Skip to content

Commit

Permalink
[8.7] [ML] trained_models_spaces api tests (#151026) (#151139)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.7`:
- [[ML] trained_models_spaces api tests
(#151026)](#151026)

<!--- 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-14T14:06:22Z","message":"[ML]
trained_models_spaces api tests (#151026)\n\nAdding api tests for
`/api/ml/saved_objects/trained_models_spaces`\r\n\r\nRelated to
#147170
trained_models_spaces api tests (#151026)\n\nAdding api tests for
`/api/ml/saved_objects/trained_models_spaces`\r\n\r\nRelated to
#147170
trained_models_spaces api tests (#151026)\n\nAdding api tests for
`/api/ml/saved_objects/trained_models_spaces`\r\n\r\nRelated to
#147170"}}]}]
BACKPORT-->

Co-authored-by: James Gowdy <jgowdy@elastic.co>
  • Loading branch information
kibanamachine and jgowdyelastic committed Feb 14, 2023
1 parent 04c258b commit 28fb73e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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 trainedModelIdSpace1 = 'trained_model_single_space1';
const trainedModelIdSpace2 = 'trained_model_single_space2';
const idSpace1 = 'space1';
const idSpace2 = 'space2';
const langIdent = 'lang_ident_model_1';

async function runRequest(expectedStatusCode: number, user: USER) {
const { body, status } = await supertest
.get(`/api/ml/saved_objects/trained_models_spaces`)
.auth(user, ml.securityCommon.getPasswordForUser(user))
.set(COMMON_REQUEST_HEADERS);

ml.api.assertResponseStatusCode(expectedStatusCode, status, body);

return body;
}

describe('GET saved_objects/trained_models_spaces', () => {
before(async () => {
await ml.testResources.setKibanaTimeZoneToUTC();

await spacesService.create({ id: idSpace1, name: 'space_one', disabledFeatures: [] });
await spacesService.create({ id: idSpace2, name: 'space_two', disabledFeatures: [] });

const trainedModelConfig1 = ml.api.createTestTrainedModelConfig(
trainedModelIdSpace1,
'regression'
);
await ml.api.createTrainedModel(trainedModelIdSpace1, trainedModelConfig1.body, idSpace1);

const trainedModelConfig2 = ml.api.createTestTrainedModelConfig(
trainedModelIdSpace2,
'regression'
);
await ml.api.createTrainedModel(trainedModelIdSpace2, trainedModelConfig2.body, idSpace2);
});

after(async () => {
await spacesService.delete(idSpace1);
await spacesService.delete(idSpace2);
await ml.api.cleanMlIndices();
await ml.testResources.cleanMLSavedObjects();
});

it('should list all trained models for user with access to all spaces', async () => {
const body = await runRequest(200, USER.ML_VIEWER_ALL_SPACES);

expect(body).to.have.property('trainedModels');
expect(body.trainedModels).to.eql({
[langIdent]: ['*'],
[trainedModelIdSpace1]: [idSpace1],
[trainedModelIdSpace2]: [idSpace2],
});
});

it('should only list trained models for the space the user has access to', async () => {
const body = await runRequest(200, USER.ML_VIEWER_SPACE1);

expect(body).to.have.property('trainedModels');
expect(body.trainedModels).to.eql({
[langIdent]: ['*'],
[trainedModelIdSpace1]: [idSpace1],
});
});
});
};
3 changes: 2 additions & 1 deletion x-pack/test/api_integration/apis/ml/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { FtrProviderContext } from '../../../ftr_provider_context';

export default function ({ loadTestFile }: FtrProviderContext) {
describe('saved objects', function () {
loadTestFile(require.resolve('./jobs_spaces'));
loadTestFile(require.resolve('./get_jobs_spaces'));
loadTestFile(require.resolve('./can_delete_job'));
loadTestFile(require.resolve('./can_delete_trained_model'));
loadTestFile(require.resolve('./get_trained_model_spaces'));
loadTestFile(require.resolve('./initialize_jobs'));
loadTestFile(require.resolve('./initialize_trained_models'));
loadTestFile(require.resolve('./status'));
Expand Down

0 comments on commit 28fb73e

Please sign in to comment.