[Index Management] Add missing enrich policies tests (#172442)

This commit is contained in:
Ignacio Rivas 2023-12-06 14:33:00 +01:00 committed by GitHub
parent 9f3f22ae68
commit 9b822b96c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 251 additions and 0 deletions

View file

@ -6,6 +6,7 @@
*/
export const API_BASE_PATH = '/api/index_management';
export const INTERNAL_API_BASE_PATH = '/internal/index_management';
export const INDEX_PATTERNS = ['test*'];

View file

@ -0,0 +1,73 @@
/*
* 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 'expect';
import { FtrProviderContext } from '../../../ftr_provider_context';
import { enrichPoliciesApi } from './lib/enrich_policies.api';
import { enrichPoliciesHelpers } from './lib/enrich_policies.helpers';
export default function ({ getService }: FtrProviderContext) {
const log = getService('log');
const { createIndex, deleteIndex, createEnrichPolicy } = enrichPoliciesHelpers(getService);
const { getAllEnrichPolicies, removeEnrichPolicy, executeEnrichPolicy } =
enrichPoliciesApi(getService);
describe('Enrich policies', function () {
const INDEX_NAME = `index-${Math.random()}`;
const POLICY_NAME = `policy-${Math.random()}`;
before(async () => {
try {
await createIndex(INDEX_NAME);
await createEnrichPolicy(POLICY_NAME, INDEX_NAME);
} catch (err) {
log.debug('[Setup error] Error creating test index and policy');
throw err;
}
});
after(async () => {
try {
await deleteIndex(INDEX_NAME);
} catch (err) {
log.debug('[Cleanup error] Error deleting test index');
throw err;
}
});
it('should list all policies', async () => {
const { body } = await getAllEnrichPolicies().expect(200);
expect(body).toStrictEqual([
{
enrichFields: ['firstName'],
matchField: 'email',
name: POLICY_NAME,
sourceIndices: [INDEX_NAME],
type: 'match',
},
]);
});
it('should be able to execute a policy', async () => {
await executeEnrichPolicy(POLICY_NAME).expect(200);
// Wait for a little bit for the policy to be executed, so that it can
// be deleted in the next test.
await new Promise((resolve) => setTimeout(resolve, 2000));
});
it('should be able to delete a policy', async () => {
const { body } = await removeEnrichPolicy(POLICY_NAME).expect(200);
expect(body).toStrictEqual({ acknowledged: true });
});
});
}

View file

@ -18,6 +18,7 @@ export default function ({ loadTestFile }: FtrProviderContext) {
loadTestFile(require.resolve('./component_templates'));
loadTestFile(require.resolve('./cluster_nodes'));
loadTestFile(require.resolve('./index_details'));
loadTestFile(require.resolve('./enrich_policies'));
loadTestFile(require.resolve('./create_enrich_policy'));
});
}

View file

@ -0,0 +1,37 @@
/*
* 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 { INTERNAL_API_BASE_PATH } from '../constants';
import { FtrProviderContext } from '../../../../ftr_provider_context';
export function enrichPoliciesApi(getService: FtrProviderContext['getService']) {
const supertest = getService('supertest');
const getAllEnrichPolicies = () =>
supertest
.get(`${INTERNAL_API_BASE_PATH}/enrich_policies`)
.set('kbn-xsrf', 'xxx')
.set('x-elastic-internal-origin', 'xxx');
const executeEnrichPolicy = (name: string) =>
supertest
.put(`${INTERNAL_API_BASE_PATH}/enrich_policies/${name}`)
.set('kbn-xsrf', 'xxx')
.set('x-elastic-internal-origin', 'xxx');
const removeEnrichPolicy = (name: string) =>
supertest
.delete(`${INTERNAL_API_BASE_PATH}/enrich_policies/${name}`)
.set('kbn-xsrf', 'xxx')
.set('x-elastic-internal-origin', 'xxx');
return {
getAllEnrichPolicies,
removeEnrichPolicy,
executeEnrichPolicy,
};
}

View file

@ -0,0 +1,51 @@
/*
* 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 { FtrProviderContext } from '../../../../ftr_provider_context';
export function enrichPoliciesHelpers(getService: FtrProviderContext['getService']) {
const es = getService('es');
const createEnrichPolicy = async (policyName: string, indexName: string) => {
await es.enrich.putPolicy({
name: policyName,
match: {
match_field: 'email',
enrich_fields: ['firstName'],
indices: [indexName],
},
});
};
const createIndex = async (indexName: string) => {
await es.indices.create({
index: indexName,
body: {
mappings: {
properties: {
email: {
type: 'text',
},
firstName: {
type: 'text',
},
},
},
},
});
};
const deleteIndex = async (indexName: string) => {
await es.indices.delete({ index: indexName });
};
return {
createEnrichPolicy,
createIndex,
deleteIndex,
};
}

View file

@ -16,6 +16,8 @@ import { componentTemplateHelpers } from '../apis/management/index_management/li
import { settingsApi } from '../apis/management/index_management/lib/settings.api';
import { clusterNodesApi } from '../apis/management/index_management/lib/cluster_nodes.api';
import { datastreamsHelpers } from '../apis/management/index_management/lib/datastreams.helpers';
import { enrichPoliciesApi } from '../apis/management/index_management/lib/enrich_policies.api';
import { enrichPoliciesHelpers } from '../apis/management/index_management/lib/enrich_policies.helpers';
export function IndexManagementProvider({ getService }: FtrProviderContext) {
return {
@ -43,5 +45,9 @@ export function IndexManagementProvider({ getService }: FtrProviderContext) {
settings: {
api: settingsApi(getService),
},
enrichPolicies: {
api: enrichPoliciesApi(getService),
helpers: enrichPoliciesHelpers(getService),
},
};
}

View file

@ -0,0 +1,81 @@
/*
* 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 'expect';
import { FtrProviderContext } from '../../../ftr_provider_context';
export default function ({ getService }: FtrProviderContext) {
const log = getService('log');
const indexManagementService = getService('indexManagement');
describe('Enrich policies', function () {
const INDEX_NAME = `index-${Math.random()}`;
const POLICY_NAME = `policy-${Math.random()}`;
let createIndex: typeof indexManagementService['enrichPolicies']['helpers']['createIndex'];
let deleteIndex: typeof indexManagementService['enrichPolicies']['helpers']['deleteIndex'];
let createEnrichPolicy: typeof indexManagementService['enrichPolicies']['helpers']['createEnrichPolicy'];
let getAllEnrichPolicies: typeof indexManagementService['enrichPolicies']['api']['getAllEnrichPolicies'];
let removeEnrichPolicy: typeof indexManagementService['enrichPolicies']['api']['removeEnrichPolicy'];
let executeEnrichPolicy: typeof indexManagementService['enrichPolicies']['api']['executeEnrichPolicy'];
before(async () => {
({
enrichPolicies: {
helpers: { createIndex, deleteIndex, createEnrichPolicy },
api: { getAllEnrichPolicies, removeEnrichPolicy, executeEnrichPolicy },
},
} = indexManagementService);
try {
await createIndex(INDEX_NAME);
await createEnrichPolicy(POLICY_NAME, INDEX_NAME);
} catch (err) {
log.debug('[Setup error] Error creating test index and policy');
throw err;
}
});
after(async () => {
try {
await deleteIndex(INDEX_NAME);
} catch (err) {
log.debug('[Cleanup error] Error deleting test index');
throw err;
}
});
it('should list all policies', async () => {
const { body } = await getAllEnrichPolicies().expect(200);
expect(body).toEqual([
{
enrichFields: ['firstName'],
matchField: 'email',
name: POLICY_NAME,
sourceIndices: [INDEX_NAME],
type: 'match',
},
]);
});
it('should be able to execute a policy', async () => {
await executeEnrichPolicy(POLICY_NAME).expect(200);
// Wait for a little bit for the policy to be executed, so that it can
// be deleted in the next test.
await new Promise((resolve) => setTimeout(resolve, 2000));
});
it('should be able to delete a policy', async () => {
const { body } = await removeEnrichPolicy(POLICY_NAME).expect(200);
expect(body).toEqual({ acknowledged: true });
});
});
}

View file

@ -13,6 +13,7 @@ export default function ({ loadTestFile }: FtrProviderContext) {
loadTestFile(require.resolve('./index_templates'));
loadTestFile(require.resolve('./indices'));
loadTestFile(require.resolve('./enrich_policies'));
loadTestFile(require.resolve('./create_enrich_policies'));
loadTestFile(require.resolve('./index_component_templates'));
loadTestFile(require.resolve('./cluster_nodes'));