Merge pull request #2132 from balena-os/lower-case-cpu-id

Always lower case the cpu id to avoid bouncing between casing when reporting
This commit is contained in:
Page- 2023-02-15 14:13:12 +00:00 committed by GitHub
commit 1b210d4fa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 3 deletions

View File

@ -73,6 +73,7 @@ export async function getCpuTemp(): Promise<number> {
}
export async function getSystemId(): Promise<string | undefined> {
let systemId: undefined | string;
try {
// This will work on arm devices
const buffer = await Promise.any([
@ -81,14 +82,20 @@ export async function getSystemId(): Promise<string | undefined> {
fs.readFile('/sys/devices/soc0/serial_number'),
]);
// Remove the null/newline bytes at the end
return buffer.toString('utf-8').replace(/\0/g, '').trim();
systemId = buffer.toString('utf-8').replace(/\0/g, '').trim();
} catch {
// Otherwise use dmidecode
const [baseBoardInfo] = (
await dmidecode('baseboard').catch(() => [] as DmiDecodeInfo[])
).filter((entry) => entry.type === 'Base Board Information');
return baseBoardInfo?.values?.['Serial Number'] || undefined;
systemId = baseBoardInfo?.values?.['Serial Number'] || undefined;
}
if (systemId == null) {
return;
}
// Always lower case so as not to jump between casing when jumping between eg /proc/device-tree/serial-number and /sys/devices/soc0/serial_number which can return different casing
// Lower case was chosen because that is what `/proc/device-tree/serial-number` usually returns and seems to be the more consistently available version
return systemId.toLowerCase();
}
export async function getSystemModel(): Promise<string | undefined> {

View File

@ -129,7 +129,7 @@ describe('System information', () => {
stdout: mockCPU.dmidecode,
});
const cpuId = await sysInfo.getSystemId();
expect(cpuId).to.equal('GEBN94600PWW');
expect(cpuId).to.equal('gebn94600pww');
});
it('gets system model', async () => {