Skip to content

Commit

Permalink
feat(MongoInstance::stdoutHandler): change "instanceError" events to …
Browse files Browse the repository at this point in the history
…use "StdoutInstanceError"
  • Loading branch information
hasezoey committed Nov 1, 2021
1 parent 382e7c6 commit 3daa5ea
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 22 deletions.
30 changes: 21 additions & 9 deletions packages/mongodb-memory-server-core/src/util/MongoInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { lt } from 'semver';
import { EventEmitter } from 'events';
import { MongoClient, MongoClientOptions, MongoNetworkError } from 'mongodb';
import { KeyFileMissingError, StartBinaryFailedError } from './errors';
import { KeyFileMissingError, StartBinaryFailedError, StdoutInstanceError } from './errors';

// ignore the nodejs warning for coverage
/* istanbul ignore next */
Expand Down Expand Up @@ -544,11 +544,14 @@ export class MongoInstance extends EventEmitter implements ManagerBase {
if (/address already in use/i.test(line)) {
this.emit(
MongoInstanceEvents.instanceError,
`Port "${this.instanceOpts.port}" already in use`
new StdoutInstanceError(`Port "${this.instanceOpts.port}" already in use`)
);
}
if (/mongod instance already running/i.test(line)) {
this.emit(MongoInstanceEvents.instanceError, 'Mongod already running');
this.emit(
MongoInstanceEvents.instanceError,
new StdoutInstanceError('Mongod already running')
);
}
if (/permission denied/i.test(line)) {
this.emit(MongoInstanceEvents.instanceError, 'Mongod permission denied');
Expand All @@ -560,15 +563,19 @@ export class MongoInstance extends EventEmitter implements ManagerBase {
if (/CURL_OPENSSL_3['\s]+not found/i.test(line)) {
this.emit(
MongoInstanceEvents.instanceError,
'libcurl3 is not available on your system. Mongod requires it and cannot be started without it.\n' +
'You should manually install libcurl3 or try to use an newer version of MongoDB\n'
new StdoutInstanceError(
'libcurl3 is not available on your system. Mongod requires it and cannot be started without it.\n' +
'You should manually install libcurl3 or try to use an newer version of MongoDB'
)
);
}
if (/CURL_OPENSSL_4['\s]+not found/i.test(line)) {
this.emit(
MongoInstanceEvents.instanceError,
'libcurl4 is not available on your system. Mongod requires it and cannot be started without it.\n' +
'You need to manually install libcurl4\n'
new StdoutInstanceError(
'libcurl4 is not available on your system. Mongod requires it and cannot be started without it.\n' +
'You need to manually install libcurl4'
)
);
}
if (/lib[\w-.]+(?=: cannot open shared object)/i.test(line)) {
Expand All @@ -577,11 +584,16 @@ export class MongoInstance extends EventEmitter implements ManagerBase {
'unknown';
this.emit(
MongoInstanceEvents.instanceError,
`Instance Failed to start because an library file is missing: "${lib}"`
new StdoutInstanceError(
`Instance Failed to start because an library file is missing: "${lib}"`
)
);
}
if (/\*\*\*aborting after/i.test(line)) {
this.emit(MongoInstanceEvents.instanceError, 'Mongod internal error');
this.emit(
MongoInstanceEvents.instanceError,
new StdoutInstanceError('Mongod internal error')
);
}
// this case needs to be infront of "transition to primary complete", otherwise it might reset "isInstancePrimary" to "false"
if (/transition to \w+ from \w+/i.test(line)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as dbUtil from '../utils';
import MongodbInstance, { MongoInstanceEvents } from '../MongoInstance';
import resolveConfig, { ResolveConfigVariables } from '../resolveConfig';
import getPort from 'get-port';
import { StartBinaryFailedError } from '../errors';
import { StartBinaryFailedError, StdoutInstanceError } from '../errors';
import { assertIsError } from '../../__tests__/testUtils/test_utils';

jest.setTimeout(100000); // 10s
Expand Down Expand Up @@ -175,14 +175,20 @@ describe('MongodbInstance', () => {
binary: { version },
});

await expect(
MongodbInstance.create({
try {
await MongodbInstance.create({
instance: { port: gotPort, dbPath: tmpDir.name },
binary: { version },
})
).rejects.toEqual(`Port "${gotPort}" already in use`);
});

await mongod.stop();
fail('Expected to Fail');
} catch (err) {
expect(err).toBeInstanceOf(StdoutInstanceError);
assertIsError(err); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
expect(err.message).toEqual(`Port "${gotPort}" already in use`);
} finally {
await mongod.stop();
}
});

it('should wait until childprocess and killerprocess are killed', async () => {
Expand Down Expand Up @@ -268,7 +274,7 @@ describe('MongodbInstance', () => {

describe('test events', () => {
let mongod: MongodbInstance;
let events: Map<MongoInstanceEvents | string, string>;
let events: Map<MongoInstanceEvents | string, unknown>;
beforeEach(() => {
mongod = new MongodbInstance({ instance: { port: 1001, dbPath: 'hello' } });
events = new Map();
Expand Down Expand Up @@ -322,7 +328,11 @@ describe('MongodbInstance', () => {

expect(events.size).toEqual(2);
expect(events.get(MongoInstanceEvents.instanceSTDOUT)).toEqual(line);
expect(events.get(MongoInstanceEvents.instanceError)).toEqual('Port "1001" already in use');

const event = events.get(MongoInstanceEvents.instanceError);
expect(event).toBeInstanceOf(StdoutInstanceError);
assertIsError(event); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
expect(event.message).toEqual('Port "1001" already in use');
});

it('should emit "instanceError" when curl-open-ssl-3 is not found', () => {
Expand All @@ -334,9 +344,13 @@ describe('MongodbInstance', () => {

expect(events.size).toEqual(2);
expect(events.get(MongoInstanceEvents.instanceSTDOUT)).toEqual(line);
expect(events.get(MongoInstanceEvents.instanceError)).toEqual(

const event = events.get(MongoInstanceEvents.instanceError);
expect(event).toBeInstanceOf(StdoutInstanceError);
assertIsError(event); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
expect(event.message).toEqual(
'libcurl3 is not available on your system. Mongod requires it and cannot be started without it.\n' +
'You should manually install libcurl3 or try to use an newer version of MongoDB\n'
'You should manually install libcurl3 or try to use an newer version of MongoDB'
);
});

Expand All @@ -349,9 +363,13 @@ describe('MongodbInstance', () => {

expect(events.size).toEqual(2);
expect(events.get(MongoInstanceEvents.instanceSTDOUT)).toEqual(line);
expect(events.get(MongoInstanceEvents.instanceError)).toEqual(

const event = events.get(MongoInstanceEvents.instanceError);
expect(event).toBeInstanceOf(StdoutInstanceError);
assertIsError(event); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
expect(event.message).toEqual(
'libcurl4 is not available on your system. Mongod requires it and cannot be started without it.\n' +
'You need to manually install libcurl4\n'
'You need to manually install libcurl4'
);
});

Expand Down Expand Up @@ -391,7 +409,11 @@ describe('MongodbInstance', () => {

expect(events.size).toEqual(2);
expect(events.get(MongoInstanceEvents.instanceSTDOUT)).toEqual(line);
expect(events.get(MongoInstanceEvents.instanceError)).toEqual(

const event = events.get(MongoInstanceEvents.instanceError);
expect(event).toBeInstanceOf(StdoutInstanceError);
assertIsError(event); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
expect(event.message).toEqual(
'Instance Failed to start because an library file is missing: "libcrypto.so.10"'
);
});
Expand Down

0 comments on commit 3daa5ea

Please sign in to comment.