Skip to content

Commit

Permalink
remove proxy in favor of mock server
Browse files Browse the repository at this point in the history
  • Loading branch information
neptunian committed Oct 23, 2024
1 parent 406db08 commit dff67dc
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -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<StrictResponse<UsageMetricsAutoOpsResponseSchemaBody>> => {
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],
],
},
],
},
});
}
);

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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',
{
Expand All @@ -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');
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
{
Expand All @@ -36,7 +36,7 @@ export default function ({ getService }: FtrProviderContext) {
});

after(() => {
proxyServer.close();
mockApiServer.close();
});
describe(`POST ${API_PATH}`, () => {
const testDataStreamName = 'test-data-stream';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit dff67dc

Please sign in to comment.