-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathconfiguration.ts
More file actions
249 lines (214 loc) · 8.4 KB
/
configuration.ts
File metadata and controls
249 lines (214 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* eslint-disable simple-import-sort/imports */
// eslint-disable-next-line @typescript-eslint/no-require-imports
require('source-map-support').install({
hookRequire: true
});
import { MongoClient } from '../../../mongodb';
import { AlpineTestConfiguration, AstrolabeTestConfiguration, TestConfiguration } from '../config';
import { getEnvironmentalOptions } from '../../utils';
import * as mock from '../../mongodb-mock/index';
import { inspect } from 'util';
import { ApiVersionFilter } from '../filters/api_version_filter';
import { AuthFilter } from '../filters/auth_filter';
import { ClientSideEncryptionFilter } from '../filters/client_encryption_filter';
import { GenericPredicateFilter } from '../filters/generic_predicate_filter';
import { IDMSMockServerFilter } from '../filters/idms_mock_server_filter';
import { MongoDBTopologyFilter } from '../filters/mongodb_topology_filter';
import { MongoDBVersionFilter } from '../filters/mongodb_version_filter';
import { NodeVersionFilter } from '../filters/node_version_filter';
import { OSFilter } from '../filters/os_filter';
import { ServerlessFilter } from '../filters/serverless_filter';
import { type Filter } from '../filters/filter';
import { type Context } from 'mocha';
import { flakyTests } from '../flaky';
// Default our tests to have auth enabled
// A better solution will be tackled in NODE-3714
process.env.AUTH = process.env.AUTH === 'noauth' ? 'noauth' : 'auth';
process.env.MONGODB_URI =
process.env.MONGODB_URI ||
(process.env.AUTH === 'auth'
? 'mongodb://bob:pwd123@localhost:27017'
: 'mongodb://localhost:27017');
// If the URI exists as an environment variable, use it. Otherwise
// determine the connection string based on the value of process.env.AUTH
const MONGODB_URI = process.env.MONGODB_URI;
const MONGODB_API_VERSION = process.env.MONGODB_API_VERSION;
// Load balancer fronting 1 mongos.
const SINGLE_MONGOS_LB_URI = process.env.SINGLE_MONGOS_LB_URI;
// Load balancer fronting 2 mongoses.
const MULTI_MONGOS_LB_URI = process.env.MULTI_MONGOS_LB_URI;
const loadBalanced = SINGLE_MONGOS_LB_URI && MULTI_MONGOS_LB_URI;
const filters: Filter[] = [];
let initializedFilters = false;
async function initializeFilters(client): Promise<Record<string, any>> {
if (initializedFilters) {
return {};
}
initializedFilters = true;
const context = {
filters: [
new ApiVersionFilter(),
new AuthFilter(),
new ClientSideEncryptionFilter(),
new GenericPredicateFilter(),
new IDMSMockServerFilter(),
new MongoDBTopologyFilter(),
new MongoDBVersionFilter(),
new NodeVersionFilter(),
new OSFilter(),
new ServerlessFilter()
]
};
for (const filter of context.filters) {
filters.push(filter);
await filter.initializeFilter(client, context);
}
return context;
}
const testSkipBeforeEachHook = async function () {
const metadata = this.currentTest.metadata;
if (metadata && metadata.requires && Object.keys(metadata.requires).length > 0) {
const failedFilter = filters.find(filter => filter.filter(this.currentTest) !== true);
if (failedFilter) {
const maybeSkipReason = failedFilter.filter(this.currentTest);
if (typeof maybeSkipReason === 'string') {
this.currentTest.skipReason = maybeSkipReason;
this.skip();
return;
}
const filterName = failedFilter.constructor.name;
if (filterName === 'GenericPredicateFilter') {
this.currentTest.skipReason = `filtered by ${filterName}: ${failedFilter.filter(
this.currentTest
)}`;
} else {
const metadataString = inspect(metadata.requires, {
colors: true,
compact: true,
depth: 10,
breakLength: Infinity
});
this.currentTest.skipReason = `filtered by ${filterName} requires ${metadataString}`;
}
this.skip();
}
}
};
/**
* TODO: NODE-3891 - fix tests that are broken with auth enabled and remove this hook
* @param skippedTests - define list of tests to skip
* @returns
*/
export const skipBrokenAuthTestBeforeEachHook = function (
{ skippedTests }: { skippedTests: string[] } = { skippedTests: [] }
) {
return function () {
if (process.env.AUTH === 'auth' && skippedTests.includes(this.currentTest.title)) {
this.currentTest.skipReason = 'TODO: NODE-3891 - fix tests broken when AUTH enabled';
this.skip();
}
};
};
const testConfigBeforeHook = async function () {
if (process.env.DRIVERS_ATLAS_TESTING_URI) {
this.configuration = new AstrolabeTestConfiguration(process.env.DRIVERS_ATLAS_TESTING_URI, {});
return;
}
const client = new MongoClient(loadBalanced ? SINGLE_MONGOS_LB_URI : MONGODB_URI, {
...getEnvironmentalOptions(),
// TODO(NODE-4884): once happy eyeballs support is added, we no longer need to set
// the default dns resolution order for CI
family: 4
});
await client.db('test').command({ ping: 1 });
const context = await initializeFilters(client);
if (MONGODB_API_VERSION) {
context.serverApi = MONGODB_API_VERSION;
}
if (SINGLE_MONGOS_LB_URI && MULTI_MONGOS_LB_URI) {
context.singleMongosLoadBalancerUri = SINGLE_MONGOS_LB_URI;
context.multiMongosLoadBalancerUri = MULTI_MONGOS_LB_URI;
}
context.parameters = await client
.db()
.admin()
.command({ getParameter: '*' })
.catch(error => ({ noReply: error }));
const Config: typeof TestConfiguration = process.env.ALPINE
? AlpineTestConfiguration
: TestConfiguration;
this.configuration = new Config(loadBalanced ? SINGLE_MONGOS_LB_URI : MONGODB_URI, context);
await client.close();
// eslint-disable-next-line @typescript-eslint/no-require-imports
const zstdVersion = require('@mongodb-js/zstd/package.json').version;
const currentEnv = {
// TODO(NODE-3714): Improve environment detection
topology: this.configuration.topologyType,
version: this.configuration.buildInfo.version,
node: process.version,
os: process.platform,
alpineLinux: Boolean(process.env.ALPINE),
cryptdUri: process.env.MONGOCRYPTD_URI,
pid: process.pid,
serverless: process.env.SERVERLESS === '1',
auth: process.env.AUTH === 'auth',
tls: process.env.SSL === 'ssl',
csfle: {
...this.configuration.clientSideEncryption
},
serverApi: MONGODB_API_VERSION,
atlas: process.env.ATLAS_CONNECTIVITY != null,
aws: MONGODB_URI.includes('authMechanism=MONGODB-AWS'),
awsSdk: process.env.MONGODB_AWS_SDK,
azure: MONGODB_URI.includes('ENVIRONMENT:azure'),
adl: this.configuration.buildInfo.dataLake
? this.configuration.buildInfo.dataLake.version
: false,
kerberos: process.env.PRINCIPAL != null,
ldap: MONGODB_URI.includes('authMechanism=PLAIN'),
socks5: MONGODB_URI.includes('proxyHost='),
compressor: process.env.COMPRESSOR,
cryptSharedLibPath: process.env.CRYPT_SHARED_LIB_PATH,
zstdVersion
};
console.error(inspect(currentEnv, { colors: true }));
};
// ensure all mock connections are closed after the suite is run
const cleanUpMocksAfterHook = () => mock.cleanup();
const beforeAllPluginImports = () => {
// optionally enable test runner-wide plugins
// eslint-disable-next-line @typescript-eslint/no-require-imports
require('../plugins/deferred');
// configure mocha
// eslint-disable-next-line @typescript-eslint/no-require-imports
require('mocha-sinon');
};
async function beforeEachLogging(this: Context) {
if (this.currentTest == null) return;
this.configuration.beforeEachLogging(this);
}
async function afterEachLogging(this: Context) {
if (this.currentTest == null) return;
this.configuration.afterEachLogging(this);
}
function checkFlakyTestList(this: Context) {
const allTests: string[] = [];
const stack = [this.test.parent];
while (stack.length) {
const suite = stack.pop();
allTests.push(...suite.tests.map(test => test.fullTitle()));
stack.push(...suite.suites);
}
allTests.reverse(); // Doesn't matter but when debugging easier to see this in the expected order.
const flakyTestDoesNotExist = flakyTests.find(testName => !allTests.includes(testName));
if (flakyTestDoesNotExist != null) {
console.error('Flaky test:', flakyTestDoesNotExist, 'is not run at all');
process.exitCode = 1;
}
}
export const mochaHooks = {
beforeAll: [beforeAllPluginImports, testConfigBeforeHook, checkFlakyTestList],
beforeEach: [testSkipBeforeEachHook, beforeEachLogging],
afterEach: [afterEachLogging],
afterAll: [cleanUpMocksAfterHook]
};