Skip to content

Commit 4a94786

Browse files
fix broken tests
1 parent e1d6879 commit 4a94786

File tree

5 files changed

+71
-64
lines changed

5 files changed

+71
-64
lines changed

src/cmap/handshake/client_metadata.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@ export interface ClientMetadata {
4747
};
4848
/** FaaS environment information */
4949
env?: {
50-
name: 'aws.lambda' | 'gcp.func' | 'azure.func' | 'vercel';
50+
name?: 'aws.lambda' | 'gcp.func' | 'azure.func' | 'vercel';
5151
timeout_sec?: Int32;
5252
memory_mb?: Int32;
5353
region?: string;
54-
url?: string;
54+
container?: {
55+
runtime?: string;
56+
orchestrator?: string;
57+
};
5558
};
5659
}
5760

@@ -194,15 +197,17 @@ export async function makeClientMetadata(
194197
}
195198

196199
let dockerPromise: Promise<boolean>;
200+
type ContainerMetadata = NonNullable<NonNullable<ClientMetadata['env']>['container']>;
197201
/** @internal */
198-
async function getContainerMetadata() {
199-
const containerMetadata: Record<string, any> = {};
202+
async function getContainerMetadata(): Promise<ContainerMetadata> {
200203
dockerPromise ??= fileIsAccessible('/.dockerenv');
201204
const isDocker = await dockerPromise;
202205

203206
const { KUBERNETES_SERVICE_HOST = '' } = process.env;
204207
const isKubernetes = KUBERNETES_SERVICE_HOST.length > 0 ? true : false;
205208

209+
const containerMetadata: ContainerMetadata = {};
210+
206211
if (isDocker) containerMetadata.runtime = 'docker';
207212
if (isKubernetes) containerMetadata.orchestrator = 'kubernetes';
208213

@@ -220,7 +225,10 @@ async function addContainerMetadata(originalMetadata: ClientMetadata): Promise<C
220225

221226
const extendedMetadata = new LimitedSizeDocument(512);
222227

223-
const extendedEnvMetadata = { ...originalMetadata?.env, container: containerMetadata };
228+
const extendedEnvMetadata: NonNullable<ClientMetadata['env']> = {
229+
...originalMetadata?.env,
230+
container: containerMetadata
231+
};
224232

225233
for (const [key, val] of Object.entries(originalMetadata)) {
226234
if (key !== 'env') {

src/mongo_client.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -407,15 +407,12 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
407407
* The consolidate, parsed, transformed and merged options.
408408
*/
409409
public readonly options: Readonly<
410-
Omit<
411-
MongoOptions,
412-
'monitorCommands' | 'ca' | 'crl' | 'key' | 'cert' | 'driverInfo' | 'metadata'
413-
>
410+
Omit<MongoOptions, 'monitorCommands' | 'ca' | 'crl' | 'key' | 'cert' | 'driverInfo'>
414411
> &
415-
Pick<
416-
MongoOptions,
417-
'monitorCommands' | 'ca' | 'crl' | 'key' | 'cert' | 'driverInfo' | 'metadata'
418-
>;
412+
Pick<MongoOptions, 'monitorCommands' | 'ca' | 'crl' | 'key' | 'cert' | 'driverInfo'> & {
413+
/** @internal */
414+
metadata: Promise<ClientMetadata>;
415+
};
419416

420417
private driverInfoList: DriverInfo[] = [];
421418

@@ -1087,7 +1084,7 @@ export interface MongoOptions
10871084
compressors: CompressorName[];
10881085
writeConcern: WriteConcern;
10891086
dbName: string;
1090-
/** @internal - Will be made internal in a future major release. */
1087+
/** @internal */
10911088
metadata: Promise<ClientMetadata>;
10921089
/** @internal */
10931090
autoEncrypter?: AutoEncrypter;

src/sdam/monitor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { clearTimeout, setTimeout } from 'timers';
33
import { type Document, Long } from '../bson';
44
import { connect, makeConnection, makeSocket, performInitialHandshake } from '../cmap/connect';
55
import type { Connection, ConnectionOptions } from '../cmap/connection';
6-
import { type ClientMetadata, getFAASEnv } from '../cmap/handshake/client_metadata';
6+
import { getFAASEnv } from '../cmap/handshake/client_metadata';
77
import { LEGACY_HELLO_COMMAND } from '../constants';
88
import { MongoError, MongoErrorLabel, MongoNetworkTimeoutError } from '../error';
99
import { MongoLoggableComponent } from '../mongo_logger';

test/tools/uri_spec_runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ export function executeUriValidationTest(
329329

330330
//** MISC SPECIAL PARSE RULE OPTIONS **/
331331
case 'appname':
332-
expectedProp = 'metadata.application.name';
332+
expectedProp = 'appName';
333333
expect(options, `${errorMessage} ${optionKey} -> ${expectedProp}`)
334334
.to.have.nested.property(expectedProp)
335335
.equal(optionValue);

test/unit/cmap/connect.test.ts

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { type Document } from 'bson';
21
import { expect } from 'chai';
32

43
import { MongoCredentials } from '../../../src/cmap/auth/mongo_credentials';
54
import { connect, prepareHandshakeDocument } from '../../../src/cmap/connect';
65
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';
810
import { LEGACY_HELLO_COMMAND } from '../../../src/constants';
911
import { MongoNetworkError } from '../../../src/error';
1012
import { MongoClientAuthProviders } from '../../../src/mongo_client_auth_providers';
@@ -22,6 +24,18 @@ const CONNECT_DEFAULTS = {
2224
loadBalanced: false
2325
};
2426

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+
2539
describe('Connect Tests', function () {
2640
context('when PLAIN auth enabled', () => {
2741
const test: {
@@ -183,28 +197,24 @@ describe('Connect Tests', function () {
183197

184198
describe('prepareHandshakeDocument', () => {
185199
describe('client environment (containers and FAAS)', () => {
186-
const cachedEnv = process.env;
187-
188200
context('when only kubernetes is present', () => {
189201
let authContext;
190202

203+
configureMockEnvHooks({
204+
KUBERNETES_SERVICE_HOST: 'I exist'
205+
});
206+
191207
beforeEach(() => {
192-
process.env.KUBERNETES_SERVICE_HOST = 'I exist';
193208
authContext = {
194209
connection: {},
195210
options: {
196211
...CONNECT_DEFAULTS,
197-
extendedMetadata: addContainerMetadata({} as ClientMetadata)
212+
metadata: makeClientMetadata([], {})
198213
}
199214
};
200215
});
201216

202217
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-
}
208218
authContext = {};
209219
});
210220

@@ -220,13 +230,21 @@ describe('Connect Tests', function () {
220230

221231
context('when 512 byte size limit is exceeded', () => {
222232
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.
224234
const longAppName = 's'.repeat(493);
235+
const metadata = makeClientMetadata(
236+
[
237+
{
238+
name: 's'.repeat(128)
239+
}
240+
],
241+
{ appName: longAppName }
242+
);
225243
const longAuthContext = {
226244
connection: {},
227245
options: {
228246
...CONNECT_DEFAULTS,
229-
extendedMetadata: addContainerMetadata({ appName: longAppName })
247+
metadata
230248
}
231249
};
232250
const handshakeDocument = await prepareHandshakeDocument(longAuthContext);
@@ -238,23 +256,22 @@ describe('Connect Tests', function () {
238256
context('when kubernetes and FAAS are both present', () => {
239257
let authContext;
240258

259+
configureMockEnvHooks({
260+
KUBERNETES_SERVICE_HOST: 'I exist',
261+
AWS_EXECUTION_ENV: 'AWS_Lambda_function'
262+
});
263+
241264
beforeEach(() => {
242-
process.env.KUBERNETES_SERVICE_HOST = 'I exist';
243265
authContext = {
244266
connection: {},
245267
options: {
246268
...CONNECT_DEFAULTS,
247-
extendedMetadata: addContainerMetadata({ env: { name: 'aws.lambda' } })
269+
metadata: makeClientMetadata([], {})
248270
}
249271
};
250272
});
251273

252274
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-
}
258275
authContext = {};
259276
});
260277

@@ -270,16 +287,21 @@ describe('Connect Tests', function () {
270287

271288
context('when 512 byte size limit is exceeded', () => {
272289
it(`should not have 'container' property in client.env`, async () => {
273-
// make metadata = 507 bytes, so it takes up entire LimitedSizeDocument
274290
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+
);
275300
const longAuthContext = {
276301
connection: {},
277302
options: {
278303
...CONNECT_DEFAULTS,
279-
extendedMetadata: {
280-
appName: longAppName,
281-
env: { name: 'aws.lambda' }
282-
} as unknown as Promise<Document>
304+
metadata
283305
}
284306
};
285307
const handshakeDocument = await prepareHandshakeDocument(longAuthContext);
@@ -296,19 +318,7 @@ describe('Connect Tests', function () {
296318
};
297319

298320
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 });
312322

313323
it(`should not have 'env' property in client`, async () => {
314324
const handshakeDocument = await prepareHandshakeDocument(authContext);
@@ -317,16 +327,8 @@ describe('Connect Tests', function () {
317327
});
318328

319329
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: ''
330332
});
331333

332334
it(`should not have 'env' property in client`, async () => {

0 commit comments

Comments
 (0)