Skip to content

Commit 826c72e

Browse files
committed
feat(MongoBinaryDownloadUrl): add ability to overwrite distro used
fixes #753
1 parent 615b681 commit 826c72e

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

docs/api/config-options.md

+16
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ Valid Options are `x64`, `arm64`, ~~`ia32`~~([will be removed in 9.0](../guides/
4949

5050
[See here for what versions are available for what architectures](https://www.mongodb.com/download-center/community/releases/archive)
5151

52+
### DISTRO
53+
54+
| Environment Variable | PackageJson |
55+
| :------------------: | :---------: |
56+
| `MONGOMS_DISTRO` | `distro` |
57+
58+
Option `DISTRO` is used to overwrite the Distribution used instead of the detected one.
59+
60+
Only works for when [`PLATFORM`](#platform) (automatic or manually set) is `linux`.
61+
62+
Format is `distro-release`, both distro and release need to be always defined.
63+
64+
Example: `ubuntu-18.04`
65+
66+
[See here for what versions are available for what distros](https://www.mongodb.com/download-center/community/releases/archive)
67+
5268
### VERSION
5369

5470
| Environment Variable | PackageJson |

packages/mongodb-memory-server-core/src/util/MongoBinaryDownloadUrl.ts

+38-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as semver from 'semver';
55
import { isNullOrUndefined } from './utils';
66
import { URL } from 'url';
77
import {
8+
GenericMMSError,
89
KnownVersionIncompatibilityError,
910
UnknownArchitectureError,
1011
UnknownPlatformError,
@@ -161,10 +162,14 @@ export class MongoBinaryDownloadUrl implements MongoBinaryDownloadUrlOpts {
161162

162163
// the highest version for "i686" seems to be 3.3
163164
if (this.arch !== 'i686') {
164-
if (!this.os) {
165+
if (!this.os && resolveConfig(ResolveConfigVariables.DISTRO)) {
165166
this.os = await getOS();
166167
}
167168

169+
if (resolveConfig(ResolveConfigVariables.DISTRO)) {
170+
this.overwriteDistro();
171+
}
172+
168173
osString = this.getLinuxOSVersionString(this.os as LinuxOS);
169174
}
170175

@@ -180,6 +185,38 @@ export class MongoBinaryDownloadUrl implements MongoBinaryDownloadUrlOpts {
180185
return name;
181186
}
182187

188+
/**
189+
* Parse and apply config option DISTRO
190+
*/
191+
protected overwriteDistro() {
192+
const env = resolveConfig(ResolveConfigVariables.DISTRO);
193+
194+
if (isNullOrUndefined(env)) {
195+
return;
196+
}
197+
198+
const split = env.split('-');
199+
200+
const distro = split[0];
201+
const release = split[1];
202+
203+
if (isNullOrUndefined(distro)) {
204+
throw new GenericMMSError('Expected DISTRO option to have a distro like "ubuntu-18.04"');
205+
}
206+
207+
if (isNullOrUndefined(release)) {
208+
throw new GenericMMSError(
209+
'Expected DISTRO option to have a release like "ubuntu-18.04" (delimited by "-")'
210+
);
211+
}
212+
213+
this.os = {
214+
os: 'linux',
215+
dist: distro,
216+
release: release,
217+
} as LinuxOS;
218+
}
219+
183220
/**
184221
* Get the version string (with distro)
185222
* @param os LinuxOS Object

packages/mongodb-memory-server-core/src/util/__tests__/MongoBinaryDownloadUrl.test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,41 @@ describe('MongoBinaryDownloadUrl', () => {
17661766
expect(du.getLegacyVersionString).toHaveBeenCalledTimes(1);
17671767
expect(ret).toBe('');
17681768
});
1769+
1770+
describe('wrap config options', () => {
1771+
const originalEnv = process.env;
1772+
1773+
beforeEach(() => {
1774+
process.env = originalEnv;
1775+
});
1776+
1777+
afterAll(() => {
1778+
process.env = originalEnv;
1779+
});
1780+
1781+
it('should apply config option DISTRO', async () => {
1782+
const du = new MongoBinaryDownloadUrl({
1783+
platform: 'linux',
1784+
arch: 'x64',
1785+
version: '5.0.0',
1786+
os: {
1787+
os: 'linux',
1788+
dist: 'unknown',
1789+
release: '0',
1790+
codename: 'unknown',
1791+
},
1792+
});
1793+
// @ts-expect-error "overwriteDistro" is protected
1794+
jest.spyOn(du, 'overwriteDistro');
1795+
1796+
process.env[envName(ResolveConfigVariables.DISTRO)] = 'ubuntu-18.04';
1797+
1798+
expect((await du.getArchiveNameLinux()).includes('ubuntu1804')).toBeTruthy();
1799+
1800+
// @ts-expect-error "overwriteDistro" is protected
1801+
expect(du.overwriteDistro).toBeCalledTimes(1);
1802+
});
1803+
});
17691804
});
17701805

17711806
describe('translateArch()', () => {

packages/mongodb-memory-server-core/src/util/resolveConfig.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export enum ResolveConfigVariables {
2525
USE_HTTP = 'USE_HTTP',
2626
SYSTEM_BINARY_VERSION_CHECK = 'SYSTEM_BINARY_VERSION_CHECK',
2727
USE_ARCHIVE_NAME_FOR_BINARY_NAME = 'USE_ARCHIVE_NAME_FOR_BINARY_NAME',
28+
DISTRO = 'DISTRO',
2829
}
2930

3031
/** The Prefix for Environmental values */

0 commit comments

Comments
 (0)