Skip to content

Commit 40ccc32

Browse files
cleanups
1 parent f78fb17 commit 40ccc32

File tree

2 files changed

+44
-75
lines changed

2 files changed

+44
-75
lines changed

scripts/setup-encryption-tests.js

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,48 @@ const { rm, readdir, writeFile } = require('fs/promises');
55
const { tmpdir } = require('os');
66
const { join, resolve } = require('path');
77

8-
async function getCryptShared() {
9-
const crypt_shared_dir = await downloadMongoDb('./data/crypt', '8.0', {
10-
enterprise: true,
11-
crypt_shared: true
12-
});
138

14-
for (const direntry of await readdir(crypt_shared_dir)) {
15-
if (/crypt/.test(direntry)) {
16-
return join(crypt_shared_dir, direntry);
17-
}
18-
}
19-
}
9+
async function main() {
10+
const runnerDir = join(resolve(__dirname), '../data');
11+
const serverVersion = '8.0';
2012

21-
const runnerDir = join(resolve(__dirname), '../data');
22-
const id = 'my-cluster';
13+
const { uri, cryptShared } = await run();
14+
await writeFile('mo-expansion.yml',
15+
`CRYPT_SHARED_LIB_PATH: "${cryptShared}"
16+
MONGODB_URI: "${uri}"`
17+
);
2318

24-
async function run() {
25-
await rm(runnerDir, { recursive: true }).catch(e => {});
19+
async function downloadCryptShared() {
20+
const crypt_shared_dir = await downloadMongoDb(join(runnerDir, 'crypt'), serverVersion, {
21+
enterprise: true,
22+
crypt_shared: true
23+
});
2624

27-
const cryptShared = await getCryptShared();
28-
const binDir = await downloadMongoDb('./data', '8.0', {
29-
enterprise: true
30-
});
25+
for (const dirEntry of await readdir(crypt_shared_dir)) {
26+
if (/crypt/.test(dirEntry)) {
27+
return join(crypt_shared_dir, dirEntry);
28+
}
29+
}
30+
}
3131

32-
const tmpDir = tmpdir();
32+
async function run() {
33+
await rm(runnerDir, { recursive: true }).catch(() => {});
3334

34-
await start({ id, binDir, topology: 'replset', runnerDir, tmpDir });
35+
const cryptShared = await downloadCryptShared();
36+
const binDir = await downloadMongoDb(runnerDir, serverVersion, {
37+
enterprise: true
38+
});
3539

36-
for await (const instance of instances({ runnerDir })) {
37-
if (instance.id === id) return {
38-
cryptShared, uri: instance.connectionString
39-
};
40-
}
40+
await start({ id: 'encryption-test-cluster', binDir, topology: 'replset', runnerDir, tmpDir: tmpdir() });
4141

42-
throw new Error('unable to find new instance.');
43-
}
44-
45-
async function stop() {
46-
await require('mongodb-runner').stop({
47-
runnerDir,
48-
id
49-
});
50-
}
42+
for await (const instance of instances({ runnerDir })) {
43+
if (instance.id === 'encryption-test-cluster') return {
44+
cryptShared, uri: instance.connectionString
45+
};
46+
}
5147

52-
async function main() {
53-
const { uri, cryptShared } = await run();
54-
await writeFile('mo-expansion.yml',
55-
`
56-
CRYPT_SHARED_LIB_PATH: "${cryptShared}"
57-
MONGODB_URI: "${uri}"
58-
`
59-
);
48+
throw new Error('Unable to location newly configured instance of mongod - should never happen.');
49+
}
6050
}
6151

62-
if (require.main === module) {
63-
main();
64-
}
65-
else {
66-
module.exports = { start: run, stop };
67-
}
52+
main();

test/encryption/encryption.test.js

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ const { ObjectId, Double, Int32, Decimal128 } = require('bson');
88
const fs = require('fs');
99
const mongoose = require('../../lib');
1010
const { Map } = require('../../lib/types');
11-
12-
const { stop, start } = require('../../scripts/setup-encryption-tests');
11+
const { join } = require('path');
1312

1413
const LOCAL_KEY = Buffer.from('Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk', 'base64');
1514

@@ -33,34 +32,19 @@ function isEncryptedValue(object, property) {
3332
}
3433

3534
describe('encryption integration tests', () => {
36-
const cachedUri = process.env.MONGOOSE_TEST_URI;
37-
const cachedLib = process.env.CRYPT_SHARED_LIB_PATH;
38-
3935
before(async function() {
40-
this.timeout(0);
41-
const cwd = process.cwd();
42-
if (!exists(cwd + '/mo-expansion.yml')) {
43-
const { uri, cryptShared } = await start();
44-
process.env.CRYPT_SHARED_LIB_PATH = cryptShared;
45-
process.env.MONGOOSE_TEST_URI = uri;
46-
} else {
47-
const file = fs.readFileSync(cwd + '/mo-expansion.yml', { encoding: 'utf-8' }).trim().split('\n');
48-
49-
const regex = /^(?<key>.*): "(?<value>.*)"$/;
50-
const variables = Object.fromEntries(file.map((line) => regex.exec(line.trim()).groups).map(({ key, value }) => [key, value]));
51-
52-
process.env.CRYPT_SHARED_LIB_PATH = variables.CRYPT_SHARED_LIB_PATH;
53-
process.env.MONGOOSE_TEST_URI = variables.MONGODB_URI;
36+
const expansionFile = join(__dirname, '../..', 'mo-expansion.yml');
37+
if (!exists(expansionFile)) {
38+
throw new Error('must setup a cluster using `npm run setup-test-encryption`.');
5439
}
55-
});
5640

57-
after(async function() {
58-
if (!exists(process.cwd() + '/mo-expansion.yml')) {
59-
await stop();
60-
}
41+
const lines = fs.readFileSync(expansionFile, { encoding: 'utf-8' }).trim().split('\n');
42+
43+
const regex = /^(?<key>.*): "(?<value>.*)"$/;
44+
const variables = Object.fromEntries(lines.map((line) => regex.exec(line.trim()).groups).map(({ key, value }) => [key, value]));
6145

62-
process.env.CRYPT_SHARED_LIB_PATH = cachedLib;
63-
process.env.MONGOOSE_TEST_URI = cachedUri;
46+
process.env.CRYPT_SHARED_LIB_PATH = variables.CRYPT_SHARED_LIB_PATH;
47+
process.env.MONGOOSE_TEST_URI = variables.MONGODB_URI;
6448
});
6549

6650
describe('meta: environmental variables are correctly set up', () => {

0 commit comments

Comments
 (0)