import Module from 'module'; import { expect } from 'chai'; import sinon, { type SinonStubbedMember } from 'sinon'; import { promises as fs } from 'fs'; import { getDeviceIdForMongosh } from './device-id'; import { EventEmitter } from 'events'; import type { MongoshBus } from '@mongosh/types'; import os from 'os'; function moduleNotFound(id: string) { return Object.assign(new Error(`Cannot find module '${id}'`), { code: 'MODULE_NOT_FOUND', requireStack: [], }); } describe('getDeviceIdForMongosh', function () { let sandbox: sinon.SinonSandbox; let abortController: AbortController; let mongoshBus: MongoshBus; beforeEach(function () { sandbox = sinon.createSandbox(); abortController = new AbortController(); mongoshBus = new EventEmitter(); }); afterEach(function () { sandbox.restore(); }); describe('on linux', function () { beforeEach(function () { if (os.platform() !== 'linux') { this.skip(); } }); describe('if native-id is not available', function () { let readFileStub: SinonStubbedMember; let emitStub: SinonStubbedMember; beforeEach(function () { sandbox .stub(Module.prototype, 'require') .withArgs('@mongodb-js/native-machine-id') .throws(moduleNotFound('@mongodb-js/native-machine-id')); readFileStub = sandbox.stub(fs, 'readFile'); emitStub = sandbox.stub(mongoshBus, 'emit'); }); function whenFileContains(filePath: string, contents: string): void { readFileStub.withArgs(filePath).returns(Promise.resolve(contents)); } function whenFileDoesNotExist(filePath: string): void { readFileStub .withArgs(filePath) .returns(Promise.reject('File does not exist.')); } function abortWhenFileIsChecked(filePath: string): void { readFileStub.withArgs(filePath).callsFake(() => { abortController.abort(); throw 'Aborted'; }); } it('reads from /var/lib/dbus/machine-id', async function () { whenFileContains( '/var/lib/dbus/machine-id', ' OK-from-dbus-machine-id ' ); whenFileDoesNotExist('/etc/machine-id'); const deviceId = await getDeviceIdForMongosh({ bus: mongoshBus, signal: abortController.signal, }); expect(deviceId).to.be.equal( '9e25f2bc10340d768096a105ad15b74938d4f29bdeacd3cc515f0d455503dde0' ); }); it('reads from /etc/machine-id if the dbus info is not available', async function () { whenFileContains('/etc/machine-id', ' OK-from-etc-machine-id '); whenFileDoesNotExist('/var/lib/dbus/machine-id'); const deviceId = await getDeviceIdForMongosh({ bus: mongoshBus, signal: abortController.signal, }); expect(deviceId).to.be.equal( '6b9baf05496306fd46e8483cee3451aeb5fb0008f9938a6f1ba875496e8b08ad' ); }); it('skips an empty id file', async function () { whenFileContains('/etc/machine-id', ' OK-from-etc-machine-id '); whenFileContains('/var/lib/dbus/machine-id', ''); const deviceId = await getDeviceIdForMongosh({ bus: mongoshBus, signal: abortController.signal, }); expect(deviceId).to.be.equal( '6b9baf05496306fd46e8483cee3451aeb5fb0008f9938a6f1ba875496e8b08ad' ); }); it('aborts when signalled before reading files', async function () { whenFileContains('/var/lib/dbus/machine-id', ''); whenFileContains('/etc/machine-id', ''); abortController.abort(); const deviceId = await getDeviceIdForMongosh({ bus: mongoshBus, signal: abortController.signal, }); expect(deviceId).to.be.equal('unknown'); // we do not track timeouts expect(emitStub).to.not.have.been.called; }); it('aborts in between files if it was signalled', async function () { abortWhenFileIsChecked('/var/lib/dbus/machine-id'); whenFileContains('/etc/machine-id', ' OK-from-etc-machine-id '); const deviceId = await getDeviceIdForMongosh({ bus: mongoshBus, signal: abortController.signal, }); expect(deviceId).to.be.equal('unknown'); // we do not track timeouts expect(emitStub).to.not.have.been.called; }); it('fails when no file is available', async function () { whenFileDoesNotExist('/etc/machine-id'); whenFileDoesNotExist('/var/lib/dbus/machine-id'); const deviceId = await getDeviceIdForMongosh({ bus: mongoshBus, signal: abortController.signal, }); expect(deviceId).to.be.equal('unknown'); expect(emitStub).to.have.been.calledWith( 'mongosh:error', sinon.match( (err) => err instanceof Error && err.message === 'No machine-id file available' ), 'telemetry' ); }); }); }); });