Skip to content

test(NODE-6288): test that container and FAAS env information are both included in handshake #4522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
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';

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;

afterEach(async function () {
await client?.close();
});

type EnvironmentVariables = Array<[string, string]>;
const tests: Array<{
context: string;
expectedProvider: string | undefined;
Expand Down Expand Up @@ -80,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(
Expand All @@ -110,6 +124,39 @@ describe('Handshake Prose Tests', function () {
});
}

context('Test 9: Valid container and FaaS provider', function () {
stubEnv([
['AWS_EXECUTION_ENV', 'AWS_Lambda_java8'],
['AWS_REGION', 'us-east-2'],
['AWS_LAMBDA_FUNCTION_MEMORY_SIZE', '1024'],
['KUBERNETES_SERVICE_HOST', '1']
]);

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(() => {
Expand Down