Skip to content

Commit

Permalink
fix(getos): check if given os is also not UNKNOWN and try different f…
Browse files Browse the repository at this point in the history
…ormats

re #735
  • Loading branch information
hasezoey committed Jan 23, 2023
1 parent a1195f1 commit 66541de
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,23 @@ HOME_URL="https://amazonlinux.com/"`;
});
});
});

describe('isValidOs', () => {
it('should return FALSE if undefined / null', () => {
expect(getos.isValidOs(undefined)).toStrictEqual(false);
expect(getos.isValidOs(null as any)).toStrictEqual(false);
});

it('should return FALSE if distro is UNKNOWN', () => {
expect(getos.isValidOs({ dist: getos.UNKNOWN, os: 'linux', release: '0' })).toStrictEqual(
false
);
});

it('should return TRUE if distro is not UNKNOWN', () => {
expect(getos.isValidOs({ dist: 'ubuntu', os: 'linux', release: '20.04' })).toStrictEqual(
true
);
});
});
});
33 changes: 25 additions & 8 deletions packages/mongodb-memory-server-core/src/util/getos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const OSRegex = {
id_like: /^id_like\s*=\s*"?([\w\s]*)"?$/im,
};

/** Helper Static so that a consistent UNKNOWN value is used */
export const UNKNOWN = 'unknown';

export interface OtherOS {
os: 'aix' | 'android' | 'darwin' | 'freebsd' | 'openbsd' | 'sunos' | 'win32' | 'cygwin' | string;
}
Expand Down Expand Up @@ -74,53 +77,67 @@ async function getLinuxInformation(): Promise<LinuxOS> {

const upstreamLSB = await tryReleaseFile('/etc/upstream-release/lsb-release', parseLSB);

if (!isNullOrUndefined(upstreamLSB)) {
if (isValidOs(upstreamLSB)) {
log('getLinuxInformation: Using UpstreamLSB');

return upstreamLSB;
}

const etcOsRelease = await tryReleaseFile('/etc/os-release', parseOS);

if (!isNullOrUndefined(etcOsRelease)) {
if (isValidOs(etcOsRelease)) {
log('getLinuxInformation: Using etcOsRelease');

return etcOsRelease;
}

const usrOsRelease = await tryReleaseFile('/usr/lib/os-release', parseOS);

if (!isNullOrUndefined(usrOsRelease)) {
if (isValidOs(usrOsRelease)) {
log('getLinuxInformation: Using usrOsRelease');

return usrOsRelease;
}

const etcLSBRelease = await tryReleaseFile('/etc/lsb-release', parseLSB);

if (!isNullOrUndefined(etcLSBRelease)) {
if (isValidOs(etcLSBRelease)) {
log('getLinuxInformation: Using etcLSBRelease');

return etcLSBRelease;
}

console.warn('Could not find any Release File, using fallback binary');
console.warn('Could not find any valid Release File, using fallback information');

// if none has worked, return unknown
return {
os: 'linux',
dist: 'unknown',
dist: UNKNOWN,
release: '',
};
}

/**
* Helper function to check if the input os is valid
* @param os The OS information to check
* @returns `true` if not undefined AND not UNKNOWN
*/
export function isValidOs(os: LinuxOS | undefined): os is LinuxOS {
// helper for debugging
if (os && os.dist === UNKNOWN) {
log('isValidOS: found defined os, but was unknown:', os);
}

return !isNullOrUndefined(os) && os.dist !== UNKNOWN;
}

/**
* Parse LSB-like output (either command or file)
*/
export function parseLSB(input: string): LinuxOS {
return {
os: 'linux',
dist: input.match(LSBRegex.name)?.[1].toLocaleLowerCase() ?? 'unknown',
dist: input.match(LSBRegex.name)?.[1].toLocaleLowerCase() ?? UNKNOWN,
codename: input.match(LSBRegex.codename)?.[1].toLocaleLowerCase(),
release: input.match(LSBRegex.release)?.[1].toLocaleLowerCase() ?? '',
};
Expand All @@ -132,7 +149,7 @@ export function parseLSB(input: string): LinuxOS {
export function parseOS(input: string): LinuxOS {
return {
os: 'linux',
dist: input.match(OSRegex.name)?.[1].toLocaleLowerCase() ?? 'unknown',
dist: input.match(OSRegex.name)?.[1].toLocaleLowerCase() ?? UNKNOWN,
codename: input.match(OSRegex.codename)?.[1].toLocaleLowerCase(),
release: input.match(OSRegex.release)?.[1].toLocaleLowerCase() ?? '',
id_like: input.match(OSRegex.id_like)?.[1].toLocaleLowerCase().split(' '),
Expand Down

0 comments on commit 66541de

Please sign in to comment.