-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(cli-repl): add snapshot package list tests
Add the ability to introspect the compiled mongosh executables for what is part of the snapshot and what is not, and then use that information in e2e tests to verify that it is accurate.
- Loading branch information
Showing
7 changed files
with
258 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import v8 from 'v8'; | ||
|
||
// Allow us to inspect the set of loaded modules at a few interesting points in time, e.g. | ||
// startup, snapshot, and after entering VM/REPL execution mode | ||
declare const __webpack_module_cache__: Record<string, unknown> | undefined; | ||
declare const __webpack_modules__: Record<string, unknown> | undefined; | ||
declare const __webpack_reverse_module_lookup__: | ||
| (() => Record<string | number, string>) | ||
| undefined; | ||
|
||
// Return all ids of modules loaded at the current time | ||
function enumerateLoadedModules(): (string | number)[] | null { | ||
if (typeof __webpack_module_cache__ !== 'undefined') { | ||
return Object.keys(__webpack_module_cache__); | ||
} | ||
return null; | ||
} | ||
// Return all ids of modules that can be loaded/are known to webpack | ||
function enumerateAllModules(): (string | number)[] | null { | ||
if (typeof __webpack_modules__ !== 'undefined') { | ||
return Object.keys(__webpack_modules__); | ||
} | ||
return null; | ||
} | ||
// Perform a reverse lookup to determine the "natural" name for a given | ||
// module id (i.e. original filename, if available). | ||
// Calling this the first time is potentially expensive. | ||
function lookupNaturalModuleName(id: string | number): string | null { | ||
let lookupTable = null; | ||
if (typeof __webpack_reverse_module_lookup__ !== 'undefined') | ||
lookupTable = __webpack_reverse_module_lookup__(); | ||
return lookupTable?.[id] ?? null; | ||
} | ||
Object.defineProperty(process, '__mongosh_webpack_stats', { | ||
value: { | ||
enumerateLoadedModules, | ||
enumerateAllModules, | ||
lookupNaturalModuleName, | ||
}, | ||
}); | ||
if ((v8 as any)?.startupSnapshot?.isBuildingSnapshot?.()) { | ||
(v8 as any).startupSnapshot.addSerializeCallback(() => { | ||
const atSnapshotTime = enumerateLoadedModules(); | ||
(process as any).__mongosh_webpack_stats.enumerateSnapshotModules = () => | ||
atSnapshotTime; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { | ||
skipIfApiStrict, | ||
startSharedTestServer, | ||
} from '../../../testing/integration-testing-hooks'; | ||
import { TestShell } from './test-shell'; | ||
import { expect } from 'chai'; | ||
|
||
const setDifference = <T>(a: T[], b: T[]) => a.filter((e) => !b.includes(e)); | ||
const expectIsSubset = <T>(a: T[], b: T[]) => | ||
expect(setDifference(a, b)).to.have.lengthOf(0); | ||
const commonPrefix = (a: string, b: string): string => | ||
a.startsWith(b) | ||
? b | ||
: b.startsWith(a) | ||
? a | ||
: b && commonPrefix(a, b.slice(0, -1)); | ||
|
||
describe('e2e startup banners', function () { | ||
skipIfApiStrict(); | ||
afterEach(TestShell.cleanup); | ||
|
||
const testServer = startSharedTestServer(); | ||
|
||
context('modules included in snapshots', function () { | ||
it.only('includes the right modules at the right point in time', async function () { | ||
if (!process.env.MONGOSH_TEST_EXECUTABLE_PATH) return this.skip(); | ||
|
||
const connectionString = await testServer.connectionString(); | ||
const helperScript = ` | ||
const S = process.__mongosh_webpack_stats; | ||
const L = (list) => list.map(S.lookupNaturalModuleName).filter(name => name && !name.endsWith('.json')); | ||
`; | ||
const commonArgs = ['--quiet', '--json=relaxed', '--eval', helperScript]; | ||
const argLists = [ | ||
[...commonArgs, '--nodb', '--eval', 'L(S.enumerateAllModules())'], | ||
[...commonArgs, '--nodb', '--eval', 'L(S.enumerateSnapshotModules())'], | ||
[...commonArgs, '--nodb', '--eval', 'L(S.enumerateLoadedModules())'], | ||
[ | ||
...commonArgs, | ||
connectionString, | ||
'--eval', | ||
'L(S.enumerateLoadedModules())', | ||
], | ||
[ | ||
...commonArgs, | ||
connectionString, | ||
'--jsContext=repl', | ||
'--eval', | ||
'L(S.enumerateLoadedModules())', | ||
], | ||
]; | ||
const [ | ||
all, | ||
atSnapshotTime, | ||
atNodbEvalTime, | ||
atDbEvalTime, | ||
atReplEvalTime, | ||
] = ( | ||
await Promise.all( | ||
argLists.map((args) => | ||
TestShell.runAndGetOutputWithoutErrors({ args }) | ||
) | ||
) | ||
).map((output) => JSON.parse(output).sort() as string[]); | ||
|
||
// Ensure that: atSnapshotTime ⊆ atNodbEvalTime ⊆ atDbEvalTime ⊆ atReplEvalTime ⊆ all | ||
expectIsSubset(atSnapshotTime, atNodbEvalTime); | ||
expectIsSubset(atNodbEvalTime, atDbEvalTime); | ||
expectIsSubset(atDbEvalTime, atReplEvalTime); | ||
expectIsSubset(atReplEvalTime, all); | ||
|
||
const prefix = all.reduce(commonPrefix); | ||
const stripPrefix = (s: string) => | ||
s.startsWith(prefix) ? s.replace(prefix, '') : s; | ||
|
||
const categorized = [ | ||
...atSnapshotTime.map(stripPrefix).map((m) => [m, 'snapshot'] as const), | ||
...setDifference(atNodbEvalTime, atSnapshotTime) | ||
.map(stripPrefix) | ||
.map((m) => [m, 'nodb-eval'] as const), | ||
...setDifference(atDbEvalTime, atNodbEvalTime) | ||
.map(stripPrefix) | ||
.map((m) => [m, 'db-eval'] as const), | ||
...setDifference(atReplEvalTime, atDbEvalTime) | ||
.map(stripPrefix) | ||
.map((m) => [m, 'repl-eval'] as const), | ||
...setDifference(all, atReplEvalTime) | ||
.map(stripPrefix) | ||
.map((m) => [m, 'not-loaded'] as const), | ||
]; | ||
|
||
// This is very helpful for inspecting snapshotted contents manually: | ||
console.table(categorized.map(([m, c]) => [m.replace(prefix, ''), c])); | ||
const verifyAllInCategoryMatch = ( | ||
category: (typeof categorized)[number][1], | ||
re: RegExp | ||
) => { | ||
for (const [module, cat] of categorized) { | ||
if (cat === category) { | ||
expect(module).to.match( | ||
re, | ||
`Found unexpected '${module}' in category '${cat}'` | ||
); | ||
} | ||
} | ||
}; | ||
const verifyAllThatMatchAreInCategory = ( | ||
category: (typeof categorized)[number][1], | ||
re: RegExp | ||
) => { | ||
for (const [module, cat] of categorized) { | ||
if (re.test(module)) { | ||
expect(cat).to.equal( | ||
category, | ||
`Expected '${module}' to be in category '${category}', actual category is '${cat}'` | ||
); | ||
} | ||
} | ||
}; | ||
|
||
// The core test: Verify that in the categories beyond 'not loaded at all' | ||
// and 'part of the snapshot', only a very specific set of modules is present, | ||
// and that some modules are only in specific categories. | ||
verifyAllInCategoryMatch('repl-eval', /^node_modules\/pretty-repl\//); | ||
verifyAllInCategoryMatch( | ||
'db-eval', | ||
/^node_modules\/(kerberos|os-dns-native|resolve-mongodb-srv)\// | ||
); | ||
verifyAllInCategoryMatch( | ||
'nodb-eval', | ||
/^node_modules\/(kerberos|mongodb-client-encryption)\// | ||
); | ||
verifyAllThatMatchAreInCategory( | ||
'not-loaded', | ||
/^node_modules\/(express|openid-client|qs|send|jose|execa|body-parser|@babel\/highlight|@babel\/code-frame)\// | ||
); | ||
verifyAllThatMatchAreInCategory( | ||
'snapshot', | ||
/^node_modules\/(@babel\/types|@babel\/traverse|@mongodb-js\/devtools-connect|mongodb)\/|^packages\// | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const zlib = require('zlib'); | ||
const { promisify } = require('util'); | ||
|
||
class WebpackEnableReverseModuleLookupPlugin { | ||
outputFilename; | ||
constructor({ outputFilename }) { this.outputFilename = outputFilename; } | ||
|
||
apply(compiler) { | ||
compiler.hooks.emit.tapPromise('EnableReverseModuleLookupPlugin', async(compilation) => { | ||
const map = Object.create(null); | ||
for (const module of compilation.modules) { | ||
const id = compilation.chunkGraph.getModuleId(module); | ||
if (id && module.resource) { | ||
map[id] = module.resource; | ||
} | ||
} | ||
const data = (await promisify(zlib.brotliCompress)(JSON.stringify(map))).toString('base64'); | ||
await fs.promises.mkdir(path.dirname(this.outputFilename), { recursive: true }); | ||
await fs.promises.writeFile(this.outputFilename, `function __webpack_reverse_module_lookup__() { | ||
return __webpack_reverse_module_lookup__.data ??= JSON.parse( | ||
require("zlib").brotliDecompressSync(Buffer.from(${JSON.stringify(data)}, 'base64'))); | ||
}`); | ||
}) | ||
} | ||
} | ||
|
||
module.exports = { WebpackEnableReverseModuleLookupPlugin }; |