Skip to content

Commit

Permalink
Added tests to systemLogs onboarding
Browse files Browse the repository at this point in the history
  • Loading branch information
yngrdyn committed Aug 3, 2023
1 parent 76d1277 commit 8ba419c
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { getFallbackESUrl } from '../../lib/get_fallback_urls';
import { getObservabilityOnboardingFlow } from '../../lib/state';
import { createObservabilityOnboardingServerRoute } from '../create_observability_onboarding_server_route';
import { generateCustomLogsYml } from './custom_logs/generate_custom_logs_yml';
import { generateSystemLogsYml } from './system_logs/generate_system_logs_yaml';
import { generateSystemLogsYml } from './system_logs/generate_system_logs_yml';

const generateConfig = createObservabilityOnboardingServerRoute({
endpoint: 'GET /internal/observability_onboarding/elastic_agent/config',
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 { v4 as uuidv4 } from 'uuid';
import { generateSystemLogsYml } from './generate_system_logs_yml';

const baseMockConfig = {
namespace: 'default',
apiKey: 'elastic:changeme',
esHost: ['http://localhost:9200'],
uuid: uuidv4(),
};

describe('generateSystemLogsYml', () => {
it('should return system logs oriented yml configuration', () => {
const result = generateSystemLogsYml(baseMockConfig);
expect(result).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,6 @@ export default function ApiTest({ getService }: FtrProviderContext) {
const logFilepath = '/my-logs.log';
const serviceName = 'my-service';

before(async () => {
const req = await observabilityOnboardingApiClient.logMonitoringUser({
endpoint: 'POST /internal/observability_onboarding/logs/flow',
params: {
body: {
type: 'logFiles',
name: 'name',
state: {
datasetName,
namespace,
logFilePaths: [logFilepath],
serviceName,
},
},
},
});

onboardingId = req.body.onboardingId;
});

describe("when onboardingId doesn't exists", () => {
it('should return input properties empty', async () => {
const req = await callApi({
Expand All @@ -67,17 +47,72 @@ export default function ApiTest({ getService }: FtrProviderContext) {
});

describe('when onboardingId exists', () => {
it('should return input properties configured', async () => {
const req = await callApi({
onboardingId,
describe('and onboarding type is logFiles', () => {
before(async () => {
const req = await observabilityOnboardingApiClient.logMonitoringUser({
endpoint: 'POST /internal/observability_onboarding/logs/flow',
params: {
body: {
type: 'logFiles',
name: 'name',
state: {
datasetName,
namespace,
logFilePaths: [logFilepath],
serviceName,
},
},
},
});

onboardingId = req.body.onboardingId;
});

expect(req.status).to.be(200);
it('should return input properties configured', async () => {
const req = await callApi({
onboardingId,
});

const ymlConfig = load(req.text);
expect(ymlConfig.inputs[0].data_stream.namespace).to.be(namespace);
expect(ymlConfig.inputs[0].streams[0].data_stream.dataset).to.be(datasetName);
expect(ymlConfig.inputs[0].streams[0].paths).to.be.eql([logFilepath]);
expect(req.status).to.be(200);

const ymlConfig = load(req.text);
expect(ymlConfig.inputs[0].data_stream.namespace).to.be(namespace);
expect(ymlConfig.inputs[0].streams[0].data_stream.dataset).to.be(datasetName);
expect(ymlConfig.inputs[0].streams[0].paths).to.be.eql([logFilepath]);
expect(ymlConfig.inputs[0].streams[0].processors[0].add_fields.fields.name).to.be.eql(
serviceName
);
});
});

describe('and onboarding type is systemLogs', () => {
before(async () => {
const req = await observabilityOnboardingApiClient.logMonitoringUser({
endpoint: 'POST /internal/observability_onboarding/logs/flow',
params: {
body: {
type: 'systemLogs',
name: 'name',
},
},
});

onboardingId = req.body.onboardingId;
});

it('should return input properties configured', async () => {
const req = await callApi({
onboardingId,
});

expect(req.status).to.be(200);

const ymlConfig = load(req.text);
expect(ymlConfig.inputs[0].data_stream.namespace).to.be('default');
expect(ymlConfig.inputs[0].streams.length).to.be(2);
expect(ymlConfig.inputs[0].streams[0].data_stream.dataset).to.be('system.auth');
expect(ymlConfig.inputs[0].streams[1].data_stream.dataset).to.be('system.syslog');
});
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function createLogDoc({
}: {
time: number;
logFilepath: string;
serviceName: string;
serviceName?: string;
namespace: string;
datasetName: string;
message: string;
Expand All @@ -30,9 +30,13 @@ export function createLogDoc({
path: logFilepath,
},
},
service: {
name: serviceName,
},
...(serviceName
? {
service: {
name: serviceName,
},
}
: {}),
data_stream: {
namespace,
type: 'logs',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,40 +132,107 @@ export default function ApiTest({ getService }: FtrProviderContext) {
});

describe('when logs have been ingested', () => {
before(async () => {
await es.indices.createDataStream({
name: `logs-${datasetName}-${namespace}`,
describe('and onboarding type is logFiles', () => {
before(async () => {
await es.indices.createDataStream({
name: `logs-${datasetName}-${namespace}`,
});

const doc = createLogDoc({
time: new Date('06/28/2023').getTime(),
logFilepath: '/my-service.log',
serviceName: 'my-service',
namespace,
datasetName,
message: 'This is a log message',
});

await es.bulk({
body: [{ create: { _index: `logs-${datasetName}-${namespace}` } }, doc],
refresh: 'wait_for',
});
});

const doc = createLogDoc({
time: new Date('06/28/2023').getTime(),
logFilepath: '/my-service.log',
serviceName: 'my-service',
namespace,
datasetName,
message: 'This is a log message',
it('should return log-ingest as complete', async () => {
const request = await callApi({
onboardingId,
});

expect(request.status).to.be(200);

const logsIngestProgress = request.body.progress['logs-ingest'];
expect(logsIngestProgress).to.have.property('status', 'complete');
});

await es.bulk({
body: [{ create: { _index: `logs-${datasetName}-${namespace}` } }, doc],
refresh: 'wait_for',
after(async () => {
await es.indices.deleteDataStream({
name: `logs-${datasetName}-${namespace}`,
});
});
});

it('should return log-ingest as complete', async () => {
const request = await callApi({
onboardingId,
describe('and onboarding type is systemLogs', () => {
let systemLogsOnboardingId: string;

before(async () => {
const req = await observabilityOnboardingApiClient.logMonitoringUser({
endpoint: 'POST /internal/observability_onboarding/logs/flow',
params: {
body: {
type: 'systemLogs',
name: 'name',
},
},
});

systemLogsOnboardingId = req.body.onboardingId;

await observabilityOnboardingApiClient.logMonitoringUser({
endpoint: 'POST /internal/observability_onboarding/flow/{id}/step/{name}',
params: {
path: {
id: systemLogsOnboardingId,
name: 'ea-status',
},
body: {
status: 'complete',
},
},
});

await es.indices.createDataStream({
name: `logs-system.syslog-${namespace}`,
});

const doc = createLogDoc({
time: new Date('06/28/2023').getTime(),
logFilepath: '/var/log/system.log',
namespace,
datasetName: 'system.syslog',
message: 'This is a system log message',
});

await es.bulk({
body: [{ create: { _index: `logs-system.syslog-${namespace}` } }, doc],
refresh: 'wait_for',
});
});

expect(request.status).to.be(200);
it('should return log-ingest as complete', async () => {
const request = await callApi({
onboardingId: systemLogsOnboardingId,
});

const logsIngestProgress = request.body.progress['logs-ingest'];
expect(logsIngestProgress).to.have.property('status', 'complete');
});
expect(request.status).to.be(200);

const logsIngestProgress = request.body.progress['logs-ingest'];
expect(logsIngestProgress).to.have.property('status', 'complete');
});

after(async () => {
await es.indices.deleteDataStream({
name: `logs-${datasetName}-${namespace}`,
after(async () => {
await es.indices.deleteDataStream({
name: `logs-system.syslog-${namespace}`,
});
});
});
});
Expand Down
Loading

0 comments on commit 8ba419c

Please sign in to comment.