mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
[os collector] trim newlines from getos
call result (#136705)
* [os collector] trim newlines from `getos` call result * just remove what we're supposed to remove * make sure the /g/ flag of native regexp works as intended.
This commit is contained in:
parent
c476b453db
commit
7b8bad518d
2 changed files with 23 additions and 3 deletions
|
@ -6,7 +6,8 @@
|
|||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
jest.mock('getos', () => (cb: Function) => cb(null, { dist: 'distrib', release: 'release' }));
|
||||
let mockGetOsResult: object = {};
|
||||
jest.mock('getos', () => (cb: Function) => cb(null, mockGetOsResult));
|
||||
|
||||
import { loggerMock } from '@kbn/logging-mocks';
|
||||
import os from 'os';
|
||||
|
@ -17,6 +18,7 @@ describe('OsMetricsCollector', () => {
|
|||
let collector: OsMetricsCollector;
|
||||
|
||||
beforeEach(() => {
|
||||
mockGetOsResult = { dist: 'distrib', release: 'release' };
|
||||
collector = new OsMetricsCollector({ logger: loggerMock.create() });
|
||||
cgroupCollectorMock.collect.mockReset();
|
||||
cgroupCollectorMock.reset.mockReset();
|
||||
|
@ -50,6 +52,19 @@ describe('OsMetricsCollector', () => {
|
|||
expect(metrics.distroRelease).toBe('distrib-release');
|
||||
});
|
||||
|
||||
it('remove whitespaces and carriage return from getos call', async () => {
|
||||
mockGetOsResult = { dist: 'dist\n', release: '\nrel\n\n' };
|
||||
|
||||
const platform = 'linux';
|
||||
|
||||
jest.spyOn(os, 'platform').mockImplementation(() => platform);
|
||||
|
||||
const metrics = await collector.collect();
|
||||
|
||||
expect(metrics.distro).toBe('dist');
|
||||
expect(metrics.distroRelease).toBe('dist-rel');
|
||||
});
|
||||
|
||||
it('collects memory info from the os package', async () => {
|
||||
const totalMemory = 1457886;
|
||||
const freeMemory = 456786;
|
||||
|
|
|
@ -64,9 +64,12 @@ export class OsMetricsCollector implements MetricsCollector<OpsOsMetrics> {
|
|||
if (platform === 'linux') {
|
||||
try {
|
||||
const distro = (await getos()) as LinuxOs;
|
||||
// getos values can sometimes contain newline characters
|
||||
const dist = removeNewlines(distro.dist);
|
||||
const release = removeNewlines(distro.release);
|
||||
return {
|
||||
distro: distro.dist,
|
||||
distroRelease: `${distro.dist}-${distro.release}`,
|
||||
distro: dist,
|
||||
distroRelease: `${dist}-${release}`,
|
||||
};
|
||||
} catch (e) {
|
||||
// ignore errors
|
||||
|
@ -76,3 +79,5 @@ export class OsMetricsCollector implements MetricsCollector<OpsOsMetrics> {
|
|||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
const removeNewlines = (str: string) => str.replace(/[\n]/g, '');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue