-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathDBSQLClient.test.ts
More file actions
1057 lines (866 loc) · 40.4 KB
/
DBSQLClient.test.ts
File metadata and controls
1057 lines (866 loc) · 40.4 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
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { expect, AssertionError } from 'chai';
import sinon from 'sinon';
import DBSQLClient, { ThriftLibrary } from '../../lib/DBSQLClient';
import DBSQLSession from '../../lib/DBSQLSession';
import ThriftBackend from '../../lib/thrift-backend/ThriftBackend';
import PlainHttpAuthentication from '../../lib/connection/auth/PlainHttpAuthentication';
import DatabricksOAuth from '../../lib/connection/auth/DatabricksOAuth';
import { DatabricksOAuthManager, AzureOAuthManager } from '../../lib/connection/auth/DatabricksOAuth/OAuthManager';
import HttpConnection from '../../lib/connection/connections/HttpConnection';
import { ConnectionOptions } from '../../lib/contracts/IDBSQLClient';
import IRetryPolicy from '../../lib/connection/contracts/IRetryPolicy';
import IConnectionProvider, { HttpTransactionDetails } from '../../lib/connection/contracts/IConnectionProvider';
import ThriftClientStub from './.stubs/ThriftClientStub';
import IThriftClient from '../../lib/contracts/IThriftClient';
import IAuthentication from '../../lib/connection/contracts/IAuthentication';
import AuthProviderStub from './.stubs/AuthProviderStub';
import ConnectionProviderStub from './.stubs/ConnectionProviderStub';
import { TProtocolVersion } from '../../thrift/TCLIService_types';
import TelemetryClientProvider from '../../lib/telemetry/TelemetryClientProvider';
import FeatureFlagCache from '../../lib/telemetry/FeatureFlagCache';
import TelemetryEventEmitter from '../../lib/telemetry/TelemetryEventEmitter';
import { LogLevel } from '../../lib/contracts/IDBSQLLogger';
const connectOptions = {
host: '127.0.0.1',
port: 80,
path: '',
token: 'dapi********************************',
} satisfies ConnectionOptions;
// Test helper: build a DBSQLClient with `getClient` stubbed to return the given
// ThriftClient stub, and pre-seed `client['backend']` with a ThriftBackend.
// Used to avoid 12 copies of the same 4-line setup across the openSession tests.
function makeStubbedClient(thriftClient: ThriftClientStub = new ThriftClientStub()): {
client: DBSQLClient;
thriftClient: ThriftClientStub;
} {
const client = new DBSQLClient();
sinon.stub(client, 'getClient').returns(Promise.resolve(thriftClient));
client['backend'] = new ThriftBackend({ context: client, onConnectionEvent: () => {} });
return { client, thriftClient };
}
describe('DBSQLClient.connect', () => {
it('should prepend "/" to path if it is missing', async () => {
const client = new DBSQLClient();
const path = 'example/path';
const connectionOptions = client['getConnectionOptions']({ ...connectOptions, path });
expect(connectionOptions.path).to.equal(`/${path}`);
});
it('should not prepend "/" to path if it is already available', async () => {
const client = new DBSQLClient();
const path = '/example/path';
const connectionOptions = client['getConnectionOptions']({ ...connectOptions, path });
expect(connectionOptions.path).to.equal(path);
});
it('should initialize connection state', async () => {
const client = new DBSQLClient();
expect(client['client']).to.be.undefined;
expect(client['authProvider']).to.be.undefined;
expect(client['connectionProvider']).to.be.undefined;
await client.connect(connectOptions);
expect(client['client']).to.be.undefined; // it should not be initialized at this point
expect(client['authProvider']).to.be.instanceOf(PlainHttpAuthentication);
expect(client['connectionProvider']).to.be.instanceOf(HttpConnection);
});
it('should listen for Thrift connection events', async () => {
const client = new DBSQLClient();
const thriftConnectionStub = {
on: sinon.stub(),
};
// This method is private, so we cannot easily `sinon.stub` it.
// But in this case we can just replace it
client['createConnectionProvider'] = () => ({
getThriftConnection: async () => thriftConnectionStub,
getAgent: async () => undefined,
setHeaders: () => {},
getRetryPolicy: (): Promise<IRetryPolicy<HttpTransactionDetails>> => {
throw new Error('Not implemented');
},
});
await client.connect(connectOptions);
expect(thriftConnectionStub.on.called).to.be.true;
});
it('should log a warning when deprecated clientId is passed', async () => {
const client = new DBSQLClient();
const logSpy = sinon.spy((client as any).logger, 'log');
const optionsWithDeprecated = {
...connectOptions,
clientId: 'clientId',
};
await client.connect(optionsWithDeprecated as any);
const warningRegex = /Warning: The "clientId" option is deprecated\. Please use "userAgentEntry" instead\./;
const callFound = logSpy.getCalls().some((call) => warningRegex.test(call.args[1]));
expect(callFound).to.be.true;
logSpy.restore();
});
it('useKernel: true routes to KernelBackend and leaves `backend` unset when connect() throws', async () => {
const client = new DBSQLClient();
// `useKernel` is on a non-exported InternalConnectionOptions; cast through any.
// An empty token makes the real KernelBackend reject during connect() (auth
// validation); where the native binding is absent (e.g. CI, which does not
// build it) construction throws even earlier. Either way connect() must
// reject, so we can assert the partial-init guard leaves `backend` unset.
const kernelOptions = { ...connectOptions, token: '', useKernel: true } as any;
try {
await client.connect(kernelOptions);
expect.fail('KernelBackend connect() should reject (empty PAT / absent native binding)');
} catch (error) {
if (error instanceof AssertionError || !(error instanceof Error)) {
throw error;
}
// The exact message differs by environment (auth rejection vs binding-load
// failure); the contract under test is simply that connect() rejected.
}
// The partial-init guard (L2 fix) means backend stays undefined after a
// failed connect, so the next openSession surfaces "not connected" rather
// than the KernelBackend's own connect/auth error.
expect((client as any).backend).to.equal(undefined);
try {
await client.openSession();
expect.fail('openSession on an unconnected client should throw');
} catch (error) {
if (error instanceof AssertionError || !(error instanceof Error)) {
throw error;
}
expect(error.message).to.match(/not connected/);
}
});
it('populates config.customHeaders with org-id parsed from ?o= (SPOG)', async () => {
const client = new DBSQLClient();
await client.connect({ ...connectOptions, path: '/sql/1.0/warehouses/abc?o=12345678901234' });
expect(client.getConfig().customHeaders).to.deep.equal({ 'x-databricks-org-id': '12345678901234' });
});
it('leaves config.customHeaders undefined when path has no ?o= and none supplied', async () => {
const client = new DBSQLClient();
await client.connect({ ...connectOptions, path: '/sql/1.0/warehouses/abc' });
expect(client.getConfig().customHeaders).to.be.undefined;
});
});
describe('DBSQLClient.openSession', () => {
it('should successfully open session', async () => {
const { client } = makeStubbedClient();
const session = await client.openSession();
expect(session).instanceOf(DBSQLSession);
});
it('should use initial namespace options', async () => {
const { client, thriftClient } = makeStubbedClient();
case1: {
const initialCatalog = 'catalog1';
const session = await client.openSession({ initialCatalog });
expect(session).instanceOf(DBSQLSession);
expect(thriftClient.openSessionReq?.initialNamespace?.catalogName).to.equal(initialCatalog);
expect(thriftClient.openSessionReq?.initialNamespace?.schemaName).to.be.null;
}
case2: {
const initialSchema = 'schema2';
const session = await client.openSession({ initialSchema });
expect(session).instanceOf(DBSQLSession);
expect(thriftClient.openSessionReq?.initialNamespace?.catalogName).to.be.null;
expect(thriftClient.openSessionReq?.initialNamespace?.schemaName).to.equal(initialSchema);
}
case3: {
const initialCatalog = 'catalog3';
const initialSchema = 'schema3';
const session = await client.openSession({ initialCatalog, initialSchema });
expect(session).instanceOf(DBSQLSession);
expect(thriftClient.openSessionReq?.initialNamespace?.catalogName).to.equal(initialCatalog);
expect(thriftClient.openSessionReq?.initialNamespace?.schemaName).to.equal(initialSchema);
}
});
it('should throw an exception when not connected', async () => {
const client = new DBSQLClient();
client['backend'] = undefined;
client['connectionProvider'] = undefined;
try {
await client.openSession();
expect.fail('It should throw an error');
} catch (error) {
if (!(error instanceof Error)) {
throw error;
}
expect(error.message).to.be.eq('DBSQLClient: not connected');
}
});
it('should correctly pass server protocol version to session', async () => {
const { client, thriftClient } = makeStubbedClient();
// Test with default protocol version (SPARK_CLI_SERVICE_PROTOCOL_V8)
{
const session = await client.openSession();
expect(session).instanceOf(DBSQLSession);
expect(((session as DBSQLSession)['backend'] as any)['serverProtocolVersion']).to.equal(
TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V8,
);
}
{
thriftClient.openSessionResp = {
...thriftClient.openSessionResp,
serverProtocolVersion: TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V7,
};
const session = await client.openSession();
expect(session).instanceOf(DBSQLSession);
expect(((session as DBSQLSession)['backend'] as any)['serverProtocolVersion']).to.equal(
TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V7,
);
}
});
it('should pass session configuration to OpenSessionReq', async () => {
const { client, thriftClient } = makeStubbedClient();
const configuration = { QUERY_TAGS: 'team:engineering', ansi_mode: 'true' };
await client.openSession({ configuration });
expect(thriftClient.openSessionReq?.configuration).to.deep.equal(configuration);
});
it('should affect session behavior based on protocol version', async () => {
const { client, thriftClient } = makeStubbedClient();
// With protocol version V6 - should support async metadata operations
{
thriftClient.openSessionResp = {
...thriftClient.openSessionResp,
serverProtocolVersion: TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V6,
};
const session = await client.openSession();
expect(session).instanceOf(DBSQLSession);
// Spy on driver.getTypeInfo to check if runAsync is set
const driver = await client.getDriver();
const getTypeInfoSpy = sinon.spy(driver, 'getTypeInfo');
await session.getTypeInfo();
expect(getTypeInfoSpy.calledOnce).to.be.true;
expect(getTypeInfoSpy.firstCall.args[0].runAsync).to.be.true;
getTypeInfoSpy.restore();
}
// With protocol version V5 - should NOT support async metadata operations
{
thriftClient.openSessionResp = {
...thriftClient.openSessionResp,
serverProtocolVersion: TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V5,
};
const session = await client.openSession();
expect(session).instanceOf(DBSQLSession);
// Spy on driver.getTypeInfo to check if runAsync is undefined
const driver = await client.getDriver();
const getTypeInfoSpy = sinon.spy(driver, 'getTypeInfo');
await session.getTypeInfo();
expect(getTypeInfoSpy.calledOnce).to.be.true;
expect(getTypeInfoSpy.firstCall.args[0].runAsync).to.be.undefined;
getTypeInfoSpy.restore();
}
});
});
describe('DBSQLClient.getClient', () => {
it('should throw an error if not connected', async () => {
const client = new DBSQLClient();
try {
await client.getClient();
expect.fail('It should throw an error');
} catch (error) {
if (error instanceof AssertionError || !(error instanceof Error)) {
throw error;
}
expect(error.message).to.contain('DBSQLClient: not connected');
}
});
it('should create client if was not initialized yet', async () => {
const client = new DBSQLClient();
const thriftClient = new ThriftClientStub();
const createThriftClient = sinon.stub().returns(thriftClient);
client['authProvider'] = new AuthProviderStub();
client['connectionProvider'] = new ConnectionProviderStub();
client['thrift'] = {
createClient: createThriftClient,
};
const result = await client.getClient();
expect(createThriftClient.called).to.be.true;
expect(result).to.be.equal(thriftClient);
});
it('should update auth credentials each time when client is requested', async () => {
const client = new DBSQLClient();
const thriftClient = new ThriftClientStub();
const createThriftClient = sinon.stub().returns(thriftClient);
const authProvider = sinon.spy(new AuthProviderStub());
const connectionProvider = sinon.spy(new ConnectionProviderStub());
client['connectionProvider'] = connectionProvider;
client['thrift'] = {
createClient: createThriftClient,
};
// just a sanity check - authProvider should not be initialized until `getClient()` call
expect(client['authProvider']).to.be.undefined;
expect(connectionProvider.setHeaders.callCount).to.be.equal(0);
await client.getClient();
expect(authProvider.authenticate.callCount).to.be.equal(0);
expect(connectionProvider.setHeaders.callCount).to.be.equal(0);
client['authProvider'] = authProvider;
// initialize client
firstCall: {
const result = await client.getClient();
expect(createThriftClient.callCount).to.be.equal(1);
expect(connectionProvider.setHeaders.callCount).to.be.equal(1);
expect(result).to.be.equal(thriftClient);
}
// credentials stay the same, client should not be re-created
secondCall: {
const result = await client.getClient();
expect(createThriftClient.callCount).to.be.equal(1);
expect(connectionProvider.setHeaders.callCount).to.be.equal(2);
expect(result).to.be.equal(thriftClient);
}
// change credentials stub - client should be re-created
thirdCall: {
authProvider.headers = { test: 'test' };
const result = await client.getClient();
expect(createThriftClient.callCount).to.be.equal(1);
expect(connectionProvider.setHeaders.callCount).to.be.equal(3);
expect(result).to.be.equal(thriftClient);
}
});
});
describe('DBSQLClient.close', () => {
it('should close the connection if it was initiated', async () => {
const client = new DBSQLClient();
client['client'] = new ThriftClientStub();
client['connectionProvider'] = new ConnectionProviderStub();
client['authProvider'] = new AuthProviderStub();
await client.close();
expect(client['client']).to.be.undefined;
expect(client['connectionProvider']).to.be.undefined;
expect(client['authProvider']).to.be.undefined;
});
it('should do nothing if the connection does not exist', async () => {
const client = new DBSQLClient();
expect(client['client']).to.be.undefined;
expect(client['connectionProvider']).to.be.undefined;
expect(client['authProvider']).to.be.undefined;
await client.close();
expect(client['client']).to.be.undefined;
expect(client['connectionProvider']).to.be.undefined;
expect(client['authProvider']).to.be.undefined;
});
it('should close sessions that belong to it', async () => {
const client = new DBSQLClient();
const thriftClient = sinon.spy(new ThriftClientStub());
client['client'] = thriftClient;
client['connectionProvider'] = new ConnectionProviderStub();
client['authProvider'] = new AuthProviderStub();
client['backend'] = new ThriftBackend({ context: client, onConnectionEvent: () => {} });
const session = await client.openSession();
if (!(session instanceof DBSQLSession)) {
throw new Error('Assertion error: expected session to be DBSQLSession');
}
expect(session.onClose).to.be.not.undefined;
expect(session['isOpen']).to.be.true;
expect(client['sessions']['items'].size).to.eq(1);
const closeAllSessionsSpy = sinon.spy(client['sessions'], 'closeAll');
const sessionCloseSpy = sinon.spy(session, 'close');
await client.close();
expect(closeAllSessionsSpy.called).to.be.true;
expect(sessionCloseSpy.called).to.be.true;
expect(session.onClose).to.be.undefined;
expect(session['isOpen']).to.be.false;
expect(client['sessions']['items'].size).to.eq(0);
expect(thriftClient.CloseSession.called).to.be.true;
});
});
describe('DBSQLClient.createAuthProvider', () => {
it('should use access token auth method', () => {
const client = new DBSQLClient();
const testAccessToken = 'token';
const provider = client['createAuthProvider']({
...connectOptions,
authType: 'access-token',
token: testAccessToken,
});
expect(provider).to.be.instanceOf(PlainHttpAuthentication);
if (!(provider instanceof PlainHttpAuthentication)) {
throw new Error('Assertion error: expected provider to be PlainHttpAuthentication');
}
expect(provider['password']).to.be.equal(testAccessToken);
});
it('should use access token auth method by default (compatibility)', () => {
const client = new DBSQLClient();
const testAccessToken = 'token';
const provider = client['createAuthProvider']({
...connectOptions,
// note: no `authType` provided
token: testAccessToken,
});
expect(provider).to.be.instanceOf(PlainHttpAuthentication);
if (!(provider instanceof PlainHttpAuthentication)) {
throw new Error('Assertion error: expected provider to be PlainHttpAuthentication');
}
expect(provider['password']).to.be.equal(testAccessToken);
});
it('should use Databricks OAuth method (AWS)', () => {
const client = new DBSQLClient();
const provider = client['createAuthProvider']({
...connectOptions,
authType: 'databricks-oauth',
// host is used when creating OAuth manager, so make it look like a real AWS instance
host: 'example.dev.databricks.com',
oauthClientSecret: 'test-secret',
});
expect(provider).to.be.instanceOf(DatabricksOAuth);
if (!(provider instanceof DatabricksOAuth)) {
throw new Error('Assertion error: expected provider to be DatabricksOAuth');
}
expect(provider['getManager']()).to.be.instanceOf(DatabricksOAuthManager);
});
it('should use Databricks OAuth method (Azure)', () => {
const client = new DBSQLClient();
const provider = client['createAuthProvider']({
...connectOptions,
authType: 'databricks-oauth',
// host is used when creating OAuth manager, so make it look like a real Azure instance
host: 'example.databricks.azure.us',
});
expect(provider).to.be.instanceOf(DatabricksOAuth);
if (!(provider instanceof DatabricksOAuth)) {
throw new Error('Assertion error: expected provider to be DatabricksOAuth');
}
expect(provider['getManager']()).to.be.instanceOf(AzureOAuthManager);
});
it('should use Databricks OAuth method (GCP)', () => {
const client = new DBSQLClient();
const provider = client['createAuthProvider']({
...connectOptions,
authType: 'databricks-oauth',
// host is used when creating OAuth manager, so make it look like a real AWS instance
host: 'example.gcp.databricks.com',
});
expect(provider).to.be.instanceOf(DatabricksOAuth);
if (!(provider instanceof DatabricksOAuth)) {
throw new Error('Assertion error: expected provider to be DatabricksOAuth');
}
expect(provider['getManager']()).to.be.instanceOf(DatabricksOAuthManager);
});
it('should use Databricks InHouse OAuth method (Azure)', () => {
const client = new DBSQLClient();
// When `useDatabricksOAuthInAzure = true`, it should use Databricks OAuth method
// only for supported Azure hosts, and fail for others
case1: {
const provider = client['createAuthProvider']({
...connectOptions,
authType: 'databricks-oauth',
// host is used when creating OAuth manager, so make it look like a real Azure instance
host: 'example.azuredatabricks.net',
useDatabricksOAuthInAzure: true,
});
expect(provider).to.be.instanceOf(DatabricksOAuth);
if (!(provider instanceof DatabricksOAuth)) {
throw new Error('Assertion error: expected provider to be DatabricksOAuth');
}
expect(provider['getManager']()).to.be.instanceOf(DatabricksOAuthManager);
}
case2: {
expect(() => {
const provider = client['createAuthProvider']({
...connectOptions,
authType: 'databricks-oauth',
// host is used when creating OAuth manager, so make it look like a real Azure instance
host: 'example.databricks.azure.us',
useDatabricksOAuthInAzure: true,
});
if (!(provider instanceof DatabricksOAuth)) {
throw new Error('Expected `provider` to be `DatabricksOAuth`');
}
provider['getManager'](); // just call the method
}).to.throw();
}
});
it('should throw error when OAuth not supported for host', () => {
const client = new DBSQLClient();
expect(() => {
const provider = client['createAuthProvider']({
...connectOptions,
authType: 'databricks-oauth',
// use host which is not supported for sure
host: 'example.com',
});
if (!(provider instanceof DatabricksOAuth)) {
throw new Error('Expected `provider` to be `DatabricksOAuth`');
}
provider['getManager'](); // just call the method
}).to.throw();
});
it('should use custom auth method', () => {
const client = new DBSQLClient();
const customProvider = {
authenticate: () => Promise.resolve({}),
};
const provider = client['createAuthProvider']({
...connectOptions,
authType: 'custom',
provider: customProvider,
});
expect(provider).to.be.equal(customProvider);
});
it('should use custom auth method (legacy way)', () => {
const client = new DBSQLClient();
const customProvider = {
authenticate: () => Promise.resolve({}),
};
const provider = client['createAuthProvider'](
// custom provider from second arg should be used no matter what's specified in config
{ ...connectOptions, authType: 'access-token', token: 'token' },
customProvider,
);
expect(provider).to.be.equal(customProvider);
});
});
describe('DBSQLClient retry-policy ConnectionOptions', () => {
it('ingests retry-policy options from connect() into ClientConfig', async () => {
const client = new DBSQLClient();
// Defaults before connect.
expect(client.getConfig().retryMaxAttempts).to.equal(5);
expect(client.getConfig().retriesTimeout).to.equal(15 * 60 * 1000);
expect(client.getConfig().retryDelayMin).to.equal(1000);
expect(client.getConfig().retryDelayMax).to.equal(60 * 1000);
await client.connect({
...connectOptions,
retryMaxAttempts: 2,
retriesTimeout: 5000,
retryDelayMin: 100,
retryDelayMax: 500,
});
expect(client.getConfig().retryMaxAttempts).to.equal(2);
expect(client.getConfig().retriesTimeout).to.equal(5000);
expect(client.getConfig().retryDelayMin).to.equal(100);
expect(client.getConfig().retryDelayMax).to.equal(500);
});
it('keeps defaults when retry-policy options are omitted', async () => {
const client = new DBSQLClient();
await client.connect({ ...connectOptions });
expect(client.getConfig().retryMaxAttempts).to.equal(5);
expect(client.getConfig().retriesTimeout).to.equal(15 * 60 * 1000);
expect(client.getConfig().retryDelayMin).to.equal(1000);
expect(client.getConfig().retryDelayMax).to.equal(60 * 1000);
});
});
describe('DBSQLClient.enableMetricViewMetadata', () => {
it('should store enableMetricViewMetadata config when enabled', async () => {
const client = new DBSQLClient();
expect(client.getConfig().enableMetricViewMetadata).to.be.undefined;
await client.connect({ ...connectOptions, enableMetricViewMetadata: true });
expect(client.getConfig().enableMetricViewMetadata).to.be.true;
});
it('should not store enableMetricViewMetadata config when disabled', async () => {
const client = new DBSQLClient();
expect(client.getConfig().enableMetricViewMetadata).to.be.undefined;
await client.connect({ ...connectOptions, enableMetricViewMetadata: false });
expect(client.getConfig().enableMetricViewMetadata).to.be.false;
});
it('should inject session parameter when enableMetricViewMetadata is true', async () => {
const { client, thriftClient } = makeStubbedClient();
await client.connect({ ...connectOptions, enableMetricViewMetadata: true });
await client.openSession();
expect(thriftClient.openSessionReq?.configuration).to.have.property(
'spark.sql.thriftserver.metadata.metricview.enabled',
'true',
);
});
it('should not inject session parameter when enableMetricViewMetadata is false', async () => {
const { client, thriftClient } = makeStubbedClient();
await client.connect({ ...connectOptions, enableMetricViewMetadata: false });
await client.openSession();
expect(thriftClient.openSessionReq?.configuration).to.not.have.property(
'spark.sql.thriftserver.metadata.metricview.enabled',
);
});
it('should not inject session parameter when enableMetricViewMetadata is not set', async () => {
const { client, thriftClient } = makeStubbedClient();
await client.connect(connectOptions);
await client.openSession();
expect(thriftClient.openSessionReq?.configuration).to.not.have.property(
'spark.sql.thriftserver.metadata.metricview.enabled',
);
});
it('should preserve user-provided session configuration', async () => {
const { client, thriftClient } = makeStubbedClient();
await client.connect({ ...connectOptions, enableMetricViewMetadata: true });
const userConfig = { QUERY_TAGS: 'team:engineering', ansi_mode: 'true' };
await client.openSession({ configuration: userConfig });
expect(thriftClient.openSessionReq?.configuration).to.deep.equal({
...userConfig,
'spark.sql.thriftserver.metadata.metricview.enabled': 'true',
});
});
it('should serialize queryTags dict and set in session configuration', async () => {
const { client, thriftClient } = makeStubbedClient();
await client.openSession({
queryTags: { team: 'data-eng', project: 'etl' },
});
expect(thriftClient.openSessionReq?.configuration).to.deep.equal({
QUERY_TAGS: 'team:data-eng,project:etl',
});
});
it('should let queryTags take precedence over configuration.QUERY_TAGS', async () => {
const { client, thriftClient } = makeStubbedClient();
await client.openSession({
queryTags: { team: 'new-team' },
configuration: { QUERY_TAGS: 'team:old-team,other:value', ansi_mode: 'true' },
});
expect(thriftClient.openSessionReq?.configuration).to.deep.equal({
QUERY_TAGS: 'team:new-team',
ansi_mode: 'true',
});
});
it('should remove QUERY_TAGS from configuration when queryTags is empty', async () => {
const { client, thriftClient } = makeStubbedClient();
await client.openSession({
queryTags: {},
configuration: { QUERY_TAGS: 'team:old-team', ansi_mode: 'true' },
});
expect(thriftClient.openSessionReq?.configuration).to.deep.equal({
ansi_mode: 'true',
});
});
});
describe('DBSQLClient telemetry paths', () => {
// Reset the process-wide singleton between tests so refcount + cached
// feature flags from one test don't leak into the next. Mirrors the e2e
// suite's afterEach pattern but scoped to unit-level state.
afterEach(() => {
TelemetryClientProvider.__resetInstanceForTests();
sinon.restore();
});
describe('DATABRICKS_TELEMETRY_DISABLED env-var parser', () => {
let savedEnv: string | undefined;
beforeEach(() => {
savedEnv = process.env.DATABRICKS_TELEMETRY_DISABLED;
});
afterEach(() => {
if (savedEnv === undefined) {
delete process.env.DATABRICKS_TELEMETRY_DISABLED;
} else {
process.env.DATABRICKS_TELEMETRY_DISABLED = savedEnv;
}
});
const recognizedTruthy = ['1', 'true', 'TRUE', 'yes', 'YES', 'on', 'ON', ' true ', 'On'];
const unrecognized = ['0', 'false', 'no', 'off', 'False', 'OFF'];
for (const val of recognizedTruthy) {
it(`DISABLES telemetry when value is '${val}'`, async () => {
process.env.DATABRICKS_TELEMETRY_DISABLED = val;
const client = new DBSQLClient();
// Stub out initializeTelemetry so we can detect it not being called.
const initSpy = sinon.spy(client as any, 'initializeTelemetry');
await client.connect(connectOptions);
expect(initSpy.callCount).to.equal(0);
});
}
for (const val of unrecognized) {
it(`WARNS and does NOT disable when value is '${val}'`, async () => {
process.env.DATABRICKS_TELEMETRY_DISABLED = val;
const client = new DBSQLClient();
const logger = (client as any).logger as { log: (l: LogLevel, m: string) => void };
const logSpy = sinon.spy(logger, 'log');
// Suppress the actual telemetry init network calls by stubbing.
sinon.stub(client as any, 'initializeTelemetry').resolves();
await client.connect(connectOptions);
const warnCalls = logSpy
.getCalls()
.filter((c) => c.args[0] === LogLevel.warn && /DATABRICKS_TELEMETRY_DISABLED/.test(c.args[1] as string));
expect(warnCalls.length, 'expected a warn-level log about ignored env value').to.be.at.least(1);
});
}
it('treats empty / unset env var as no-op (no warn)', async () => {
delete process.env.DATABRICKS_TELEMETRY_DISABLED;
const client = new DBSQLClient();
const logger = (client as any).logger as { log: (l: LogLevel, m: string) => void };
const logSpy = sinon.spy(logger, 'log');
sinon.stub(client as any, 'initializeTelemetry').resolves();
await client.connect(connectOptions);
const warnCalls = logSpy
.getCalls()
.filter((c) => c.args[0] === LogLevel.warn && /DATABRICKS_TELEMETRY_DISABLED/.test(c.args[1] as string));
expect(warnCalls.length).to.equal(0);
});
});
describe('extractWorkspaceId', () => {
it('returns the numeric o= param from httpPath', () => {
const id = (DBSQLClient as any).extractWorkspaceId('/sql/1.0/warehouses/abc?o=12345678901234');
expect(id).to.equal('12345678901234');
});
it('returns undefined when no query string', () => {
expect((DBSQLClient as any).extractWorkspaceId('/sql/1.0/warehouses/abc')).to.be.undefined;
});
it('returns undefined when o= is not numeric', () => {
expect((DBSQLClient as any).extractWorkspaceId('/sql/1.0/warehouses/abc?o=tenant_xyz')).to.be.undefined;
});
it('handles o= as a non-first param', () => {
expect((DBSQLClient as any).extractWorkspaceId('/sql/1.0/warehouses/abc?foo=bar&o=42&baz=qux')).to.equal('42');
});
it('returns undefined when httpPath is unset', () => {
expect((DBSQLClient as any).extractWorkspaceId(undefined)).to.be.undefined;
});
it('returns the numeric workspace id from all-purpose cluster path form', () => {
expect((DBSQLClient as any).extractWorkspaceId('sql/protocolv1/o/99999999999999/0101-000000-aaaaaaaa')).to.equal(
'99999999999999',
);
});
it('returns the numeric workspace id from all-purpose cluster path with leading slash', () => {
expect((DBSQLClient as any).extractWorkspaceId('/sql/protocolv1/o/12345/0101-000000-aaaaaaaa')).to.equal('12345');
});
it('returns undefined when all-purpose cluster path has non-numeric workspace segment', () => {
expect((DBSQLClient as any).extractWorkspaceId('sql/protocolv1/o/tenant_xyz/0101-000000-aaaaaaaa')).to.be
.undefined;
});
it('prefers ?o= query form over /o/ path form when both are present', () => {
expect((DBSQLClient as any).extractWorkspaceId('sql/protocolv1/o/111/cluster?o=222')).to.equal('222');
});
});
describe('buildCustomHeaders (SPOG)', () => {
it('injects x-databricks-org-id from ?o= in httpPath', () => {
const client = new DBSQLClient();
const headers = (client as any).buildCustomHeaders('/sql/1.0/warehouses/abc?o=12345678901234', undefined);
expect(headers).to.deep.equal({ 'x-databricks-org-id': '12345678901234' });
});
it('returns undefined when no ?o= and no user-supplied customHeaders', () => {
const client = new DBSQLClient();
const headers = (client as any).buildCustomHeaders('/sql/1.0/warehouses/abc', undefined);
expect(headers).to.be.undefined;
});
it('preserves user-supplied customHeaders alongside parsed org-id', () => {
const client = new DBSQLClient();
const headers = (client as any).buildCustomHeaders('/sql/1.0/warehouses/abc?o=42', { 'x-trace-id': 'tid-001' });
expect(headers).to.deep.equal({ 'x-trace-id': 'tid-001', 'x-databricks-org-id': '42' });
});
it('user-supplied x-databricks-org-id wins over ?o= parsed value (case-insensitive)', () => {
const client = new DBSQLClient();
const headers = (client as any).buildCustomHeaders('/sql/1.0/warehouses/abc?o=42', {
'X-Databricks-Org-Id': '999',
});
expect(headers).to.deep.equal({ 'X-Databricks-Org-Id': '999' });
});
it('does not inject org-id when ?o= value is non-numeric', () => {
const client = new DBSQLClient();
const headers = (client as any).buildCustomHeaders('/sql/1.0/warehouses/abc?o=tenant_xyz', undefined);
expect(headers).to.be.undefined;
});
it('injects x-databricks-org-id from all-purpose cluster path form', () => {
const client = new DBSQLClient();
const headers = (client as any).buildCustomHeaders(
'sql/protocolv1/o/99999999999999/0101-000000-aaaaaaaa',
undefined,
);
expect(headers).to.deep.equal({ 'x-databricks-org-id': '99999999999999' });
});
it('logs a warning when workspace ID segment is non-numeric (path form)', () => {
const client = new DBSQLClient();
const logSpy = sinon.spy((client as any).logger, 'log');
try {
(client as any).buildCustomHeaders('sql/protocolv1/o/tenant_xyz/cluster', undefined);
const warnCalls = logSpy.getCalls().filter((c) => c.args[0] === LogLevel.warn);
expect(warnCalls).to.have.lengthOf(1);
expect(warnCalls[0].args[1]).to.match(/non-numeric workspace ID/);
} finally {
logSpy.restore();
}
});
it('logs a warning when ?o= is present but non-numeric', () => {
const client = new DBSQLClient();
const logSpy = sinon.spy((client as any).logger, 'log');
try {
(client as any).buildCustomHeaders('/sql/1.0/warehouses/abc?o=tenant_xyz', undefined);
const warnCalls = logSpy.getCalls().filter((c) => c.args[0] === LogLevel.warn);
expect(warnCalls).to.have.lengthOf(1);
expect(warnCalls[0].args[1]).to.match(/non-numeric workspace ID/);
} finally {
logSpy.restore();
}
});
it('logs a debug line when injecting org-id from httpPath', () => {
const client = new DBSQLClient();
const logSpy = sinon.spy((client as any).logger, 'log');
try {
(client as any).buildCustomHeaders('/sql/1.0/warehouses/abc?o=42', undefined);
const injectLog = logSpy
.getCalls()
.find((c) => c.args[0] === LogLevel.debug && /injecting x-databricks-org-id=42/.test(String(c.args[1])));
expect(injectLog, 'expected SPOG inject debug log').to.exist;
} finally {
logSpy.restore();
}
});
it('logs a debug line when caller supplies x-databricks-org-id', () => {
const client = new DBSQLClient();
const logSpy = sinon.spy((client as any).logger, 'log');
try {
(client as any).buildCustomHeaders('/sql/1.0/warehouses/abc?o=42', { 'x-databricks-org-id': '999' });
const callerLog = logSpy
.getCalls()
.find((c) => c.args[0] === LogLevel.debug && /supplied by caller/.test(String(c.args[1])));
expect(callerLog, 'expected SPOG caller-supplied debug log').to.exist;
} finally {
logSpy.restore();
}
});
});
describe('telemetry refcount on reconnect', () => {
it('releases the prior refcount when connect() is called twice', async () => {
const client = new DBSQLClient();
// Stub out feature-flag fetch to return true so the telemetry path runs.
sinon.stub(FeatureFlagCache.prototype, 'isTelemetryEnabled').resolves(true);
const releaseSpy = sinon.spy(TelemetryClientProvider.prototype, 'releaseClient');
await client.connect(connectOptions);
// Sanity: refcount on host should be 1 after first connect.
expect(TelemetryClientProvider.getInstance().getRefCount(connectOptions.host)).to.equal(1);
// Second connect to a different host should release the prior refcount.
await client.connect({ ...connectOptions, host: '127.0.0.2' });
expect(releaseSpy.called, 'releaseClient should fire on reconnect').to.be.true;
// The old host should have decremented to 0 (closed and removed).
expect(TelemetryClientProvider.getInstance().getRefCount(connectOptions.host)).to.equal(0);
await client.close();
});
});
describe('telemetry refcount release path on init failure', () => {
it('releases refcount when feature flag fetch throws', async () => {
const client = new DBSQLClient();
sinon.stub(FeatureFlagCache.prototype, 'isTelemetryEnabled').rejects(new Error('boom'));
const releaseSpy = sinon.spy(TelemetryClientProvider.prototype, 'releaseClient');
await client.connect(connectOptions);