@@ -463,6 +463,70 @@ func TestPossibleToUpdatePodWithNewCert(t *testing.T) {
463
463
assert .Equal (t , expectedCredSpec0 , extractContainerCredSpecContents (t , pod3 , testName3 ))
464
464
}
465
465
466
+ func TestPossibleHostnameRandomization (t * testing.T ) {
467
+ deployMethod := os .Getenv ("DEPLOY_METHOD" )
468
+ if deployMethod != "chart" {
469
+ t .Skip ("Non chart deployment method not supported for this test" )
470
+ }
471
+
472
+ webHookNs := os .Getenv ("NAMESPACE" )
473
+ webHookDeploymentName := os .Getenv ("DEPLOYMENT_NAME" )
474
+ webhook , err := kubeClient (t ).AppsV1 ().Deployments (webHookNs ).Get (context .Background (), webHookDeploymentName , metav1.GetOptions {})
475
+ if err != nil {
476
+ t .Fatal (err )
477
+ }
478
+
479
+ randomHostnameEnabled := false
480
+ for _ , envVar := range webhook .Spec .Template .Spec .Containers [0 ].Env {
481
+ if strings .EqualFold (envVar .Name , "RANDOM_HOSTNAME" ) && strings .EqualFold (envVar .Value , "true" ) {
482
+ randomHostnameEnabled = true
483
+ }
484
+ }
485
+
486
+ if randomHostnameEnabled {
487
+ testName1 := "happy-path-with-hostname-randomization"
488
+ credSpecTemplates1 := []string {"credspec-0" }
489
+ templates1 := []string {"credspecs-users-rbac-role" , "service-account" , "sa-rbac-binding" , "simple-with-gmsa" }
490
+
491
+ testConfig1 , tearDownFunc1 := integrationTestSetup (t , testName1 , credSpecTemplates1 , templates1 )
492
+ defer tearDownFunc1 ()
493
+
494
+ pod := waitForPodToComeUp (t , testConfig1 .Namespace , "app=" + testName1 )
495
+ assert .NotEqual (t , testName1 , pod .Spec .Hostname )
496
+ assert .Equal (t , 15 , len (pod .Spec .Hostname ))
497
+
498
+ testName2 := "hostnameset-no-hostname-randomization"
499
+ credSpecTemplates2 := []string {"credspec-0" }
500
+ templates2 := []string {"credspecs-users-rbac-role" , "service-account" , "sa-rbac-binding" , "simple-with-gmsa-hostname" }
501
+
502
+ testConfig2 , tearDownFunc2 := integrationTestSetup (t , testName2 , credSpecTemplates2 , templates2 )
503
+ defer tearDownFunc2 ()
504
+
505
+ pod = waitForPodToComeUp (t , testConfig2 .Namespace , "app=" + testName2 )
506
+ assert .Equal (t , testName2 , pod .Spec .Hostname )
507
+
508
+ testName3 := "nogmsa-hostname-randomization"
509
+ credSpecTemplates3 := []string {"credspec-0" }
510
+ templates3 := []string {"credspecs-users-rbac-role" , "service-account" , "sa-rbac-binding" , "simple-without-gmsa" }
511
+
512
+ testConfig3 , tearDownFunc3 := integrationTestSetup (t , testName3 , credSpecTemplates3 , templates3 )
513
+ defer tearDownFunc3 ()
514
+ pod = waitForPodToComeUp (t , testConfig3 .Namespace , "app=" + testName3 )
515
+
516
+ assert .Equal (t , "" , pod .Spec .Hostname )
517
+ } else {
518
+ testName4 := "notenabled-hostname-randomization"
519
+ credSpecTemplates4 := []string {"credspec-0" }
520
+ templates4 := []string {"credspecs-users-rbac-role" , "service-account" , "sa-rbac-binding" , "simple-with-gmsa" }
521
+
522
+ testConfig4 , tearDownFunc4 := integrationTestSetup (t , testName4 , credSpecTemplates4 , templates4 )
523
+ defer tearDownFunc4 ()
524
+ pod := waitForPodToComeUp (t , testConfig4 .Namespace , "app=" + testName4 )
525
+
526
+ assert .Equal (t , "" , pod .Spec .Hostname )
527
+ }
528
+ }
529
+
466
530
/* Helpers */
467
531
468
532
type testConfig struct {
0 commit comments