@@ -18,6 +18,7 @@ import (
18
18
"context"
19
19
"encoding/json"
20
20
"fmt"
21
+ "path/filepath"
21
22
"reflect"
22
23
"strconv"
23
24
"strings"
@@ -2800,13 +2801,40 @@ func TestMapObjectToRepoSync(t *testing.T) {
2800
2801
}
2801
2802
}
2802
2803
2804
+ func validateContainerEnv (container , key , expectedValue string ) validateFunc {
2805
+ return func (deployment * appsv1.Deployment ) error {
2806
+ hasContainer := false
2807
+ var envVars []corev1.EnvVar
2808
+ for _ , c := range deployment .Spec .Template .Spec .Containers {
2809
+ if c .Name == container {
2810
+ hasContainer = true
2811
+ envVars = c .Env
2812
+ }
2813
+ }
2814
+ if ! hasContainer {
2815
+ return fmt .Errorf ("the container %q is not found in the deployment %q/%q" , container , deployment .Namespace , deployment .Name )
2816
+ }
2817
+
2818
+ for _ , env := range envVars {
2819
+ if env .Name == key {
2820
+ if env .Value == expectedValue {
2821
+ return nil
2822
+ }
2823
+ return fmt .Errorf ("the value for ENV %q in the %q container is expected to be %q, but got %q" , key , container , expectedValue , env .Value )
2824
+ }
2825
+ }
2826
+ return fmt .Errorf ("the ENV %q is not found in the %q container" , key , container )
2827
+ }
2828
+ }
2829
+
2803
2830
func TestInjectFleetWorkloadIdentityCredentialsToRepoSync (t * testing.T ) {
2804
2831
// Mock out parseDeployment for testing.
2805
2832
parseDeployment = parsedDeployment
2806
2833
2807
2834
rs := repoSyncWithGit (reposyncNs , reposyncName , reposyncRef (gitRevision ), reposyncBranch (branch ), reposyncSecretType (configsync .AuthGCPServiceAccount ), reposyncGCPSAEmail (gcpSAEmail ))
2808
2835
reqNamespacedName := namespacedName (rs .Name , rs .Namespace )
2809
2836
fakeClient , fakeDynamicClient , testReconciler := setupNSReconciler (t , rs , secretObj (t , reposyncSSHKey , configsync .AuthSSH , v1beta1 .GitSource , core .Namespace (rs .Namespace )))
2837
+ // The membership doesn't have WorkloadIdentityPool and IdentityProvider specified, so FWI creds won't be injected.
2810
2838
testReconciler .membership = & hubv1.Membership {
2811
2839
Spec : hubv1.MembershipSpec {
2812
2840
Owner : hubv1.MembershipOwner {
@@ -2842,6 +2870,7 @@ func TestInjectFleetWorkloadIdentityCredentialsToRepoSync(t *testing.T) {
2842
2870
workloadIdentityPool := "test-gke-dev.svc.id.goog"
2843
2871
testReconciler .membership = & hubv1.Membership {
2844
2872
Spec : hubv1.MembershipSpec {
2873
+ // Configuring WorkloadIdentityPool and IdentityProvider to validate if FWI creds are injected.
2845
2874
WorkloadIdentityPool : workloadIdentityPool ,
2846
2875
IdentityProvider : "https://container.googleapis.com/v1/projects/test-gke-dev/locations/us-central1-c/clusters/fleet-workload-identity-test-cluster" ,
2847
2876
},
@@ -2865,7 +2894,11 @@ func TestInjectFleetWorkloadIdentityCredentialsToRepoSync(t *testing.T) {
2865
2894
wantDeployments = map [core.ID ]* appsv1.Deployment {core .IDOf (repoDeployment ): repoDeployment }
2866
2895
2867
2896
// compare Deployment.
2868
- if err := validateDeployments (wantDeployments , fakeDynamicClient ); err != nil {
2897
+ if err := validateDeployments (wantDeployments , fakeDynamicClient ,
2898
+ // Validate the credentials are injected in the askpass container
2899
+ validateContainerEnv (reconcilermanager .GCENodeAskpassSidecar , gsaEmailEnvKey , gcpSAEmail ),
2900
+ validateContainerEnv (reconcilermanager .GCENodeAskpassSidecar , googleApplicationCredentialsEnvKey , filepath .Join (gcpKSATokenDir , googleApplicationCredentialsFile )),
2901
+ ); err != nil {
2869
2902
t .Errorf ("Deployment validation failed. err: %v" , err )
2870
2903
}
2871
2904
if t .Failed () {
@@ -3808,8 +3841,10 @@ func validateClusterRoleBinding(want *rbacv1.ClusterRoleBinding, fakeClient *syn
3808
3841
return nil
3809
3842
}
3810
3843
3844
+ type validateFunc func (* appsv1.Deployment ) error
3845
+
3811
3846
// validateDeployments validates that important fields in the `wants` deployments match those same fields in the current deployments found in the unstructured Map
3812
- func validateDeployments (wants map [core.ID ]* appsv1.Deployment , fakeDynamicClient * syncerFake.DynamicClient ) error {
3847
+ func validateDeployments (wants map [core.ID ]* appsv1.Deployment , fakeDynamicClient * syncerFake.DynamicClient , validations ... validateFunc ) error {
3813
3848
ctx := context .Background ()
3814
3849
for id , want := range wants {
3815
3850
uObj , err := fakeDynamicClient .Resource (kinds .DeploymentResource ()).
@@ -3928,6 +3963,12 @@ func validateDeployments(wants map[core.ID]*appsv1.Deployment, fakeDynamicClient
3928
3963
if diff := cmp .Diff (want .ResourceVersion , got .ResourceVersion ); diff != "" {
3929
3964
return errors .Errorf ("Unexpected Deployment ResourceVersion found for %q. Diff (- want, + got): %v" , id , diff )
3930
3965
}
3966
+
3967
+ for _ , v := range validations {
3968
+ if err := v (got ); err != nil {
3969
+ return err
3970
+ }
3971
+ }
3931
3972
}
3932
3973
return nil
3933
3974
}
0 commit comments