mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
[Status] Organize metrics stuff into metrics_collector, tests into __test__ directories, remove mock-fs for the cgroup test (#17788)
* [Status] Organize tests into __test__ directories * custom mock for fs in cgroups test * work around unhandled rejection errors * simplify fserror construct * put fs readFile mockImpl in beforeAll for pretty * undo hacking the code to suppress unhandled promise rejections * test files back to sibling of the module
This commit is contained in:
parent
ef8657067a
commit
706f0f2916
6 changed files with 55 additions and 19 deletions
|
@ -1,5 +1,5 @@
|
|||
import ServerStatus from './server_status';
|
||||
import { Metrics } from './metrics';
|
||||
import { Metrics } from './metrics_collector/metrics';
|
||||
import { registerStatusPage, registerStatusApi } from './routes';
|
||||
|
||||
export function statusMixin(kbnServer, server, config) {
|
||||
|
|
|
@ -40,3 +40,32 @@ export function cGroups(hierarchy) {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
class FSError extends Error {
|
||||
constructor(fileName, code) {
|
||||
super('Stub File Sytem Stub Error: ' + fileName);
|
||||
this.code = code;
|
||||
this.stack = null;
|
||||
}
|
||||
}
|
||||
|
||||
let _mockFiles = Object.create({});
|
||||
|
||||
export const setMockFiles = mockFiles => {
|
||||
_mockFiles = Object.create({});
|
||||
if (mockFiles) {
|
||||
const files = Object.keys(mockFiles);
|
||||
for(const file of files) {
|
||||
_mockFiles[file] = mockFiles[file];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const readFileMock = (fileName, callback) => {
|
||||
if (_mockFiles.hasOwnProperty(fileName)) {
|
||||
callback(null, _mockFiles[fileName]);
|
||||
} else {
|
||||
const err = new FSError(fileName, 'ENOENT');
|
||||
callback(err, null);
|
||||
}
|
||||
};
|
|
@ -135,9 +135,9 @@ export function getAllStats(options = {}) {
|
|||
function rejectUnlessFileNotFound(err) {
|
||||
if (err.code === 'ENOENT') {
|
||||
resolve(null);
|
||||
} else {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
}
|
|
@ -1,17 +1,25 @@
|
|||
import mockFs from 'mock-fs';
|
||||
import { cGroups as cGroupsFsStub } from './_fs_stubs';
|
||||
jest.mock('fs', () => ({
|
||||
readFile: jest.fn()
|
||||
}));
|
||||
|
||||
import fs from 'fs';
|
||||
import { cGroups as cGroupsFsStub, setMockFiles, readFileMock } from './__mocks__/_fs_stubs';
|
||||
import { getAllStats, readControlGroups, readCPUStat } from './cgroup';
|
||||
|
||||
describe('Control Group', function () {
|
||||
const fsStub = cGroupsFsStub();
|
||||
|
||||
beforeAll(() => {
|
||||
fs.readFile.mockImplementation(readFileMock);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mockFs.restore();
|
||||
setMockFiles();
|
||||
});
|
||||
|
||||
describe('readControlGroups', () => {
|
||||
it('parses the file', async () => {
|
||||
mockFs({ '/proc/self/cgroup': fsStub.cGroupContents });
|
||||
setMockFiles({ '/proc/self/cgroup': fsStub.cGroupContents });
|
||||
const cGroup = await readControlGroups();
|
||||
|
||||
expect(cGroup).toEqual({
|
||||
|
@ -34,7 +42,7 @@ describe('Control Group', function () {
|
|||
|
||||
describe('readCPUStat', () => {
|
||||
it('parses the file', async () => {
|
||||
mockFs({ '/sys/fs/cgroup/cpu/fakeGroup/cpu.stat': fsStub.cpuStatContents });
|
||||
setMockFiles({ '/sys/fs/cgroup/cpu/fakeGroup/cpu.stat': fsStub.cpuStatContents });
|
||||
const cpuStat = await readCPUStat('fakeGroup');
|
||||
|
||||
expect(cpuStat).toEqual({
|
||||
|
@ -45,7 +53,7 @@ describe('Control Group', function () {
|
|||
});
|
||||
|
||||
it('returns default stats for missing file', async () => {
|
||||
mockFs();
|
||||
setMockFiles();
|
||||
const cpuStat = await readCPUStat('fakeGroup');
|
||||
|
||||
expect(cpuStat).toEqual({
|
||||
|
@ -58,7 +66,7 @@ describe('Control Group', function () {
|
|||
|
||||
describe('getAllStats', () => {
|
||||
it('can override the cpu group path', async () => {
|
||||
mockFs({
|
||||
setMockFiles({
|
||||
'/proc/self/cgroup': fsStub.cGroupContents,
|
||||
[`${fsStub.cpuAcctDir}/cpuacct.usage`]: '357753491408',
|
||||
'/sys/fs/cgroup/cpu/docker/cpu.cfs_period_us': '100000',
|
||||
|
@ -87,7 +95,7 @@ describe('Control Group', function () {
|
|||
});
|
||||
|
||||
it('handles an undefined control group', async () => {
|
||||
mockFs({
|
||||
setMockFiles({
|
||||
'/proc/self/cgroup': '',
|
||||
[`${fsStub.cpuAcctDir}/cpuacct.usage`]: '357753491408',
|
||||
[`${fsStub.cpuDir}/cpu.stat`]: fsStub.cpuStatContents,
|
||||
|
@ -101,7 +109,7 @@ describe('Control Group', function () {
|
|||
});
|
||||
|
||||
it('can override the cpuacct group path', async () => {
|
||||
mockFs({
|
||||
setMockFiles({
|
||||
'/proc/self/cgroup': fsStub.cGroupContents,
|
||||
'/sys/fs/cgroup/cpuacct/docker/cpuacct.usage': '357753491408',
|
||||
[`${fsStub.cpuDir}/cpu.cfs_period_us`]: '100000',
|
||||
|
@ -130,7 +138,7 @@ describe('Control Group', function () {
|
|||
});
|
||||
|
||||
it('extracts control group stats', async () => {
|
||||
mockFs(fsStub.files);
|
||||
setMockFiles(fsStub.files);
|
||||
const stats = await getAllStats();
|
||||
|
||||
expect(stats).toEqual({
|
||||
|
@ -152,13 +160,13 @@ describe('Control Group', function () {
|
|||
});
|
||||
|
||||
it('returns null when all files are missing', async () => {
|
||||
mockFs({});
|
||||
setMockFiles();
|
||||
const stats = await getAllStats();
|
||||
expect(stats).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null if CPU accounting files are missing', async () => {
|
||||
mockFs({
|
||||
setMockFiles({
|
||||
'/proc/self/cgroup': fsStub.cGroupContents,
|
||||
[`${fsStub.cpuDir}/cpu.stat`]: fsStub.cpuStatContents
|
||||
});
|
||||
|
@ -168,7 +176,7 @@ describe('Control Group', function () {
|
|||
});
|
||||
|
||||
it('returns -1 stat values if cpuStat file is missing', async () => {
|
||||
mockFs({
|
||||
setMockFiles({
|
||||
'/proc/self/cgroup': fsStub.cGroupContents,
|
||||
[`${fsStub.cpuAcctDir}/cpuacct.usage`]: '357753491408',
|
||||
[`${fsStub.cpuDir}/cpu.cfs_period_us`]: '100000',
|
|
@ -1,5 +1,5 @@
|
|||
import { get, isObject, merge } from 'lodash';
|
||||
import { keysToSnakeCaseShallow } from '../../utils/case_conversion';
|
||||
import { keysToSnakeCaseShallow } from '../../../utils/case_conversion';
|
||||
import { getAllStats as cGroupStats } from './cgroup';
|
||||
|
||||
export class Metrics {
|
|
@ -1,8 +1,7 @@
|
|||
import _ from 'lodash';
|
||||
import sinon from 'sinon';
|
||||
import mockFs from 'mock-fs';
|
||||
import { cGroups as cGroupsFsStub } from './_fs_stubs';
|
||||
|
||||
import { cGroups as cGroupsFsStub } from './__mocks__/_fs_stubs';
|
||||
import { Metrics } from './metrics';
|
||||
|
||||
describe('Metrics', function () {
|
Loading…
Add table
Add a link
Reference in a new issue