@@ -82,13 +82,29 @@ func simulateRunningOnGCE(t *testing.T, gce bool) {
82
82
t .Cleanup (func () { onGCE = oldOnGCE })
83
83
}
84
84
85
+ // ensure universeDomain is set to the expected default,
86
+ // and clean it up again after the test.
87
+ func useCleanUniverseDomain (t * testing.T ) {
88
+ universeDomainMu .Lock ()
89
+ defer universeDomainMu .Unlock ()
90
+ if universeDomain != "" {
91
+ t .Fatalf ("universe domain unexpectedly initialized: %v" , universeDomain )
92
+ }
93
+ t .Cleanup (func () {
94
+ universeDomainMu .Lock ()
95
+ universeDomain = ""
96
+ universeDomainMu .Unlock ()
97
+ })
98
+ }
99
+
85
100
// Tests the scenario where the bootstrap env vars are set and we're running on
86
101
// GCE. The test builds a google-c2p resolver and verifies that an xDS resolver
87
102
// is built and that we don't fallback to DNS (because federation is enabled by
88
103
// default).
89
104
func (s ) TestBuildWithBootstrapEnvSet (t * testing.T ) {
90
105
replaceResolvers (t )
91
106
simulateRunningOnGCE (t , true )
107
+ useCleanUniverseDomain (t )
92
108
93
109
builder := resolver .Get (c2pScheme )
94
110
for i , envP := range []* string {& envconfig .XDSBootstrapFileName , & envconfig .XDSBootstrapFileContent } {
@@ -118,6 +134,7 @@ func (s) TestBuildWithBootstrapEnvSet(t *testing.T) {
118
134
func (s ) TestBuildNotOnGCE (t * testing.T ) {
119
135
replaceResolvers (t )
120
136
simulateRunningOnGCE (t , false )
137
+ useCleanUniverseDomain (t )
121
138
builder := resolver .Get (c2pScheme )
122
139
123
140
// Build the google-c2p resolver.
@@ -152,6 +169,7 @@ func bootstrapConfig(t *testing.T, opts bootstrap.ConfigOptionsForTesting) *boot
152
169
func (s ) TestBuildXDS (t * testing.T ) {
153
170
replaceResolvers (t )
154
171
simulateRunningOnGCE (t , true )
172
+ useCleanUniverseDomain (t )
155
173
builder := resolver .Get (c2pScheme )
156
174
157
175
// Override the zone returned by the metadata server.
@@ -295,6 +313,7 @@ func (s) TestBuildXDS(t *testing.T) {
295
313
// google-c2p scheme with a non-empty authority and verifies that it fails with
296
314
// an expected error.
297
315
func (s ) TestBuildFailsWhenCalledWithAuthority (t * testing.T ) {
316
+ useCleanUniverseDomain (t )
298
317
uri := "google-c2p://an-authority/resource"
299
318
cc , err := grpc .Dial (uri , grpc .WithTransportCredentials (insecure .NewCredentials ()))
300
319
defer func () {
@@ -307,3 +326,188 @@ func (s) TestBuildFailsWhenCalledWithAuthority(t *testing.T) {
307
326
t .Fatalf ("grpc.Dial(%s) returned error: %v, want: %v" , uri , err , wantErr )
308
327
}
309
328
}
329
+
330
+ func (s ) TestSetUniverseDomainNonDefault (t * testing.T ) {
331
+ replaceResolvers (t )
332
+ simulateRunningOnGCE (t , true )
333
+ useCleanUniverseDomain (t )
334
+ builder := resolver .Get (c2pScheme )
335
+
336
+ // Override the zone returned by the metadata server.
337
+ oldGetZone := getZone
338
+ getZone = func (time.Duration ) string { return "test-zone" }
339
+ defer func () { getZone = oldGetZone }()
340
+
341
+ // Override IPv6 capability returned by the metadata server.
342
+ oldGetIPv6Capability := getIPv6Capable
343
+ getIPv6Capable = func (time.Duration ) bool { return false }
344
+ defer func () { getIPv6Capable = oldGetIPv6Capability }()
345
+
346
+ // Override the random func used in the node ID.
347
+ origRandInd := randInt
348
+ randInt = func () int { return 666 }
349
+ defer func () { randInt = origRandInd }()
350
+
351
+ // Set the universe domain
352
+ testUniverseDomain := "test-universe-domain.test"
353
+ if err := SetUniverseDomain (testUniverseDomain ); err != nil {
354
+ t .Fatalf ("SetUniverseDomain(%s) failed: %v" , testUniverseDomain , err )
355
+ }
356
+
357
+ // Now set universe domain to something different, it should fail
358
+ domain := "test-universe-domain-2.test"
359
+ err := SetUniverseDomain (domain )
360
+ wantErr := "already set"
361
+ if err == nil || ! strings .Contains (err .Error (), wantErr ) {
362
+ t .Fatalf ("googlec2p.SetUniverseDomain(%s) returned error: %v, want: %v" , domain , err , wantErr )
363
+ }
364
+
365
+ // Now explicitly set universe domain to the default, it should also fail
366
+ domain = "googleapis.com"
367
+ err = SetUniverseDomain (domain )
368
+ wantErr = "already set"
369
+ if err == nil || ! strings .Contains (err .Error (), wantErr ) {
370
+ t .Fatalf ("googlec2p.SetUniverseDomain(%s) returned error: %v, want: %v" , domain , err , wantErr )
371
+ }
372
+
373
+ // Now set universe domain to the original value, it should work
374
+ if err := SetUniverseDomain (testUniverseDomain ); err != nil {
375
+ t .Fatalf ("googlec2p.SetUniverseDomain(%s) failed: %v" , testUniverseDomain , err )
376
+ }
377
+
378
+ // Build the google-c2p resolver.
379
+ r , err := builder .Build (resolver.Target {}, nil , resolver.BuildOptions {})
380
+ if err != nil {
381
+ t .Fatalf ("failed to build resolver: %v" , err )
382
+ }
383
+ defer r .Close ()
384
+
385
+ // Build should return xDS, not DNS.
386
+ if r != testXDSResolver {
387
+ t .Fatalf ("Build() returned %#v, want xds resolver" , r )
388
+ }
389
+
390
+ gotConfig , err := bootstrap .GetConfiguration ()
391
+ if err != nil {
392
+ t .Fatalf ("Failed to get bootstrap config: %v" , err )
393
+ }
394
+
395
+ // Check that we use directpath-pa.test-universe-domain.test in the
396
+ // bootstrap config.
397
+ wantBootstrapConfig := bootstrapConfig (t , bootstrap.ConfigOptionsForTesting {
398
+ Servers : []byte (`[{
399
+ "server_uri": "dns:///directpath-pa.test-universe-domain.test",
400
+ "channel_creds": [{"type": "google_default"}],
401
+ "server_features": ["ignore_resource_deletion"]
402
+ }]` ),
403
+ Authorities : map [string ]json.RawMessage {
404
+ "traffic-director-c2p.xds.googleapis.com" : []byte (`{
405
+ "xds_servers": [
406
+ {
407
+ "server_uri": "dns:///directpath-pa.test-universe-domain.test",
408
+ "channel_creds": [{"type": "google_default"}],
409
+ "server_features": ["ignore_resource_deletion"]
410
+ }
411
+ ]
412
+ }` ),
413
+ },
414
+ Node : []byte (`{
415
+ "id": "C2P-666",
416
+ "locality": {"zone": "test-zone"}
417
+ }` ),
418
+ })
419
+ if diff := cmp .Diff (wantBootstrapConfig , gotConfig ); diff != "" {
420
+ t .Fatalf ("Unexpected diff in bootstrap config (-want +got):\n %s" , diff )
421
+ }
422
+ }
423
+
424
+ func (s ) TestDefaultUniverseDomain (t * testing.T ) {
425
+ replaceResolvers (t )
426
+ simulateRunningOnGCE (t , true )
427
+ useCleanUniverseDomain (t )
428
+ builder := resolver .Get (c2pScheme )
429
+
430
+ // Override the zone returned by the metadata server.
431
+ oldGetZone := getZone
432
+ getZone = func (time.Duration ) string { return "test-zone" }
433
+ defer func () { getZone = oldGetZone }()
434
+
435
+ // Override IPv6 capability returned by the metadata server.
436
+ oldGetIPv6Capability := getIPv6Capable
437
+ getIPv6Capable = func (time.Duration ) bool { return false }
438
+ defer func () { getIPv6Capable = oldGetIPv6Capability }()
439
+
440
+ // Override the random func used in the node ID.
441
+ origRandInd := randInt
442
+ randInt = func () int { return 666 }
443
+ defer func () { randInt = origRandInd }()
444
+
445
+ // Build the google-c2p resolver.
446
+ r , err := builder .Build (resolver.Target {}, nil , resolver.BuildOptions {})
447
+ if err != nil {
448
+ t .Fatalf ("failed to build resolver: %v" , err )
449
+ }
450
+ defer r .Close ()
451
+
452
+ // Build should return xDS, not DNS.
453
+ if r != testXDSResolver {
454
+ t .Fatalf ("Build() returned %#v, want xds resolver" , r )
455
+ }
456
+
457
+ gotConfig , err := bootstrap .GetConfiguration ()
458
+ if err != nil {
459
+ t .Fatalf ("Failed to get bootstrap config: %v" , err )
460
+ }
461
+
462
+ // Check that we use directpath-pa.googleapis.com in the bootstrap config
463
+ wantBootstrapConfig := bootstrapConfig (t , bootstrap.ConfigOptionsForTesting {
464
+ Servers : []byte (`[{
465
+ "server_uri": "dns:///directpath-pa.googleapis.com",
466
+ "channel_creds": [{"type": "google_default"}],
467
+ "server_features": ["ignore_resource_deletion"]
468
+ }]` ),
469
+ Authorities : map [string ]json.RawMessage {
470
+ "traffic-director-c2p.xds.googleapis.com" : []byte (`{
471
+ "xds_servers": [
472
+ {
473
+ "server_uri": "dns:///directpath-pa.googleapis.com",
474
+ "channel_creds": [{"type": "google_default"}],
475
+ "server_features": ["ignore_resource_deletion"]
476
+ }
477
+ ]
478
+ }` ),
479
+ },
480
+ Node : []byte (`{
481
+ "id": "C2P-666",
482
+ "locality": {"zone": "test-zone"}
483
+ }` ),
484
+ })
485
+ if diff := cmp .Diff (wantBootstrapConfig , gotConfig ); diff != "" {
486
+ t .Fatalf ("Unexpected diff in bootstrap config (-want +got):\n %s" , diff )
487
+ }
488
+
489
+ // Now set universe domain to something different than the default, it should fail
490
+ domain := "test-universe-domain.test"
491
+ err = SetUniverseDomain (domain )
492
+ wantErr := "already set"
493
+ if err == nil || ! strings .Contains (err .Error (), wantErr ) {
494
+ t .Fatalf ("googlec2p.SetUniverseDomain(%s) returned error: %v, want: %v" , domain , err , wantErr )
495
+ }
496
+
497
+ // Now explicitly set universe domain to the default, it should work
498
+ domain = "googleapis.com"
499
+ if err := SetUniverseDomain (domain ); err != nil {
500
+ t .Fatalf ("googlec2p.SetUniverseDomain(%s) failed: %v" , domain , err )
501
+ }
502
+ }
503
+
504
+ func (s ) TestSetUniverseDomainEmptyString (t * testing.T ) {
505
+ replaceResolvers (t )
506
+ simulateRunningOnGCE (t , true )
507
+ useCleanUniverseDomain (t )
508
+ wantErr := "cannot be empty"
509
+ err := SetUniverseDomain ("" )
510
+ if err == nil || ! strings .Contains (err .Error (), wantErr ) {
511
+ t .Fatalf ("googlec2p.SetUniverseDomain(\" \" ) returned error: %v, want: %v" , err , wantErr )
512
+ }
513
+ }
0 commit comments