-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathmongodb-handshake.prose.test.ts
More file actions
953 lines (837 loc) · 35.9 KB
/
mongodb-handshake.prose.test.ts
File metadata and controls
953 lines (837 loc) · 35.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
import { expect } from 'chai';
import * as sinon from 'sinon';
import { type ClientMetadata, type DriverInfo } from '../../../mongodb';
import { MongoClient as RawMongoClient } from '../../../src';
import {
Connection,
getFAASEnv,
Int32,
isDriverInfoEqual,
LEGACY_HELLO_COMMAND,
type MongoClient
} from '../../mongodb';
import { sleep } from '../../tools/utils';
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();
});
const tests: Array<{
context: string;
expectedProvider: string | undefined;
env: EnvironmentVariables;
}> = [
{
context: '1. Valid AWS',
expectedProvider: 'aws.lambda',
env: [
['AWS_EXECUTION_ENV', 'AWS_Lambda_java8'],
['AWS_REGION', 'us-east-2'],
['AWS_LAMBDA_FUNCTION_MEMORY_SIZE', '1024']
]
},
{
context: '2. Valid Azure',
expectedProvider: 'azure.func',
env: [['FUNCTIONS_WORKER_RUNTIME', 'node']]
},
{
context: '3. Valid GCP',
expectedProvider: 'gcp.func',
env: [
['K_SERVICE', 'servicename'],
['FUNCTION_MEMORY_MB', '1024'],
['FUNCTION_TIMEOUT_SEC', '60'],
['FUNCTION_REGION', 'us-central1']
]
},
{
context: '4. Valid Vercel',
expectedProvider: 'vercel',
env: [
['VERCEL', '1'],
['VERCEL_REGION', 'cdg1']
]
},
{
expectedProvider: undefined,
context: '5. Invalid - multiple providers',
env: [
['AWS_EXECUTION_ENV', 'AWS_Lambda_java8'],
['FUNCTIONS_WORKER_RUNTIME', 'node']
]
},
{
expectedProvider: 'aws.lambda',
context: '6. Invalid - long string',
env: [
['AWS_EXECUTION_ENV', 'AWS_Lambda_java8'],
['AWS_REGION', 'a'.repeat(1024)]
]
},
{
expectedProvider: 'aws.lambda',
context: '7. Invalid - wrong types',
env: [
['AWS_EXECUTION_ENV', 'AWS_Lambda_java8'],
['AWS_LAMBDA_FUNCTION_MEMORY_SIZE', 'big']
]
},
{
expectedProvider: undefined,
context: '8. Invalid - AWS_EXECUTION_ENV does not start with "AWS_Lambda_"',
env: [['AWS_EXECUTION_ENV', 'EC2']]
}
];
for (const { context: name, env, expectedProvider } of tests) {
context(name, function () {
stubEnv(env);
it(`metadata confirmation test for ${name}`, function () {
expect(getFAASEnv()?.get('name')).to.equal(
expectedProvider,
'determined the wrong cloud provider'
);
});
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();
});
});
}
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.metadata).to.exist;
const { env } = await client.topology.s.options.metadata;
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(() => {
// Mock the server response in a way that saslSupportedMechs array in the hello command response contains an arbitrary string.
sinon
.stub(Connection.prototype, 'command')
.callsFake(async function (ns, cmd, options, responseType) {
// @ts-expect-error: sinon will place wrappedMethod there
const command = Connection.prototype.command.wrappedMethod.bind(this);
if (cmd.hello || cmd[LEGACY_HELLO_COMMAND]) {
return stub();
}
return command(ns, cmd, options, responseType);
async function stub() {
stubCalled = true;
const response = await command(ns, cmd, options, responseType);
return {
...response,
saslSupportedMechs: [...(response.saslSupportedMechs ?? []), 'random string']
};
}
});
});
afterEach(() => sinon.restore());
it('no error is thrown', { requires: { auth: 'enabled' } }, async function () {
// Create and connect a Connection object that connects to the server that returns the mocked response.
// Assert that no error is raised.
client = this.configuration.newClient();
await client.connect();
await client.db('foo').collection('bar').insertOne({ name: 'john doe' });
expect(stubCalled).to.be.true;
await client.close();
});
});
});
describe('Client Metadata Update Prose Tests', function () {
let client: MongoClient;
afterEach(async function () {
await client?.close();
sinon.restore();
});
describe('Test 1: Test that the driver updates metadata', function () {
let initialClientMetadata;
let updatedClientMetadata;
const tests = [
{ testCase: 1, name: 'framework', version: '2.0', platform: 'Framework Platform' },
{ testCase: 2, name: 'framework', version: '2.0' },
{ testCase: 3, name: 'framework', platform: 'Framework Platform' },
{ testCase: 4, name: 'framework' }
];
for (const { testCase, name, version, platform } of tests) {
context(`Case: ${testCase}`, function () {
// 1. Create a `MongoClient` instance with the following:
// - `maxIdleTimeMS` set to `1ms`
// - Wrapping library metadata:
// | Field | Value |
// | -------- | ---------------- |
// | name | library |
// | version | 1.2 |
// | platform | Library Platform |
// 2. Send a `ping` command to the server and verify that the command succeeds.
// 3. Save intercepted `client` document as `initialClientMetadata`.
// 4. Wait 5ms for the connection to become idle.
beforeEach(async function () {
client = this.configuration.newClient(
{},
{
maxIdleTimeMS: 1,
driverInfo: { name: 'library', version: '1.2', platform: 'Library Platform' }
}
);
sinon
.stub(Connection.prototype, 'command')
.callsFake(async function (ns, cmd, options, responseType) {
// @ts-expect-error: sinon will place wrappedMethod on the command method.
const command = Connection.prototype.command.wrappedMethod.bind(this);
if (cmd.hello || cmd[LEGACY_HELLO_COMMAND]) {
if (!initialClientMetadata) {
initialClientMetadata = cmd.client;
} else {
updatedClientMetadata = cmd.client;
}
}
return command(ns, cmd, options, responseType);
});
await client.db('test').command({ ping: 1 });
await sleep(5);
});
it('appends the metadata', async function () {
// 1. Append the `DriverInfoOptions` from the selected test case to the `MongoClient` metadata.
// 2. Send a `ping` command to the server and verify:
// - The command succeeds.
// - The framework metadata is appended to the existing `DriverInfoOptions` in the `client.driver` fields of the `hello`
// command, with values separated by a pipe `|`.
client.appendMetadata({ name, version, platform });
await client.db('test').command({ ping: 1 });
// Since we have our own driver metadata getting added, we really want to just
// assert that the last driver info values are appended at the end.
expect(updatedClientMetadata.driver.name).to.match(/^.*\|framework$/);
expect(updatedClientMetadata.driver.version).to.match(
new RegExp(`^.*\\|${version ? version : '1.2'}$`)
);
expect(updatedClientMetadata.platform).to.match(
new RegExp(`^.*\\|${platform ? platform : 'Library Platform'}$`)
);
// - All other subfields in the client document remain unchanged from initialClientMetadata.
// (Note os is the only one getting set in these tests)
expect(updatedClientMetadata.os).to.deep.equal(initialClientMetadata.os);
});
});
}
});
describe('Test 2: Multiple Successive Metadata Updates', function () {
let initialClientMetadata;
let updatedClientMetadata;
const tests = [
{ testCase: 1, name: 'framework', version: '2.0', platform: 'Framework Platform' },
{ testCase: 2, name: 'framework', version: '2.0' },
{ testCase: 3, name: 'framework', platform: 'Framework Platform' },
{ testCase: 4, name: 'framework' }
];
for (const { testCase, name, version, platform } of tests) {
context(`Case: ${testCase}`, function () {
// 1. Create a `MongoClient` instance with the following:
// - `maxIdleTimeMS` set to `1ms`
// 2. Append the following `DriverInfoOptions` to the `MongoClient` metadata:
// | Field | Value |
// | -------- | ---------------- |
// | name | library |
// | version | 1.2 |
// | platform | Library Platform |
// 3. Send a `ping` command to the server and verify that the command succeeds.
// 4. Save intercepted `client` document as `updatedClientMetadata`.
// 5. Wait 5ms for the connection to become idle.
beforeEach(async function () {
client = this.configuration.newClient({}, { maxIdleTimeMS: 1 });
client.appendMetadata({ name: 'library', version: '1.2', platform: 'Library Platform' });
sinon
.stub(Connection.prototype, 'command')
.callsFake(async function (ns, cmd, options, responseType) {
// @ts-expect-error: sinon will place wrappedMethod on the command method.
const command = Connection.prototype.command.wrappedMethod.bind(this);
if (cmd.hello || cmd[LEGACY_HELLO_COMMAND]) {
if (!initialClientMetadata) {
initialClientMetadata = cmd.client;
} else {
updatedClientMetadata = cmd.client;
}
}
return command(ns, cmd, options, responseType);
});
await client.db('test').command({ ping: 1 });
await sleep(5);
});
it('appends the metadata', async function () {
// 1. Append the `DriverInfoOptions` from the selected test case to the `MongoClient` metadata.
// 2. Send a `ping` command to the server and verify:
// - The command succeeds.
// - The framework metadata is appended to the existing `DriverInfoOptions` in the `client.driver` fields of the `hello`
// command, with values separated by a pipe `|`.
client.appendMetadata({ name, version, platform });
await client.db('test').command({ ping: 1 });
// Since we have our own driver metadata getting added, we really want to just
// assert that the last driver info values are appended at the end.
expect(updatedClientMetadata.driver.name).to.match(/^.*\|framework$/);
expect(updatedClientMetadata.driver.version).to.match(
new RegExp(`^.*\\|${version ? version : '1.2'}$`)
);
expect(updatedClientMetadata.platform).to.match(
new RegExp(`^.*\\|${platform ? platform : 'Library Platform'}$`)
);
// - All other subfields in the client document remain unchanged from initialClientMetadata.
// (Note os is the only one getting set in these tests)
expect(updatedClientMetadata.os).to.deep.equal(initialClientMetadata.os);
});
});
}
});
describe('Test 3: Multiple Successive Metadata Updates with Duplicate Data', function () {
const originalDriverInfo = { name: 'library', version: '1.2', platform: 'Library Platform' };
let initialClientMetadata: ClientMetadata;
let updatedClientMetadata: ClientMetadata;
// TODO(NODE-6599): mongodb-legacy adds additional client metadata, breaking these prose tests
let client: RawMongoClient;
// | Case | Name | Version | Platform |
// | ---- | --------- | ------- | ------------------ |
// | 1 | library | 1.2 | Library Platform |
// | 2 | framework | 1.2 | Library Platform |
// | 3 | library | 2.0 | Library Platform |
// | 4 | library | 1.2 | Framework Platform |
// | 5 | framework | 2.0 | Library Platform |
// | 6 | framework | 1.2 | Framework Platform |
// | 7 | library | 2.0 | Framework Platform |
const tests = [
{ testCase: 1, name: 'library', version: '1.2', platform: 'Library Platform' },
{ testCase: 2, name: 'framework', version: '1.2', platform: 'Library Platform' },
{ testCase: 3, name: 'library', version: '2.0', platform: 'Library Platform' },
{ testCase: 4, name: 'library', version: '1.2', platform: 'Framework Platform' },
{ testCase: 5, name: 'framework', version: '2.0', platform: 'Library Platform' },
{ testCase: 6, name: 'framework', version: '1.2', platform: 'Framework Platform' },
{ testCase: 7, name: 'library', version: '2.0', platform: 'Framework Platform' }
];
for (const { testCase, ...driverInfo } of tests) {
context(`Case ${testCase}: ${JSON.stringify(driverInfo)}`, function () {
// 1. Create a `MongoClient` instance with:
// - `maxIdleTimeMS` set to `1ms`
// 2. Append the following `DriverInfoOptions` to the `MongoClient` metadata:
// | Field | Value |
// | -------- | ---------------- |
// | name | library |
// | version | 1.2 |
// | platform | Library Platform |
// 3. Send a `ping` command to the server and verify that the command succeeds.
// 4. Save intercepted `client` document as `updatedClientMetadata`.
// 5. Wait 5ms for the connection to become idle.
beforeEach(async function () {
client = new RawMongoClient(this.configuration.url(), {
maxIdleTimeMS: 1,
serverApi: this.configuration.serverApi
});
client.appendMetadata(originalDriverInfo);
sinon
.stub(Connection.prototype, 'command')
.callsFake(async function (ns, cmd, options, responseType) {
// @ts-expect-error: sinon will place wrappedMethod on the command method.
const command = Connection.prototype.command.wrappedMethod.bind(this);
if (cmd.hello || cmd[LEGACY_HELLO_COMMAND]) {
if (!initialClientMetadata) {
initialClientMetadata = cmd.client;
} else {
updatedClientMetadata = cmd.client;
}
}
return command(ns, cmd, options, responseType);
});
await client.db('test').command({ ping: 1 });
await sleep(5);
});
afterEach(async function () {
await client.close();
});
it('metadata is updated correctly, if necessary', async function () {
// 1. Append the `DriverInfoOptions` from the selected test case to the `MongoClient` metadata.
client.appendMetadata(driverInfo);
// 2. Send a `ping` command to the server and verify:
// - The command succeeds.
await client.db('test').command({ ping: 1 });
// - The framework metadata is appended to the existing `DriverInfoOptions` in the `client.driver` fields of the `hello`
// command, with values separated by a pipe `|`. To simplify assertions in these tests, strip out the default driver info
// that is automatically added by the driver (ex: `metadata.name.split('|').slice(1).join('|')`).
// - If the test case's DriverInfo is identical to the driver info from setup step 2 (test case 1):
// - Assert metadata.name is equal to `library`
// - Assert metadata.version is equal to `1.2`
// - Assert metadata.platform is equal to `LibraryPlatform`
// - Otherwise:
// - Assert metadata.name is equal to `library|<name>`
// - Assert metadata.version is equal to `1.2|<version>`
// - Assert metadata.platform is equal to `LibraryPlatform|<platform>`
const { driver, platform, ...updatedRest } = updatedClientMetadata;
const { driver: _driver, platform: _platform, ...originalRest } = initialClientMetadata;
const extractParts = (s: string) => s.split('|').slice(1).join('|');
const actual = {
name: extractParts(driver.name),
version: extractParts(driver.version),
platform: extractParts(platform)
};
const expected = isDriverInfoEqual(driverInfo, originalDriverInfo)
? originalDriverInfo
: {
name: `library|${driverInfo.name}`,
platform: `Library Platform|${driverInfo.platform}`,
version: `1.2|${driverInfo.version}`
};
expect(actual).to.deep.equal(expected);
// All other subfields in the `client` document remain unchanged from `updatedClientMetadata`.
expect(updatedRest).to.deep.equal(originalRest);
});
});
}
});
describe('Test 4: Multiple Metadata Updates with Duplicate Data', function () {
let initialClientMetadata: ClientMetadata;
let updatedClientMetadata: ClientMetadata;
// TODO(NODE-6599): mongodb-legacy adds additional client metadata, breaking these prose tests
let client: RawMongoClient;
afterEach(async function () {
await client.close();
});
it('does not append duplicate metdata', async function () {
// 1. Create a `MongoClient` instance with:
// - `maxIdleTimeMS` set to `1ms`
client = new RawMongoClient(this.configuration.url(), {
maxIdleTimeMS: 1,
serverApi: this.configuration.serverApi
});
// 2. Append the following `DriverInfoOptions` to the `MongoClient` metadata:
// | Field | Value |
// | -------- | ---------------- |
// | name | library |
// | version | 1.2 |
// | platform | Library Platform |
client.appendMetadata({
name: 'library',
version: '1.2',
platform: 'Library Platform'
});
// 3. Send a `ping` command to the server and verify that the command succeeds.
await client.db('test').command({ ping: 1 });
// 4. Wait 5ms for the connection to become idle.
await sleep(5);
// 5. Append the following `DriverInfoOptions` to the `MongoClient` metadata:
// | Field | Value |
// | -------- | ------------------ |
// | name | framework |
// | version | 2.0 |
// | platform | Framework Platform |
client.appendMetadata({
name: 'framework',
version: '2.0',
platform: 'Framework Platform'
});
// 6. Send a `ping` command to the server and verify that the command succeeds.
// 7. Save intercepted `client` document as `clientMetadata`.
// 11. Save intercepted `client` document as `updatedClientMetadata`.
sinon
.stub(Connection.prototype, 'command')
.callsFake(async function (ns, cmd, options, responseType) {
// @ts-expect-error: sinon will place wrappedMethod on the command method.
const command = Connection.prototype.command.wrappedMethod.bind(this);
if (cmd.hello || cmd[LEGACY_HELLO_COMMAND]) {
if (!initialClientMetadata) {
initialClientMetadata = cmd.client;
} else {
updatedClientMetadata = cmd.client;
}
}
return command(ns, cmd, options, responseType);
});
await client.db('test').command({ ping: 1 });
// 8. Wait 5ms for the connection to become idle.
await sleep(5);
// 9. Append the following `DriverInfoOptions` to the `MongoClient` metadata:
// | Field | Value |
// | -------- | ---------------- |
// | name | library |
// | version | 1.2 |
// | platform | Library Platform |
client.appendMetadata({
name: 'library',
version: '1.2',
platform: 'Library Platform'
});
// 10. Send a `ping` command to the server and verify that the command succeeds.
await client.db('test').command({ ping: 1 });
// 12. Assert that `clientMetadata` is identical to `updatedClientMetadata`.
expect(updatedClientMetadata).to.deep.equal(initialClientMetadata);
});
});
describe('Test 5: Metadata is not appended if identical to initial metadata', function () {
let initialClientMetadata: ClientMetadata;
let updatedClientMetadata: ClientMetadata;
// TODO(NODE-6599): mongodb-legacy adds additional client metadata, breaking these prose tests
let client: RawMongoClient;
// 1. Create a `MongoClient` instance with the following:
// - `maxIdleTimeMS` set to `1ms`
// - Wrapping library metadata:
// | Field | Value |
// | -------- | ---------------- |
// | name | library |
// | version | 1.2 |
// | platform | Library Platform |
// 2. Send a `ping` command to the server and verify that the command succeeds.
// 3. Save intercepted `client` document as `initialClientMetadata`.
// 4. Wait 5ms for the connection to become idle.
beforeEach(async function () {
// 1. Create a `MongoClient` instance with:
// - `maxIdleTimeMS` set to `1ms`
// - `driverInfo` set to the following:
// | Field | Value |
// | -------- | ---------------- |
// | name | library |
// | version | 1.2 |
// | platform | Library Platform |
client = new RawMongoClient(this.configuration.url(), {
maxIdleTimeMS: 1,
serverApi: this.configuration.serverApi,
driverInfo: { name: 'library', version: '1.2', platform: 'Library Platform' }
});
// 2. Send a `ping` command to the server and verify that the command succeeds.
// 3. Save intercepted `client` document as `clientMetadata`.
sinon
.stub(Connection.prototype, 'command')
.callsFake(async function (ns, cmd, options, responseType) {
// @ts-expect-error: sinon will place wrappedMethod on the command method.
const command = Connection.prototype.command.wrappedMethod.bind(this);
if (cmd.hello || cmd[LEGACY_HELLO_COMMAND]) {
if (!initialClientMetadata) {
initialClientMetadata = cmd.client;
} else {
updatedClientMetadata = cmd.client;
}
}
return command(ns, cmd, options, responseType);
});
await client.db('test').command({ ping: 1 });
// 4. Wait 5ms for the connection to become idle.
await sleep(5);
});
afterEach(async function () {
await client.close();
});
it('does not append the duplicate metadata', async function () {
// 5. Append the following `DriverInfoOptions` to the `MongoClient` metadata:
// | Field | Value |
// | -------- | ---------------- |
// | name | library |
// | version | 1.2 |
// | platform | Library Platform |
client.appendMetadata({ name: 'library', version: '1.2', platform: 'Library Platform' });
// 6. Send a `ping` command to the server and verify that the command succeeds.
// 7. Save intercepted `client` document as `updatedClientMetadata`.
await client.db('test').command({ ping: 1 });
// 8. Assert that `clientMetadata` is identical to `updatedClientMetadata`.
expect(initialClientMetadata).to.deep.equal(updatedClientMetadata);
});
});
describe('Test 6: Metadata is not appended if identical to initial metadata (separated by non-identical metadata)', function () {
let clientMetadata: ClientMetadata;
let updatedClientMetadata: ClientMetadata;
// TODO(NODE-6599): mongodb-legacy adds additional client metadata, breaking these prose tests
let client: RawMongoClient;
afterEach(async function () {
await client.close();
});
it('does not append duplicate metdaata', async function () {
// 1. Create a `MongoClient` instance with:
// - `maxIdleTimeMS` set to `1ms`
// - `driverInfo` set to the following:
// | Field | Value |
// | -------- | ---------------- |
// | name | library |
// | version | 1.2 |
// | platform | Library Platform |
client = new RawMongoClient(this.configuration.url(), {
maxIdleTimeMS: 1,
driverInfo: {
name: 'library',
version: '1.2',
platform: 'Library Platform'
},
serverApi: this.configuration.serverApi
});
// 2. Send a `ping` command to the server and verify that the command succeeds.
await client.db('test').command({ ping: 1 });
// 3. Wait 5ms for the connection to become idle.
await sleep(5);
// 4. Append the following `DriverInfoOptions` to the `MongoClient` metadata:
// | Field | Value |
// | -------- | ------------------ |
// | name | framework |
// | version | 2.0 |
// | platform | Framework Platform |
client.appendMetadata({
name: 'framework',
version: '2.0',
platform: 'Framework Platform'
});
// 5. Send a `ping` command to the server and verify that the command succeeds.
// 6. Save intercepted `client` document as `clientMetadata`.
// 10. Save intercepted `client` document as `updatedClientMetadata`.
sinon
.stub(Connection.prototype, 'command')
.callsFake(async function (ns, cmd, options, responseType) {
// @ts-expect-error: sinon will place wrappedMethod on the command method.
const command = Connection.prototype.command.wrappedMethod.bind(this);
if (cmd.hello || cmd[LEGACY_HELLO_COMMAND]) {
if (!clientMetadata) {
clientMetadata = cmd.client;
} else {
updatedClientMetadata = cmd.client;
}
}
return command(ns, cmd, options, responseType);
});
await client.db('test').command({ ping: 1 });
// 7. Wait 5ms for the connection to become idle.
await sleep(5);
// 8. Append the following `DriverInfoOptions` to the `MongoClient` metadata:
// | Field | Value |
// | -------- | ---------------- |
// | name | library |
// | version | 1.2 |
// | platform | Library Platform |
client.appendMetadata({
name: 'library',
version: '1.2',
platform: 'Library Platform'
});
// 9. Send a `ping` command to the server and verify that the command succeeds.
await client.db('test').command({ ping: 1 });
// 11. Assert that `clientMetadata` is identical to `updatedClientMetadata`.
expect(updatedClientMetadata).to.deep.equal(clientMetadata);
});
});
describe('Test 7: Empty strings are considered unset when appending duplicate metadata', function () {
let initialClientMetadata: ClientMetadata;
let updatedClientMetadata: ClientMetadata;
// TODO(NODE-6599): mongodb-legacy adds additional client metadata, breaking these prose tests
let client: RawMongoClient;
afterEach(async function () {
await client.close();
initialClientMetadata = undefined;
updatedClientMetadata = undefined;
});
const driverInfos: Array<{
initial: DriverInfo;
appended: DriverInfo;
}> = [
{
initial: {
name: undefined,
version: '1.2',
platform: 'Library Platform'
},
appended: {
name: '',
version: '1.2',
platform: 'Library Platform'
}
},
{
initial: {
name: 'library',
version: undefined,
platform: 'Library Platform'
},
appended: {
name: 'library',
version: '',
platform: 'Library Platform'
}
},
{
initial: {
name: 'library',
version: '1.2',
platform: undefined
},
appended: {
name: 'library',
version: '1.2',
platform: ''
}
}
];
for (const [metadata, index] of driverInfos.map((infos, i) => [infos, i] as const)) {
describe(`Test ${index + 1}`, function () {
it('does not appended duplicate metadata', async function () {
// 1. Create a `MongoClient` instance with:
// - `maxIdleTimeMS` set to `1ms`
client = new RawMongoClient(this.configuration.url(), {
maxIdleTimeMS: 1,
serverApi: this.configuration.serverApi
});
// 2. Append the `DriverInfoOptions` from the selected test case from the initial metadata section.
client.appendMetadata(metadata.initial);
// 3. Send a `ping` command to the server and verify that the command succeeds.
// 4. Save intercepted `client` document as `initialClientMetadata`.
// 8. Store the response as `updatedClientMetadata`.
sinon
.stub(Connection.prototype, 'command')
.callsFake(async function (ns, cmd, options, responseType) {
// @ts-expect-error: sinon will place wrappedMethod on the command method.
const command = Connection.prototype.command.wrappedMethod.bind(this);
if (cmd.hello || cmd[LEGACY_HELLO_COMMAND]) {
if (!initialClientMetadata) {
initialClientMetadata = cmd.client;
} else {
updatedClientMetadata = cmd.client;
}
}
return command(ns, cmd, options, responseType);
});
await client.db('test').command({ ping: 1 });
// 5. Wait 5ms for the connection to become idle.
await sleep(5);
// 6. Append the `DriverInfoOptions` from the selected test case from the appended metadata section.
client.appendMetadata(metadata.appended);
// 7. Send a `ping` command to the server and verify the command succeeds.
await client.db('test').command({ ping: 1 });
// 9. Assert that `initialClientMetadata` is identical to `updatedClientMetadata`.
expect(updatedClientMetadata).to.deep.equal(initialClientMetadata);
});
});
}
});
describe('Test 8: Empty strings are considered unset when appending metadata identical to initial metadata', function () {
let initialClientMetadata: ClientMetadata;
let updatedClientMetadata: ClientMetadata;
// TODO(NODE-6599): mongodb-legacy adds additional client metadata, breaking these prose tests
let client: RawMongoClient;
afterEach(async function () {
await client.close();
initialClientMetadata = undefined;
updatedClientMetadata = undefined;
});
const driverInfos: Array<{
initial: DriverInfo;
appended: DriverInfo;
}> = [
{
initial: {
name: undefined,
version: '1.2',
platform: 'Library Platform'
},
appended: {
name: '',
version: '1.2',
platform: 'Library Platform'
}
},
{
initial: {
name: 'library',
version: undefined,
platform: 'Library Platform'
},
appended: {
name: 'library',
version: '',
platform: 'Library Platform'
}
},
{
initial: {
name: 'library',
version: '1.2',
platform: undefined
},
appended: {
name: 'library',
version: '1.2',
platform: ''
}
}
];
for (const [metadata, index] of driverInfos.map((infos, i) => [infos, i] as const)) {
describe(`Test ${index + 1}`, function () {
it('does not appended duplicate metadata', async function () {
// 1. Create a `MongoClient` instance with:
// - `maxIdleTimeMS` set to `1ms`
// - `driverInfo` set to the `DriverInfoOptions` from the selected test case from the initial metadata section.
client = new RawMongoClient(this.configuration.url(), {
maxIdleTimeMS: 1,
serverApi: this.configuration.serverApi,
driverInfo: metadata.initial
});
// 2. Send a `ping` command to the server and verify that the command succeeds.
// 3. Save intercepted `client` document as `initialClientMetadata`.
// 7. Store the response as `updatedClientMetadata`.
sinon
.stub(Connection.prototype, 'command')
.callsFake(async function (ns, cmd, options, responseType) {
// @ts-expect-error: sinon will place wrappedMethod on the command method.
const command = Connection.prototype.command.wrappedMethod.bind(this);
if (cmd.hello || cmd[LEGACY_HELLO_COMMAND]) {
if (!initialClientMetadata) {
initialClientMetadata = cmd.client;
} else {
updatedClientMetadata = cmd.client;
}
}
return command(ns, cmd, options, responseType);
});
await client.db('test').command({ ping: 1 });
// 4. Wait 5ms for the connection to become idle.
await sleep(5);
// 5. Append the `DriverInfoOptions` from the selected test case from the appended metadata section.
client.appendMetadata(metadata.appended);
// 6. Send a `ping` command to the server and verify the command succeeds.
await client.db('test').command({ ping: 1 });
// 8. Assert that `initialClientMetadata` is identical to `updatedClientMetadata`.
expect(updatedClientMetadata).to.deep.equal(initialClientMetadata);
});
});
}
});
});