From 4c82794d479961206c44f2461db03277b643d61f Mon Sep 17 00:00:00 2001 From: bailey Date: Thu, 24 Apr 2025 13:55:28 -0600 Subject: [PATCH 1/2] implement prose test 9 --- .../mongodb-handshake.prose.test.ts | 51 ++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts b/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts index 915ea30524e..abe7ba1dc2b 100644 --- a/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts +++ b/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts @@ -1,7 +1,13 @@ import { expect } from 'chai'; import * as sinon from 'sinon'; -import { Connection, getFAASEnv, LEGACY_HELLO_COMMAND, type MongoClient } from '../../mongodb'; +import { + Connection, + getFAASEnv, + Int32, + LEGACY_HELLO_COMMAND, + type MongoClient +} from '../../mongodb'; describe('Handshake Prose Tests', function () { let client: MongoClient; @@ -110,6 +116,49 @@ describe('Handshake Prose Tests', function () { }); } + context('Test 9: Valid container and FaaS provider', function () { + const env = [ + ['AWS_EXECUTION_ENV', 'AWS_Lambda_java8'], + ['AWS_REGION', 'us-east-2'], + ['AWS_LAMBDA_FUNCTION_MEMORY_SIZE', '1024'], + ['KUBERNETES_SERVICE_HOST', '1'] + ] as EnvironmentVariables; + before(() => { + for (const [key, value] of env) { + process.env[key] = value; + } + }); + after(() => { + for (const [key] of env) { + delete process.env[key]; + } + }); + + it('runs a hello successfully', async function () { + client = this.configuration.newClient({ + // if the handshake is not truncated, the `hello`s fail and the client does + // not connect. Lowering the server selection timeout causes the tests + // to fail more quickly in that scenario. + serverSelectionTimeoutMS: 3000 + }); + await client.connect(); + }); + + it('includes both container and FAAS provider information in the client metadata', async function () { + client = this.configuration.newClient(); + await client.connect(); + expect(client.topology?.s.options.extendedMetadata).to.exist; + const { env } = await client.topology.s.options.extendedMetadata; + + expect(env).to.deep.equal({ + region: 'us-east-2', + name: 'aws.lambda', + memory_mb: new Int32(1024), + container: { orchestrator: 'kubernetes' } + }); + }); + }); + context(`Test 2: Test that the driver accepts an arbitrary auth mechanism`, function () { let stubCalled = false; beforeEach(() => { From 02d85f7794f2b6df3e05fba84578cafcb058ba0d Mon Sep 17 00:00:00 2001 From: bailey Date: Fri, 25 Apr 2025 09:29:49 -0600 Subject: [PATCH 2/2] comments --- .../mongodb-handshake.prose.test.ts | 44 +++++++++---------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts b/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts index abe7ba1dc2b..d2180bd6380 100644 --- a/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts +++ b/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts @@ -8,6 +8,24 @@ import { LEGACY_HELLO_COMMAND, type MongoClient } from '../../mongodb'; + +type EnvironmentVariables = Array<[string, string]>; + +function stubEnv(env: EnvironmentVariables) { + let cachedEnv: NodeJS.ProcessEnv; + before(function () { + cachedEnv = process.env; + process.env = { + ...process.env, + ...Object.fromEntries(env) + }; + }); + + after(function () { + process.env = cachedEnv; + }); +} + describe('Handshake Prose Tests', function () { let client: MongoClient; @@ -15,7 +33,6 @@ describe('Handshake Prose Tests', function () { await client?.close(); }); - type EnvironmentVariables = Array<[string, string]>; const tests: Array<{ context: string; expectedProvider: string | undefined; @@ -86,16 +103,7 @@ describe('Handshake Prose Tests', function () { for (const { context: name, env, expectedProvider } of tests) { context(name, function () { - before(() => { - for (const [key, value] of env) { - process.env[key] = value; - } - }); - after(() => { - for (const [key] of env) { - delete process.env[key]; - } - }); + stubEnv(env); it(`metadata confirmation test for ${name}`, function () { expect(getFAASEnv()?.get('name')).to.equal( @@ -117,22 +125,12 @@ describe('Handshake Prose Tests', function () { } context('Test 9: Valid container and FaaS provider', function () { - const env = [ + stubEnv([ ['AWS_EXECUTION_ENV', 'AWS_Lambda_java8'], ['AWS_REGION', 'us-east-2'], ['AWS_LAMBDA_FUNCTION_MEMORY_SIZE', '1024'], ['KUBERNETES_SERVICE_HOST', '1'] - ] as EnvironmentVariables; - before(() => { - for (const [key, value] of env) { - process.env[key] = value; - } - }); - after(() => { - for (const [key] of env) { - delete process.env[key]; - } - }); + ]); it('runs a hello successfully', async function () { client = this.configuration.newClient({