Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.1] [ML] Adding update datafeed API test (#124540) #124686

Merged
merged 1 commit into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions x-pack/test/api_integration/apis/ml/datafeeds/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export default function ({ loadTestFile }: FtrProviderContext) {
describe('anomaly detectors', function () {
loadTestFile(require.resolve('./get_with_spaces'));
loadTestFile(require.resolve('./get_stats_with_spaces'));
loadTestFile(require.resolve('./update'));
});
}
98 changes: 98 additions & 0 deletions x-pack/test/api_integration/apis/ml/datafeeds/update.ts
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 type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
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 jobIdSpace1 = 'fq_single_space1';
const datafeedIdSpace1 = `datafeed-${jobIdSpace1}`;
const idSpace1 = 'space1';
const idSpace2 = 'space2';

const newQueryDelay = '80000ms';

async function updateDatafeed(
datafeedConfig: estypes.MlUpdateDatafeedRequest,
user: USER,
expectedStatusCode: number,
space?: string
) {
const datafeedId = datafeedConfig.datafeed_id;
const { body } = await supertest
.post(`${space ? `/s/${space}` : ''}/api/ml/datafeeds/${datafeedId}/_update`)
.auth(user, ml.securityCommon.getPasswordForUser(user))
.set(COMMON_REQUEST_HEADERS)
.send(datafeedConfig.body)
.expect(expectedStatusCode);

return body;
}

describe('update datafeeds with spaces', () => {
before(async () => {
await spacesService.create({ id: idSpace1, name: 'space_one', disabledFeatures: [] });
await spacesService.create({ id: idSpace2, name: 'space_two', disabledFeatures: [] });

const jobConfig = ml.commonConfig.getADFqSingleMetricJobConfig(jobIdSpace1);
await ml.api.createAnomalyDetectionJob(jobConfig, idSpace1);
const datafeedConfig = ml.commonConfig.getADFqDatafeedConfig(jobIdSpace1);
await ml.api.createDatafeed(datafeedConfig, idSpace1);

await ml.testResources.setKibanaTimeZoneToUTC();
});

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

it('should update datafeed with correct space', async () => {
await updateDatafeed(
{ datafeed_id: datafeedIdSpace1, body: { query_delay: newQueryDelay } },
USER.ML_POWERUSER_ALL_SPACES,
200,
idSpace1
);

const { body } = await ml.api.getDatafeed(datafeedIdSpace1);
const receivedQueryDelay = body.datafeeds[0]?.query_delay;

expect(receivedQueryDelay).to.eql(
newQueryDelay,
`response datafeed query_delay list should be ${newQueryDelay} (got ${receivedQueryDelay})`
);
});

it('should not update datafeed with incorrect space', async () => {
await updateDatafeed(
{ datafeed_id: datafeedIdSpace1, body: { query_delay: newQueryDelay } },
USER.ML_POWERUSER_ALL_SPACES,
404,
idSpace2
);
});

it('should not be updatable by ml viewer user', async () => {
await updateDatafeed(
{ datafeed_id: datafeedIdSpace1, body: { query_delay: newQueryDelay } },
USER.ML_VIEWER_ALL_SPACES,
403,
idSpace1
);
});
});
};