Skip to content

Commit

Permalink
adding testsadding testsadding testsadding testsadding testsadding te…
Browse files Browse the repository at this point in the history
…stsadding testsadding tests
  • Loading branch information
jgowdyelastic committed Jun 14, 2021
1 parent 432721e commit 1b1a506
Showing 1 changed file with 79 additions and 14 deletions.
93 changes: 79 additions & 14 deletions x-pack/test/api_integration/apis/ml/saved_objects/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import expect from '@kbn/expect';
import { cloneDeep } from 'lodash';
import { FtrProviderContext } from '../../../ftr_provider_context';
import { USER } from '../../../../functional/services/ml/security_common';
import { COMMON_REQUEST_HEADERS } from '../../../../functional/services/ml/common_api';
Expand All @@ -23,7 +24,7 @@ export default ({ getService }: FtrProviderContext) => {

async function runRequest(user: USER, expectedStatusCode: number) {
const { body } = await supertest
.get(`/s/space1/api/ml/saved_objects/sync`)
.get(`/s/${idSpace1}/api/ml/saved_objects/sync`)
.auth(user, ml.securityCommon.getPasswordForUser(user))
.set(COMMON_REQUEST_HEADERS)
.expect(expectedStatusCode);
Expand All @@ -32,9 +33,19 @@ export default ({ getService }: FtrProviderContext) => {
}

describe('GET saved_objects/sync', () => {
before(async () => {
beforeEach(async () => {
await spacesService.create({ id: idSpace1, name: 'space_one', disabledFeatures: [] });
await ml.testResources.setKibanaTimeZoneToUTC();
});

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

it('should sync datafeeds and saved objects', async () => {
// prepare test data
await ml.api.createAnomalyDetectionJob(
ml.commonConfig.getADFqSingleMetricJobConfig(adJobId1),
idSpace1
Expand All @@ -51,18 +62,6 @@ export default ({ getService }: FtrProviderContext) => {
ml.commonConfig.getADFqSingleMetricJobConfig(adJobIdES)
);

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

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

it('should sync datafeeds and saved objects', async () => {
// prepare test data

// datafeed should be added with the request
const datafeedConfig2 = ml.commonConfig.getADFqDatafeedConfig(adJobId2);
await ml.api.createDatafeedES(datafeedConfig2);
Expand All @@ -89,7 +88,73 @@ export default ({ getService }: FtrProviderContext) => {
});
});

it('should sync datafeeds and saved objects2', async () => {
// prepare test data
const jobConfig1 = ml.commonConfig.getADFqSingleMetricJobConfig(adJobId1);
await ml.api.createAnomalyDetectionJob(jobConfig1, idSpace1);

// datafeed should be added with the request
const datafeedConfig1 = ml.commonConfig.getADFqDatafeedConfig(adJobId1);
await ml.api.createDatafeedES(datafeedConfig1);

// run the sync request and verify the response
const body = await runRequest(USER.ML_POWERUSER_ALL_SPACES, 200);

// expect datafeed to be added
expect(body).to.eql({
datafeedsAdded: { [adJobId1]: { success: true } },
datafeedsRemoved: {},
savedObjectsCreated: {},
savedObjectsDeleted: {},
});

// delete the datafeed but do not sync
await ml.api.deleteDatafeedES(datafeedConfig1.datafeed_id);

// create a new datafeed with a different id
const datafeedConfig2 = cloneDeep(datafeedConfig1);
datafeedConfig2.datafeed_id = `different_${datafeedConfig2.datafeed_id}`;
await ml.api.createDatafeedES(datafeedConfig2);

const body2 = await runRequest(USER.ML_POWERUSER_ALL_SPACES, 200);

// previous datafeed should be removed on first sync
expect(body2).to.eql({
datafeedsAdded: {},
datafeedsRemoved: { [adJobId1]: { success: true } },
savedObjectsCreated: {},
savedObjectsDeleted: {},
});

const body3 = await runRequest(USER.ML_POWERUSER_ALL_SPACES, 200);

// new datafeed will be added on second sync
expect(body3).to.eql({
datafeedsAdded: { [adJobId1]: { success: true } },
datafeedsRemoved: {},
savedObjectsCreated: {},
savedObjectsDeleted: {},
});
});

it('should not sync anything if all objects are already synced', async () => {
await ml.api.createAnomalyDetectionJob(
ml.commonConfig.getADFqSingleMetricJobConfig(adJobId1),
idSpace1
);
await ml.api.createAnomalyDetectionJob(
ml.commonConfig.getADFqSingleMetricJobConfig(adJobId2),
idSpace1
);
await ml.api.createAnomalyDetectionJob(
ml.commonConfig.getADFqSingleMetricJobConfig(adJobId3),
idSpace1
);
await ml.api.createAnomalyDetectionJobES(
ml.commonConfig.getADFqSingleMetricJobConfig(adJobIdES)
);

await runRequest(USER.ML_POWERUSER_ALL_SPACES, 200);
const body = await runRequest(USER.ML_POWERUSER_ALL_SPACES, 200);

expect(body).to.eql({
Expand Down

0 comments on commit 1b1a506

Please sign in to comment.