From dff67dce7e54a4bf31b7f5e21db742bdf21203bd Mon Sep 17 00:00:00 2001 From: neptunian Date: Wed, 23 Oct 2024 13:52:03 -0400 Subject: [PATCH] remove proxy in favor of mock server --- .../test_suites/common/data_usage/mock_api.ts | 58 ++++++++++++++ .../common/data_usage/proxy_server.ts | 77 ------------------- .../common/data_usage/tests/data_streams.ts | 9 ++- .../common/data_usage/tests/metrics.ts | 14 ++-- .../test_suites/observability/config.ts | 1 + .../test_suites/search/config.ts | 1 + .../test_suites/security/config.ts | 1 + 7 files changed, 73 insertions(+), 88 deletions(-) create mode 100644 x-pack/test_serverless/api_integration/test_suites/common/data_usage/mock_api.ts delete mode 100644 x-pack/test_serverless/api_integration/test_suites/common/data_usage/proxy_server.ts diff --git a/x-pack/test_serverless/api_integration/test_suites/common/data_usage/mock_api.ts b/x-pack/test_serverless/api_integration/test_suites/common/data_usage/mock_api.ts new file mode 100644 index 0000000000000..9fdbd8b6d6169 --- /dev/null +++ b/x-pack/test_serverless/api_integration/test_suites/common/data_usage/mock_api.ts @@ -0,0 +1,58 @@ +/* + * 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 { createServer } from '@mswjs/http-middleware'; +import { UsageMetricsAutoOpsResponseSchemaBody } from '@kbn/data-usage-plugin/common/rest_types'; + +import { http, HttpResponse, StrictResponse } from 'msw'; + +export const setupMockServer = () => { + const server = createServer(autoOpsHandler); + return server; +}; + +const autoOpsHandler = http.post( + '/', + async ({ request }): Promise> => { + return HttpResponse.json({ + metrics: { + ingest_rate: [ + { + name: 'metrics-system.cpu-default', + data: [ + [1726858530000, 13756849], + [1726862130000, 14657904], + ], + }, + { + name: 'logs-nginx.access-default', + data: [ + [1726858530000, 12894623], + [1726862130000, 14436905], + ], + }, + ], + storage_retained: [ + { + name: 'metrics-system.cpu-default', + data: [ + [1726858530000, 12576413], + [1726862130000, 13956423], + ], + }, + { + name: 'logs-nginx.access-default', + data: [ + [1726858530000, 12894623], + [1726862130000, 14436905], + ], + }, + ], + }, + }); + } +); diff --git a/x-pack/test_serverless/api_integration/test_suites/common/data_usage/proxy_server.ts b/x-pack/test_serverless/api_integration/test_suites/common/data_usage/proxy_server.ts deleted file mode 100644 index 2f2729a7a2abe..0000000000000 --- a/x-pack/test_serverless/api_integration/test_suites/common/data_usage/proxy_server.ts +++ /dev/null @@ -1,77 +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 { UsageMetricsAutoOpsResponseSchemaBody } from '@kbn/data-usage-plugin/common/rest_types'; -import { ToolingLog } from '@kbn/tooling-log'; -import http from 'http'; - -export function createProxyServer(port: number, log: ToolingLog): http.Server { - const server = http.createServer((req, res) => { - log.debug(`Received request: ${req.method} ${req.url}`); - - let body = ''; - - req.on('data', (chunk) => { - body += chunk.toString(); - }); - - req.on('end', () => { - log.debug(`Request body: ${body}`); - - // interception logic here - if (req.method === 'POST' && req.url === '/') { - // TODO: decide on how to generate data - const response: UsageMetricsAutoOpsResponseSchemaBody = { - metrics: { - ingest_rate: [ - { - name: 'metrics-system.cpu-default', - data: [ - [1726858530000, 13756849], - [1726862130000, 14657904], - ], - }, - { - name: 'logs-nginx.access-default', - data: [ - [1726858530000, 12894623], - [1726862130000, 14436905], - ], - }, - ], - storage_retained: [ - { - name: 'metrics-system.cpu-default', - data: [ - [1726858530000, 12576413], - [1726862130000, 13956423], - ], - }, - { - name: 'logs-nginx.access-default', - data: [ - [1726858530000, 12894623], - [1726862130000, 14436905], - ], - }, - ], - }, - }; - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify(response)); - } else { - res.writeHead(404); - res.end('Not Found'); - } - }); - }); - - server.listen(port, () => { - log.debug(`Proxy server is listening on port ${port}`); - }); - - return server; -} diff --git a/x-pack/test_serverless/api_integration/test_suites/common/data_usage/tests/data_streams.ts b/x-pack/test_serverless/api_integration/test_suites/common/data_usage/tests/data_streams.ts index c387324f37546..a30127d327d6c 100644 --- a/x-pack/test_serverless/api_integration/test_suites/common/data_usage/tests/data_streams.ts +++ b/x-pack/test_serverless/api_integration/test_suites/common/data_usage/tests/data_streams.ts @@ -17,9 +17,10 @@ export default function ({ getService }: FtrProviderContext) { let supertestAdminWithCookieCredentials: SupertestWithRoleScope; const testDataStreamName = 'test-data-stream'; describe(`GET ${API_PATH}`, function () { - // due to the plugin depending on yml config (xpack.dataUsage.enabled), we cannot test in MKI until it is by default + // due to the plugin depending on yml config (xpack.dataUsage.enabled), we cannot test in MKI until it is on by default this.tags(['skipMKI']); before(async () => { + await svlDatastreamsHelpers.createDataStream(testDataStreamName); supertestAdminWithCookieCredentials = await roleScopedSupertest.getSupertestWithRoleScope( 'admin', { @@ -28,10 +29,11 @@ export default function ({ getService }: FtrProviderContext) { } ); }); + after(async () => { + await svlDatastreamsHelpers.deleteDataStream(testDataStreamName); + }); it('returns created data streams', async () => { - await svlDatastreamsHelpers.createDataStream(testDataStreamName); - const res = await supertestAdminWithCookieCredentials .get(API_PATH) .set('elastic-api-version', '1'); @@ -40,7 +42,6 @@ export default function ({ getService }: FtrProviderContext) { expect(foundStream?.name).to.be(testDataStreamName); expect(foundStream?.storageSizeBytes).to.be(0); expect(res.statusCode).to.be(200); - await svlDatastreamsHelpers.deleteDataStream(testDataStreamName); }); it('returns system indices', async () => { const res = await supertestAdminWithCookieCredentials diff --git a/x-pack/test_serverless/api_integration/test_suites/common/data_usage/tests/metrics.ts b/x-pack/test_serverless/api_integration/test_suites/common/data_usage/tests/metrics.ts index 6c9d8407eb4c5..2dff998b4533a 100644 --- a/x-pack/test_serverless/api_integration/test_suites/common/data_usage/tests/metrics.ts +++ b/x-pack/test_serverless/api_integration/test_suites/common/data_usage/tests/metrics.ts @@ -11,21 +11,21 @@ import http from 'http'; import { SupertestWithRoleScope } from '@kbn/test-suites-xpack/api_integration/deployment_agnostic/services/role_scoped_supertest'; import { UsageMetricsRequestBody } from '@kbn/data-usage-plugin/common/rest_types'; import { FtrProviderContext } from '../../../../ftr_provider_context'; -import { createProxyServer } from '../proxy_server'; +import { setupMockServer } from '../mock_api'; const API_PATH = '/internal/api/data_usage/metrics'; export default function ({ getService }: FtrProviderContext) { const svlDatastreamsHelpers = getService('svlDatastreamsHelpers'); const roleScopedSupertest = getService('roleScopedSupertest'); - const log = getService('log'); let supertestAdminWithCookieCredentials: SupertestWithRoleScope; - describe.skip('Metrics', function () { - // proxy does not work with MKI + const mockAutoopsApiService = setupMockServer(); + describe('Metrics', function () { + let mockApiServer: http.Server; + // due to the plugin depending on yml config (xpack.dataUsage.enabled), we cannot test in MKI until it is on by default this.tags(['skipMKI']); - let proxyServer: http.Server; before(async () => { - proxyServer = createProxyServer(9000, log); + mockApiServer = mockAutoopsApiService.listen(9000); supertestAdminWithCookieCredentials = await roleScopedSupertest.getSupertestWithRoleScope( 'admin', { @@ -36,7 +36,7 @@ export default function ({ getService }: FtrProviderContext) { }); after(() => { - proxyServer.close(); + mockApiServer.close(); }); describe(`POST ${API_PATH}`, () => { const testDataStreamName = 'test-data-stream'; diff --git a/x-pack/test_serverless/api_integration/test_suites/observability/config.ts b/x-pack/test_serverless/api_integration/test_suites/observability/config.ts index 3ba65ed53d690..ac662ab146ee4 100644 --- a/x-pack/test_serverless/api_integration/test_suites/observability/config.ts +++ b/x-pack/test_serverless/api_integration/test_suites/observability/config.ts @@ -27,6 +27,7 @@ export default createTestConfig({ // useful for testing (also enabled in MKI QA) '--coreApp.allowDynamicConfigOverrides=true', '--xpack.dataUsage.enabled=true', + // dataUsage.autoops* config is set in kibana controller '--xpack.dataUsage.autoops.enabled=true', '--xpack.dataUsage.autoops.api.url=http://localhost:9000', `--xpack.dataUsage.autoops.api.tls.certificate=${path.resolve( diff --git a/x-pack/test_serverless/api_integration/test_suites/search/config.ts b/x-pack/test_serverless/api_integration/test_suites/search/config.ts index b1554353add1d..0c776e9865d30 100644 --- a/x-pack/test_serverless/api_integration/test_suites/search/config.ts +++ b/x-pack/test_serverless/api_integration/test_suites/search/config.ts @@ -23,6 +23,7 @@ export default createTestConfig({ // useful for testing (also enabled in MKI QA) '--coreApp.allowDynamicConfigOverrides=true', '--xpack.dataUsage.enabled=true', + // dataUsage.autoops* config is set in kibana controller '--xpack.dataUsage.autoops.api.url=http://localhost:9000', `--xpack.dataUsage.autoops.api.tls.certificate=${path.resolve( __dirname, diff --git a/x-pack/test_serverless/api_integration/test_suites/security/config.ts b/x-pack/test_serverless/api_integration/test_suites/security/config.ts index 3f10ef1a70ab5..026cdbc149b01 100644 --- a/x-pack/test_serverless/api_integration/test_suites/security/config.ts +++ b/x-pack/test_serverless/api_integration/test_suites/security/config.ts @@ -27,6 +27,7 @@ export default createTestConfig({ `--xpack.securitySolutionServerless.cloudSecurityUsageReportingTaskInterval=5s`, `--xpack.securitySolutionServerless.usageApi.url=http://localhost:8081`, '--xpack.dataUsage.enabled=true', + // dataUsage.autoops* config is set in kibana controller '--xpack.dataUsage.autoops.api.url=http://localhost:9000', `--xpack.dataUsage.autoops.api.tls.certificate=${path.resolve( __dirname,