[Index Management] Add templates tests for serverless (#171972)

## Summary

Fixes https://github.com/elastic/kibana/issues/170980
Fixes https://github.com/elastic/kibana/issues/171345

This PR fixes the failing api integration test for index templates, adds
a test for `simulate` route and adds missing tests for serverless.


### Checklist

Delete any items that are not applicable to this PR.

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] Any UI touched in this PR is usable by keyboard only (learn more
about [keyboard accessibility](https://webaim.org/techniques/keyboard/))
- [ ] Any UI touched in this PR does not create any new axe failures
(run axe in browser:
[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),
[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This renders correctly on smaller devices using a responsive
layout. (You can test this [in your
browser](https://www.browserstack.com/guide/responsive-testing-on-local-server))
- [ ] This was checked for [cross-browser
compatibility](https://www.elastic.co/support/matrix#matrix_browsers)


### Risk Matrix

Delete this section if it is not applicable to this PR.

Before closing this PR, invite QA, stakeholders, and other developers to
identify risks that should be tested prior to the change/feature
release.

When forming the risk matrix, consider some of the following examples
and how they may potentially impact the change:

| Risk | Probability | Severity | Mitigation/Notes |

|---------------------------|-------------|----------|-------------------------|
| Multiple Spaces—unexpected behavior in non-default Kibana Space.
| Low | High | Integration tests will verify that all features are still
supported in non-default Kibana Space and when user switches between
spaces. |
| Multiple nodes—Elasticsearch polling might have race conditions
when multiple Kibana nodes are polling for the same tasks. | High | Low
| Tasks are idempotent, so executing them multiple times will not result
in logical error, but will degrade performance. To test for this case we
add plenty of unit tests around this logic and document manual testing
procedure. |
| Code should gracefully handle cases when feature X or plugin Y are
disabled. | Medium | High | Unit tests will verify that any feature flag
or plugin combination still results in our service operational. |
| [See more potential risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) |


### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
This commit is contained in:
Yulia Čech 2023-11-30 19:39:43 +01:00 committed by GitHub
parent 1453b4d7ca
commit ee991020b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 486 additions and 276 deletions

View file

@ -74,7 +74,7 @@ export function deserializeTemplate(
indexPatterns: indexPatterns.sort(),
template,
ilmPolicy: settings?.index?.lifecycle,
composedOf,
composedOf: composedOf ?? [],
dataStream,
allowAutoCreate,
_meta,

View file

@ -1,21 +0,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.
*/
/**
* Helpers to create and delete indices on the Elasticsearch instance
* during our tests.
* @param {ElasticsearchClient} es The Elasticsearch client instance
*/
export const initElasticsearchHelpers = (getService) => {
const es = getService('es');
const catTemplate = (name) => es.cat.templates({ name, format: 'json' }, { meta: true });
return {
catTemplate,
};
};

View file

@ -5,8 +5,4 @@
* 2.0.
*/
export { initElasticsearchHelpers } from './elasticsearch';
export { getRandomString } from './random';
export { wait } from './utils';

View file

@ -0,0 +1,63 @@
/*
* 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 { TemplateDeserialized, TemplateSerialized } from '@kbn/index-management-plugin/common';
import { API_BASE_PATH } from '../constants';
import { FtrProviderContext } from '../../../../ftr_provider_context';
export function templatesApi(getService: FtrProviderContext['getService']) {
const supertest = getService('supertest');
let templatesCreated: Array<{ name: string; isLegacy?: boolean }> = [];
const getAllTemplates = () => supertest.get(`${API_BASE_PATH}/index_templates`);
const getOneTemplate = (name: string, isLegacy: boolean = false) =>
supertest.get(`${API_BASE_PATH}/index_templates/${name}?legacy=${isLegacy}`);
const createTemplate = (template: TemplateDeserialized) => {
templatesCreated.push({ name: template.name, isLegacy: template._kbnMeta.isLegacy });
return supertest.post(`${API_BASE_PATH}/index_templates`).set('kbn-xsrf', 'xxx').send(template);
};
const deleteTemplates = (templates: Array<{ name: string; isLegacy?: boolean }>) =>
supertest
.post(`${API_BASE_PATH}/delete_index_templates`)
.set('kbn-xsrf', 'xxx')
.send({ templates });
const updateTemplate = (payload: TemplateDeserialized, templateName: string) =>
supertest
.put(`${API_BASE_PATH}/index_templates/${templateName}`)
.set('kbn-xsrf', 'xxx')
.send(payload);
// Delete all templates created during tests
const cleanUpTemplates = async () => {
try {
await deleteTemplates(templatesCreated);
templatesCreated = [];
} catch (e) {
// Silently swallow errors
}
};
const simulateTemplate = (payload: TemplateSerialized) =>
supertest
.post(`${API_BASE_PATH}/index_templates/simulate`)
.set('kbn-xsrf', 'xxx')
.send(payload);
return {
getAllTemplates,
getOneTemplate,
createTemplate,
updateTemplate,
deleteTemplates,
cleanUpTemplates,
simulateTemplate,
};
}

View file

@ -0,0 +1,77 @@
/*
* 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 { TemplateDeserialized, TemplateSerialized } from '@kbn/index-management-plugin/common';
import { FtrProviderContext } from '../../../../ftr_provider_context';
import { INDEX_PATTERNS } from '../constants';
const templateMock = {
settings: {
number_of_shards: 1,
},
mappings: {
_source: {
enabled: false,
},
properties: {
host_name: {
type: 'keyword',
},
created_at: {
type: 'date',
format: 'EEE MMM dd HH:mm:ss Z yyyy',
},
},
},
aliases: {
alias1: {},
},
};
export function templatesHelpers(getService: FtrProviderContext['getService']) {
const es = getService('es');
const catTemplate = (name: string) => es.cat.templates({ name, format: 'json' }, { meta: true });
const getTemplatePayload = (
name: string,
indexPatterns: string[] = INDEX_PATTERNS,
isLegacy: boolean = false
) => {
const baseTemplate: TemplateDeserialized = {
name,
indexPatterns,
version: 1,
template: { ...templateMock },
_kbnMeta: {
isLegacy,
type: 'default',
hasDatastream: false,
},
};
if (isLegacy) {
baseTemplate.order = 1;
} else {
baseTemplate.priority = 1;
}
return baseTemplate;
};
const getSerializedTemplate = (indexPatterns: string[] = INDEX_PATTERNS): TemplateSerialized => {
return {
index_patterns: indexPatterns,
template: { ...templateMock },
};
};
return {
catTemplate,
getTemplatePayload,
getSerializedTemplate,
};
}

View file

@ -1,8 +0,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.
*/
export const wait = (time = 1000) => new Promise((resolve) => setTimeout(resolve, time));

View file

@ -1,106 +0,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 { API_BASE_PATH, INDEX_PATTERNS } from './constants';
export const registerHelpers = ({ supertest }) => {
let templatesCreated = [];
const getAllTemplates = () => supertest.get(`${API_BASE_PATH}/index_templates`);
const getOneTemplate = (name, isLegacy = false) =>
supertest.get(`${API_BASE_PATH}/index_templates/${name}?legacy=${isLegacy}`);
const getTemplatePayload = (
name,
indexPatterns = INDEX_PATTERNS,
isLegacy = false,
type = 'default'
) => {
const baseTemplate = {
name,
indexPatterns,
version: 1,
template: {
settings: {
number_of_shards: 1,
index: {
lifecycle: {
name: 'my_policy',
},
},
},
mappings: {
_source: {
enabled: false,
},
properties: {
host_name: {
type: 'keyword',
},
created_at: {
type: 'date',
format: 'EEE MMM dd HH:mm:ss Z yyyy',
},
},
},
aliases: {
alias1: {},
},
},
_kbnMeta: {
isLegacy,
type,
},
};
if (isLegacy) {
baseTemplate.order = 1;
} else {
baseTemplate.priority = 1;
}
return baseTemplate;
};
const createTemplate = (template) => {
templatesCreated.push({ name: template.name, isLegacy: template._kbnMeta.isLegacy });
return supertest.post(`${API_BASE_PATH}/index_templates`).set('kbn-xsrf', 'xxx').send(template);
};
const deleteTemplates = (templates) =>
supertest
.post(`${API_BASE_PATH}/delete_index_templates`)
.set('kbn-xsrf', 'xxx')
.send({ templates });
const updateTemplate = (payload, templateName) =>
supertest
.put(`${API_BASE_PATH}/index_templates/${templateName}`)
.set('kbn-xsrf', 'xxx')
.send(payload);
// Delete all templates created during tests
const cleanUpTemplates = async () => {
try {
await deleteTemplates(templatesCreated);
templatesCreated = [];
} catch (e) {
// Silently swallow errors
}
};
return {
getAllTemplates,
getOneTemplate,
getTemplatePayload,
createTemplate,
updateTemplate,
deleteTemplates,
cleanUpTemplates,
};
};

View file

@ -7,26 +7,26 @@
import expect from '@kbn/expect';
import { initElasticsearchHelpers, getRandomString } from './lib';
import { registerHelpers } from './templates.helpers';
export default function ({ getService }) {
const supertest = getService('supertest');
const { catTemplate } = initElasticsearchHelpers(getService);
import { TemplateDeserialized } from '@kbn/index-management-plugin/common';
import { FtrProviderContext } from '../../../ftr_provider_context';
import { templatesApi } from './lib/templates.api';
import { templatesHelpers } from './lib/templates.helpers';
import { getRandomString } from './lib/random';
export default function ({ getService }: FtrProviderContext) {
const log = getService('log');
const { catTemplate, getTemplatePayload, getSerializedTemplate } = templatesHelpers(getService);
const {
getAllTemplates,
getOneTemplate,
createTemplate,
getTemplatePayload,
deleteTemplates,
updateTemplate,
cleanUpTemplates,
} = registerHelpers({ supertest });
simulateTemplate,
} = templatesApi(getService);
// FAILING ES PROMOTION: https://github.com/elastic/kibana/issues/170980
describe.skip('index templates', () => {
describe('index templates', () => {
after(async () => await cleanUpTemplates());
describe('get all', () => {
@ -39,7 +39,7 @@ export default function ({ getService }) {
true
);
const tmpTemplate = getTemplatePayload(`template-${getRandomString()}`, [getRandomString()]);
const indexTemplateWithLifecycle = {
const indexTemplateWithDSL = {
...tmpTemplate,
dataStream: {},
template: {
@ -50,21 +50,31 @@ export default function ({ getService }) {
},
},
};
const tmpTemplate2 = getTemplatePayload(`template-${getRandomString()}`, [getRandomString()]);
const indexTemplateWithILM = {
...tmpTemplate2,
template: {
...tmpTemplate2.template,
settings: {
...tmpTemplate2.template?.settings,
index: {
lifecycle: {
name: 'my_policy',
},
},
},
},
};
beforeEach(async () => {
const res1 = await createTemplate(indexTemplate);
if (res1.status !== 200) {
throw new Error(res1.body.message);
}
const res2 = await createTemplate(legacyTemplate);
if (res2.status !== 200) {
throw new Error(res2.body.message);
}
const res3 = await createTemplate(indexTemplateWithLifecycle);
if (res3.status !== 200) {
throw new Error(res3.body.message);
try {
await createTemplate(indexTemplate);
await createTemplate(legacyTemplate);
await createTemplate(indexTemplateWithDSL);
await createTemplate(indexTemplateWithILM);
} catch (err) {
log.debug('[Setup error] Error creating index template');
throw err;
}
});
@ -73,18 +83,10 @@ export default function ({ getService }) {
// Index templates (composable)
const indexTemplateFound = allTemplates.templates.find(
(template) => template.name === indexTemplate.name
(template: TemplateDeserialized) => template.name === indexTemplate.name
);
if (!indexTemplateFound) {
throw new Error(
`Index template "${indexTemplate.name}" not found in ${JSON.stringify(
allTemplates.templates,
null,
2
)}`
);
}
expect(indexTemplateFound).to.be.ok();
const expectedKeys = [
'name',
@ -92,7 +94,6 @@ export default function ({ getService }) {
'hasSettings',
'hasAliases',
'hasMappings',
'ilmPolicy',
'priority',
'composedOf',
'version',
@ -103,18 +104,10 @@ export default function ({ getService }) {
// Legacy index templates
const legacyTemplateFound = allTemplates.legacyTemplates.find(
(template) => template.name === legacyTemplate.name
(template: TemplateDeserialized) => template.name === legacyTemplate.name
);
if (!legacyTemplateFound) {
throw new Error(
`Legacy template "${legacyTemplate.name}" not found in ${JSON.stringify(
allTemplates.legacyTemplates,
null,
2
)}`
);
}
expect(legacyTemplateFound).to.be.ok();
const expectedLegacyKeys = [
'name',
@ -122,35 +115,28 @@ export default function ({ getService }) {
'hasSettings',
'hasAliases',
'hasMappings',
'ilmPolicy',
'order',
'version',
'_kbnMeta',
'composedOf',
].sort();
expect(Object.keys(legacyTemplateFound).sort()).to.eql(expectedLegacyKeys);
// Index template with lifecycle
const templateWithLifecycle = allTemplates.templates.find(
(template) => template.name === indexTemplateWithLifecycle.name
// Index template with DSL
const templateWithDSL = allTemplates.templates.find(
(template: TemplateDeserialized) => template.name === indexTemplateWithDSL.name
);
if (!templateWithLifecycle) {
throw new Error(
`Index template with lifecycle "${
indexTemplateWithLifecycle.name
}" not found in ${JSON.stringify(allTemplates.templates, null, 2)}`
);
}
expect(templateWithDSL).to.be.ok();
const expectedWithLifecycleKeys = [
const expectedWithDSLKeys = [
'name',
'indexPatterns',
'lifecycle',
'hasSettings',
'hasAliases',
'hasMappings',
'ilmPolicy',
'priority',
'composedOf',
'dataStream',
@ -158,7 +144,29 @@ export default function ({ getService }) {
'_kbnMeta',
].sort();
expect(Object.keys(templateWithLifecycle).sort()).to.eql(expectedWithLifecycleKeys);
expect(Object.keys(templateWithDSL).sort()).to.eql(expectedWithDSLKeys);
// Index template with ILM
const templateWithILM = allTemplates.templates.find(
(template: TemplateDeserialized) => template.name === indexTemplateWithILM.name
);
expect(templateWithILM).to.be.ok();
const expectedWithILMKeys = [
'name',
'indexPatterns',
'ilmPolicy',
'hasSettings',
'hasAliases',
'hasMappings',
'priority',
'composedOf',
'version',
'_kbnMeta',
].sort();
expect(Object.keys(templateWithILM).sort()).to.eql(expectedWithILMKeys);
});
});
@ -175,7 +183,6 @@ export default function ({ getService }) {
'indexPatterns',
'template',
'composedOf',
'ilmPolicy',
'priority',
'version',
'_kbnMeta',
@ -196,10 +203,10 @@ export default function ({ getService }) {
'name',
'indexPatterns',
'template',
'ilmPolicy',
'order',
'version',
'_kbnMeta',
'composedOf',
].sort();
const expectedTemplateKeys = ['aliases', 'mappings', 'settings'].sort();
@ -226,7 +233,7 @@ export default function ({ getService }) {
it('should throw a 409 conflict when trying to create 2 templates with the same name', async () => {
const templateName = `template-${getRandomString()}`;
const payload = getTemplatePayload(templateName, [getRandomString()], true);
const payload = getTemplatePayload(templateName, [getRandomString()]);
await createTemplate(payload);
@ -235,7 +242,8 @@ export default function ({ getService }) {
it('should validate the request payload', async () => {
const templateName = `template-${getRandomString()}`;
const payload = getTemplatePayload(templateName, [getRandomString()], true);
// need to cast as any to avoid errors after deleting index patterns
const payload = getTemplatePayload(templateName, [getRandomString()]) as any;
delete payload.indexPatterns; // index patterns are required
@ -256,7 +264,7 @@ export default function ({ getService }) {
},
},
};
payload.template.mappings = { ...payload.template.mappings, runtime };
payload.template!.mappings = { ...payload.template!.mappings, runtime };
const { body } = await createTemplate(payload).expect(400);
expect(body.attributes).an('object');
@ -278,8 +286,8 @@ export default function ({ getService }) {
const { name, version } = indexTemplate;
expect(
catTemplateResponse.find(({ name: templateName }) => templateName === name).version
).to.equal(version.toString());
catTemplateResponse.find(({ name: catTemplateName }) => catTemplateName === name)?.version
).to.equal(version?.toString());
// Update template with new version
const updatedVersion = 2;
@ -290,7 +298,7 @@ export default function ({ getService }) {
({ body: catTemplateResponse } = await catTemplate(templateName));
expect(
catTemplateResponse.find(({ name: templateName }) => templateName === name).version
catTemplateResponse.find(({ name: catTemplateName }) => catTemplateName === name)?.version
).to.equal(updatedVersion.toString());
});
@ -305,8 +313,8 @@ export default function ({ getService }) {
const { name, version } = legacyIndexTemplate;
expect(
catTemplateResponse.find(({ name: templateName }) => templateName === name).version
).to.equal(version.toString());
catTemplateResponse.find(({ name: catTemplateName }) => catTemplateName === name)?.version
).to.equal(version?.toString());
// Update template with new version
const updatedVersion = 2;
@ -318,12 +326,12 @@ export default function ({ getService }) {
({ body: catTemplateResponse } = await catTemplate(templateName));
expect(
catTemplateResponse.find(({ name: templateName }) => templateName === name).version
catTemplateResponse.find(({ name: catTemplateName }) => catTemplateName === name)?.version
).to.equal(updatedVersion.toString());
});
it('should parse the ES error and return the cause', async () => {
const templateName = `template-update-parse-es-error}`;
const templateName = `template-update-parse-es-error`;
const payload = getTemplatePayload(templateName, ['update-parse-es-error']);
const runtime = {
myRuntimeField: {
@ -335,12 +343,12 @@ export default function ({ getService }) {
};
// Add runtime field
payload.template.mappings = { ...payload.template.mappings, runtime };
payload.template!.mappings = { ...payload.template!.mappings, runtime };
await createTemplate(payload).expect(200);
// Update template with an error in the runtime field script
payload.template.mappings.runtime.myRuntimeField.script = 'emit("hello with error';
payload.template!.mappings.runtime.myRuntimeField.script = 'emit("hello with error';
const { body } = await updateTemplate(payload, templateName).expect(400);
expect(body.attributes).an('object');
@ -362,7 +370,7 @@ export default function ({ getService }) {
let { body: catTemplateResponse } = await catTemplate(templateName);
expect(
catTemplateResponse.find((template) => template.name === payload.name).name
catTemplateResponse.find((template) => template.name === payload.name)?.name
).to.equal(templateName);
const { status: deleteStatus, body: deleteBody } = await deleteTemplates([
@ -372,7 +380,7 @@ export default function ({ getService }) {
throw new Error(`Error deleting template: ${deleteBody.message}`);
}
expect(deleteBody.errors).to.be.empty;
expect(deleteBody.errors).to.be.empty();
expect(deleteBody.templatesDeleted[0]).to.equal(templateName);
({ body: catTemplateResponse } = await catTemplate(templateName));
@ -391,14 +399,14 @@ export default function ({ getService }) {
let { body: catTemplateResponse } = await catTemplate(templateName);
expect(
catTemplateResponse.find((template) => template.name === payload.name).name
catTemplateResponse.find((template) => template.name === payload.name)?.name
).to.equal(templateName);
const { body } = await deleteTemplates([
{ name: templateName, isLegacy: payload._kbnMeta.isLegacy },
]).expect(200);
expect(body.errors).to.be.empty;
expect(body.errors).to.be.empty();
expect(body.templatesDeleted[0]).to.equal(templateName);
({ body: catTemplateResponse } = await catTemplate(templateName));
@ -408,5 +416,14 @@ export default function ({ getService }) {
);
});
});
describe('simulate', () => {
it('should simulate an index template', async () => {
const payload = getSerializedTemplate([getRandomString()]);
const { body } = await simulateTemplate(payload).expect(200);
expect(body.template).to.be.ok();
});
});
});
}

View file

@ -9,6 +9,8 @@ import { FtrProviderContext } from '../ftr_provider_context';
import { indicesApi } from '../apis/management/index_management/lib/indices.api';
import { mappingsApi } from '../apis/management/index_management/lib/mappings.api';
import { indicesHelpers } from '../apis/management/index_management/lib/indices.helpers';
import { templatesApi } from '../apis/management/index_management/lib/templates.api';
import { templatesHelpers } from '../apis/management/index_management/lib/templates.helpers';
import { componentTemplatesApi } from '../apis/management/index_management/lib/component_templates.api';
import { componentTemplateHelpers } from '../apis/management/index_management/lib/component_template.helpers';
import { settingsApi } from '../apis/management/index_management/lib/settings.api';
@ -34,6 +36,10 @@ export function IndexManagementProvider({ getService }: FtrProviderContext) {
mappings: {
api: mappingsApi(getService),
},
templates: {
api: templatesApi(getService),
helpers: templatesHelpers(getService),
},
settings: {
api: settingsApi(getService),
},

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import expect from 'expect';
import expect from '@kbn/expect';
import { FtrProviderContext } from '../../../ftr_provider_context';
const API_BASE_PATH = '/api/index_management';
@ -14,80 +14,266 @@ export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const es = getService('es');
const log = getService('log');
const randomness = getService('randomness');
const indexManagementService = getService('indexManagement');
let getTemplatePayload: typeof indexManagementService['templates']['helpers']['getTemplatePayload'];
let catTemplate: typeof indexManagementService['templates']['helpers']['catTemplate'];
let getSerializedTemplate: typeof indexManagementService['templates']['helpers']['getSerializedTemplate'];
let createTemplate: typeof indexManagementService['templates']['api']['createTemplate'];
let updateTemplate: typeof indexManagementService['templates']['api']['updateTemplate'];
let deleteTemplates: typeof indexManagementService['templates']['api']['deleteTemplates'];
let simulateTemplate: typeof indexManagementService['templates']['api']['simulateTemplate'];
let getRandomString: () => string;
describe('Index templates', function () {
const templateName = `template-${Math.random()}`;
const indexTemplate = {
name: templateName,
body: {
index_patterns: ['test*'],
},
};
before(async () => {
// Create a new index template to test against
try {
await es.indices.putIndexTemplate(indexTemplate);
} catch (err) {
log.debug('[Setup error] Error creating index template');
throw err;
}
({
templates: {
helpers: { getTemplatePayload, catTemplate, getSerializedTemplate },
api: { createTemplate, updateTemplate, deleteTemplates, simulateTemplate },
},
} = indexManagementService);
getRandomString = () => randomness.string({ casing: 'lower', alpha: true });
});
after(async () => {
// Cleanup template created for testing purposes
try {
await es.indices.deleteIndexTemplate({
describe('get', () => {
let templateName: string;
before(async () => {
templateName = `template-${getRandomString()}`;
const indexTemplate = {
name: templateName,
body: {
index_patterns: ['test*'],
},
};
// Create a new index template to test against
try {
await es.indices.putIndexTemplate(indexTemplate);
} catch (err) {
log.debug('[Setup error] Error creating index template');
throw err;
}
});
after(async () => {
// Cleanup template created for testing purposes
try {
await es.indices.deleteIndexTemplate({
name: templateName,
});
} catch (err) {
log.debug('[Cleanup error] Error deleting index template');
throw err;
}
});
describe('all', () => {
it('should list all the index templates with the expected parameters', async () => {
const { body: allTemplates } = await supertest
.get(`${API_BASE_PATH}/index_templates`)
.set('kbn-xsrf', 'xxx')
.set('x-elastic-internal-origin', 'xxx')
.expect(200);
// Legacy templates are not applicable on serverless
expect(allTemplates.legacyTemplates.length).to.eql(0);
const indexTemplateFound = allTemplates.templates.find(
(template: { name: string }) => template.name === templateName
);
expect(indexTemplateFound).to.be.ok();
const expectedKeys = [
'name',
'indexPatterns',
'hasSettings',
'hasAliases',
'hasMappings',
'_kbnMeta',
'composedOf',
].sort();
expect(Object.keys(indexTemplateFound).sort()).to.eql(expectedKeys);
});
} catch (err) {
log.debug('[Cleanup error] Error deleting index template');
throw err;
}
});
});
describe('get all', () => {
it('should list all the index templates with the expected parameters', async () => {
const { body: allTemplates } = await supertest
.get(`${API_BASE_PATH}/index_templates`)
.set('kbn-xsrf', 'xxx')
.set('x-elastic-internal-origin', 'xxx')
.expect(200);
describe('one', () => {
it('should return an index template with the expected parameters', async () => {
const { body } = await supertest
.get(`${API_BASE_PATH}/index_templates/${templateName}`)
.set('kbn-xsrf', 'xxx')
.set('x-elastic-internal-origin', 'xxx')
.expect(200);
// Legacy templates are not applicable on serverless
expect(allTemplates.legacyTemplates.length).toEqual(0);
const expectedKeys = [
'name',
'indexPatterns',
'template',
'_kbnMeta',
'composedOf',
].sort();
const indexTemplateFound = allTemplates.templates.find(
(template: { name: string }) => template.name === indexTemplate.name
);
expect(indexTemplateFound).toBeTruthy();
const expectedKeys = [
'name',
'indexPatterns',
'hasSettings',
'hasAliases',
'hasMappings',
'_kbnMeta',
].sort();
expect(Object.keys(indexTemplateFound).sort()).toEqual(expectedKeys);
expect(body.name).to.eql(templateName);
expect(Object.keys(body).sort()).to.eql(expectedKeys);
});
});
});
describe('get one', () => {
it('should return an index template with the expected parameters', async () => {
const { body } = await supertest
.get(`${API_BASE_PATH}/index_templates/${templateName}`)
.set('kbn-xsrf', 'xxx')
describe('create', () => {
it('should create an index template', async () => {
const payload = getTemplatePayload(`template-${getRandomString()}`, [getRandomString()]);
await createTemplate(payload).set('x-elastic-internal-origin', 'xxx').expect(200);
});
it('should throw a 409 conflict when trying to create 2 templates with the same name', async () => {
const templateName = `template-${getRandomString()}`;
const payload = getTemplatePayload(templateName, [getRandomString()]);
await createTemplate(payload).set('x-elastic-internal-origin', 'xxx');
await createTemplate(payload).set('x-elastic-internal-origin', 'xxx').expect(409);
});
it('should validate the request payload', async () => {
const templateName = `template-${getRandomString()}`;
// need to cast as any to avoid errors after deleting index patterns
const payload = getTemplatePayload(templateName, [getRandomString()]) as any;
delete payload.indexPatterns; // index patterns are required
const { body } = await createTemplate(payload).set('x-elastic-internal-origin', 'xxx');
expect(body.message).to.contain(
'[request body.indexPatterns]: expected value of type [array] '
);
});
it('should parse the ES error and return the cause', async () => {
const templateName = `template-create-parse-es-error`;
const payload = getTemplatePayload(templateName, ['create-parse-es-error']);
const runtime = {
myRuntimeField: {
type: 'boolean',
script: {
source: 'emit("hello with error', // error in script
},
},
};
payload.template!.mappings = { ...payload.template!.mappings, runtime };
const { body } = await createTemplate(payload)
.set('x-elastic-internal-origin', 'xxx')
.expect(400);
expect(body.attributes).an('object');
expect(body.attributes.error.reason).contain('template after composition is invalid');
// one of the item of the cause array should point to our script
expect(body.attributes.causes.join(',')).contain('"hello with error');
});
});
describe('update', () => {
it('should update an index template', async () => {
const templateName = `template-${getRandomString()}`;
const indexTemplate = getTemplatePayload(templateName, [getRandomString()]);
await createTemplate(indexTemplate).set('x-elastic-internal-origin', 'xxx').expect(200);
let { body: catTemplateResponse } = await catTemplate(templateName);
const { name, version } = indexTemplate;
expect(
catTemplateResponse.find(({ name: catTemplateName }) => catTemplateName === name)?.version
).to.equal(version?.toString());
// Update template with new version
const updatedVersion = 2;
await updateTemplate({ ...indexTemplate, version: updatedVersion }, templateName)
.set('x-elastic-internal-origin', 'xxx')
.expect(200);
const expectedKeys = ['name', 'indexPatterns', 'template', '_kbnMeta'].sort();
({ body: catTemplateResponse } = await catTemplate(templateName));
expect(body.name).toEqual(templateName);
expect(Object.keys(body).sort()).toEqual(expectedKeys);
expect(
catTemplateResponse.find(({ name: catTemplateName }) => catTemplateName === name)?.version
).to.equal(updatedVersion.toString());
});
it('should parse the ES error and return the cause', async () => {
const templateName = `template-update-parse-es-error`;
const payload = getTemplatePayload(templateName, ['update-parse-es-error']);
const runtime = {
myRuntimeField: {
type: 'keyword',
script: {
source: 'emit("hello")',
},
},
};
// Add runtime field
payload.template!.mappings = { ...payload.template!.mappings, runtime };
await createTemplate(payload).set('x-elastic-internal-origin', 'xxx').expect(200);
// Update template with an error in the runtime field script
payload.template!.mappings.runtime.myRuntimeField.script = 'emit("hello with error';
const { body } = await updateTemplate(payload, templateName)
.set('x-elastic-internal-origin', 'xxx')
.expect(400);
expect(body.attributes).an('object');
// one of the item of the cause array should point to our script
expect(body.attributes.causes.join(',')).contain('"hello with error');
});
});
describe('delete', () => {
it('should delete an index template', async () => {
const templateName = `template-${getRandomString()}`;
const payload = getTemplatePayload(templateName, [getRandomString()]);
const { status: createStatus, body: createBody } = await createTemplate(payload).set(
'x-elastic-internal-origin',
'xxx'
);
if (createStatus !== 200) {
throw new Error(`Error creating template: ${createStatus} ${createBody.message}`);
}
let { body: catTemplateResponse } = await catTemplate(templateName);
expect(
catTemplateResponse.find((template) => template.name === payload.name)?.name
).to.equal(templateName);
const { status: deleteStatus, body: deleteBody } = await deleteTemplates([
{ name: templateName },
]).set('x-elastic-internal-origin', 'xxx');
if (deleteStatus !== 200) {
throw new Error(`Error deleting template: ${deleteBody.message}`);
}
expect(deleteBody.errors).to.be.empty();
expect(deleteBody.templatesDeleted[0]).to.equal(templateName);
({ body: catTemplateResponse } = await catTemplate(templateName));
expect(catTemplateResponse.find((template) => template.name === payload.name)).to.equal(
undefined
);
});
});
describe('simulate', () => {
it('should simulate an index template', async () => {
const payload = getSerializedTemplate([getRandomString()]);
const { body } = await simulateTemplate(payload)
.set('x-elastic-internal-origin', 'xxx')
.expect(200);
expect(body.template).to.be.ok();
});
});
});