diff --git a/x-pack/legacy/plugins/epm/server/lib/ilm/ilm.test.ts b/x-pack/legacy/plugins/epm/server/lib/ilm/ilm.test.ts new file mode 100644 index 0000000000000..00bf12a6316d2 --- /dev/null +++ b/x-pack/legacy/plugins/epm/server/lib/ilm/ilm.test.ts @@ -0,0 +1,15 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { getIndexWithWithAlias } from './ilm'; + +test('get index with alias', () => { + const aliasName = 'bar'; + + const data = getIndexWithWithAlias(aliasName); + // Verifies that the bar key exists and write index is set to true + expect(data.aliases.bar.is_write_index).toStrictEqual(true); +}); diff --git a/x-pack/legacy/plugins/epm/server/lib/ilm/ilm.ts b/x-pack/legacy/plugins/epm/server/lib/ilm/ilm.ts new file mode 100644 index 0000000000000..65c7783669908 --- /dev/null +++ b/x-pack/legacy/plugins/epm/server/lib/ilm/ilm.ts @@ -0,0 +1,38 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +export function getIndexWithWithAlias(aliasName: string) { + return { + aliases: { + [aliasName]: { + is_write_index: true, + }, + }, + }; +} + +/** + * Returns the current default policy used for Beats. + * This will later be replaced by the default policies. + * + * This policy will have to be pushed to PUT /_ilm/policy/{policy-name} + */ +export function getPolicy() { + return { + policy: { + phases: { + hot: { + actions: { + rollover: { + max_size: '50gb', + max_age: '30d', + }, + }, + }, + }, + }, + }; +} diff --git a/x-pack/test/epm_api_integration/apis/ilm.ts b/x-pack/test/epm_api_integration/apis/ilm.ts new file mode 100644 index 0000000000000..8037f813633eb --- /dev/null +++ b/x-pack/test/epm_api_integration/apis/ilm.ts @@ -0,0 +1,64 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import expect from '@kbn/expect'; +import { FtrProviderContext } from '../../api_integration/ftr_provider_context'; +import { getPolicy, getIndexWithWithAlias } from '../../../legacy/plugins/epm/server/lib/ilm/ilm'; + +export default function({ getService }: FtrProviderContext) { + describe('ilm', () => { + it('setup policy', async () => { + const policyName = 'foo'; + const es = getService('es'); + const policy = getPolicy(); + + const data = await es.transport.request({ + method: 'PUT', + path: '/_ilm/policy/' + policyName, + body: policy, + }); + + expect(data.body.acknowledged).to.eql(true); + expect(data.statusCode).to.eql(200); + }); + + it('setup index with alias', async () => { + const indexName = 'test-index-with-alias'; + const aliasName = 'alias-to-index'; + const es = getService('es'); + + // Delete index first if it exists as otherwise we get an error + const existsBody = await es.indices.exists({ index: indexName }); + if (existsBody.statusCode === 200) { + const response = await es.indices.delete({ index: indexName }); + + // Sanity check to make sure removal work as expected + // If it didn't we already know where the problem lays in the test + expect(response.statusCode).to.eql(200); + } + + // Calls the given esClient, creates and index and sets it as write index on the given alias. + // + // This should be moved later to the ilm lib but have it here for now as passing the client + // does not work. + const body = getIndexWithWithAlias(aliasName); + const data = await es.indices.create({ + index: indexName, + body, + }); + + // Sanity checks to make sure ES confirmed the data we sent is sane + // and the index with the alias was created. + expect(data.body.acknowledged).to.eql(true); + expect(data.statusCode).to.eql(200); + + // Retreiving the index information again to see if the is_write_index + // is set correctly for the alias. + const indexData = await es.indices.get({ index: indexName }); + expect(indexData.body[indexName].aliases[aliasName].is_write_index).to.eql(true); + }); + }); +} diff --git a/x-pack/test/epm_api_integration/apis/index.js b/x-pack/test/epm_api_integration/apis/index.js index 117aa51cae5d2..3dc4624d15cf4 100644 --- a/x-pack/test/epm_api_integration/apis/index.js +++ b/x-pack/test/epm_api_integration/apis/index.js @@ -10,5 +10,6 @@ export default function ({ loadTestFile }) { loadTestFile(require.resolve('./list')); loadTestFile(require.resolve('./file')); loadTestFile(require.resolve('./template')); + loadTestFile(require.resolve('./ilm')); }); } diff --git a/x-pack/test/epm_api_integration/apis/template.ts b/x-pack/test/epm_api_integration/apis/template.ts index 97cf24619f65f..e46ffff4cdc9c 100644 --- a/x-pack/test/epm_api_integration/apis/template.ts +++ b/x-pack/test/epm_api_integration/apis/template.ts @@ -15,15 +15,23 @@ export default function({ getService }: FtrProviderContext) { // This test was inspired by https://github.com/elastic/kibana/blob/master/x-pack/test/api_integration/apis/monitoring/common/mappings_exist.js describe('template', async () => { it('can be loaded', async () => { - const body = getTemplate(indexPattern); + const template = getTemplate(indexPattern); - const { body: response } = await es.indices.putTemplate({ + // This test is not an API integration test with Kibana + // We want to test here if the template is valid and for this we need a running ES instance. + // If the ES instance takes the template, we assume it is a valid template. + const { body: response1 } = await es.indices.putTemplate({ name: templateName, - body, + body: template, }); - expect(response).to.eql({ acknowledged: true }); - const { body: indexTemplate } = await es.indices.getTemplate({ name: templateName }); - expect(indexTemplate[templateName].index_patterns).to.eql([indexPattern]); + // Checks if template loading worked as expected + expect(response1).to.eql({ acknowledged: true }); + + const { body: response2 } = await es.indices.getTemplate({ name: templateName }); + // Checks if the content of the template that was loaded is as expected + // We already know based on the above test that the template was valid + // but we check here also if we wrote the index pattern inside the template as expected + expect(response2[templateName].index_patterns).to.eql([indexPattern]); }); }); }