From 5c13e901ac1bf01bcbcca1d4103c43438ee8f6e6 Mon Sep 17 00:00:00 2001 From: Abdul Wahab Zahid Date: Wed, 8 Jan 2025 11:31:16 +0100 Subject: [PATCH] [Dataset Quality] Move `/integrations/{id}/dashboards` API Integration test to Deployment Agnostic (#205256) Move the `GET /internal/dataset_quality/integrations/{integration}/dashboards` API test to Deployment Agnostic. --- .../observability/dataset_quality/index.ts | 1 + .../dataset_quality/integration_dashboards.ts | 145 ++++++++++++++++++ .../common/config.ts | 3 - .../common/package_service.ts | 30 ---- .../tests/data_streams/es_utils.ts | 33 ---- .../integration_dashboards.spec.ts | 86 ----------- .../tests/integrations/package_utils.ts | 40 ----- .../utils/data_stream.ts | 26 ---- .../utils/index.ts | 1 - 9 files changed, 146 insertions(+), 219 deletions(-) create mode 100644 x-pack/test/api_integration/deployment_agnostic/apis/observability/dataset_quality/integration_dashboards.ts delete mode 100644 x-pack/test/dataset_quality_api_integration/common/package_service.ts delete mode 100644 x-pack/test/dataset_quality_api_integration/tests/integrations/integration_dashboards.spec.ts delete mode 100644 x-pack/test/dataset_quality_api_integration/tests/integrations/package_utils.ts delete mode 100644 x-pack/test/dataset_quality_api_integration/utils/data_stream.ts diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/dataset_quality/index.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/dataset_quality/index.ts index 17ce93321b511..fd2a7e85baed1 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/dataset_quality/index.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/dataset_quality/index.ts @@ -10,6 +10,7 @@ import { DeploymentAgnosticFtrProviderContext } from '../../../ftr_provider_cont export default function ({ loadTestFile }: DeploymentAgnosticFtrProviderContext) { describe('Dataset quality', () => { loadTestFile(require.resolve('./integrations')); + loadTestFile(require.resolve('./integration_dashboards')); loadTestFile(require.resolve('./degraded_field_analyze')); loadTestFile(require.resolve('./data_stream_settings')); loadTestFile(require.resolve('./data_stream_rollover')); diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/dataset_quality/integration_dashboards.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/dataset_quality/integration_dashboards.ts new file mode 100644 index 0000000000000..4d9b6355cf3e9 --- /dev/null +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/dataset_quality/integration_dashboards.ts @@ -0,0 +1,145 @@ +/* + * 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 { CustomIntegration } from '../../../services/package_api'; +import { DeploymentAgnosticFtrProviderContext } from '../../../ftr_provider_context'; +import { RoleCredentials, SupertestWithRoleScopeType } from '../../../services'; + +export default function ({ getService }: DeploymentAgnosticFtrProviderContext) { + const samlAuth = getService('samlAuth'); + const roleScopedSupertest = getService('roleScopedSupertest'); + const packageApi = getService('packageApi'); + + const integrationPackages = ['nginx', 'apache']; + const customIntegrations: CustomIntegration[] = [ + { + integrationName: 'my.custom.integration', + datasets: [ + { + name: 'my.custom.integration', + type: 'logs', + }, + ], + }, + ]; + + async function callApiAs( + roleScopedSupertestWithCookieCredentials: SupertestWithRoleScopeType, + integration: string + ) { + return roleScopedSupertestWithCookieCredentials.get( + `/internal/dataset_quality/integrations/${integration}/dashboards` + ); + } + + describe('Integration dashboards', () => { + let adminRoleAuthc: RoleCredentials; + before(async () => { + adminRoleAuthc = await samlAuth.createM2mApiKeyWithRoleScope('admin'); + await Promise.all( + integrationPackages.map((pkg) => + packageApi.installPackage({ + roleAuthc: adminRoleAuthc, + pkg, + }) + ) + ); + + await Promise.all( + customIntegrations.map((customIntegration) => + packageApi.installCustomIntegration({ + roleAuthc: adminRoleAuthc, + customIntegration, + }) + ) + ); + }); + + after(async () => { + await Promise.all( + integrationPackages.map((pkg) => + packageApi.uninstallPackage({ roleAuthc: adminRoleAuthc, pkg }) + ) + ); + + await Promise.all( + customIntegrations.map((customIntegration) => + packageApi.uninstallPackage({ + roleAuthc: adminRoleAuthc, + pkg: customIntegration.integrationName, + }) + ) + ); + + await samlAuth.invalidateM2mApiKeyWithRoleScope(adminRoleAuthc); + }); + + describe('gets the installed integration dashboards', () => { + let supertestViewerWithCookieCredentials: SupertestWithRoleScopeType; + before(async () => { + supertestViewerWithCookieCredentials = await roleScopedSupertest.getSupertestWithRoleScope( + 'viewer', + { + useCookieHeader: true, + withInternalHeaders: true, + } + ); + }); + + it('returns a non-empty body', async () => { + const resp = await callApiAs(supertestViewerWithCookieCredentials, integrationPackages[0]); + expect(resp.body).not.empty(); + }); + + it('returns correct number of dashboard', async () => { + const resp = await callApiAs(supertestViewerWithCookieCredentials, integrationPackages[1]); + expect(resp.body.dashboards.length).to.eql(2); + }); + + it('returns a list of dashboards in the correct format', async () => { + const expectedResult = { + dashboards: [ + { + id: 'nginx-023d2930-f1a5-11e7-a9ef-93c69af7b129', + title: '[Metrics Nginx] Overview', + }, + { + id: 'nginx-046212a0-a2a1-11e7-928f-5dbe6f6f5519', + title: '[Logs Nginx] Access and error logs', + }, + { + id: 'nginx-55a9e6e0-a29e-11e7-928f-5dbe6f6f5519', + title: '[Logs Nginx] Overview', + }, + ], + }; + const resp = await callApiAs(supertestViewerWithCookieCredentials, integrationPackages[0]); + expect(resp.body).to.eql(expectedResult); + }); + + it('returns an empty array for an integration without dashboards', async () => { + const expectedResult = { + dashboards: [], + }; + const resp = await callApiAs( + supertestViewerWithCookieCredentials, + customIntegrations[0].integrationName + ); + expect(resp.body).to.eql(expectedResult); + }); + + it('returns an empty array for an invalid integration', async () => { + const expectedResult = { + dashboards: [], + }; + const resp = await callApiAs(supertestViewerWithCookieCredentials, 'invalid'); + expect(resp.body).to.eql(expectedResult); + }); + }); + }); +} diff --git a/x-pack/test/dataset_quality_api_integration/common/config.ts b/x-pack/test/dataset_quality_api_integration/common/config.ts index f8e3fdc92016a..c27d6536c53da 100644 --- a/x-pack/test/dataset_quality_api_integration/common/config.ts +++ b/x-pack/test/dataset_quality_api_integration/common/config.ts @@ -28,7 +28,6 @@ import { InheritedFtrProviderContext, InheritedServices, } from './ftr_provider_context'; -import { PackageService } from './package_service'; import { RegistryProvider } from './registry'; export interface DatasetQualityFtrConfig { @@ -81,7 +80,6 @@ export interface CreateTest { context: InheritedFtrProviderContext ) => SyntheticsSynthtraceEsClient; datasetQualityApiClient: (context: InheritedFtrProviderContext) => DatasetQualityApiClient; - packageService: ({ getService }: FtrProviderContext) => ReturnType; }; junit: { reportName: string }; esTestCluster: any; @@ -132,7 +130,6 @@ export function createTestConfig( servicesRequiredForTestAnalysis: ['datasetQualityFtrConfig', 'registry'], services: { ...services, - packageService: PackageService, datasetQualityFtrConfig: () => config, registry: RegistryProvider, logSynthtraceEsClient: (context: InheritedFtrProviderContext) => diff --git a/x-pack/test/dataset_quality_api_integration/common/package_service.ts b/x-pack/test/dataset_quality_api_integration/common/package_service.ts deleted file mode 100644 index 0449fd9f921dd..0000000000000 --- a/x-pack/test/dataset_quality_api_integration/common/package_service.ts +++ /dev/null @@ -1,30 +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 { FtrProviderContext } from './ftr_provider_context'; - -interface IntegrationPackage { - name: string; - version: string; -} - -export function PackageService({ getService }: FtrProviderContext) { - const supertest = getService('supertest'); - - function uninstallPackage({ name, version }: IntegrationPackage) { - return supertest.delete(`/api/fleet/epm/packages/${name}/${version}`).set('kbn-xsrf', 'xxxx'); - } - - function installPackage({ name, version }: IntegrationPackage) { - return supertest - .post(`/api/fleet/epm/packages/${name}/${version}`) - .set('kbn-xsrf', 'xxxx') - .send({ force: true }); - } - - return { uninstallPackage, installPackage }; -} diff --git a/x-pack/test/dataset_quality_api_integration/tests/data_streams/es_utils.ts b/x-pack/test/dataset_quality_api_integration/tests/data_streams/es_utils.ts index d4b7eb2dc0824..607522089952f 100644 --- a/x-pack/test/dataset_quality_api_integration/tests/data_streams/es_utils.ts +++ b/x-pack/test/dataset_quality_api_integration/tests/data_streams/es_utils.ts @@ -6,7 +6,6 @@ */ import { Client } from '@elastic/elasticsearch'; -import { IndicesIndexSettings } from '@elastic/elasticsearch/lib/api/types'; export async function addIntegrationToLogIndexTemplate({ esClient, @@ -53,35 +52,3 @@ export async function cleanLogIndexTemplate({ esClient }: { esClient: Client }) }, }); } - -function getCurrentDateFormatted() { - const date = new Date(); - const year = date.getFullYear(); - const month = String(date.getMonth() + 1).padStart(2, '0'); - const day = String(date.getDate()).padStart(2, '0'); - - return `${year}.${month}.${day}`; -} - -export function createBackingIndexNameWithoutVersion({ - type, - dataset, - namespace = 'default', -}: { - type: string; - dataset: string; - namespace: string; -}) { - return `.ds-${type}-${dataset}-${namespace}-${getCurrentDateFormatted()}`; -} - -export async function setDataStreamSettings( - esClient: Client, - name: string, - settings: IndicesIndexSettings -) { - return esClient.indices.putSettings({ - index: name, - settings, - }); -} diff --git a/x-pack/test/dataset_quality_api_integration/tests/integrations/integration_dashboards.spec.ts b/x-pack/test/dataset_quality_api_integration/tests/integrations/integration_dashboards.spec.ts deleted file mode 100644 index 70567202c4341..0000000000000 --- a/x-pack/test/dataset_quality_api_integration/tests/integrations/integration_dashboards.spec.ts +++ /dev/null @@ -1,86 +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 expect from '@kbn/expect'; -import { DatasetQualityApiClientKey } from '../../common/config'; -import { FtrProviderContext } from '../../common/ftr_provider_context'; -import { installPackage, uninstallPackage } from './package_utils'; - -export default function ApiTest({ getService }: FtrProviderContext) { - const registry = getService('registry'); - const supertest = getService('supertest'); - const datasetQualityApiClient = getService('datasetQualityApiClient'); - - const integrationPackages = ['nginx', 'apm']; - - async function callApiAs(integration: string) { - const user = 'datasetQualityMonitorUser' as DatasetQualityApiClientKey; - return await datasetQualityApiClient[user]({ - endpoint: 'GET /internal/dataset_quality/integrations/{integration}/dashboards', - params: { - path: { - integration, - }, - }, - }); - } - - registry.when('Integration dashboards', { config: 'basic' }, () => { - describe('gets the installed integration dashboards', () => { - before(async () => { - await Promise.all(integrationPackages.map((pkg) => installPackage({ supertest, pkg }))); - }); - - it('returns a non-empty body', async () => { - const resp = await callApiAs(integrationPackages[0]); - expect(resp.body).not.empty(); - }); - - it('returns a list of dashboards in the correct format', async () => { - const expectedResult = { - dashboards: [ - { - id: 'nginx-023d2930-f1a5-11e7-a9ef-93c69af7b129', - title: '[Metrics Nginx] Overview', - }, - { - id: 'nginx-046212a0-a2a1-11e7-928f-5dbe6f6f5519', - title: '[Logs Nginx] Access and error logs', - }, - { - id: 'nginx-55a9e6e0-a29e-11e7-928f-5dbe6f6f5519', - title: '[Logs Nginx] Overview', - }, - ], - }; - const resp = await callApiAs(integrationPackages[0]); - expect(resp.body).to.eql(expectedResult); - }); - - it('returns an empty array for an integration without dashboards', async () => { - const expectedResult = { - dashboards: [], - }; - const resp = await callApiAs(integrationPackages[1]); - expect(resp.body).to.eql(expectedResult); - }); - - it('returns an empty array for an invalid integration', async () => { - const expectedResult = { - dashboards: [], - }; - const resp = await callApiAs('invalid'); - expect(resp.body).to.eql(expectedResult); - }); - - after( - async () => - await Promise.all(integrationPackages.map((pkg) => uninstallPackage({ supertest, pkg }))) - ); - }); - }); -} diff --git a/x-pack/test/dataset_quality_api_integration/tests/integrations/package_utils.ts b/x-pack/test/dataset_quality_api_integration/tests/integrations/package_utils.ts deleted file mode 100644 index 728d9e0b81e2e..0000000000000 --- a/x-pack/test/dataset_quality_api_integration/tests/integrations/package_utils.ts +++ /dev/null @@ -1,40 +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 { Agent as SuperTestAgent } from 'supertest'; - -export async function installPackage({ - supertest, - pkg, -}: { - supertest: SuperTestAgent; - pkg: string; -}) { - const { - body: { - item: { latestVersion: version }, - }, - } = await supertest - .get(`/api/fleet/epm/packages/${pkg}`) - .set('kbn-xsrf', 'xxxx') - .send({ force: true }); - - return supertest - .post(`/api/fleet/epm/packages/${pkg}/${version}`) - .set('kbn-xsrf', 'xxxx') - .send({ force: true }); -} - -export async function uninstallPackage({ - supertest, - pkg, -}: { - supertest: SuperTestAgent; - pkg: string; -}) { - return supertest.delete(`/api/fleet/epm/packages/${pkg}`).set('kbn-xsrf', 'xxxx'); -} diff --git a/x-pack/test/dataset_quality_api_integration/utils/data_stream.ts b/x-pack/test/dataset_quality_api_integration/utils/data_stream.ts deleted file mode 100644 index bdf5187db0725..0000000000000 --- a/x-pack/test/dataset_quality_api_integration/utils/data_stream.ts +++ /dev/null @@ -1,26 +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 { Client } from '@elastic/elasticsearch'; - -export async function rolloverDataStream(es: Client, name: string) { - return es.indices.rollover({ alias: name }); -} - -export async function getDataStreamSettingsOfEarliestIndex(es: Client, name: string) { - const matchingIndexesObj = await es.indices.getSettings({ index: name }); - - const matchingIndexes = Object.keys(matchingIndexesObj ?? {}); - matchingIndexes.sort((a, b) => { - return ( - Number(matchingIndexesObj[a].settings?.index?.creation_date) - - Number(matchingIndexesObj[b].settings?.index?.creation_date) - ); - }); - - return matchingIndexesObj[matchingIndexes[0]].settings; -} diff --git a/x-pack/test/dataset_quality_api_integration/utils/index.ts b/x-pack/test/dataset_quality_api_integration/utils/index.ts index 31fae4fe71199..ae42a0d359d0b 100644 --- a/x-pack/test/dataset_quality_api_integration/utils/index.ts +++ b/x-pack/test/dataset_quality_api_integration/utils/index.ts @@ -8,4 +8,3 @@ export { joinByKey } from './join_by_key'; export { maybe } from './maybe'; export { expectToReject } from './expect_to_reject'; -export * from './data_stream';