Skip to content

Commit

Permalink
[ML] Edits to setup and clean-up steps following review
Browse files Browse the repository at this point in the history
  • Loading branch information
peteharverson committed Sep 3, 2021
1 parent ac532dc commit fa88781
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
19 changes: 10 additions & 9 deletions x-pack/test/api_integration/apis/ml/jobs/force_start_datafeeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default ({ getService }: FtrProviderContext) => {
user: USER,
requestBody: object,
expectedResponsecode: number
): Promise<any> {
): Promise<Record<string, { started: boolean; error?: string }>> {
const { body } = await supertest
.post('/api/ml/jobs/force_start_datafeeds')
.auth(user, ml.securityCommon.getPasswordForUser(user))
Expand Down Expand Up @@ -115,13 +115,7 @@ export default ({ getService }: FtrProviderContext) => {
before(async () => {
await esArchiver.loadIfNeeded('x-pack/test/functional/es_archives/ml/farequote');
await ml.testResources.setKibanaTimeZoneToUTC();
});

after(async () => {
await ml.api.cleanMlIndices();
});

it('sets up jobs', async () => {
for (const job of testSetupJobConfigs) {
const datafeedId = `datafeed-${job.job_id}`;
await ml.api.createAnomalyDetectionJob(job);
Expand All @@ -133,6 +127,13 @@ export default ({ getService }: FtrProviderContext) => {
}
});

after(async () => {
for (const job of testSetupJobConfigs) {
await ml.api.deleteAnomalyDetectionJobES(job.job_id);
}
await ml.api.cleanMlIndices();
});

describe('rejects requests for unauthorized users', function () {
for (const testData of testDataListUnauthorized) {
describe('fails to force start supplied datafeed IDs', function () {
Expand Down Expand Up @@ -184,7 +185,7 @@ export default ({ getService }: FtrProviderContext) => {

// check datafeeds have started
for (const id of testData.requestBody.datafeedIds) {
await ml.api.waitForDatafeedState(id, DATAFEED_STATE.STARTED, 4 * 60 * 1000);
await ml.api.waitForDatafeedState(id, DATAFEED_STATE.STARTED);
}
});
}
Expand Down Expand Up @@ -215,7 +216,7 @@ export default ({ getService }: FtrProviderContext) => {

// check datafeeds are still started
for (const id of testData.requestBody.datafeedIds) {
await ml.api.waitForDatafeedState(id, DATAFEED_STATE.STARTED, 4 * 60 * 1000);
await ml.api.waitForDatafeedState(id, DATAFEED_STATE.STARTED);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default ({ getService }: FtrProviderContext) => {
datafeedIds: string[],
start: number,
end: number
) {
): Promise<Record<string, { started: boolean; error?: string }>> {
const { body } = await supertest
.post(`/s/${space}/api/ml/jobs/force_start_datafeeds`)
.auth(
Expand Down Expand Up @@ -84,6 +84,8 @@ export default ({ getService }: FtrProviderContext) => {
afterEach(async () => {
await ml.api.closeAnomalyDetectionJob(jobIdSpace1);
await ml.api.closeAnomalyDetectionJob(jobIdSpace2);
await ml.api.deleteAnomalyDetectionJobES(jobIdSpace1);
await ml.api.deleteAnomalyDetectionJobES(jobIdSpace2);
await ml.api.cleanMlIndices();
await ml.testResources.cleanMLSavedObjects();
});
Expand All @@ -96,13 +98,13 @@ export default ({ getService }: FtrProviderContext) => {
it('should start single datafeed from same space', async () => {
const body = await runRequest(idSpace1, 200, [datafeedIdSpace1], startMs, endMs);
expect(body).to.eql({ [datafeedIdSpace1]: { started: true } });
await ml.api.waitForDatafeedState(datafeedIdSpace1, DATAFEED_STATE.STARTED, 4 * 60 * 1000);
await ml.api.waitForDatafeedState(datafeedIdSpace1, DATAFEED_STATE.STARTED);
});

it('should not start single datafeed from different space', async () => {
const body = await runRequest(idSpace2, 200, [datafeedIdSpace1], startMs, endMs);
expect(body).to.eql({ [datafeedIdSpace1]: { error: 'Job has no datafeed', started: false } });
await ml.api.waitForDatafeedState(datafeedIdSpace1, DATAFEED_STATE.STOPPED, 4 * 60 * 1000);
await ml.api.waitForDatafeedState(datafeedIdSpace1, DATAFEED_STATE.STOPPED);
});

it('should only start datafeed from same space when called with a list of datafeeds', async () => {
Expand All @@ -117,8 +119,8 @@ export default ({ getService }: FtrProviderContext) => {
[datafeedIdSpace1]: { started: true },
[datafeedIdSpace2]: { error: 'Job has no datafeed', started: false },
});
await ml.api.waitForDatafeedState(datafeedIdSpace1, DATAFEED_STATE.STARTED, 4 * 60 * 1000);
await ml.api.waitForDatafeedState(datafeedIdSpace2, DATAFEED_STATE.STOPPED, 4 * 60 * 1000);
await ml.api.waitForDatafeedState(datafeedIdSpace1, DATAFEED_STATE.STARTED);
await ml.api.waitForDatafeedState(datafeedIdSpace2, DATAFEED_STATE.STOPPED);
});
});
};
15 changes: 8 additions & 7 deletions x-pack/test/api_integration/apis/ml/jobs/stop_datafeeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default ({ getService }: FtrProviderContext) => {
user: USER,
requestBody: object,
expectedResponsecode: number
): Promise<any> {
): Promise<Record<string, { stopped: boolean; error?: string }>> {
const { body } = await supertest
.post('/api/ml/jobs/stop_datafeeds')
.auth(user, ml.securityCommon.getPasswordForUser(user))
Expand Down Expand Up @@ -109,13 +109,7 @@ export default ({ getService }: FtrProviderContext) => {
before(async () => {
await esArchiver.loadIfNeeded('x-pack/test/functional/es_archives/ml/farequote');
await ml.testResources.setKibanaTimeZoneToUTC();
});

after(async () => {
await ml.api.cleanMlIndices();
});

it('sets up jobs', async () => {
for (const job of testSetupJobConfigs) {
const datafeedId = `datafeed-${job.job_id}`;
await ml.api.createAnomalyDetectionJob(job);
Expand All @@ -130,6 +124,13 @@ export default ({ getService }: FtrProviderContext) => {
}
});

after(async () => {
for (const job of testSetupJobConfigs) {
await ml.api.deleteAnomalyDetectionJobES(job.job_id);
}
await ml.api.cleanMlIndices();
});

describe('rejects requests for unauthorized users', function () {
for (const testData of testDataListUnauthorized) {
describe('fails to stop supplied datafeed IDs', function () {
Expand Down
16 changes: 11 additions & 5 deletions x-pack/test/api_integration/apis/ml/jobs/stop_datafeeds_spaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ export default ({ getService }: FtrProviderContext) => {
const datafeedIdSpace1 = `datafeed-${jobIdSpace1}`;
const datafeedIdSpace2 = `datafeed-${jobIdSpace2}`;

async function runRequest(space: string, expectedStatusCode: number, datafeedIds: string[]) {
async function runRequest(
space: string,
expectedStatusCode: number,
datafeedIds: string[]
): Promise<Record<string, { stopped: boolean; error?: string }>> {
const { body } = await supertest
.post(`/s/${space}/api/ml/jobs/stop_datafeeds`)
.auth(
Expand Down Expand Up @@ -83,6 +87,8 @@ export default ({ getService }: FtrProviderContext) => {
afterEach(async () => {
await ml.api.closeAnomalyDetectionJob(jobIdSpace1);
await ml.api.closeAnomalyDetectionJob(jobIdSpace2);
await ml.api.deleteAnomalyDetectionJobES(jobIdSpace1);
await ml.api.deleteAnomalyDetectionJobES(jobIdSpace2);
await ml.api.cleanMlIndices();
await ml.testResources.cleanMLSavedObjects();
});
Expand All @@ -95,13 +101,13 @@ export default ({ getService }: FtrProviderContext) => {
it('should stop single datafeed from same space', async () => {
const body = await runRequest(idSpace1, 200, [datafeedIdSpace1]);
expect(body).to.eql({ [datafeedIdSpace1]: { stopped: true } });
await ml.api.waitForDatafeedState(datafeedIdSpace1, DATAFEED_STATE.STOPPED, 4 * 60 * 1000);
await ml.api.waitForDatafeedState(datafeedIdSpace1, DATAFEED_STATE.STOPPED);
});

it('should not stop single datafeed from different space', async () => {
const body = await runRequest(idSpace2, 200, [datafeedIdSpace1]);
expect(body).to.eql({ [datafeedIdSpace1]: { stopped: false } });
await ml.api.waitForDatafeedState(datafeedIdSpace1, DATAFEED_STATE.STARTED, 4 * 60 * 1000);
await ml.api.waitForDatafeedState(datafeedIdSpace1, DATAFEED_STATE.STARTED);
});

it('should only stop datafeed from same space when called with a list of datafeeds', async () => {
Expand All @@ -110,8 +116,8 @@ export default ({ getService }: FtrProviderContext) => {
[datafeedIdSpace1]: { stopped: true },
[datafeedIdSpace2]: { stopped: false },
});
await ml.api.waitForDatafeedState(datafeedIdSpace1, DATAFEED_STATE.STOPPED, 4 * 60 * 1000);
await ml.api.waitForDatafeedState(datafeedIdSpace2, DATAFEED_STATE.STARTED, 4 * 60 * 1000);
await ml.api.waitForDatafeedState(datafeedIdSpace1, DATAFEED_STATE.STOPPED);
await ml.api.waitForDatafeedState(datafeedIdSpace2, DATAFEED_STATE.STARTED);
});
});
};

0 comments on commit fa88781

Please sign in to comment.