Skip to content

Commit

Permalink
ensure jobs are created/started before stopping attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarezmelissa87 committed Mar 2, 2021
1 parent 9361c38 commit 6504556
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export default ({ getService }: FtrProviderContext) => {
});

after(async () => {
await ml.api.deleteDataFrameAnalyticsJobES(`${jobId}_0`);
await ml.api.cleanMlIndices();
await ml.api.deleteIndices(destinationIndex);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export default ({ getService }: FtrProviderContext) => {
});

after(async () => {
await ml.api.deleteDataFrameAnalyticsJobES(jobIdSpace1);
await ml.api.deleteDataFrameAnalyticsJobES(jobIdSpace2);
await spacesService.delete(idSpace1);
await spacesService.delete(idSpace2);
await ml.api.cleanMlIndices();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default ({ getService }: FtrProviderContext) => {
});

after(async () => {
await ml.api.deleteDataFrameAnalyticsJobES(analyticsId);
await ml.api.cleanMlIndices();
await ml.api.deleteIndices(destinationIndex);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import expect from '@kbn/expect';
import { FtrProviderContext } from '../../../ftr_provider_context';
import { COMMON_REQUEST_HEADERS } from '../../../../functional/services/ml/common_api';
import { USER } from '../../../../functional/services/ml/security_common';
import { DATA_FRAME_TASK_STATE } from '../../../../../plugins/ml/common/constants/data_frame_analytics';

export default ({ getService }: FtrProviderContext) => {
const esArchiver = getService('esArchiver');
Expand Down Expand Up @@ -59,12 +60,18 @@ export default ({ getService }: FtrProviderContext) => {
await ml.api.createDataFrameAnalyticsJob(jobConfigSpace4, idSpace4);
// start jobs
await runRequest(jobIdSpace3, idSpace3, '_start', 200);
await ml.api.waitForAnalyticsState(jobIdSpace3, DATA_FRAME_TASK_STATE.STARTED);
await ml.api.assertIndicesExist(`user-${jobIdSpace3}`);
await runRequest(jobIdSpace4, idSpace4, '_start', 200);
await ml.api.waitForAnalyticsState(jobIdSpace4, DATA_FRAME_TASK_STATE.STARTED);
await ml.api.assertIndicesExist(`user-${jobIdSpace4}`);

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

after(async () => {
await ml.api.deleteDataFrameAnalyticsJobES(jobIdSpace3);
await ml.api.deleteDataFrameAnalyticsJobES(jobIdSpace4);
await spacesService.delete(idSpace3);
await spacesService.delete(idSpace4);
await ml.api.deleteIndices(`user-${jobIdSpace3}`);
Expand All @@ -76,11 +83,13 @@ export default ({ getService }: FtrProviderContext) => {
it('should stop job from same space', async () => {
const body = await runRequest(jobIdSpace3, idSpace3, '_stop', 200);
expect(body).to.have.property('stopped', true);
await ml.api.waitForAnalyticsState(jobIdSpace3, DATA_FRAME_TASK_STATE.STOPPED, 5000);
});

it('should fail to stop job from different space', async () => {
const body = await runRequest(jobIdSpace4, idSpace3, '_stop', 404);
expect(body.error).to.eql('Not Found');
await ml.api.waitForAnalyticsState(jobIdSpace4, DATA_FRAME_TASK_STATE.STARTED, 5000);
});
});
};
18 changes: 14 additions & 4 deletions x-pack/test/functional/services/ml/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,11 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) {
async waitForAnalyticsState(
analyticsId: string,
expectedAnalyticsState: DataFrameTaskStateType,
timeoutInMs?: number
timeoutInMs: number = 2 * 60 * 1000
) {
const defaultTimeout = 2 * 60 * 1000;

await retry.waitForWithTimeout(
`analytics state to be ${expectedAnalyticsState}`,
timeoutInMs ? timeoutInMs : defaultTimeout,
timeoutInMs,
async () => {
const state = await this.getAnalyticsState(analyticsId);
if (state === expectedAnalyticsState) {
Expand Down Expand Up @@ -723,6 +721,18 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) {
log.debug('> DFA job created.');
},

async deleteDataFrameAnalyticsJobES(analyticsId: string) {
log.debug(`Deleting data frame analytics job with id '${analyticsId}' ...`);

await esSupertest
.delete(`/_ml/data_frame/analytics/${analyticsId}`)
.query({ force: true })
.expect(200);

await this.waitForDataFrameAnalyticsJobNotToExist(analyticsId);
log.debug('> DFA job deleted.');
},

async getADJobRecordCount(jobId: string): Promise<number> {
const jobStats = await this.getADJobStats(jobId);

Expand Down

0 comments on commit 6504556

Please sign in to comment.