1
- import { type Document } from 'bson' ;
2
1
import { expect } from 'chai' ;
3
2
4
3
import { MongoCredentials } from '../../../src/cmap/auth/mongo_credentials' ;
5
4
import { connect , prepareHandshakeDocument } from '../../../src/cmap/connect' ;
6
5
import { type Connection , type ConnectionOptions } from '../../../src/cmap/connection' ;
7
- import { type ClientMetadata } from '../../../src/cmap/handshake/client_metadata' ;
6
+ import {
7
+ type ClientMetadata ,
8
+ makeClientMetadata
9
+ } from '../../../src/cmap/handshake/client_metadata' ;
8
10
import { LEGACY_HELLO_COMMAND } from '../../../src/constants' ;
9
11
import { MongoNetworkError } from '../../../src/error' ;
10
12
import { MongoClientAuthProviders } from '../../../src/mongo_client_auth_providers' ;
@@ -22,6 +24,18 @@ const CONNECT_DEFAULTS = {
22
24
loadBalanced : false
23
25
} ;
24
26
27
+ function configureMockEnvHooks ( env : NodeJS . ProcessEnv ) {
28
+ const cachedEnv = process . env ;
29
+
30
+ beforeEach ( function ( ) {
31
+ process . env = env ;
32
+ } ) ;
33
+
34
+ afterEach ( function ( ) {
35
+ process . env = cachedEnv ;
36
+ } ) ;
37
+ }
38
+
25
39
describe ( 'Connect Tests' , function ( ) {
26
40
context ( 'when PLAIN auth enabled' , ( ) => {
27
41
const test : {
@@ -183,28 +197,24 @@ describe('Connect Tests', function () {
183
197
184
198
describe ( 'prepareHandshakeDocument' , ( ) => {
185
199
describe ( 'client environment (containers and FAAS)' , ( ) => {
186
- const cachedEnv = process . env ;
187
-
188
200
context ( 'when only kubernetes is present' , ( ) => {
189
201
let authContext ;
190
202
203
+ configureMockEnvHooks ( {
204
+ KUBERNETES_SERVICE_HOST : 'I exist'
205
+ } ) ;
206
+
191
207
beforeEach ( ( ) => {
192
- process . env . KUBERNETES_SERVICE_HOST = 'I exist' ;
193
208
authContext = {
194
209
connection : { } ,
195
210
options : {
196
211
...CONNECT_DEFAULTS ,
197
- extendedMetadata : addContainerMetadata ( { } as ClientMetadata )
212
+ metadata : makeClientMetadata ( [ ] , { } )
198
213
}
199
214
} ;
200
215
} ) ;
201
216
202
217
afterEach ( ( ) => {
203
- if ( cachedEnv . KUBERNETES_SERVICE_HOST != null ) {
204
- process . env . KUBERNETES_SERVICE_HOST = cachedEnv . KUBERNETES_SERVICE_HOST ;
205
- } else {
206
- delete process . env . KUBERNETES_SERVICE_HOST ;
207
- }
208
218
authContext = { } ;
209
219
} ) ;
210
220
@@ -220,13 +230,21 @@ describe('Connect Tests', function () {
220
230
221
231
context ( 'when 512 byte size limit is exceeded' , ( ) => {
222
232
it ( `should not 'env' property in client` , async ( ) => {
223
- // make metadata = 507 bytes, so it takes up entire LimitedSizeDocument
233
+ // make a metadata object that, with just the name and appName, is already at capacity.
224
234
const longAppName = 's' . repeat ( 493 ) ;
235
+ const metadata = makeClientMetadata (
236
+ [
237
+ {
238
+ name : 's' . repeat ( 128 )
239
+ }
240
+ ] ,
241
+ { appName : longAppName }
242
+ ) ;
225
243
const longAuthContext = {
226
244
connection : { } ,
227
245
options : {
228
246
...CONNECT_DEFAULTS ,
229
- extendedMetadata : addContainerMetadata ( { appName : longAppName } )
247
+ metadata
230
248
}
231
249
} ;
232
250
const handshakeDocument = await prepareHandshakeDocument ( longAuthContext ) ;
@@ -238,23 +256,22 @@ describe('Connect Tests', function () {
238
256
context ( 'when kubernetes and FAAS are both present' , ( ) => {
239
257
let authContext ;
240
258
259
+ configureMockEnvHooks ( {
260
+ KUBERNETES_SERVICE_HOST : 'I exist' ,
261
+ AWS_EXECUTION_ENV : 'AWS_Lambda_function'
262
+ } ) ;
263
+
241
264
beforeEach ( ( ) => {
242
- process . env . KUBERNETES_SERVICE_HOST = 'I exist' ;
243
265
authContext = {
244
266
connection : { } ,
245
267
options : {
246
268
...CONNECT_DEFAULTS ,
247
- extendedMetadata : addContainerMetadata ( { env : { name : 'aws.lambda' } } )
269
+ metadata : makeClientMetadata ( [ ] , { } )
248
270
}
249
271
} ;
250
272
} ) ;
251
273
252
274
afterEach ( ( ) => {
253
- if ( cachedEnv . KUBERNETES_SERVICE_HOST != null ) {
254
- process . env . KUBERNETES_SERVICE_HOST = cachedEnv . KUBERNETES_SERVICE_HOST ;
255
- } else {
256
- delete process . env . KUBERNETES_SERVICE_HOST ;
257
- }
258
275
authContext = { } ;
259
276
} ) ;
260
277
@@ -270,16 +287,21 @@ describe('Connect Tests', function () {
270
287
271
288
context ( 'when 512 byte size limit is exceeded' , ( ) => {
272
289
it ( `should not have 'container' property in client.env` , async ( ) => {
273
- // make metadata = 507 bytes, so it takes up entire LimitedSizeDocument
274
290
const longAppName = 's' . repeat ( 447 ) ;
291
+ // make a metadata object that, with just the name and appName, is already at capacity.
292
+ const metadata = makeClientMetadata (
293
+ [
294
+ {
295
+ name : 's' . repeat ( 128 )
296
+ }
297
+ ] ,
298
+ { appName : longAppName }
299
+ ) ;
275
300
const longAuthContext = {
276
301
connection : { } ,
277
302
options : {
278
303
...CONNECT_DEFAULTS ,
279
- extendedMetadata : {
280
- appName : longAppName ,
281
- env : { name : 'aws.lambda' }
282
- } as unknown as Promise < Document >
304
+ metadata
283
305
}
284
306
} ;
285
307
const handshakeDocument = await prepareHandshakeDocument ( longAuthContext ) ;
@@ -296,19 +318,7 @@ describe('Connect Tests', function () {
296
318
} ;
297
319
298
320
context ( 'when process.env.KUBERNETES_SERVICE_HOST = undefined' , ( ) => {
299
- beforeEach ( ( ) => {
300
- delete process . env . KUBERNETES_SERVICE_HOST ;
301
- } ) ;
302
-
303
- afterEach ( ( ) => {
304
- afterEach ( ( ) => {
305
- if ( cachedEnv . KUBERNETES_SERVICE_HOST != null ) {
306
- process . env . KUBERNETES_SERVICE_HOST = cachedEnv . KUBERNETES_SERVICE_HOST ;
307
- } else {
308
- delete process . env . KUBERNETES_SERVICE_HOST ;
309
- }
310
- } ) ;
311
- } ) ;
321
+ configureMockEnvHooks ( { KUBERNETES_SERVICE_HOST : undefined } ) ;
312
322
313
323
it ( `should not have 'env' property in client` , async ( ) => {
314
324
const handshakeDocument = await prepareHandshakeDocument ( authContext ) ;
@@ -317,16 +327,8 @@ describe('Connect Tests', function () {
317
327
} ) ;
318
328
319
329
context ( 'when process.env.KUBERNETES_SERVICE_HOST is an empty string' , ( ) => {
320
- beforeEach ( ( ) => {
321
- process . env . KUBERNETES_SERVICE_HOST = '' ;
322
- } ) ;
323
-
324
- afterEach ( ( ) => {
325
- if ( cachedEnv . KUBERNETES_SERVICE_HOST != null ) {
326
- process . env . KUBERNETES_SERVICE_HOST = cachedEnv . KUBERNETES_SERVICE_HOST ;
327
- } else {
328
- delete process . env . KUBERNETES_SERVICE_HOST ;
329
- }
330
+ configureMockEnvHooks ( {
331
+ KUBERNETES_SERVICE_HOST : ''
330
332
} ) ;
331
333
332
334
it ( `should not have 'env' property in client` , async ( ) => {
0 commit comments