Skip to content

Commit

Permalink
adding api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdyelastic committed Oct 18, 2021
1 parent c89ca51 commit a346964
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export default ({ getService }: FtrProviderContext) => {
const destinationIndex = generateDestinationIndex(analyticsId);

before(async () => {
await ml.api.createIndices(destinationIndex);
await ml.api.createIndex(destinationIndex);
await ml.api.assertIndicesExist(destinationIndex);
});

Expand Down Expand Up @@ -189,7 +189,7 @@ export default ({ getService }: FtrProviderContext) => {

before(async () => {
// Mimic real job by creating target index & index pattern after DFA job is created
await ml.api.createIndices(destinationIndex);
await ml.api.createIndex(destinationIndex);
await ml.api.assertIndicesExist(destinationIndex);
await ml.testResources.createIndexPatternIfNeeded(destinationIndex);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* 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.
*/
/*
* 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 expect from '@kbn/expect';
import { estypes } from '@elastic/elasticsearch';
import { FtrProviderContext } from '../../../ftr_provider_context';
import { USER } from '../../../../functional/services/ml/security_common';
import { COMMON_REQUEST_HEADERS } from '../../../../functional/services/ml/common_api';

const farequoteMappings: estypes.MappingTypeMapping = {
properties: {
'@timestamp': {
type: 'date',
},
airline: {
type: 'keyword',
},
responsetime: {
type: 'float',
},
},
};

function getBaseJobConfig() {
return {
job_id: 'test',
description: '',
analysis_config: {
bucket_span: '15m',
detectors: [
{
function: 'mean',
field_name: 'responsetime',
},
],
influencers: [],
},
analysis_limits: {
model_memory_limit: '11MB',
},
data_description: {
time_field: '@timestamp',
time_format: 'epoch_ms',
},
model_plot_config: {
enabled: false,
annotations_enabled: false,
},
model_snapshot_retention_days: 10,
daily_model_snapshot_retention_after_days: 1,
allow_lazy_open: false,
datafeed_config: {
query: {
bool: {
must: [
{
match_all: {},
},
],
},
},
indices: ['ft_farequote'],
scroll_size: 1000,
delayed_data_check_config: {
enabled: true,
},
job_id: 'test',
datafeed_id: 'datafeed-test',
},
};
}

export default ({ getService }: FtrProviderContext) => {
const esArchiver = getService('esArchiver');
const supertest = getService('supertestWithoutAuth');
const ml = getService('ml');

describe('Validate datafeed preview', function () {
before(async () => {
await esArchiver.loadIfNeeded('x-pack/test/functional/es_archives/ml/farequote');
await ml.testResources.setKibanaTimeZoneToUTC();
ml.api.createIndex('farequote_empty', farequoteMappings);
});

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

it(`should validate a job with documents`, async () => {
const job = getBaseJobConfig();

const { body } = await supertest
.post('/api/ml/validate/datafeed_preview')
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
.set(COMMON_REQUEST_HEADERS)
.send({ job })
.expect(200);

expect(body.valid).to.eql(true, `valid should be true, but got ${body.valid}`);
expect(body.documentsFound).to.eql(
true,
`documentsFound should be true, but got ${body.documentsFound}`
);
});

it(`should fail to validate a job with documents`, async () => {
const job = getBaseJobConfig();
job.analysis_config.detectors[0].field_name = 'no_such_field';

const { body } = await supertest
.post('/api/ml/validate/datafeed_preview')
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
.set(COMMON_REQUEST_HEADERS)
.send({ job })
.expect(200);

expect(body.valid).to.eql(false, `valid should be false, but got ${body.valid}`);
expect(body.documentsFound).to.eql(
false,
`documentsFound should be false, but got ${body.documentsFound}`
);
});

it(`should validate a job with no documents`, async () => {
const job = getBaseJobConfig();
job.datafeed_config.indices = ['farequote_empty'];

const { body } = await supertest
.post('/api/ml/validate/datafeed_preview')
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
.set(COMMON_REQUEST_HEADERS)
.send({ job })
.expect(200);

expect(body.valid).to.eql(true, `valid should be true, but got ${body.valid}`);
expect(body.documentsFound).to.eql(
false,
`documentsFound should be false, but got ${body.documentsFound}`
);
});
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export default function ({ loadTestFile }: FtrProviderContext) {
loadTestFile(require.resolve('./calculate_model_memory_limit'));
loadTestFile(require.resolve('./cardinality'));
loadTestFile(require.resolve('./validate'));
loadTestFile(require.resolve('./datafeed_preview_validation'));
});
}
10 changes: 8 additions & 2 deletions x-pack/test/functional/services/ml/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,20 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) {
);
},

async createIndices(indices: string) {
async createIndex(
indices: string,
mappings?: Record<string, estypes.MappingTypeMapping> | estypes.MappingTypeMapping
) {
log.debug(`Creating indices: '${indices}'...`);
if ((await es.indices.exists({ index: indices, allow_no_indices: false })).body === true) {
log.debug(`Indices '${indices}' already exist. Nothing to create.`);
return;
}

const { body } = await es.indices.create({ index: indices });
const { body } = await es.indices.create({
index: indices,
...(mappings ? { body: { mappings } } : {}),
});
expect(body)
.to.have.property('acknowledged')
.eql(true, 'Response for create request indices should be acknowledged.');
Expand Down

0 comments on commit a346964

Please sign in to comment.