11import { expect } from 'chai' ;
22import * as sinon from 'sinon' ;
33
4- import { Connection , getFAASEnv , LEGACY_HELLO_COMMAND , type MongoClient } from '../../mongodb' ;
4+ import {
5+ Connection ,
6+ getFAASEnv ,
7+ Int32 ,
8+ LEGACY_HELLO_COMMAND ,
9+ type MongoClient
10+ } from '../../mongodb' ;
11+
12+ type EnvironmentVariables = Array < [ string , string ] > ;
13+
14+ function stubEnv ( env : EnvironmentVariables ) {
15+ let cachedEnv : NodeJS . ProcessEnv ;
16+ before ( function ( ) {
17+ cachedEnv = process . env ;
18+ process . env = {
19+ ...process . env ,
20+ ...Object . fromEntries ( env )
21+ } ;
22+ } ) ;
23+
24+ after ( function ( ) {
25+ process . env = cachedEnv ;
26+ } ) ;
27+ }
28+
529describe ( 'Handshake Prose Tests' , function ( ) {
630 let client : MongoClient ;
731
832 afterEach ( async function ( ) {
933 await client ?. close ( ) ;
1034 } ) ;
1135
12- type EnvironmentVariables = Array < [ string , string ] > ;
1336 const tests : Array < {
1437 context : string ;
1538 expectedProvider : string | undefined ;
@@ -80,16 +103,7 @@ describe('Handshake Prose Tests', function () {
80103
81104 for ( const { context : name , env, expectedProvider } of tests ) {
82105 context ( name , function ( ) {
83- before ( ( ) => {
84- for ( const [ key , value ] of env ) {
85- process . env [ key ] = value ;
86- }
87- } ) ;
88- after ( ( ) => {
89- for ( const [ key ] of env ) {
90- delete process . env [ key ] ;
91- }
92- } ) ;
106+ stubEnv ( env ) ;
93107
94108 it ( `metadata confirmation test for ${ name } ` , function ( ) {
95109 expect ( getFAASEnv ( ) ?. get ( 'name' ) ) . to . equal (
@@ -110,6 +124,39 @@ describe('Handshake Prose Tests', function () {
110124 } ) ;
111125 }
112126
127+ context ( 'Test 9: Valid container and FaaS provider' , function ( ) {
128+ stubEnv ( [
129+ [ 'AWS_EXECUTION_ENV' , 'AWS_Lambda_java8' ] ,
130+ [ 'AWS_REGION' , 'us-east-2' ] ,
131+ [ 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE' , '1024' ] ,
132+ [ 'KUBERNETES_SERVICE_HOST' , '1' ]
133+ ] ) ;
134+
135+ it ( 'runs a hello successfully' , async function ( ) {
136+ client = this . configuration . newClient ( {
137+ // if the handshake is not truncated, the `hello`s fail and the client does
138+ // not connect. Lowering the server selection timeout causes the tests
139+ // to fail more quickly in that scenario.
140+ serverSelectionTimeoutMS : 3000
141+ } ) ;
142+ await client . connect ( ) ;
143+ } ) ;
144+
145+ it ( 'includes both container and FAAS provider information in the client metadata' , async function ( ) {
146+ client = this . configuration . newClient ( ) ;
147+ await client . connect ( ) ;
148+ expect ( client . topology ?. s . options . extendedMetadata ) . to . exist ;
149+ const { env } = await client . topology . s . options . extendedMetadata ;
150+
151+ expect ( env ) . to . deep . equal ( {
152+ region : 'us-east-2' ,
153+ name : 'aws.lambda' ,
154+ memory_mb : new Int32 ( 1024 ) ,
155+ container : { orchestrator : 'kubernetes' }
156+ } ) ;
157+ } ) ;
158+ } ) ;
159+
113160 context ( `Test 2: Test that the driver accepts an arbitrary auth mechanism` , function ( ) {
114161 let stubCalled = false ;
115162 beforeEach ( ( ) => {
0 commit comments