diff --git a/x-pack/plugins/fleet/server/services/epm/kibana/assets/install.ts b/x-pack/plugins/fleet/server/services/epm/kibana/assets/install.ts index 1c804991cbeaf..20a0484c77a4a 100644 --- a/x-pack/plugins/fleet/server/services/epm/kibana/assets/install.ts +++ b/x-pack/plugins/fleet/server/services/epm/kibana/assets/install.ts @@ -140,15 +140,21 @@ export async function installKibanaAssets(options: { return []; } - // As we use `import` to create our saved objects, we have to install - // their references (the index patterns) at the same time - // to prevent a reference error + // Create index patterns separately with `overwrite: false` to prevent blowing away users' runtime fields. + // These don't get retried on conflict, because we expect that they exist once an integration has been installed. const indexPatternSavedObjects = getIndexPatternSavedObjects() as ArchiveAsset[]; + await savedObjectsImporter.import({ + overwrite: false, + readStream: createListStream(indexPatternSavedObjects), + createNewCopies: false, + refresh: false, + managed: true, + }); const installedAssets = await installKibanaSavedObjects({ logger, savedObjectsImporter, - kibanaAssets: [...indexPatternSavedObjects, ...assetsToInstall], + kibanaAssets: assetsToInstall, }); return installedAssets; diff --git a/x-pack/test/fleet_api_integration/apis/epm/data_views.ts b/x-pack/test/fleet_api_integration/apis/epm/data_views.ts new file mode 100644 index 0000000000000..1fc5e660dbc57 --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/epm/data_views.ts @@ -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 expect from '@kbn/expect'; + +import { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; +import { skipIfNoDockerRegistry } from '../../helpers'; +import { setupFleetAndAgents } from '../agents/services'; + +export default function (providerContext: FtrProviderContext) { + const { getService } = providerContext; + + const supertest = getService('supertest'); + + const testPkgs = [ + { + name: 'apache', + version: '0.1.4', + }, + { + name: 'nginx', + version: '1.2.1', + }, + ]; + + const uninstallPackage = async (name: string, version: string) => { + await supertest.delete(`/api/fleet/epm/packages/${name}/${version}`).set('kbn-xsrf', 'xxxx'); + }; + + const installPackage = async (name: string, version: string) => { + await supertest + .post(`/api/fleet/epm/packages/${name}/${version}`) + .set('kbn-xsrf', 'xxxx') + .send({ force: true }); + }; + + const listDataViews = async () => { + const response = await supertest.get('/api/data_views'); + + return response.body.data_view; + }; + + describe('EPM - data views', () => { + skipIfNoDockerRegistry(providerContext); + setupFleetAndAgents(providerContext); + + afterEach(async () => { + await Promise.all(testPkgs.map((pkg) => uninstallPackage(pkg.name, pkg.version))); + }); + + describe('with subsequent integration installation', async () => { + it('does not recreate managed data views', async () => { + await installPackage(testPkgs[0].name, testPkgs[0].version); + const initialDataViews: any[] = await listDataViews(); + const initialLogsDataView = initialDataViews.find(({ title }) => title === 'logs-*'); + const initialMetricsDataView = initialDataViews.find(({ title }) => title === 'metrics-*'); + + expect(initialLogsDataView).to.be.ok(); + expect(initialMetricsDataView).to.be.ok(); + + await installPackage(testPkgs[1].name, testPkgs[1].version); + const subsequentDataViews: any[] = await listDataViews(); + const subsequentLogsDataView = subsequentDataViews.find(({ title }) => title === 'logs-*'); + const subsequentMetricsDataView = subsequentDataViews.find( + ({ title }) => title === 'metrics-*' + ); + + // ID's should not have changed as the data views should not have been recreated + expect(initialLogsDataView.id).to.eql(subsequentLogsDataView.id); + expect(initialMetricsDataView.id).to.eql(subsequentMetricsDataView.id); + }); + }); + }); +} diff --git a/x-pack/test/fleet_api_integration/apis/epm/index.js b/x-pack/test/fleet_api_integration/apis/epm/index.js index 94f7c12a15ce4..3c4315eff20cb 100644 --- a/x-pack/test/fleet_api_integration/apis/epm/index.js +++ b/x-pack/test/fleet_api_integration/apis/epm/index.js @@ -47,5 +47,6 @@ export default function loadTests({ loadTestFile, getService }) { loadTestFile(require.resolve('./routing_rules')); loadTestFile(require.resolve('./install_runtime_field')); loadTestFile(require.resolve('./get_templates_inputs')); + loadTestFile(require.resolve('./data_views')); }); }