diff --git a/docs/api/config-options.md b/docs/api/config-options.md index d92258908..7fd7307ca 100644 --- a/docs/api/config-options.md +++ b/docs/api/config-options.md @@ -49,6 +49,22 @@ Valid Options are `x64`, `arm64` [See here for what versions are available for what architectures](https://www.mongodb.com/download-center/community/releases/archive) +### DISTRO + +| Environment Variable | PackageJson | +| :------------------: | :---------: | +| `MONGOMS_DISTRO` | `distro` | + +Option `DISTRO` is used to overwrite the Distribution used instead of the detected one. + +Only works for when [`PLATFORM`](#platform) (automatic or manually set) is `linux`. + +Format is `distro-release`, both distro and release need to be always defined. + +Example: `ubuntu-18.04` + +[See here for what versions are available for what distros](https://www.mongodb.com/download-center/community/releases/archive) + ### VERSION | Environment Variable | PackageJson | diff --git a/packages/mongodb-memory-server-core/src/util/MongoBinaryDownloadUrl.ts b/packages/mongodb-memory-server-core/src/util/MongoBinaryDownloadUrl.ts index f93d2bdd3..0a2e1a35c 100644 --- a/packages/mongodb-memory-server-core/src/util/MongoBinaryDownloadUrl.ts +++ b/packages/mongodb-memory-server-core/src/util/MongoBinaryDownloadUrl.ts @@ -5,6 +5,7 @@ import * as semver from 'semver'; import { isNullOrUndefined } from './utils'; import { URL } from 'url'; import { + GenericMMSError, KnownVersionIncompatibilityError, UnknownArchitectureError, UnknownLinuxDistro, @@ -160,10 +161,14 @@ export class MongoBinaryDownloadUrl implements MongoBinaryDownloadUrlOpts { * (from: https://www.mongodb.org/dl/linux) */ async getArchiveNameLinux(): Promise { - if (!this.os) { + if (!this.os && resolveConfig(ResolveConfigVariables.DISTRO)) { this.os = await getOS(); } + if (resolveConfig(ResolveConfigVariables.DISTRO)) { + this.overwriteDistro(); + } + const osString: string = this.getLinuxOSVersionString(this.os as LinuxOS); // this is below, to allow overwriting the arch (like arm64 to aarch64) @@ -179,6 +184,38 @@ export class MongoBinaryDownloadUrl implements MongoBinaryDownloadUrlOpts { return name; } + /** + * Parse and apply config option DISTRO + */ + protected overwriteDistro() { + const env = resolveConfig(ResolveConfigVariables.DISTRO); + + if (isNullOrUndefined(env)) { + return; + } + + const split = env.split('-'); + + const distro = split[0]; + const release = split[1]; + + if (isNullOrUndefined(distro)) { + throw new GenericMMSError('Expected DISTRO option to have a distro like "ubuntu-18.04"'); + } + + if (isNullOrUndefined(release)) { + throw new GenericMMSError( + 'Expected DISTRO option to have a release like "ubuntu-18.04" (delimited by "-")' + ); + } + + this.os = { + os: 'linux', + dist: distro, + release: release, + } as LinuxOS; + } + /** * Get the version string (with distro) * @param os LinuxOS Object diff --git a/packages/mongodb-memory-server-core/src/util/__tests__/MongoBinaryDownloadUrl.test.ts b/packages/mongodb-memory-server-core/src/util/__tests__/MongoBinaryDownloadUrl.test.ts index d9520df50..474cee64f 100644 --- a/packages/mongodb-memory-server-core/src/util/__tests__/MongoBinaryDownloadUrl.test.ts +++ b/packages/mongodb-memory-server-core/src/util/__tests__/MongoBinaryDownloadUrl.test.ts @@ -1735,6 +1735,41 @@ describe('MongoBinaryDownloadUrl', () => { expect(err.message).toStrictEqual('Unknown/unsupported linux "unknown" id_like\'s: []'); } }); + + describe('wrap config options', () => { + const originalEnv = process.env; + + beforeEach(() => { + process.env = originalEnv; + }); + + afterAll(() => { + process.env = originalEnv; + }); + + it('should apply config option DISTRO', async () => { + const du = new MongoBinaryDownloadUrl({ + platform: 'linux', + arch: 'x64', + version: '5.0.0', + os: { + os: 'linux', + dist: 'unknown', + release: '0', + codename: 'unknown', + }, + }); + // @ts-expect-error "overwriteDistro" is protected + jest.spyOn(du, 'overwriteDistro'); + + process.env[envName(ResolveConfigVariables.DISTRO)] = 'ubuntu-18.04'; + + expect((await du.getArchiveNameLinux()).includes('ubuntu1804')).toBeTruthy(); + + // @ts-expect-error "overwriteDistro" is protected + expect(du.overwriteDistro).toBeCalledTimes(1); + }); + }); }); describe('translateArch()', () => { diff --git a/packages/mongodb-memory-server-core/src/util/resolveConfig.ts b/packages/mongodb-memory-server-core/src/util/resolveConfig.ts index 0054d13bb..b23c4faf1 100644 --- a/packages/mongodb-memory-server-core/src/util/resolveConfig.ts +++ b/packages/mongodb-memory-server-core/src/util/resolveConfig.ts @@ -25,6 +25,7 @@ export enum ResolveConfigVariables { USE_HTTP = 'USE_HTTP', SYSTEM_BINARY_VERSION_CHECK = 'SYSTEM_BINARY_VERSION_CHECK', USE_ARCHIVE_NAME_FOR_BINARY_NAME = 'USE_ARCHIVE_NAME_FOR_BINARY_NAME', + DISTRO = 'DISTRO', } /** The Prefix for Environmental values */