Skip to content

Commit

Permalink
[ML] API integration tests - stabilize saved objects endpoint tests (e…
Browse files Browse the repository at this point in the history
…lastic#87288) (elastic#87554)

This PR stabilizes some saved objects endpoint tests by making sure a different order of objects in a response array is still accepted.
  • Loading branch information
pheyos authored Jan 7, 2021
1 parent 91cc547 commit e32fd9e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import expect from '@kbn/expect';
import { sortBy } from 'lodash';

import { FtrProviderContext } from '../../../ftr_provider_context';
import { USER } from '../../../../functional/services/ml/security_common';
Expand Down Expand Up @@ -174,16 +175,6 @@ export default ({ getService }: FtrProviderContext) => {
return body;
}

function compareByFieldName(a: { fieldName: string }, b: { fieldName: string }) {
if (a.fieldName < b.fieldName) {
return -1;
}
if (a.fieldName > b.fieldName) {
return 1;
}
return 0;
}

describe('get_field_stats', function () {
before(async () => {
await esArchiver.loadIfNeeded('ml/farequote');
Expand Down Expand Up @@ -221,11 +212,8 @@ export default ({ getService }: FtrProviderContext) => {
nonMetricFieldsTestData.expected.responseCode
);

// Sort the fields in the response before validating.
const expectedRspFields = nonMetricFieldsTestData.expected.responseBody.sort(
compareByFieldName
);
const actualRspFields = body.sort(compareByFieldName);
const expectedRspFields = sortBy(nonMetricFieldsTestData.expected.responseBody, 'fieldName');
const actualRspFields = sortBy(body, 'fieldName');
expect(actualRspFields).to.eql(expectedRspFields);
});

Expand Down
47 changes: 21 additions & 26 deletions x-pack/test/api_integration/apis/ml/jobs/jobs_summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import expect from '@kbn/expect';
import { sortBy } from 'lodash';

import { FtrProviderContext } from '../../../ftr_provider_context';
import { COMMON_REQUEST_HEADERS } from '../../../../functional/services/ml/common_api';
Expand Down Expand Up @@ -181,16 +182,6 @@ export default ({ getService }: FtrProviderContext) => {
return body;
}

function compareById(a: { id: string }, b: { id: string }) {
if (a.id < b.id) {
return -1;
}
if (a.id > b.id) {
return 1;
}
return 0;
}

function getGroups(jobs: Array<{ groups: string[] }>) {
const groupIds: string[] = [];
jobs.forEach((job) => {
Expand Down Expand Up @@ -236,16 +227,18 @@ export default ({ getService }: FtrProviderContext) => {
expect(body).to.have.length(expectedResponse.length);

// Validate job IDs.
const expectedRspJobIds = expectedResponse
.map((job: { id: string }) => {
const expectedRspJobIds = sortBy(
expectedResponse.map((job: { id: string }) => {
return { id: job.id };
})
.sort(compareById);
const actualRspJobIds = body
.map((job: { id: string }) => {
}),
'id'
);
const actualRspJobIds = sortBy(
body.map((job: { id: string }) => {
return { id: job.id };
})
.sort(compareById);
}),
'id'
);

expect(actualRspJobIds).to.eql(expectedRspJobIds);

Expand Down Expand Up @@ -273,16 +266,18 @@ export default ({ getService }: FtrProviderContext) => {
expect(body).to.have.length(expectedResponse.length);

// Validate job IDs.
const expectedRspJobIds = expectedResponse
.map((job: { id: string }) => {
const expectedRspJobIds = sortBy(
expectedResponse.map((job: { id: string }) => {
return { id: job.id };
})
.sort(compareById);
const actualRspJobIds = body
.map((job: { id: string }) => {
}),
'id'
);
const actualRspJobIds = sortBy(
body.map((job: { id: string }) => {
return { id: job.id };
})
.sort(compareById);
}),
'id'
);

expect(actualRspJobIds).to.eql(expectedRspJobIds);

Expand Down
58 changes: 26 additions & 32 deletions x-pack/test/api_integration/apis/ml/modules/setup_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import expect from '@kbn/expect';
import { isEmpty } from 'lodash';
import { isEmpty, sortBy } from 'lodash';

import { FtrProviderContext } from '../../../ftr_provider_context';

Expand Down Expand Up @@ -701,22 +701,13 @@ export default ({ getService }: FtrProviderContext) => {
return body;
}

function compareById(a: { id: string }, b: { id: string }) {
if (a.id < b.id) {
return -1;
}
if (a.id > b.id) {
return 1;
}
return 0;
}

function mapIdsToSuccessObjects(ids: string[]) {
const successObjects = ids
.map((id) => {
const successObjects = sortBy(
ids.map((id) => {
return { id, success: true };
})
.sort(compareById);
}),
'id'
);

return successObjects;
}
Expand Down Expand Up @@ -765,7 +756,7 @@ export default ({ getService }: FtrProviderContext) => {
const expectedJobIds = testData.expected.jobs.map((job) => job.jobId);
const expectedRspJobs = mapIdsToSuccessObjects(expectedJobIds);

const actualRspJobs = rspBody.jobs.sort(compareById);
const actualRspJobs = sortBy(rspBody.jobs, 'id');

expect(actualRspJobs).to.eql(
expectedRspJobs,
Expand All @@ -777,17 +768,18 @@ export default ({ getService }: FtrProviderContext) => {
// datafeeds
expect(rspBody).to.have.property('datafeeds');

const expectedRspDatafeeds = testData.expected.jobs
.map((job) => {
const expectedRspDatafeeds = sortBy(
testData.expected.jobs.map((job) => {
return {
id: `datafeed-${job.jobId}`,
success: true,
started: testData.requestBody.startDatafeed,
};
})
.sort(compareById);
}),
'id'
);

const actualRspDatafeeds = rspBody.datafeeds.sort(compareById);
const actualRspDatafeeds = sortBy(rspBody.datafeeds, 'id');

expect(actualRspDatafeeds).to.eql(
expectedRspDatafeeds,
Expand All @@ -803,9 +795,9 @@ export default ({ getService }: FtrProviderContext) => {
let actualDashboards = [];

if (isEmpty(rspKibana) === false) {
actualSearches = rspBody.kibana.search.sort(compareById);
actualVisualizations = rspBody.kibana.visualization.sort(compareById);
actualDashboards = rspBody.kibana.dashboard.sort(compareById);
actualSearches = sortBy(rspBody.kibana.search, 'id');
actualVisualizations = sortBy(rspBody.kibana.visualization, 'id');
actualDashboards = sortBy(rspBody.kibana.dashboard, 'id');
}

const expectedSearches = mapIdsToSuccessObjects(testData.expected.searches);
Expand Down Expand Up @@ -847,12 +839,13 @@ export default ({ getService }: FtrProviderContext) => {
}

// compare model memory limits for created jobs
const expectedModelMemoryLimits = testData.expected.jobs
.map((j) => ({
const expectedModelMemoryLimits = sortBy(
testData.expected.jobs.map((j) => ({
id: j.jobId,
modelMemoryLimit: j.modelMemoryLimit,
}))
.sort(compareById);
})),
'id'
);

const {
body: { jobs },
Expand All @@ -864,12 +857,13 @@ export default ({ getService }: FtrProviderContext) => {
testData.expected.jobs.map((j) => j.jobId).join()
);

const actualModelMemoryLimits = jobs
.map((j) => ({
const actualModelMemoryLimits = sortBy(
jobs.map((j) => ({
id: j.job_id,
modelMemoryLimit: j.analysis_limits!.model_memory_limit,
}))
.sort(compareById);
})),
'id'
);

expect(actualModelMemoryLimits).to.eql(
expectedModelMemoryLimits,
Expand Down
15 changes: 8 additions & 7 deletions x-pack/test/api_integration/apis/ml/saved_objects/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/

import expect from '@kbn/expect';
import { sortBy } 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 Down Expand Up @@ -52,13 +54,12 @@ export default ({ getService }: FtrProviderContext) => {

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

expect(body).to.eql({
jobs: [
{ id: adJobId, type: 'anomaly-detector' },
{ id: dfaJobId, type: 'data-frame-analytics' },
],
success: true,
});
expect(body).to.have.property('jobs');
expect(sortBy(body.jobs, 'id')).to.eql([
{ id: adJobId, type: 'anomaly-detector' },
{ id: dfaJobId, type: 'data-frame-analytics' },
]);
expect(body).to.have.property('success', true);
await ml.api.assertJobSpaces(adJobId, 'anomaly-detector', ['*']);
await ml.api.assertJobSpaces(dfaJobId, 'data-frame-analytics', ['*']);
});
Expand Down
34 changes: 18 additions & 16 deletions x-pack/test/api_integration/apis/ml/saved_objects/jobs_spaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,30 @@ export default ({ getService }: FtrProviderContext) => {
it('should list all jobs for user with access to all spaces', async () => {
const body = await runRequest(200, USER.ML_VIEWER_ALL_SPACES);

expect(body).to.eql({
'anomaly-detector': {
[adJobIdSpace1]: [idSpace1],
[adJobIdSpace2]: [idSpace2],
},
'data-frame-analytics': {
[dfaJobIdSpace1]: [idSpace1],
[dfaJobIdSpace2]: [idSpace2],
},
expect(body).to.have.property('anomaly-detector');
expect(body['anomaly-detector']).to.eql({
[adJobIdSpace1]: [idSpace1],
[adJobIdSpace2]: [idSpace2],
});

expect(body).to.have.property('data-frame-analytics');
expect(body['data-frame-analytics']).to.eql({
[dfaJobIdSpace1]: [idSpace1],
[dfaJobIdSpace2]: [idSpace2],
});
});

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

expect(body).to.eql({
'anomaly-detector': {
[adJobIdSpace1]: [idSpace1],
},
'data-frame-analytics': {
[dfaJobIdSpace1]: [idSpace1],
},
expect(body).to.have.property('anomaly-detector');
expect(body['anomaly-detector']).to.eql({
[adJobIdSpace1]: [idSpace1],
});

expect(body).to.have.property('data-frame-analytics');
expect(body['data-frame-analytics']).to.eql({
[dfaJobIdSpace1]: [idSpace1],
});
});
});
Expand Down
10 changes: 6 additions & 4 deletions x-pack/test/api_integration/apis/ml/saved_objects/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/

import expect from '@kbn/expect';
import { sortBy } 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 Down Expand Up @@ -76,7 +78,7 @@ export default ({ getService }: FtrProviderContext) => {
expect(body).to.have.property('jobs');

expect(body.jobs).to.have.property('anomaly-detector');
expect(body.jobs['anomaly-detector']).to.eql([
expect(sortBy(body.jobs['anomaly-detector'], 'id')).to.eql([
{
checks: { savedObjectExits: true },
datafeedId: null,
Expand All @@ -90,7 +92,7 @@ export default ({ getService }: FtrProviderContext) => {
]);

expect(body.jobs).to.have.property('data-frame-analytics');
expect(body.jobs['data-frame-analytics']).to.eql([
expect(sortBy(body.jobs['data-frame-analytics'], 'id')).to.eql([
{
checks: { savedObjectExits: true },
datafeedId: null,
Expand All @@ -107,7 +109,7 @@ export default ({ getService }: FtrProviderContext) => {
expect(body).to.have.property('savedObjects');

expect(body.savedObjects).to.have.property('anomaly-detector');
expect(body.savedObjects['anomaly-detector']).to.eql([
expect(sortBy(body.savedObjects['anomaly-detector'], 'id')).to.eql([
{
checks: { datafeedExists: false, jobExists: true },
datafeedId: null,
Expand All @@ -125,7 +127,7 @@ export default ({ getService }: FtrProviderContext) => {
]);

expect(body.savedObjects).to.have.property('data-frame-analytics');
expect(body.savedObjects['data-frame-analytics']).to.eql([
expect(sortBy(body.savedObjects['data-frame-analytics'], 'id')).to.eql([
{
checks: { jobExists: true },
jobId: dfaJobIdSpace1,
Expand Down

0 comments on commit e32fd9e

Please sign in to comment.