-
Notifications
You must be signed in to change notification settings - Fork 0
/
spanner_instance_admin_aiptest.pb.go
1572 lines (1450 loc) · 49.4 KB
/
spanner_instance_admin_aiptest.pb.go
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
// Code generated by protoc-gen-go-aip-test. DO NOT EDIT.
package instancepb
import (
context "context"
cmpopts "github.com/google/go-cmp/cmp/cmpopts"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
proto "google.golang.org/protobuf/proto"
protocmp "google.golang.org/protobuf/testing/protocmp"
fieldmaskpb "google.golang.org/protobuf/types/known/fieldmaskpb"
assert "gotest.tools/v3/assert"
strings "strings"
testing "testing"
)
type InstanceAdminTestSuite struct {
T *testing.T
// Server to test.
Server InstanceAdminServer
}
func (fx InstanceAdminTestSuite) TestInstance(ctx context.Context, options InstanceAdminInstanceTestSuiteConfig) {
fx.T.Run("Instance", func(t *testing.T) {
options.ctx = ctx
options.service = fx.Server
options.test(t)
})
}
func (fx InstanceAdminTestSuite) TestInstanceConfig(ctx context.Context, options InstanceAdminInstanceConfigTestSuiteConfig) {
fx.T.Run("InstanceConfig", func(t *testing.T) {
options.ctx = ctx
options.service = fx.Server
options.test(t)
})
}
func (fx InstanceAdminTestSuite) TestInstancePartition(ctx context.Context, options InstanceAdminInstancePartitionTestSuiteConfig) {
fx.T.Run("InstancePartition", func(t *testing.T) {
options.ctx = ctx
options.service = fx.Server
options.test(t)
})
}
type InstanceAdminInstanceTestSuiteConfig struct {
ctx context.Context
service InstanceAdminServer
currParent int
// The parents to use when creating resources.
// At least one parent needs to be set. Depending on methods available on the resource,
// more may be required. If insufficient number of parents are
// provided the test will fail.
Parents []string
// Create should return a resource which is valid to create, i.e.
// all required fields set.
Create func(parent string) *Instance
// Update should return a resource which is valid to update, i.e.
// all required fields set.
Update func(parent string) *Instance
// Patterns of tests to skip.
// For example if a service has a Get method:
// Skip: ["Get"] will skip all tests for Get.
// Skip: ["Get/persisted"] will only skip the subtest called "persisted" of Get.
Skip []string
}
func (fx *InstanceAdminInstanceTestSuiteConfig) test(t *testing.T) {
t.Run("Create", fx.testCreate)
t.Run("Get", fx.testGet)
t.Run("Update", fx.testUpdate)
t.Run("List", fx.testList)
t.Run("Delete", fx.testDelete)
}
func (fx *InstanceAdminInstanceTestSuiteConfig) testCreate(t *testing.T) {
fx.maybeSkip(t)
// Method should fail with InvalidArgument if no parent is provided.
t.Run("missing parent", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.CreateInstance(fx.ctx, &CreateInstanceRequest{
Parent: "",
Instance: fx.Create(fx.nextParent(t, false)),
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Method should fail with InvalidArgument if provided parent is invalid.
t.Run("invalid parent", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.CreateInstance(fx.ctx, &CreateInstanceRequest{
Parent: "invalid resource name",
Instance: fx.Create(fx.nextParent(t, false)),
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// The method should fail with InvalidArgument if the resource has any
// required fields and they are not provided.
t.Run("required fields", func(t *testing.T) {
fx.maybeSkip(t)
t.Run(".name", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
msg := fx.Create(parent)
container := msg
if container == nil {
t.Skip("not reachable")
}
fd := container.ProtoReflect().Descriptor().Fields().ByName("name")
container.ProtoReflect().Clear(fd)
_, err := fx.service.CreateInstance(fx.ctx, &CreateInstanceRequest{
Parent: parent,
Instance: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
t.Run(".config", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
msg := fx.Create(parent)
container := msg
if container == nil {
t.Skip("not reachable")
}
fd := container.ProtoReflect().Descriptor().Fields().ByName("config")
container.ProtoReflect().Clear(fd)
_, err := fx.service.CreateInstance(fx.ctx, &CreateInstanceRequest{
Parent: parent,
Instance: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
t.Run(".display_name", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
msg := fx.Create(parent)
container := msg
if container == nil {
t.Skip("not reachable")
}
fd := container.ProtoReflect().Descriptor().Fields().ByName("display_name")
container.ProtoReflect().Clear(fd)
_, err := fx.service.CreateInstance(fx.ctx, &CreateInstanceRequest{
Parent: parent,
Instance: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
t.Run(".autoscaling_config.autoscaling_limits", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
msg := fx.Create(parent)
container := msg.GetAutoscalingConfig()
if container == nil {
t.Skip("not reachable")
}
fd := container.ProtoReflect().Descriptor().Fields().ByName("autoscaling_limits")
container.ProtoReflect().Clear(fd)
_, err := fx.service.CreateInstance(fx.ctx, &CreateInstanceRequest{
Parent: parent,
Instance: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
t.Run(".autoscaling_config.autoscaling_targets", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
msg := fx.Create(parent)
container := msg.GetAutoscalingConfig()
if container == nil {
t.Skip("not reachable")
}
fd := container.ProtoReflect().Descriptor().Fields().ByName("autoscaling_targets")
container.ProtoReflect().Clear(fd)
_, err := fx.service.CreateInstance(fx.ctx, &CreateInstanceRequest{
Parent: parent,
Instance: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
t.Run(".autoscaling_config.autoscaling_targets.high_priority_cpu_utilization_percent", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
msg := fx.Create(parent)
container := msg.GetAutoscalingConfig().GetAutoscalingTargets()
if container == nil {
t.Skip("not reachable")
}
fd := container.ProtoReflect().Descriptor().Fields().ByName("high_priority_cpu_utilization_percent")
container.ProtoReflect().Clear(fd)
_, err := fx.service.CreateInstance(fx.ctx, &CreateInstanceRequest{
Parent: parent,
Instance: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
t.Run(".autoscaling_config.autoscaling_targets.storage_utilization_percent", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
msg := fx.Create(parent)
container := msg.GetAutoscalingConfig().GetAutoscalingTargets()
if container == nil {
t.Skip("not reachable")
}
fd := container.ProtoReflect().Descriptor().Fields().ByName("storage_utilization_percent")
container.ProtoReflect().Clear(fd)
_, err := fx.service.CreateInstance(fx.ctx, &CreateInstanceRequest{
Parent: parent,
Instance: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
})
// The method should fail with InvalidArgument if the resource has any
// resource references and they are invalid.
t.Run("resource references", func(t *testing.T) {
fx.maybeSkip(t)
t.Run(".config", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
msg := fx.Create(parent)
container := msg
if container == nil {
t.Skip("not reachable")
}
container.Config = "invalid resource name"
_, err := fx.service.CreateInstance(fx.ctx, &CreateInstanceRequest{
Parent: parent,
Instance: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
})
}
func (fx *InstanceAdminInstanceTestSuiteConfig) testGet(t *testing.T) {
fx.maybeSkip(t)
// Method should fail with InvalidArgument if no name is provided.
t.Run("missing name", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.GetInstance(fx.ctx, &GetInstanceRequest{
Name: "",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Method should fail with InvalidArgument if the provided name is not valid.
t.Run("invalid name", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.GetInstance(fx.ctx, &GetInstanceRequest{
Name: "invalid resource name",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Resource should be returned without errors if it exists.
t.Run("exists", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
created := fx.create(t, parent)
msg, err := fx.service.GetInstance(fx.ctx, &GetInstanceRequest{
Name: created.Name,
})
assert.NilError(t, err)
assert.DeepEqual(t, msg, created, protocmp.Transform())
})
// Method should fail with NotFound if the resource does not exist.
t.Run("not found", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
created := fx.create(t, parent)
_, err := fx.service.GetInstance(fx.ctx, &GetInstanceRequest{
Name: created.Name + "notfound",
})
assert.Equal(t, codes.NotFound, status.Code(err), err)
})
// Method should fail with InvalidArgument if the provided name only contains wildcards ('-')
t.Run("only wildcards", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.GetInstance(fx.ctx, &GetInstanceRequest{
Name: "projects/-/instances/-",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
}
func (fx *InstanceAdminInstanceTestSuiteConfig) testUpdate(t *testing.T) {
fx.maybeSkip(t)
// Method should fail with InvalidArgument if no name is provided.
t.Run("missing name", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
msg := fx.Update(parent)
msg.Name = ""
_, err := fx.service.UpdateInstance(fx.ctx, &UpdateInstanceRequest{
Instance: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Method should fail with InvalidArgument if provided name is not valid.
t.Run("invalid name", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
msg := fx.Update(parent)
msg.Name = "invalid resource name"
_, err := fx.service.UpdateInstance(fx.ctx, &UpdateInstanceRequest{
Instance: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
parent := fx.nextParent(t, false)
created := fx.create(t, parent)
// Method should fail with NotFound if the resource does not exist.
t.Run("not found", func(t *testing.T) {
fx.maybeSkip(t)
msg := fx.Update(parent)
msg.Name = created.Name + "notfound"
_, err := fx.service.UpdateInstance(fx.ctx, &UpdateInstanceRequest{
Instance: msg,
})
assert.Equal(t, codes.NotFound, status.Code(err), err)
})
// Method should fail with InvalidArgument if any required field is missing
// when called with '*' update_mask.
t.Run("required fields", func(t *testing.T) {
fx.maybeSkip(t)
t.Run(".name", func(t *testing.T) {
fx.maybeSkip(t)
msg := proto.Clone(created).(*Instance)
container := msg
if container == nil {
t.Skip("not reachable")
}
fd := container.ProtoReflect().Descriptor().Fields().ByName("name")
container.ProtoReflect().Clear(fd)
_, err := fx.service.UpdateInstance(fx.ctx, &UpdateInstanceRequest{
Instance: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
t.Run(".config", func(t *testing.T) {
fx.maybeSkip(t)
msg := proto.Clone(created).(*Instance)
container := msg
if container == nil {
t.Skip("not reachable")
}
fd := container.ProtoReflect().Descriptor().Fields().ByName("config")
container.ProtoReflect().Clear(fd)
_, err := fx.service.UpdateInstance(fx.ctx, &UpdateInstanceRequest{
Instance: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
t.Run(".display_name", func(t *testing.T) {
fx.maybeSkip(t)
msg := proto.Clone(created).(*Instance)
container := msg
if container == nil {
t.Skip("not reachable")
}
fd := container.ProtoReflect().Descriptor().Fields().ByName("display_name")
container.ProtoReflect().Clear(fd)
_, err := fx.service.UpdateInstance(fx.ctx, &UpdateInstanceRequest{
Instance: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
t.Run(".autoscaling_config.autoscaling_limits", func(t *testing.T) {
fx.maybeSkip(t)
msg := proto.Clone(created).(*Instance)
container := msg.GetAutoscalingConfig()
if container == nil {
t.Skip("not reachable")
}
fd := container.ProtoReflect().Descriptor().Fields().ByName("autoscaling_limits")
container.ProtoReflect().Clear(fd)
_, err := fx.service.UpdateInstance(fx.ctx, &UpdateInstanceRequest{
Instance: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
t.Run(".autoscaling_config.autoscaling_targets", func(t *testing.T) {
fx.maybeSkip(t)
msg := proto.Clone(created).(*Instance)
container := msg.GetAutoscalingConfig()
if container == nil {
t.Skip("not reachable")
}
fd := container.ProtoReflect().Descriptor().Fields().ByName("autoscaling_targets")
container.ProtoReflect().Clear(fd)
_, err := fx.service.UpdateInstance(fx.ctx, &UpdateInstanceRequest{
Instance: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
t.Run(".autoscaling_config.autoscaling_targets.high_priority_cpu_utilization_percent", func(t *testing.T) {
fx.maybeSkip(t)
msg := proto.Clone(created).(*Instance)
container := msg.GetAutoscalingConfig().GetAutoscalingTargets()
if container == nil {
t.Skip("not reachable")
}
fd := container.ProtoReflect().Descriptor().Fields().ByName("high_priority_cpu_utilization_percent")
container.ProtoReflect().Clear(fd)
_, err := fx.service.UpdateInstance(fx.ctx, &UpdateInstanceRequest{
Instance: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
t.Run(".autoscaling_config.autoscaling_targets.storage_utilization_percent", func(t *testing.T) {
fx.maybeSkip(t)
msg := proto.Clone(created).(*Instance)
container := msg.GetAutoscalingConfig().GetAutoscalingTargets()
if container == nil {
t.Skip("not reachable")
}
fd := container.ProtoReflect().Descriptor().Fields().ByName("storage_utilization_percent")
container.ProtoReflect().Clear(fd)
_, err := fx.service.UpdateInstance(fx.ctx, &UpdateInstanceRequest{
Instance: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
})
}
func (fx *InstanceAdminInstanceTestSuiteConfig) testList(t *testing.T) {
fx.maybeSkip(t)
// Method should fail with InvalidArgument if provided parent is invalid.
t.Run("invalid parent", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.ListInstances(fx.ctx, &ListInstancesRequest{
Parent: "invalid resource name",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Method should fail with InvalidArgument is provided page token is not valid.
t.Run("invalid page token", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
_, err := fx.service.ListInstances(fx.ctx, &ListInstancesRequest{
Parent: parent,
PageToken: "invalid page token",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Method should fail with InvalidArgument is provided page size is negative.
t.Run("negative page size", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
_, err := fx.service.ListInstances(fx.ctx, &ListInstancesRequest{
Parent: parent,
PageSize: -10,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
const resourcesCount = 15
parent := fx.nextParent(t, true)
parentMsgs := make([]*Instance, resourcesCount)
for i := 0; i < resourcesCount; i++ {
parentMsgs[i] = fx.create(t, parent)
}
// If parent is provided the method must only return resources
// under that parent.
t.Run("isolation", func(t *testing.T) {
fx.maybeSkip(t)
response, err := fx.service.ListInstances(fx.ctx, &ListInstancesRequest{
Parent: parent,
PageSize: 999,
})
assert.NilError(t, err)
assert.DeepEqual(
t,
parentMsgs,
response.Instances,
cmpopts.SortSlices(func(a, b *Instance) bool {
return a.Name < b.Name
}),
protocmp.Transform(),
)
})
// If there are no more resources, next_page_token should not be set.
t.Run("last page", func(t *testing.T) {
fx.maybeSkip(t)
response, err := fx.service.ListInstances(fx.ctx, &ListInstancesRequest{
Parent: parent,
PageSize: resourcesCount,
})
assert.NilError(t, err)
assert.Equal(t, "", response.NextPageToken)
})
// If there are more resources, next_page_token should be set.
t.Run("more pages", func(t *testing.T) {
fx.maybeSkip(t)
response, err := fx.service.ListInstances(fx.ctx, &ListInstancesRequest{
Parent: parent,
PageSize: resourcesCount - 1,
})
assert.NilError(t, err)
assert.Check(t, response.NextPageToken != "")
})
// Listing resource one by one should eventually return all resources.
t.Run("one by one", func(t *testing.T) {
fx.maybeSkip(t)
msgs := make([]*Instance, 0, resourcesCount)
var nextPageToken string
for {
response, err := fx.service.ListInstances(fx.ctx, &ListInstancesRequest{
Parent: parent,
PageSize: 1,
PageToken: nextPageToken,
})
assert.NilError(t, err)
assert.Equal(t, 1, len(response.Instances))
msgs = append(msgs, response.Instances...)
nextPageToken = response.NextPageToken
if nextPageToken == "" {
break
}
}
assert.DeepEqual(
t,
parentMsgs,
msgs,
cmpopts.SortSlices(func(a, b *Instance) bool {
return a.Name < b.Name
}),
protocmp.Transform(),
)
})
// Method should not return deleted resources.
t.Run("deleted", func(t *testing.T) {
fx.maybeSkip(t)
const deleteCount = 5
for i := 0; i < deleteCount; i++ {
_, err := fx.service.DeleteInstance(fx.ctx, &DeleteInstanceRequest{
Name: parentMsgs[i].Name,
})
assert.NilError(t, err)
}
response, err := fx.service.ListInstances(fx.ctx, &ListInstancesRequest{
Parent: parent,
PageSize: 9999,
})
assert.NilError(t, err)
assert.DeepEqual(
t,
parentMsgs[deleteCount:],
response.Instances,
cmpopts.SortSlices(func(a, b *Instance) bool {
return a.Name < b.Name
}),
protocmp.Transform(),
)
})
}
func (fx *InstanceAdminInstanceTestSuiteConfig) testDelete(t *testing.T) {
fx.maybeSkip(t)
// Method should fail with InvalidArgument if no name is provided.
t.Run("missing name", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.DeleteInstance(fx.ctx, &DeleteInstanceRequest{
Name: "",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Method should fail with InvalidArgument if the provided name is not valid.
t.Run("invalid name", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.DeleteInstance(fx.ctx, &DeleteInstanceRequest{
Name: "invalid resource name",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Resource should be deleted without errors if it exists.
t.Run("exists", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
created := fx.create(t, parent)
_, err := fx.service.DeleteInstance(fx.ctx, &DeleteInstanceRequest{
Name: created.Name,
})
assert.NilError(t, err)
})
// Method should fail with NotFound if the resource does not exist.
t.Run("not found", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
created := fx.create(t, parent)
_, err := fx.service.DeleteInstance(fx.ctx, &DeleteInstanceRequest{
Name: created.Name + "notfound",
})
assert.Equal(t, codes.NotFound, status.Code(err), err)
})
// Method should fail with InvalidArgument if the provided name only contains wildcards ('-')
t.Run("only wildcards", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.DeleteInstance(fx.ctx, &DeleteInstanceRequest{
Name: "projects/-/instances/-",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
}
func (fx *InstanceAdminInstanceTestSuiteConfig) nextParent(t *testing.T, pristine bool) string {
if pristine {
fx.currParent++
}
if fx.currParent >= len(fx.Parents) {
t.Fatal("need at least", fx.currParent+1, "parents")
}
return fx.Parents[fx.currParent]
}
func (fx *InstanceAdminInstanceTestSuiteConfig) peekNextParent(t *testing.T) string {
next := fx.currParent + 1
if next >= len(fx.Parents) {
t.Fatal("need at least", next+1, "parents")
}
return fx.Parents[next]
}
func (fx *InstanceAdminInstanceTestSuiteConfig) maybeSkip(t *testing.T) {
for _, skip := range fx.Skip {
if strings.Contains(t.Name(), skip) {
t.Skip("skipped because of .Skip")
}
}
}
func (fx *InstanceAdminInstanceTestSuiteConfig) create(t *testing.T, parent string) *Instance {
t.Helper()
t.Skip("Long running create method not supported")
return nil
}
type InstanceAdminInstanceConfigTestSuiteConfig struct {
ctx context.Context
service InstanceAdminServer
currParent int
// The parents to use when creating resources.
// At least one parent needs to be set. Depending on methods available on the resource,
// more may be required. If insufficient number of parents are
// provided the test will fail.
Parents []string
// Create should return a resource which is valid to create, i.e.
// all required fields set.
Create func(parent string) *InstanceConfig
// Update should return a resource which is valid to update, i.e.
// all required fields set.
Update func(parent string) *InstanceConfig
// Patterns of tests to skip.
// For example if a service has a Get method:
// Skip: ["Get"] will skip all tests for Get.
// Skip: ["Get/persisted"] will only skip the subtest called "persisted" of Get.
Skip []string
}
func (fx *InstanceAdminInstanceConfigTestSuiteConfig) test(t *testing.T) {
t.Run("Create", fx.testCreate)
t.Run("Get", fx.testGet)
t.Run("Update", fx.testUpdate)
t.Run("List", fx.testList)
t.Run("Delete", fx.testDelete)
}
func (fx *InstanceAdminInstanceConfigTestSuiteConfig) testCreate(t *testing.T) {
fx.maybeSkip(t)
// Method should fail with InvalidArgument if no parent is provided.
t.Run("missing parent", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.CreateInstanceConfig(fx.ctx, &CreateInstanceConfigRequest{
Parent: "",
InstanceConfig: fx.Create(fx.nextParent(t, false)),
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Method should fail with InvalidArgument if provided parent is invalid.
t.Run("invalid parent", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.CreateInstanceConfig(fx.ctx, &CreateInstanceConfigRequest{
Parent: "invalid resource name",
InstanceConfig: fx.Create(fx.nextParent(t, false)),
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// The method should fail with InvalidArgument if the resource has any
// resource references and they are invalid.
t.Run("resource references", func(t *testing.T) {
fx.maybeSkip(t)
t.Run(".base_config", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
msg := fx.Create(parent)
container := msg
if container == nil {
t.Skip("not reachable")
}
container.BaseConfig = "invalid resource name"
_, err := fx.service.CreateInstanceConfig(fx.ctx, &CreateInstanceConfigRequest{
Parent: parent,
InstanceConfig: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
})
}
func (fx *InstanceAdminInstanceConfigTestSuiteConfig) testGet(t *testing.T) {
fx.maybeSkip(t)
// Method should fail with InvalidArgument if no name is provided.
t.Run("missing name", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.GetInstanceConfig(fx.ctx, &GetInstanceConfigRequest{
Name: "",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Method should fail with InvalidArgument if the provided name is not valid.
t.Run("invalid name", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.GetInstanceConfig(fx.ctx, &GetInstanceConfigRequest{
Name: "invalid resource name",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Resource should be returned without errors if it exists.
t.Run("exists", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
created := fx.create(t, parent)
msg, err := fx.service.GetInstanceConfig(fx.ctx, &GetInstanceConfigRequest{
Name: created.Name,
})
assert.NilError(t, err)
assert.DeepEqual(t, msg, created, protocmp.Transform())
})
// Method should fail with NotFound if the resource does not exist.
t.Run("not found", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
created := fx.create(t, parent)
_, err := fx.service.GetInstanceConfig(fx.ctx, &GetInstanceConfigRequest{
Name: created.Name + "notfound",
})
assert.Equal(t, codes.NotFound, status.Code(err), err)
})
// Method should fail with InvalidArgument if the provided name only contains wildcards ('-')
t.Run("only wildcards", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.GetInstanceConfig(fx.ctx, &GetInstanceConfigRequest{
Name: "projects/-/instanceConfigs/-",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
}
func (fx *InstanceAdminInstanceConfigTestSuiteConfig) testUpdate(t *testing.T) {
fx.maybeSkip(t)
// Method should fail with InvalidArgument if no name is provided.
t.Run("missing name", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
msg := fx.Update(parent)
msg.Name = ""
_, err := fx.service.UpdateInstanceConfig(fx.ctx, &UpdateInstanceConfigRequest{
InstanceConfig: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Method should fail with InvalidArgument if provided name is not valid.
t.Run("invalid name", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
msg := fx.Update(parent)
msg.Name = "invalid resource name"
_, err := fx.service.UpdateInstanceConfig(fx.ctx, &UpdateInstanceConfigRequest{
InstanceConfig: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
parent := fx.nextParent(t, false)
created := fx.create(t, parent)
// Method should fail with NotFound if the resource does not exist.
t.Run("not found", func(t *testing.T) {
fx.maybeSkip(t)
msg := fx.Update(parent)
msg.Name = created.Name + "notfound"
_, err := fx.service.UpdateInstanceConfig(fx.ctx, &UpdateInstanceConfigRequest{
InstanceConfig: msg,
})
assert.Equal(t, codes.NotFound, status.Code(err), err)
})
// The method should fail with InvalidArgument if the update_mask is invalid.
t.Run("invalid update mask", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.UpdateInstanceConfig(fx.ctx, &UpdateInstanceConfigRequest{
InstanceConfig: created,
UpdateMask: &fieldmaskpb.FieldMask{
Paths: []string{
"invalid_field_xyz",
},
},
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
}
func (fx *InstanceAdminInstanceConfigTestSuiteConfig) testList(t *testing.T) {
fx.maybeSkip(t)
// Method should fail with InvalidArgument if provided parent is invalid.
t.Run("invalid parent", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.ListInstanceConfigs(fx.ctx, &ListInstanceConfigsRequest{
Parent: "invalid resource name",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Method should fail with InvalidArgument is provided page token is not valid.
t.Run("invalid page token", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
_, err := fx.service.ListInstanceConfigs(fx.ctx, &ListInstanceConfigsRequest{
Parent: parent,
PageToken: "invalid page token",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Method should fail with InvalidArgument is provided page size is negative.
t.Run("negative page size", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
_, err := fx.service.ListInstanceConfigs(fx.ctx, &ListInstanceConfigsRequest{
Parent: parent,
PageSize: -10,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
const resourcesCount = 15
parent := fx.nextParent(t, true)
parentMsgs := make([]*InstanceConfig, resourcesCount)
for i := 0; i < resourcesCount; i++ {
parentMsgs[i] = fx.create(t, parent)
}
// If parent is provided the method must only return resources
// under that parent.
t.Run("isolation", func(t *testing.T) {
fx.maybeSkip(t)
response, err := fx.service.ListInstanceConfigs(fx.ctx, &ListInstanceConfigsRequest{
Parent: parent,
PageSize: 999,
})
assert.NilError(t, err)
assert.DeepEqual(
t,
parentMsgs,
response.InstanceConfigs,
cmpopts.SortSlices(func(a, b *InstanceConfig) bool {
return a.Name < b.Name
}),
protocmp.Transform(),
)
})
// If there are no more resources, next_page_token should not be set.
t.Run("last page", func(t *testing.T) {
fx.maybeSkip(t)
response, err := fx.service.ListInstanceConfigs(fx.ctx, &ListInstanceConfigsRequest{
Parent: parent,
PageSize: resourcesCount,
})
assert.NilError(t, err)
assert.Equal(t, "", response.NextPageToken)
})
// If there are more resources, next_page_token should be set.
t.Run("more pages", func(t *testing.T) {
fx.maybeSkip(t)
response, err := fx.service.ListInstanceConfigs(fx.ctx, &ListInstanceConfigsRequest{
Parent: parent,
PageSize: resourcesCount - 1,
})
assert.NilError(t, err)
assert.Check(t, response.NextPageToken != "")
})
// Listing resource one by one should eventually return all resources.
t.Run("one by one", func(t *testing.T) {
fx.maybeSkip(t)
msgs := make([]*InstanceConfig, 0, resourcesCount)
var nextPageToken string
for {
response, err := fx.service.ListInstanceConfigs(fx.ctx, &ListInstanceConfigsRequest{
Parent: parent,
PageSize: 1,
PageToken: nextPageToken,
})
assert.NilError(t, err)
assert.Equal(t, 1, len(response.InstanceConfigs))
msgs = append(msgs, response.InstanceConfigs...)
nextPageToken = response.NextPageToken
if nextPageToken == "" {
break
}
}
assert.DeepEqual(
t,
parentMsgs,
msgs,
cmpopts.SortSlices(func(a, b *InstanceConfig) bool {
return a.Name < b.Name
}),
protocmp.Transform(),
)
})
// Method should not return deleted resources.
t.Run("deleted", func(t *testing.T) {
fx.maybeSkip(t)
const deleteCount = 5
for i := 0; i < deleteCount; i++ {
_, err := fx.service.DeleteInstanceConfig(fx.ctx, &DeleteInstanceConfigRequest{
Name: parentMsgs[i].Name,
})
assert.NilError(t, err)
}
response, err := fx.service.ListInstanceConfigs(fx.ctx, &ListInstanceConfigsRequest{
Parent: parent,
PageSize: 9999,
})
assert.NilError(t, err)
assert.DeepEqual(
t,
parentMsgs[deleteCount:],
response.InstanceConfigs,
cmpopts.SortSlices(func(a, b *InstanceConfig) bool {
return a.Name < b.Name
}),
protocmp.Transform(),
)
})
}
func (fx *InstanceAdminInstanceConfigTestSuiteConfig) testDelete(t *testing.T) {
fx.maybeSkip(t)
// Method should fail with InvalidArgument if no name is provided.
t.Run("missing name", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.DeleteInstanceConfig(fx.ctx, &DeleteInstanceConfigRequest{
Name: "",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})