@@ -2,6 +2,7 @@ import { expect } from 'chai';
2
2
import { promisify } from 'util' ;
3
3
4
4
import {
5
+ addContainerMetadata ,
5
6
CancellationToken ,
6
7
type ClientMetadata ,
7
8
connect ,
@@ -24,6 +25,7 @@ const CONNECT_DEFAULTS = {
24
25
generation : 1 ,
25
26
monitorCommands : false ,
26
27
metadata : { } as ClientMetadata ,
28
+ extendedMetadata : addContainerMetadata ( { } as ClientMetadata ) ,
27
29
loadBalanced : false
28
30
} ;
29
31
@@ -207,7 +209,162 @@ describe('Connect Tests', function () {
207
209
} ) ;
208
210
} ) ;
209
211
210
- context ( 'prepareHandshakeDocument' , ( ) => {
212
+ describe ( 'prepareHandshakeDocument' , ( ) => {
213
+ describe ( 'client environment (containers and FAAS)' , ( ) => {
214
+ const cachedEnv = process . env ;
215
+
216
+ context ( 'when only kubernetes is present' , ( ) => {
217
+ let authContext ;
218
+
219
+ beforeEach ( ( ) => {
220
+ process . env . KUBERNETES_SERVICE_HOST = 'I exist' ;
221
+ authContext = {
222
+ connection : { } ,
223
+ options : {
224
+ ...CONNECT_DEFAULTS ,
225
+ extendedMetadata : addContainerMetadata ( { } as ClientMetadata )
226
+ }
227
+ } ;
228
+ } ) ;
229
+
230
+ afterEach ( ( ) => {
231
+ if ( cachedEnv . KUBERNETES_SERVICE_HOST != null ) {
232
+ process . env . KUBERNETES_SERVICE_HOST = cachedEnv . KUBERNETES_SERVICE_HOST ;
233
+ } else {
234
+ delete process . env . KUBERNETES_SERVICE_HOST ;
235
+ }
236
+ authContext = { } ;
237
+ } ) ;
238
+
239
+ it ( `should include { orchestrator: 'kubernetes'} in client.env.container` , async ( ) => {
240
+ const handshakeDocument = await prepareHandshakeDocument ( authContext ) ;
241
+ expect ( handshakeDocument . client . env . container . orchestrator ) . to . equal ( 'kubernetes' ) ;
242
+ } ) ;
243
+
244
+ it ( `should not have 'name' property in client.env ` , async ( ) => {
245
+ const handshakeDocument = await prepareHandshakeDocument ( authContext ) ;
246
+ expect ( handshakeDocument . client . env ) . to . not . have . property ( 'name' ) ;
247
+ } ) ;
248
+
249
+ context ( 'when 512 byte size limit is exceeded' , async ( ) => {
250
+ it ( `should not 'env' property in client` , async ( ) => {
251
+ // make metadata = 507 bytes, so it takes up entire LimitedSizeDocument
252
+ const longAppName = 's' . repeat ( 493 ) ;
253
+ const longAuthContext = {
254
+ connection : { } ,
255
+ options : {
256
+ ...CONNECT_DEFAULTS ,
257
+ extendedMetadata : addContainerMetadata ( { appName : longAppName } )
258
+ }
259
+ } ;
260
+ const handshakeDocument = await prepareHandshakeDocument ( longAuthContext ) ;
261
+ expect ( handshakeDocument . client ) . to . not . have . property ( 'env' ) ;
262
+ } ) ;
263
+ } ) ;
264
+ } ) ;
265
+
266
+ context ( 'when kubernetes and FAAS are both present' , ( ) => {
267
+ let authContext ;
268
+
269
+ beforeEach ( ( ) => {
270
+ process . env . KUBERNETES_SERVICE_HOST = 'I exist' ;
271
+ authContext = {
272
+ connection : { } ,
273
+ options : {
274
+ ...CONNECT_DEFAULTS ,
275
+ extendedMetadata : addContainerMetadata ( { env : { name : 'aws.lambda' } } )
276
+ }
277
+ } ;
278
+ } ) ;
279
+
280
+ afterEach ( ( ) => {
281
+ if ( cachedEnv . KUBERNETES_SERVICE_HOST != null ) {
282
+ process . env . KUBERNETES_SERVICE_HOST = cachedEnv . KUBERNETES_SERVICE_HOST ;
283
+ } else {
284
+ delete process . env . KUBERNETES_SERVICE_HOST ;
285
+ }
286
+ authContext = { } ;
287
+ } ) ;
288
+
289
+ it ( `should include { orchestrator: 'kubernetes'} in client.env.container` , async ( ) => {
290
+ const handshakeDocument = await prepareHandshakeDocument ( authContext ) ;
291
+ expect ( handshakeDocument . client . env . container . orchestrator ) . to . equal ( 'kubernetes' ) ;
292
+ } ) ;
293
+
294
+ it ( `should still have properly set 'name' property in client.env ` , async ( ) => {
295
+ const handshakeDocument = await prepareHandshakeDocument ( authContext ) ;
296
+ expect ( handshakeDocument . client . env . name ) . to . equal ( 'aws.lambda' ) ;
297
+ } ) ;
298
+
299
+ context ( 'when 512 byte size limit is exceeded' , async ( ) => {
300
+ it ( `should not have 'container' property in client.env` , async ( ) => {
301
+ // make metadata = 507 bytes, so it takes up entire LimitedSizeDocument
302
+ const longAppName = 's' . repeat ( 447 ) ;
303
+ const longAuthContext = {
304
+ connection : { } ,
305
+ options : {
306
+ ...CONNECT_DEFAULTS ,
307
+ extendedMetadata : {
308
+ appName : longAppName ,
309
+ env : { name : 'aws.lambda' }
310
+ } as unknown as Promise < Document >
311
+ }
312
+ } ;
313
+ const handshakeDocument = await prepareHandshakeDocument ( longAuthContext ) ;
314
+ expect ( handshakeDocument . client . env . name ) . to . equal ( 'aws.lambda' ) ;
315
+ expect ( handshakeDocument . client . env ) . to . not . have . property ( 'container' ) ;
316
+ } ) ;
317
+ } ) ;
318
+ } ) ;
319
+
320
+ context ( 'when container nor FAAS env is not present (empty string case)' , ( ) => {
321
+ const authContext = {
322
+ connection : { } ,
323
+ options : { ...CONNECT_DEFAULTS }
324
+ } ;
325
+
326
+ context ( 'when process.env.KUBERNETES_SERVICE_HOST = undefined' , ( ) => {
327
+ beforeEach ( ( ) => {
328
+ delete process . env . KUBERNETES_SERVICE_HOST ;
329
+ } ) ;
330
+
331
+ afterEach ( ( ) => {
332
+ afterEach ( ( ) => {
333
+ if ( cachedEnv . KUBERNETES_SERVICE_HOST != null ) {
334
+ process . env . KUBERNETES_SERVICE_HOST = cachedEnv . KUBERNETES_SERVICE_HOST ;
335
+ } else {
336
+ delete process . env . KUBERNETES_SERVICE_HOST ;
337
+ }
338
+ } ) ;
339
+ } ) ;
340
+
341
+ it ( `should not have 'env' property in client` , async ( ) => {
342
+ const handshakeDocument = await prepareHandshakeDocument ( authContext ) ;
343
+ expect ( handshakeDocument . client ) . to . not . have . property ( 'env' ) ;
344
+ } ) ;
345
+ } ) ;
346
+
347
+ context ( 'when process.env.KUBERNETES_SERVICE_HOST is an empty string' , ( ) => {
348
+ beforeEach ( ( ) => {
349
+ process . env . KUBERNETES_SERVICE_HOST = '' ;
350
+ } ) ;
351
+
352
+ afterEach ( ( ) => {
353
+ if ( cachedEnv . KUBERNETES_SERVICE_HOST != null ) {
354
+ process . env . KUBERNETES_SERVICE_HOST = cachedEnv . KUBERNETES_SERVICE_HOST ;
355
+ } else {
356
+ delete process . env . KUBERNETES_SERVICE_HOST ;
357
+ }
358
+ } ) ;
359
+
360
+ it ( `should not have 'env' property in client` , async ( ) => {
361
+ const handshakeDocument = await prepareHandshakeDocument ( authContext ) ;
362
+ expect ( handshakeDocument . client ) . to . not . have . property ( 'env' ) ;
363
+ } ) ;
364
+ } ) ;
365
+ } ) ;
366
+ } ) ;
367
+
211
368
context ( 'when serverApi.version is present' , ( ) => {
212
369
const options = {
213
370
authProviders : new MongoClientAuthProviders ( )
0 commit comments