Fix issue rendering hosts with unexpected CPU brand string (#1606)

- Hosts with unexpected CPU brand string render properly
- Other unexpected host CPU information renders properly
- Add tests for host CPU parsing

Fixes #1604
This commit is contained in:
Zachary Wasserman 2017-11-13 20:25:41 -08:00 committed by GitHub
parent a0909e6bee
commit c769bd45a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 7 deletions

View file

@ -1,3 +1,5 @@
* Fixed bug rendering hosts when clock speed cannot be parsed.
## Kolide Fleet 1.0.5 (Oct 17, 2017)
* Renamed the binary from kolide to fleet

View file

@ -4,14 +4,21 @@ export const parseEntityFunc = (host) => {
const { network_interfaces: networkInterfaces } = host;
const networkInterface = networkInterfaces && find(networkInterfaces, { id: host.primary_ip_id });
let clockSpeed = null;
let clockSpeedFlt = null;
let hostCpuOutput = null;
if (host && host.cpu_brand) {
clockSpeed = host.cpu_brand.split('@ ')[1] || host.cpu_brand.split('@')[1];
clockSpeedFlt = parseFloat(clockSpeed.split('GHz')[0].trim());
hostCpuOutput = `${host.cpu_physical_cores} x ${Math.floor(clockSpeedFlt * 10) / 10} GHz`;
if (host) {
let clockSpeedOutput = null;
try {
const clockSpeed = host.cpu_brand.split('@ ')[1] || host.cpu_brand.split('@')[1];
const clockSpeedFlt = parseFloat(clockSpeed.split('GHz')[0].trim());
clockSpeedOutput = Math.floor(clockSpeedFlt * 10) / 10;
} catch (e) {
// Some CPU brand strings do not fit this format and we can't parse the
// clock speed. Leave it set to 'Unknown'.
console.log(`Unable to parse clock speed from cpu_brand: ${host.cpu_brand}`);
}
if (host.cpu_physical_cores || clockSpeedOutput) {
hostCpuOutput = `${host.cpu_physical_cores || 'Unknown'} x ${clockSpeedOutput || 'Unknown'} GHz`;
}
}
const additionalAttrs = {

View file

@ -0,0 +1,42 @@
import expect from 'expect';
import { parseEntityFunc } from './helpers';
describe('reduxConfig - hosts helpers', () => {
describe('parseEntityFunc', () => {
it('parses an expected CPU string', () => {
const host = {
cpu_brand: 'Intel(R) Xeon(R) CPU E5-2420 0 @ 1.90GHz',
cpu_physical_cores: 2,
};
expect(parseEntityFunc(host).host_cpu).toEqual('2 x 1.9 GHz');
});
it('parses a host missing clock speed', () => {
const host = {
cpu_brand: 'Intel(R) Xeon(R) CPU E5-242',
cpu_physical_cores: 2,
};
expect(parseEntityFunc(host).host_cpu).toEqual('2 x Unknown GHz');
});
it('parses a host missing CPU brand', () => {
const host = {
cpu_physical_cores: 2,
};
expect(parseEntityFunc(host).host_cpu).toEqual('2 x Unknown GHz');
});
it('parses a host missing CPU cores', () => {
const host = {
cpu_brand: 'Intel(R) Xeon(R) CPU E5-2420 0 @ 1.90GHz',
};
expect(parseEntityFunc(host).host_cpu).toEqual('Unknown x 1.9 GHz');
});
it('parses a host missing CPU info entirely', () => {
const host = {};
expect(parseEntityFunc(host).host_cpu).toEqual(undefined);
});
});
});