This repository was archived by the owner on Mar 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathinstance.ts
More file actions
941 lines (757 loc) · 25.4 KB
/
instance.ts
File metadata and controls
941 lines (757 loc) · 25.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
/*!
* Copyright 2017 Google Inc. All Rights Reserved.
*
* 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 assert from 'assert';
import {describe, it} from 'mocha';
import {ApiError} from '@google-cloud/common';
import * as extend from 'extend';
import * as proxyquire from 'proxyquire';
import * as pfy from '@google-cloud/promisify';
import {ServiceError} from 'grpc';
import * as sinon from 'sinon';
import snakeCase = require('lodash.snakecase');
import * as inst from '../src/instance';
import {Spanner, Database} from '../src';
import arrify = require('arrify');
import {SessionPoolOptions} from '../src/session-pool';
const fakePaginator = {
paginator: {
streamify(methodName) {
return methodName;
},
},
};
let promisified = false;
const fakePfy = extend({}, pfy, {
promisifyAll(klass, options) {
if (klass.name !== 'Instance') {
return;
}
promisified = true;
assert.deepStrictEqual(options.exclude, ['database']);
},
});
class FakeDatabase {
calledWith_: IArguments;
constructor() {
this.calledWith_ = arguments;
}
}
class FakeGrpcServiceObject {
calledWith_: IArguments;
constructor() {
this.calledWith_ = arguments;
}
}
describe('Instance', () => {
// tslint:disable-next-line variable-name
let Instance: typeof inst.Instance;
let instance: inst.Instance;
const sandbox = sinon.createSandbox();
const SPANNER = ({
request: () => {},
requestStream: () => {},
projectId: 'project-id',
instances_: new Map(),
} as {}) as Spanner;
const NAME = 'instance-name';
before(() => {
Instance = proxyquire('../src/instance.js', {
'./common-grpc/service-object': {
GrpcServiceObject: FakeGrpcServiceObject,
},
'@google-cloud/promisify': fakePfy,
'@google-cloud/paginator': fakePaginator,
'./database.js': {Database: FakeDatabase},
}).Instance;
});
beforeEach(() => {
instance = new Instance(SPANNER, NAME);
});
describe('instantiation', () => {
it('should localize an database map', () => {
assert(instance.databases_ instanceof Map);
});
it('should promisify all the things', () => {
assert(promisified);
});
it('should format the name', () => {
const formatName_ = Instance.formatName_;
const formattedName = 'formatted-name';
Instance.formatName_ = (projectId, name) => {
Instance.formatName_ = formatName_;
assert.strictEqual(projectId, SPANNER.projectId);
assert.strictEqual(name, NAME);
return formattedName;
};
const instance = new Instance(SPANNER, NAME);
assert(instance.formattedName_, formattedName);
});
it('should localize the request function', done => {
const spannerInstance = extend({}, SPANNER);
spannerInstance.request = function() {
assert.strictEqual(this, spannerInstance);
done();
};
const instance = new Instance(spannerInstance, NAME);
// tslint:disable-next-line: no-any
(instance as any).request();
});
it('should localize the requestStream function', done => {
const spannerInstance = extend({}, SPANNER);
spannerInstance.requestStream = function() {
assert.strictEqual(this, spannerInstance);
done();
};
const instance = new Instance(spannerInstance, NAME);
instance.requestStream();
});
it('should inherit from ServiceObject', done => {
const options = {};
const spannerInstance = extend({}, SPANNER, {
createInstance(name, options_, callback) {
assert.strictEqual(name, instance.formattedName_);
assert.strictEqual(options_, options);
callback(); // done()
},
});
const instance = new Instance(spannerInstance, NAME);
assert(instance instanceof FakeGrpcServiceObject);
const calledWith = instance.calledWith_[0];
assert.strictEqual(calledWith.parent, spannerInstance);
assert.strictEqual(calledWith.id, NAME);
assert.deepStrictEqual(calledWith.methods, {create: true});
calledWith.createMethod(null, options, done);
});
});
describe('formatName_', () => {
const PATH = 'projects/' + SPANNER.projectId + '/instances/' + NAME;
it('should return the name if already formatted', () => {
assert.strictEqual(Instance.formatName_(SPANNER.projectId, PATH), PATH);
});
it('should format the name', () => {
const formattedName = Instance.formatName_(SPANNER.projectId, NAME);
assert.strictEqual(formattedName, PATH);
});
});
describe('createDatabase', () => {
const NAME = 'database-name';
const PATH = 'projects/project-id/databases/' + NAME;
const OPTIONS = {
a: 'b',
} as inst.CreateDatabaseOptions;
const ORIGINAL_OPTIONS = extend({}, OPTIONS);
it('should throw if a name is not provided', () => {
assert.throws(() => {
instance.createDatabase(null!);
}, /A name is required to create a database\./);
});
it('should make the correct default request', done => {
instance.request = config => {
assert.strictEqual(config.client, 'DatabaseAdminClient');
assert.strictEqual(config.method, 'createDatabase');
assert.deepStrictEqual(config.reqOpts, {
parent: instance.formattedName_,
createStatement: 'CREATE DATABASE `' + NAME + '`',
});
done();
};
instance.createDatabase(NAME, assert.ifError);
});
it('should accept options', done => {
instance.request = config => {
assert.deepStrictEqual(OPTIONS, ORIGINAL_OPTIONS);
const expectedReqOpts = extend(
{
parent: instance.formattedName_,
createStatement: 'CREATE DATABASE `' + NAME + '`',
},
OPTIONS
);
assert.deepStrictEqual(config.reqOpts, expectedReqOpts);
done();
};
instance.createDatabase(NAME, OPTIONS, assert.ifError);
});
it('should only use the name in the createStatement', done => {
instance.request = config => {
const expectedReqOpts = extend(
{
parent: instance.formattedName_,
createStatement: 'CREATE DATABASE `' + NAME + '`',
},
OPTIONS
);
assert.deepStrictEqual(config.reqOpts, expectedReqOpts);
done();
};
instance.createDatabase(PATH, OPTIONS, assert.ifError);
});
describe('options.poolOptions', () => {
it('should allow specifying session pool options', done => {
const poolOptions = {};
const options = extend({}, OPTIONS, {
poolOptions,
});
instance.request = (config, callback: Function) => {
assert.strictEqual(config.reqOpts.poolOptions, undefined);
callback();
};
instance.database = (name, poolOptions_) => {
assert.strictEqual(poolOptions_, poolOptions);
done();
return {} as Database;
};
instance.createDatabase(PATH, options, assert.ifError);
});
});
describe('options.schema', () => {
it('should arrify and rename to extraStatements', done => {
const SCHEMA = 'schema';
const options = extend({}, OPTIONS, {
schema: SCHEMA,
});
instance.request = config => {
assert.deepStrictEqual(config.reqOpts.extraStatements, [SCHEMA]);
assert.strictEqual(config.reqOpts.schema, undefined);
done();
};
instance.createDatabase(NAME, options, assert.ifError);
});
});
describe('error', () => {
const ERROR = new Error('Error.');
const API_RESPONSE = {};
beforeEach(() => {
instance.request = (config, callback: Function) => {
callback(ERROR, null, API_RESPONSE);
};
});
it('should execute callback with error & API response', done => {
instance.createDatabase(NAME, OPTIONS, (err, db, op, resp) => {
assert.strictEqual(err, ERROR);
assert.strictEqual(op, null);
assert.strictEqual(resp, API_RESPONSE);
done();
});
});
});
describe('success', () => {
const OPERATION = {};
const API_RESPONSE = {};
beforeEach(() => {
instance.request = (config, callback: Function) => {
callback(null, OPERATION, API_RESPONSE);
};
});
it('should exec callback with a Database and Operation', done => {
const fakeDatabaseInstance = {};
instance.database = name => {
assert.strictEqual(name, NAME);
return fakeDatabaseInstance as Database;
};
instance.createDatabase(NAME, OPTIONS, (err, db, op, resp) => {
assert.ifError(err);
assert.strictEqual(db, fakeDatabaseInstance);
assert.strictEqual(op, OPERATION);
assert.strictEqual(resp, API_RESPONSE);
done();
});
});
});
});
describe('database', () => {
const NAME = 'database-name';
it('should throw if a name is not provided', () => {
assert.throws(() => {
instance.database(null!);
}, /A name is required to access a Database object\./);
});
it('should create and cache a Database', () => {
const cache = instance.databases_;
const poolOptions = {};
assert.strictEqual(cache.has(NAME), false);
const database = (instance.database(
NAME,
poolOptions
) as {}) as FakeDatabase;
assert(database instanceof FakeDatabase);
assert.strictEqual(database.calledWith_[0], instance);
assert.strictEqual(database.calledWith_[1], NAME);
assert.strictEqual(database.calledWith_[2], poolOptions);
assert.strictEqual(database, cache.get(NAME));
});
it('should re-use cached objects', () => {
const cache = instance.databases_;
const fakeDatabase = {} as Database;
cache.set(NAME, fakeDatabase);
const database = instance.database(NAME);
assert.strictEqual(database, fakeDatabase);
});
it('should create and cache different objects when called with different session pool options', () => {
const cache = instance.databases_;
const fakeDatabase = {} as Database;
const fakeDatabaseWithSessionPoolOptions = {} as Database;
const emptySessionPoolOptions = {} as SessionPoolOptions;
const fakeSessionPoolOptions = {
min: 1000,
max: 1000,
} as SessionPoolOptions;
const fakeSessionPoolOptionsInOtherOrder = {
max: 1000,
min: 1000,
} as SessionPoolOptions;
cache.set(NAME, fakeDatabase);
cache.set(
NAME +
'/' +
JSON.stringify(Object.entries(fakeSessionPoolOptions).sort()),
fakeDatabaseWithSessionPoolOptions
);
const database = instance.database(NAME);
const databaseWithEmptyOptions = instance.database(
NAME,
emptySessionPoolOptions
);
const databaseWithOptions = instance.database(
NAME,
fakeSessionPoolOptions
);
const databaseWithOptionsInOtherOrder = instance.database(
NAME,
fakeSessionPoolOptionsInOtherOrder
);
assert.strictEqual(database, fakeDatabase);
assert.strictEqual(databaseWithEmptyOptions, fakeDatabase);
assert.strictEqual(
databaseWithOptions,
fakeDatabaseWithSessionPoolOptions
);
assert.strictEqual(
databaseWithOptionsInOtherOrder,
fakeDatabaseWithSessionPoolOptions
);
});
});
describe('delete', () => {
beforeEach(() => {
instance.parent = SPANNER;
});
it('should close all cached databases', done => {
let closed = false;
instance.databases_.set('key', ({
close() {
closed = true;
return Promise.resolve();
},
} as {}) as Database);
instance.request = () => {
assert.strictEqual(closed, true);
assert.strictEqual(instance.databases_.size, 0);
done();
};
instance.delete(assert.ifError);
});
it('should ignore closing errors', done => {
instance.databases_.set('key', ({
close() {
return Promise.reject(new Error('err'));
},
} as {}) as Database);
instance.request = () => {
done();
};
instance.delete(assert.ifError);
});
it('should make the correct request', done => {
instance.request = (config, callback: Function) => {
assert.strictEqual(config.client, 'InstanceAdminClient');
assert.strictEqual(config.method, 'deleteInstance');
assert.deepStrictEqual(config.reqOpts, {
name: instance.formattedName_,
});
callback(); // done()
};
instance.delete(done);
});
it('should remove the Instance from the cache', done => {
const cache = instance.parent.instances_;
instance.request = (config, callback) => {
callback(null);
};
cache.set(instance.id, instance);
assert.strictEqual(cache.get(instance.id), instance);
instance.delete(err => {
assert.ifError(err);
assert.strictEqual(cache.has(instance.id), false);
done();
});
});
});
describe('exists', () => {
afterEach(() => sandbox.restore());
it('should return any non-404 like errors', done => {
const error = {code: 3};
sandbox
.stub(instance, 'getMetadata')
.callsFake(
(
opts_:
| inst.GetInstanceMetadataOptions
| inst.GetInstanceMetadataCallback,
cb
) => {
cb = typeof opts_ === 'function' ? opts_ : cb;
cb(error as ServiceError);
}
);
instance.exists((err, exists) => {
assert.strictEqual(err, error);
assert.strictEqual(exists, null);
done();
});
});
it('should return true if error is absent', done => {
sandbox
.stub(instance, 'getMetadata')
.callsFake(
(
opts_:
| inst.GetInstanceMetadataOptions
| inst.GetInstanceMetadataCallback,
cb
) => {
cb = typeof opts_ === 'function' ? opts_ : cb;
cb(null);
}
);
instance.exists((err, exists) => {
assert.ifError(err);
assert.strictEqual(exists, true);
done();
});
});
it('should return false if not found error if present', done => {
const error = {code: 5};
sandbox
.stub(instance, 'getMetadata')
.callsFake(
(
opts_:
| inst.GetInstanceMetadataOptions
| inst.GetInstanceMetadataCallback,
callback
) => {
callback = typeof opts_ === 'function' ? opts_ : callback;
callback(error as ServiceError);
}
);
instance.exists((err, exists) => {
assert.ifError(err);
assert.strictEqual(exists, false);
done();
});
});
});
describe('get', () => {
it('should call getMetadata', done => {
const options = {};
sandbox.stub(instance, 'getMetadata').callsFake(() => done());
instance.get(options, assert.ifError);
});
it('should not require an options object', done => {
sandbox.stub(instance, 'getMetadata').callsFake(() => done());
instance.get(assert.ifError);
});
it('should accept and pass `fields` string as is', () => {
const fieldNames = 'nodeCount';
const spyMetadata = sandbox.spy(instance, 'getMetadata');
instance.get({fieldNames}, assert.ifError);
assert.ok(spyMetadata.calledWith({fieldNames}));
});
it('should accept and pass `fields` array as is', () => {
const fieldNames = ['name', 'labels', 'nodeCount'];
const spyMetadata = sandbox.stub(instance, 'getMetadata');
instance.get({fieldNames}, assert.ifError);
assert.ok(spyMetadata.calledWith({fieldNames}));
});
describe('autoCreate', () => {
const error = new ApiError('Error.') as ServiceError;
error.code = 5;
const OPTIONS = {
autoCreate: true,
};
const OPERATION = {
listeners: {},
on(eventName, callback) {
OPERATION.listeners[eventName] = callback;
return OPERATION;
},
};
beforeEach(() => {
OPERATION.listeners = {};
sandbox
.stub(instance, 'getMetadata')
.callsFake((opts_: {}, callback) => callback!(error));
instance.create = (options, callback) => {
callback(null, null, OPERATION);
};
});
it('should call create', done => {
instance.create = options => {
assert.strictEqual(options, OPTIONS);
done();
};
instance.get(OPTIONS, assert.ifError);
});
it('should return error if create failed', done => {
const error = new Error('Error.');
instance.create = (options, callback) => {
callback(error);
};
instance.get(OPTIONS, err => {
assert.strictEqual(err, error);
done();
});
});
it('should return operation error', done => {
const error = new Error('Error.');
setImmediate(() => {
OPERATION.listeners['error'](error);
});
instance.get(OPTIONS, err => {
assert.strictEqual(err, error);
done();
});
});
it('should execute callback if opereation succeeded', done => {
const metadata = {};
setImmediate(() => {
OPERATION.listeners['complete'](metadata);
});
instance.get(OPTIONS, (err, instance_, apiResponse) => {
assert.ifError(err);
assert.strictEqual(instance_, instance);
assert.strictEqual(instance.metadata, metadata);
assert.strictEqual(metadata, apiResponse);
done();
});
});
});
it('should not auto create without error code 5', done => {
const error = new Error('Error.') as ServiceError;
// tslint:disable-next-line no-any
(error as any).code = 'NOT-5';
const options = {
autoCreate: true,
};
sandbox
.stub(instance, 'getMetadata')
.callsFake((opts_: {}, callback) => callback!(error));
instance.create = () => {
throw new Error('Should not create.');
};
instance.get(options, err => {
assert.strictEqual(err, error);
done();
});
});
it('should not auto create unless requested', done => {
const error = new ApiError('Error.') as ServiceError;
error.code = 5;
sandbox
.stub(instance, 'getMetadata')
.callsFake((opts_: {}, callback) => callback!(error));
instance.create = () => {
throw new Error('Should not create.');
};
instance.get(err => {
assert.strictEqual(err, error);
done();
});
});
it('should return an error from getMetadata', done => {
const error = new Error('Error.') as ServiceError;
sandbox
.stub(instance, 'getMetadata')
.callsFake((opts_: {}, callback) => callback!(error));
instance.get(err => {
assert.strictEqual(err, error);
done();
});
});
it('should return self and API response', done => {
const apiResponse = {} as inst.IInstance;
sandbox
.stub(instance, 'getMetadata')
.callsFake((opts_: {}, callback) => callback!(null, apiResponse));
instance.get((err, instance_, apiResponse_) => {
assert.ifError(err);
assert.strictEqual(instance_, instance);
assert.strictEqual(apiResponse_, apiResponse);
done();
});
});
});
describe('getDatabases', () => {
const QUERY = {
a: 'b',
} as inst.GetDatabasesRequest;
const ORIGINAL_QUERY = extend({}, QUERY);
it('should make the correct request', done => {
const expectedReqOpts = extend({}, QUERY, {
parent: instance.formattedName_,
});
instance.request = config => {
assert.strictEqual(config.client, 'DatabaseAdminClient');
assert.strictEqual(config.method, 'listDatabases');
assert.deepStrictEqual(config.reqOpts, expectedReqOpts);
assert.notStrictEqual(config.reqOpts, QUERY);
assert.deepStrictEqual(QUERY, ORIGINAL_QUERY);
assert.strictEqual(config.gaxOpts, QUERY);
done();
};
instance.getDatabases(QUERY, assert.ifError);
});
it('should not require a query', done => {
instance.request = config => {
assert.deepStrictEqual(config.reqOpts, {
parent: instance.formattedName_,
});
assert.deepStrictEqual(config.gaxOpts, {});
done();
};
instance.getDatabases(assert.ifError);
});
describe('error', () => {
const REQUEST_RESPONSE_ARGS = [new Error('Error.'), null, {}];
beforeEach(() => {
instance.request = (config, callback: Function) => {
callback.apply(null, REQUEST_RESPONSE_ARGS);
};
});
it('should execute callback with original arguments', done => {
instance.getDatabases(QUERY, (...args) => {
assert.deepStrictEqual(args, REQUEST_RESPONSE_ARGS);
done();
});
});
});
describe('success', () => {
const DATABASES = [
{
name: 'database-name',
},
];
// tslint:disable-next-line no-any
const REQUEST_RESPONSE_ARGS: any = [null, DATABASES, {}];
beforeEach(() => {
instance.request = (config, callback) => {
callback.apply(null, REQUEST_RESPONSE_ARGS);
};
});
it('should create and return Database objects', done => {
const fakeDatabaseInstance = {};
instance.database = name => {
assert.strictEqual(name, DATABASES[0].name);
return fakeDatabaseInstance as Database;
};
instance.getDatabases(QUERY, (...args) => {
assert.ifError(args[0]);
assert.strictEqual(args[0], REQUEST_RESPONSE_ARGS[0]);
const database = args[1]!.pop();
assert.strictEqual(database, fakeDatabaseInstance);
assert.strictEqual(database!.metadata, REQUEST_RESPONSE_ARGS[1][0]);
assert.strictEqual(args[2], REQUEST_RESPONSE_ARGS[2]);
done();
});
});
});
});
describe('getMetadata', () => {
it('should correctly call and return request', () => {
const requestReturnValue = {};
function callback() {}
instance.request = (config, callback_) => {
assert.strictEqual(config.client, 'InstanceAdminClient');
assert.strictEqual(config.method, 'getInstance');
assert.deepStrictEqual(config.reqOpts, {
name: instance.formattedName_,
});
assert.strictEqual(callback_, callback);
return requestReturnValue;
};
const returnValue = instance.getMetadata(callback);
assert.strictEqual(returnValue, requestReturnValue);
});
it('should accept `fieldNames` as string', done => {
const fieldNames = 'nodeCount';
instance.request = config => {
assert.deepStrictEqual(config.reqOpts, {
fieldMask: {
paths: arrify(fieldNames).map(snakeCase),
},
name: instance.formattedName_,
});
done();
};
instance.getMetadata({fieldNames}, assert.ifError);
});
it('should accept `fieldNames` as string array', done => {
const fieldNames = ['name', 'labels', 'nodeCount'];
instance.request = config => {
assert.deepStrictEqual(config.reqOpts, {
fieldMask: {
paths: fieldNames.map(snakeCase),
},
name: instance.formattedName_,
});
done();
};
instance.getMetadata({fieldNames}, assert.ifError);
});
});
describe('setMetadata', () => {
const METADATA = {
needsToBeSnakeCased: true,
} as inst.IInstance;
const ORIGINAL_METADATA = extend({}, METADATA);
it('should make and return the request', () => {
const requestReturnValue = {};
function callback() {}
instance.request = (config, callback_) => {
assert.strictEqual(config.client, 'InstanceAdminClient');
assert.strictEqual(config.method, 'updateInstance');
const expectedReqOpts = extend({}, METADATA, {
name: instance.formattedName_,
});
assert.deepStrictEqual(config.reqOpts.instance, expectedReqOpts);
assert.deepStrictEqual(config.reqOpts.fieldMask, {
paths: ['needs_to_be_snake_cased'],
});
assert.deepStrictEqual(METADATA, ORIGINAL_METADATA);
assert.strictEqual(callback_, callback);
return requestReturnValue;
};
const returnValue = instance.setMetadata(METADATA, callback);
assert.strictEqual(returnValue, requestReturnValue);
});
it('should not require a callback', () => {
assert.doesNotThrow(() => {
instance.setMetadata(METADATA);
});
});
});
});