-
Notifications
You must be signed in to change notification settings - Fork 59
/
instance.ts
1840 lines (1684 loc) · 59.2 KB
/
instance.ts
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
// Copyright 2016 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as promisify from '@google-cloud/promisify';
import * as assert from 'assert';
import {before, beforeEach, afterEach, describe, it} from 'mocha';
import * as sinon from 'sinon';
import * as proxyquire from 'proxyquire';
import {ServiceError} from 'google-gax';
import * as inst from '../src/instance';
import {AppProfile, AppProfileOptions} from '../src/app-profile';
import {Cluster, CreateClusterOptions} from '../src/cluster';
import {Family} from '../src/family';
import {
Policy,
Table,
GetIamPolicyOptions,
GetTablesOptions,
} from '../src/table';
import {Bigtable, RequestOptions} from '../src';
import {PassThrough} from 'stream';
import * as pumpify from 'pumpify';
const sandbox = sinon.createSandbox();
let promisified = false;
const fakePromisify = Object.assign({}, promisify, {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
promisifyAll(klass: Function, options: any) {
if (klass.name !== 'Instance') {
return;
}
promisified = true;
assert.deepStrictEqual(options.exclude, [
'appProfile',
'cluster',
'table',
'getBackupsStream',
'getTablesStream',
'getAppProfilesStream',
]);
},
});
class FakeAppProfile extends AppProfile {
calledWith_: Array<{}>;
constructor(...args: [inst.Instance, string]) {
super(args[0], args[1]);
this.calledWith_ = args;
}
}
class FakeBackup {}
class FakeCluster extends Cluster {
calledWith_: Array<{}>;
constructor(...args: [inst.Instance, string]) {
super(args[0], args[1]);
this.calledWith_ = args;
}
}
class FakeFamily extends Family {
calledWith_: Array<{}>;
constructor(...args: [Table, string]) {
super(args[0], args[1]);
this.calledWith_ = args;
}
}
class FakeTable extends Table {
calledWith_: Array<{}>;
VIEWS?: {[index: string]: number};
constructor(...args: [inst.Instance, string]) {
super(args[0], args[1]);
this.calledWith_ = args;
}
}
describe('Bigtable/Instance', () => {
const INSTANCE_ID = 'my-instance';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const BIGTABLE = {
projectName: 'projects/my-project',
projectId: 'my-project',
request: () => {},
} as Bigtable;
const INSTANCE_NAME = `${BIGTABLE.projectName}/instances/${INSTANCE_ID}`;
const APP_PROFILE_ID = 'my-app-profile';
const CLUSTER_ID = 'my-cluster';
let Instance: typeof inst.Instance;
let instance: inst.Instance;
before(() => {
Instance = proxyquire('../src/instance.js', {
'@google-cloud/promisify': fakePromisify,
'./app-profile.js': {AppProfile: FakeAppProfile},
'./backup.js': {Backup: FakeBackup},
'./cluster.js': {Cluster: FakeCluster},
'./family.js': {Family: FakeFamily},
'./table.js': {Table: FakeTable},
pumpify,
}).Instance;
});
beforeEach(() => {
instance = new Instance(BIGTABLE, INSTANCE_ID);
});
afterEach(() => sandbox.restore());
describe('instantiation', () => {
it('should promisify all the things', () => {
assert(promisified);
});
it('should localize Bigtable instance', () => {
assert.strictEqual(instance.bigtable, BIGTABLE);
});
it('should create full ID from name', () => {
assert.strictEqual(instance.name, INSTANCE_NAME);
});
it('should localize name', () => {
assert.strictEqual(instance.id, INSTANCE_ID);
});
it('should leave full instance name unaltered and localize the id from the name', () => {
const instance = new Instance(BIGTABLE, INSTANCE_NAME);
assert.strictEqual(instance.name, INSTANCE_NAME);
assert.strictEqual(instance.id, INSTANCE_ID);
});
it('should throw if instance id in wrong format', () => {
const id = `instances/${INSTANCE_ID}`;
assert.throws(() => {
new Instance(BIGTABLE, id);
}, Error);
});
});
describe('getTypeType_', () => {
const types = {
unspecified: 0,
production: 1,
development: 2,
} as {[index: string]: number};
it('should default to unspecified', () => {
assert.strictEqual(Instance.getTypeType_(), types.unspecified);
assert.strictEqual(
Instance.getTypeType_('not-real-type'),
types.unspecified
);
});
it('should lowercase a type', () => {
assert.strictEqual(Instance.getTypeType_('PRODUCTION'), types.production);
});
Object.keys(types).forEach(type => {
it(`should get the storage type for "${type}"`, () => {
assert.strictEqual(Instance.getTypeType_(type), types[type]);
});
});
});
describe('create', () => {
it('should call createInstance from bigtable', done => {
const options = {} as inst.InstanceOptions;
(instance.bigtable.createInstance as Function) = (
id: string,
options_: {},
callback: Function
) => {
assert.strictEqual(id, instance.id);
assert.strictEqual(options_, options);
callback(); // done()
};
instance.create(options, done);
});
});
describe('createAppProfile', () => {
it('should provide the proper request options', done => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.strictEqual(config.client, 'BigtableInstanceAdminClient');
assert.strictEqual(config.method, 'createAppProfile');
assert.strictEqual(config.reqOpts.parent, INSTANCE_NAME);
assert.strictEqual(config.reqOpts.appProfileId, APP_PROFILE_ID);
assert.strictEqual(config.gaxOpts, undefined);
done();
};
instance.createAppProfile(
APP_PROFILE_ID,
{routing: 'any'},
assert.ifError
);
});
it('should throw if the routing option is not provided', () => {
assert.throws(
instance.createAppProfile.bind(null, APP_PROFILE_ID, assert.ifError),
/An app profile must contain a routing policy\./
);
});
it('should accept gaxOptions', done => {
const options = {
routing: 'any',
gaxOptions: {},
} as AppProfileOptions;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.strictEqual(config.gaxOpts, options.gaxOptions);
done();
};
instance.createAppProfile(APP_PROFILE_ID, options, assert.ifError);
});
describe('should respect the routing option with', () => {
const cluster = new FakeCluster({} as inst.Instance, CLUSTER_ID);
it("an 'any' value", done => {
const options = {
routing: 'any',
} as AppProfileOptions;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.deepStrictEqual(
config.reqOpts.appProfile.multiClusterRoutingUseAny,
{}
);
done();
};
instance.createAppProfile(APP_PROFILE_ID, options, assert.ifError);
});
it('a cluster value', done => {
const options = {routing: cluster};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.deepStrictEqual(
config.reqOpts.appProfile.singleClusterRouting,
{clusterId: CLUSTER_ID}
);
done();
};
instance.createAppProfile(APP_PROFILE_ID, options, assert.ifError);
});
});
it('should respect the allowTransactionalWrites option', done => {
const cluster = instance.cluster(CLUSTER_ID);
const options = {
routing: cluster,
allowTransactionalWrites: true,
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.deepStrictEqual(
config.reqOpts.appProfile.singleClusterRouting
.allowTransactionalWrites,
true
);
done();
};
instance.createAppProfile(APP_PROFILE_ID, options, assert.ifError);
});
it('should respect the description option', done => {
const options = {
routing: 'any',
description: 'My App Profile',
} as AppProfileOptions;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.deepStrictEqual(
config.reqOpts.appProfile.description,
options.description
);
done();
};
instance.createAppProfile(APP_PROFILE_ID, options, assert.ifError);
});
it('should respect the ignoreWarnings option', done => {
const options = {
routing: 'any',
ignoreWarnings: true,
} as AppProfileOptions;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.deepStrictEqual(config.reqOpts.ignoreWarnings, true);
done();
};
instance.createAppProfile(APP_PROFILE_ID, options, assert.ifError);
});
it('should execute callback with arguments from GAPIC', done => {
const response = {};
sandbox
.stub(instance.bigtable, 'request')
.callsArgWith(1, null, response);
const fakeAppProfile = {};
(instance.appProfile as Function) = (id: string) => {
assert.strictEqual(id, APP_PROFILE_ID);
return fakeAppProfile;
};
instance.createAppProfile(
APP_PROFILE_ID,
{routing: 'any'},
(err, appProfile, apiResponse) => {
assert.ifError(err);
assert.strictEqual(appProfile, fakeAppProfile);
assert.strictEqual(apiResponse, response);
done();
}
);
});
});
describe('createCluster', () => {
it('should provide the proper request options', done => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.strictEqual(config.client, 'BigtableInstanceAdminClient');
assert.strictEqual(config.method, 'createCluster');
assert.strictEqual(config.reqOpts.parent, INSTANCE_NAME);
assert.strictEqual(config.reqOpts.clusterId, CLUSTER_ID);
assert.strictEqual(config.gaxOpts, undefined);
done();
};
instance.createCluster(CLUSTER_ID, assert.ifError);
});
it('should accept gaxOptions', done => {
const options = {
gaxOptions: {},
} as CreateClusterOptions;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.strictEqual(config.gaxOpts, options.gaxOptions);
done();
};
instance.createCluster(CLUSTER_ID, options, assert.ifError);
});
it('should respect the location option', done => {
const options = {
location: 'us-central1-b',
} as CreateClusterOptions;
const fakeLocation = 'a/b/c/d';
sandbox
.stub(FakeCluster, 'getLocation_')
.callsFake((project, location) => {
assert.strictEqual(project, BIGTABLE.projectId);
assert.strictEqual(location, options.location);
return fakeLocation;
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.strictEqual(config.reqOpts.cluster.location, fakeLocation);
done();
};
instance.createCluster(CLUSTER_ID, options, assert.ifError);
});
it('should respect the nodes option', done => {
const options = {
nodes: 3,
location: 'us-central1-c',
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.strictEqual(config.reqOpts.cluster.serveNodes, options.nodes);
done();
};
instance.createCluster(CLUSTER_ID, options, assert.ifError);
});
it('should respect the storage option', done => {
const options = {
storage: 'ssd',
} as CreateClusterOptions;
const fakeStorageType = 2;
sandbox.stub(FakeCluster, 'getStorageType_').callsFake(type => {
assert.strictEqual(type, options.storage);
return fakeStorageType;
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.strictEqual(
config.reqOpts.cluster.defaultStorageType,
fakeStorageType
);
done();
};
instance.createCluster(CLUSTER_ID, options, assert.ifError);
});
it('should execute callback with arguments from GAPIC', done => {
const response = {};
sandbox
.stub(instance.bigtable, 'request')
.callsArgWith(1, null, response);
const fakeCluster = {};
(instance.cluster as Function) = (name: string) => {
assert.strictEqual(name, CLUSTER_ID);
return fakeCluster;
};
(instance.createCluster as Function)(
CLUSTER_ID,
(err: Error, cluster: {}, apiResponse: {}) => {
assert.ifError(err);
assert.strictEqual(cluster, fakeCluster);
assert.strictEqual(apiResponse, response);
done();
}
);
});
});
describe('createTable', () => {
const TABLE_ID = 'my-table';
const TABLE_NAME =
'projects/my-project/instances/my-instance/tables/my-table';
it('should throw if an id is not provided', () => {
assert.throws(() => {
(instance.createTable as Function)();
}, /An id is required to create a table\./);
});
it('should provide the proper request options', done => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.strictEqual(config.client, 'BigtableTableAdminClient');
assert.strictEqual(config.method, 'createTable');
assert.strictEqual(config.reqOpts.parent, INSTANCE_NAME);
assert.strictEqual(config.reqOpts.tableId, TABLE_ID);
assert.deepStrictEqual(config.reqOpts.table, {granularity: 0});
assert.strictEqual(config.gaxOpts, undefined);
done();
};
instance.createTable(TABLE_ID, assert.ifError);
});
it('should accept gaxOptions', done => {
const options = {
gaxOptions: {},
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.strictEqual(config.gaxOpts, options.gaxOptions);
done();
};
instance.createTable(TABLE_ID, options, assert.ifError);
});
it('should set the initial split keys', done => {
const options = {
splits: ['a', 'b'],
};
const expectedSplits = [{key: 'a'}, {key: 'b'}];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.deepStrictEqual(config.reqOpts.initialSplits, expectedSplits);
done();
};
instance.createTable(TABLE_ID, options, assert.ifError);
});
describe('creating column families', () => {
it('should accept a family name', done => {
const options = {
families: ['a', 'b'],
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.deepStrictEqual(config.reqOpts.table.columnFamilies, {
a: {},
b: {},
});
done();
};
instance.createTable(TABLE_ID, options, assert.ifError);
});
it('should accept a garbage collection object', done => {
const options = {
families: [
{
name: 'e',
rule: {},
},
],
};
const fakeRule = {a: 'b'};
(FakeFamily.formatRule_ as Function) = (rule: {}) => {
assert.strictEqual(rule, options.families[0].rule);
return fakeRule;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.deepStrictEqual(config.reqOpts.table.columnFamilies, {
e: {
gcRule: fakeRule,
},
});
done();
};
instance.createTable(TABLE_ID, options, assert.ifError);
});
});
it('should return a Table object', done => {
const response = {
name: TABLE_NAME,
};
const fakeTable = {} as Table;
sandbox.stub(instance, 'table').callsFake(id => {
assert.strictEqual(id, response.name.split('/').pop());
return fakeTable;
});
sandbox
.stub(instance.bigtable, 'request')
.callsArgWith(1, null, response);
instance.createTable(TABLE_ID, (err, table, apiResponse) => {
assert.ifError(err);
assert.strictEqual(table, fakeTable);
assert.strictEqual(table!.metadata, response);
assert.strictEqual(apiResponse, response);
done();
});
});
});
describe('createTableFromBackup', () => {
it('should throw if a table is not provided', () => {
assert.throws(() => {
(instance.createTableFromBackup as Function)({});
}, /A table id is required to restore from a backup\./);
});
it('should restore from a provided Backup instance', done => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const backup = new FakeBackup() as any;
const table = 'table';
backup.restore = (
_table: string,
gaxOptions: object,
callback: Function
) => {
assert.strictEqual(_table, table);
assert.strictEqual(typeof gaxOptions, 'undefined');
callback(); // done()
};
instance.createTableFromBackup(
{
table,
backup,
},
done
);
});
it('should create a Backup using the provided name', done => {
const clusterId = 'my-cluster';
const backupId = 'my-backup';
const backup = `/clusters/${clusterId}/backups/${backupId}`;
const table = 'table';
(instance.cluster as Function) = (id: string) => {
assert.strictEqual(id, clusterId);
return {
backup: (id: string) => {
assert.strictEqual(id, backup);
return {
restore: (
_table: string,
gaxOptions: object,
callback: Function
) => {
assert.strictEqual(_table, table);
assert.strictEqual(typeof gaxOptions, 'undefined');
callback(); // done()
},
};
},
};
};
instance.createTableFromBackup(
{
table,
backup,
},
done
);
});
it('should throw if an unformatted backup name is provided', () => {
const backup = 'backup-id';
const table = 'table';
assert.throws(() => {
instance.createTableFromBackup(
{
table,
backup,
},
assert.ifError
);
}, /A complete backup name \(path\) is required or a Backup object\./);
});
it('should accept gaxOptions', done => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const backup = new FakeBackup() as any;
const table = 'table';
const gaxOptions = {};
backup.restore = (table: string, _gaxOptions: object) => {
assert.strictEqual(_gaxOptions, gaxOptions);
done();
};
instance.createTableFromBackup(
{
table,
backup,
gaxOptions,
},
assert.ifError
);
});
});
describe('cluster', () => {
it('should return a Cluster object', () => {
const cluster = instance.cluster(CLUSTER_ID);
assert(cluster instanceof FakeCluster);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const args = (cluster as any).calledWith_;
assert.strictEqual(args[0], instance);
assert.strictEqual(args[1], CLUSTER_ID);
});
});
describe('delete', () => {
it('should make the correct request', done => {
(instance.bigtable.request as Function) = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config: any,
callback: Function
) => {
assert.strictEqual(config.client, 'BigtableInstanceAdminClient');
assert.strictEqual(config.method, 'deleteInstance');
assert.deepStrictEqual(config.reqOpts, {
name: instance.name,
});
assert.deepStrictEqual(config.gaxOpts, {});
callback(); // done()
};
instance.delete(done);
});
it('should accept gaxOptions', done => {
const gaxOptions = {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.strictEqual(config.gaxOpts, gaxOptions);
done();
};
instance.delete(gaxOptions, assert.ifError);
});
});
describe('exists', () => {
it('should not require gaxOptions', done => {
(instance.getMetadata as Function) = (gaxOptions: {}) => {
assert.deepStrictEqual(gaxOptions, {});
done();
};
instance.exists(assert.ifError);
});
it('should pass gaxOptions to getMetadata', done => {
const gaxOptions = {};
(instance.getMetadata as Function) = (gaxOptions_: {}) => {
assert.strictEqual(gaxOptions_, gaxOptions);
done();
};
instance.exists(gaxOptions, assert.ifError);
});
it('should return false if error code is 5', done => {
const error = new Error('Error.') as ServiceError;
error.code = 5;
sandbox.stub(instance, 'getMetadata').callsArgWith(1, error);
instance.exists((err, exists) => {
assert.ifError(err);
assert.strictEqual(exists, false);
done();
});
});
it('should return error if code is not 5', done => {
const error = new Error('Error.') as ServiceError;
error.code = ('NOT-5' as {}) as number;
sandbox.stub(instance, 'getMetadata').callsArgWith(1, error);
instance.exists(err => {
assert.strictEqual(err, error);
done();
});
});
it('should return true if no error', done => {
sandbox.stub(instance, 'getMetadata').callsArgWith(1, null, {});
instance.exists((err, exists) => {
assert.ifError(err);
assert.strictEqual(exists, true);
done();
});
});
});
describe('get', () => {
it('should call getMetadata', done => {
const gaxOptions = {};
(instance.getMetadata as Function) = (gaxOptions_: {}) => {
assert.strictEqual(gaxOptions_, gaxOptions);
done();
};
instance.get(gaxOptions, assert.ifError);
});
it('should not require gaxOptions', done => {
(instance.getMetadata as Function) = (gaxOptions: {}) => {
assert.deepStrictEqual(gaxOptions, {});
done();
};
instance.get(assert.ifError);
});
it('should return an error from getMetadata', done => {
const error = new Error('Error.');
sandbox.stub(instance, 'getMetadata').callsArgWith(1, error);
instance.get(err => {
assert.strictEqual(err, error);
done();
});
});
it('should return self and API response', done => {
const metadata = {};
sandbox.stub(instance, 'getMetadata').callsArgWith(1, null, metadata);
instance.get((err, instance_, metadata_) => {
assert.ifError(err);
assert.strictEqual(instance_, instance);
assert.strictEqual(metadata_, metadata);
done();
});
});
});
describe('getAppProfiles', () => {
it('should provide the proper request options', done => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.strictEqual(config.client, 'BigtableInstanceAdminClient');
assert.strictEqual(config.method, 'listAppProfiles');
assert.deepStrictEqual(config.reqOpts, {
parent: INSTANCE_NAME,
});
assert.deepStrictEqual(config.gaxOpts, {});
done();
};
instance.getAppProfiles(assert.ifError);
});
it('should accept gaxOptions', done => {
const gaxOptions = {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.deepStrictEqual(config.gaxOpts, gaxOptions);
done();
};
instance.getAppProfiles(gaxOptions, assert.ifError);
});
it('should pass pageToken from gaxOptions into reqOpts', done => {
const pageToken = 'token';
const gaxOptions = {pageToken, timeout: 1000};
const expectedGaxOpts = {timeout: 1000};
const expectedReqOpts = Object.assign(
{},
{gaxOptions},
{parent: instance.name},
{pageToken: gaxOptions.pageToken}
);
delete expectedReqOpts.gaxOptions;
(instance.bigtable.request as Function) = (config: RequestOptions) => {
assert.deepStrictEqual(config.reqOpts, expectedReqOpts);
assert.notStrictEqual(config.gaxOpts, gaxOptions);
assert.notDeepStrictEqual(config.gaxOpts, gaxOptions);
assert.deepStrictEqual(config.gaxOpts, expectedGaxOpts);
done();
};
instance.getAppProfiles(gaxOptions, assert.ifError);
});
it('should pass pageSize from gaxOptions into reqOpts', done => {
const pageSize = 3;
const gaxOptions = {pageSize, timeout: 1000};
const expectedGaxOpts = {timeout: 1000};
const expectedReqOpts = Object.assign(
{},
{gaxOptions},
{parent: instance.name},
{pageSize: gaxOptions.pageSize}
);
delete expectedReqOpts.gaxOptions;
(instance.bigtable.request as Function) = (config: RequestOptions) => {
assert.deepStrictEqual(config.reqOpts, expectedReqOpts);
assert.notStrictEqual(config.gaxOpts, gaxOptions);
assert.notDeepStrictEqual(config.gaxOpts, gaxOptions);
assert.deepStrictEqual(config.gaxOpts, expectedGaxOpts);
done();
};
instance.getAppProfiles(gaxOptions, assert.ifError);
});
it('should return error from gapic', done => {
const error = new Error('Error.');
sandbox.stub(instance.bigtable, 'request').callsArgWith(1, error);
instance.getAppProfiles(err => {
assert.strictEqual(err, error);
done();
});
});
it('should return an array of AppProfile objects', done => {
const response = [{name: 'a'}, {name: 'b'}];
sandbox
.stub(instance.bigtable, 'request')
.callsArgWith(1, null, response);
instance.getAppProfiles((err, appProfiles, apiResponse) => {
assert.ifError(err);
assert.strictEqual(appProfiles![0].id, 'a');
assert.deepStrictEqual(appProfiles![0].metadata, response[0]);
assert.strictEqual(appProfiles![1].id, 'b');
assert.deepStrictEqual(appProfiles![1].metadata, response[1]);
assert.strictEqual(apiResponse, response);
done();
});
});
});
describe('getAppProfilesStream', () => {
let returnStream: PassThrough;
beforeEach(() => {
returnStream = new PassThrough({
objectMode: true,
});
});
it('should provide the proper request options', done => {
const stub = sandbox.stub(pumpify, 'obj');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.strictEqual(config.client, 'BigtableInstanceAdminClient');
assert.strictEqual(config.method, 'listAppProfilesStream');
assert.deepStrictEqual(config.reqOpts, {
parent: INSTANCE_NAME,
});
assert.deepStrictEqual(config.gaxOpts, {});
setImmediate(done);
return returnStream;
};
instance.getAppProfilesStream();
assert.strictEqual(stub.getCall(0).args[0][0], returnStream);
});
it('should accept gaxOptions', done => {
const gaxOptions = {timeout: 1000};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = (config: any) => {
assert.deepStrictEqual(config.gaxOpts, gaxOptions);
setImmediate(done);
return returnStream;
};
instance.getAppProfilesStream(gaxOptions);
});
it('should pass pageToken from gaxOptions into reqOpts', done => {
const pageToken = 'token';
const gaxOptions = {pageToken, timeout: 1000};
const expectedGaxOpts = {timeout: 1000};
const expectedReqOpts = Object.assign(
{},
{gaxOptions},
{parent: instance.name},
{pageToken: gaxOptions.pageToken}
);
delete expectedReqOpts.gaxOptions;
(instance.bigtable.request as Function) = (config: RequestOptions) => {
assert.deepStrictEqual(config.reqOpts, expectedReqOpts);
assert.notStrictEqual(config.gaxOpts, gaxOptions);
assert.notDeepStrictEqual(config.gaxOpts, gaxOptions);
assert.deepStrictEqual(config.gaxOpts, expectedGaxOpts);
setImmediate(done);
return returnStream;
};
instance.getAppProfilesStream(gaxOptions);
});
it('should pass pageSize from gaxOptions into reqOpts', done => {
const pageSize = 3;
const gaxOptions = {pageSize, timeout: 1000};
const expectedGaxOpts = {timeout: 1000};
const expectedReqOpts = Object.assign(
{},
{gaxOptions},
{parent: instance.name},
{pageSize: gaxOptions.pageSize}
);
delete expectedReqOpts.gaxOptions;
(instance.bigtable.request as Function) = (config: RequestOptions) => {
assert.deepStrictEqual(config.reqOpts, expectedReqOpts);
assert.notStrictEqual(config.gaxOpts, gaxOptions);
assert.notDeepStrictEqual(config.gaxOpts, gaxOptions);
assert.deepStrictEqual(config.gaxOpts, expectedGaxOpts);
setImmediate(done);
return returnStream;
};
instance.getAppProfilesStream(gaxOptions);
});
it('should return error from gapic', done => {
const error = new Error('Error.');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(instance.bigtable.request as Function) = () => {
return returnStream;
};
setImmediate(() => {
returnStream.destroy(error);
});
instance.getAppProfilesStream().on('error', err => {
assert.strictEqual(err, error);
done();
});
});
it('should return a decorated error with failedLocations list', done => {
let counter = 0;
let failedLocations: string[] = [];
const pages = [
{
appProfiles: [
{
name: '/projects/p/instances/i/appProfiles/profile-a',
},
{
name: '/projects/p/instances/i/appProfiles/profile-b',
},
],
response: {failedLocations: []},
},
{
appProfiles: [
{
name: '/projects/p/instances/i/appProfiles/profile-c',
},
{
name: '/projects/p/instances/i/appProfiles/profile-d',
},
],