@@ -2,6 +2,7 @@ package handlers
22
33import (
44 "context"
5+ "fmt"
56 "testing"
67
78 "ambient-code-operator/internal/config"
@@ -13,6 +14,7 @@ import (
1314 "k8s.io/apimachinery/pkg/runtime"
1415 k8stypes "k8s.io/apimachinery/pkg/types"
1516 "k8s.io/client-go/kubernetes/fake"
17+ clienttesting "k8s.io/client-go/testing"
1618)
1719
1820// setupTestClient initializes a fake Kubernetes client for testing
@@ -568,6 +570,193 @@ func TestDeleteAmbientVertexSecret_NotFound(t *testing.T) {
568570 }
569571}
570572
573+ // TestApplyTrustedCABundle_ConfigMapPresent verifies that applyTrustedCABundle adds the volume
574+ // and VolumeMount when the trusted-ca-bundle ConfigMap exists in the session namespace.
575+ func TestApplyTrustedCABundle_ConfigMapPresent (t * testing.T ) {
576+ cm := & corev1.ConfigMap {
577+ ObjectMeta : metav1.ObjectMeta {
578+ Name : types .TrustedCABundleConfigMapName ,
579+ Namespace : "session-ns" ,
580+ },
581+ Data : map [string ]string {
582+ "ca-bundle.crt" : "--- fake CA data ---" ,
583+ },
584+ }
585+ setupTestClient (cm )
586+
587+ pod := & corev1.Pod {
588+ Spec : corev1.PodSpec {
589+ Containers : []corev1.Container {
590+ {Name : "ambient-code-runner" },
591+ },
592+ },
593+ }
594+
595+ applyTrustedCABundle (config .K8sClient , "session-ns" , pod )
596+
597+ if len (pod .Spec .Volumes ) != 1 {
598+ t .Fatalf ("expected 1 volume, got %d" , len (pod .Spec .Volumes ))
599+ }
600+ vol := pod .Spec .Volumes [0 ]
601+ if vol .Name != "trusted-ca-bundle" {
602+ t .Errorf ("expected volume name 'trusted-ca-bundle', got %q" , vol .Name )
603+ }
604+ if vol .ConfigMap == nil || vol .ConfigMap .Name != types .TrustedCABundleConfigMapName {
605+ t .Errorf ("expected ConfigMap volume sourced from %q" , types .TrustedCABundleConfigMapName )
606+ }
607+
608+ mounts := pod .Spec .Containers [0 ].VolumeMounts
609+ if len (mounts ) != 1 {
610+ t .Fatalf ("expected 1 VolumeMount, got %d" , len (mounts ))
611+ }
612+ m := mounts [0 ]
613+ if m .Name != "trusted-ca-bundle" {
614+ t .Errorf ("expected mount name 'trusted-ca-bundle', got %q" , m .Name )
615+ }
616+ if m .MountPath != "/etc/pki/tls/certs/ca-bundle.crt" {
617+ t .Errorf ("unexpected MountPath: %q" , m .MountPath )
618+ }
619+ if m .SubPath != "ca-bundle.crt" {
620+ t .Errorf ("expected SubPath 'ca-bundle.crt', got %q" , m .SubPath )
621+ }
622+ if ! m .ReadOnly {
623+ t .Error ("expected ReadOnly=true" )
624+ }
625+ }
626+
627+ // TestApplyTrustedCABundle_ConfigMapAbsent verifies that applyTrustedCABundle leaves the pod
628+ // unchanged when the trusted-ca-bundle ConfigMap is not present in the session namespace.
629+ func TestApplyTrustedCABundle_ConfigMapAbsent (t * testing.T ) {
630+ setupTestClient () // no ConfigMap
631+
632+ pod := & corev1.Pod {
633+ Spec : corev1.PodSpec {
634+ Containers : []corev1.Container {
635+ {Name : "ambient-code-runner" },
636+ },
637+ },
638+ }
639+
640+ applyTrustedCABundle (config .K8sClient , "session-ns" , pod )
641+
642+ if len (pod .Spec .Volumes ) != 0 {
643+ t .Errorf ("expected no volumes, got %d" , len (pod .Spec .Volumes ))
644+ }
645+ if len (pod .Spec .Containers [0 ].VolumeMounts ) != 0 {
646+ t .Errorf ("expected no VolumeMounts, got %d" , len (pod .Spec .Containers [0 ].VolumeMounts ))
647+ }
648+ }
649+
650+ // TestApplyTrustedCABundle_ExistingMountsPreserved verifies that applyTrustedCABundle appends
651+ // to, rather than replacing, existing VolumeMounts on the runner container.
652+ func TestApplyTrustedCABundle_ExistingMountsPreserved (t * testing.T ) {
653+ cm := & corev1.ConfigMap {
654+ ObjectMeta : metav1.ObjectMeta {
655+ Name : types .TrustedCABundleConfigMapName ,
656+ Namespace : "session-ns" ,
657+ },
658+ Data : map [string ]string {
659+ "ca-bundle.crt" : "--- fake CA data ---" ,
660+ },
661+ }
662+ setupTestClient (cm )
663+
664+ existingMount := corev1.VolumeMount {
665+ Name : "runner-token" ,
666+ MountPath : "/var/run/secrets/ambient" ,
667+ ReadOnly : true ,
668+ }
669+ pod := & corev1.Pod {
670+ Spec : corev1.PodSpec {
671+ Volumes : []corev1.Volume {
672+ {Name : "runner-token" },
673+ },
674+ Containers : []corev1.Container {
675+ {
676+ Name : "ambient-code-runner" ,
677+ VolumeMounts : []corev1.VolumeMount {existingMount },
678+ },
679+ },
680+ },
681+ }
682+
683+ applyTrustedCABundle (config .K8sClient , "session-ns" , pod )
684+
685+ if len (pod .Spec .Volumes ) != 2 {
686+ t .Fatalf ("expected 2 volumes (runner-token + trusted-ca-bundle), got %d" , len (pod .Spec .Volumes ))
687+ }
688+ mounts := pod .Spec .Containers [0 ].VolumeMounts
689+ if len (mounts ) != 2 {
690+ t .Fatalf ("expected 2 VolumeMounts, got %d" , len (mounts ))
691+ }
692+ // Existing mount must still be at index 0
693+ if mounts [0 ].Name != "runner-token" {
694+ t .Errorf ("expected first mount to be 'runner-token', got %q" , mounts [0 ].Name )
695+ }
696+ if mounts [1 ].Name != "trusted-ca-bundle" {
697+ t .Errorf ("expected second mount to be 'trusted-ca-bundle', got %q" , mounts [1 ].Name )
698+ }
699+ }
700+
701+ // TestApplyTrustedCABundle_MissingKey verifies that applyTrustedCABundle leaves the pod
702+ // unchanged when the ConfigMap exists but lacks the ca-bundle.crt key.
703+ func TestApplyTrustedCABundle_MissingKey (t * testing.T ) {
704+ cm := & corev1.ConfigMap {
705+ ObjectMeta : metav1.ObjectMeta {
706+ Name : types .TrustedCABundleConfigMapName ,
707+ Namespace : "session-ns" ,
708+ },
709+ Data : map [string ]string {
710+ "wrong-key.pem" : "--- fake CA data ---" ,
711+ },
712+ }
713+ setupTestClient (cm )
714+
715+ pod := & corev1.Pod {
716+ Spec : corev1.PodSpec {
717+ Containers : []corev1.Container {
718+ {Name : "ambient-code-runner" },
719+ },
720+ },
721+ }
722+
723+ applyTrustedCABundle (config .K8sClient , "session-ns" , pod )
724+
725+ if len (pod .Spec .Volumes ) != 0 {
726+ t .Errorf ("expected no volumes when key is missing, got %d" , len (pod .Spec .Volumes ))
727+ }
728+ if len (pod .Spec .Containers [0 ].VolumeMounts ) != 0 {
729+ t .Errorf ("expected no VolumeMounts when key is missing, got %d" , len (pod .Spec .Containers [0 ].VolumeMounts ))
730+ }
731+ }
732+
733+ // TestApplyTrustedCABundle_APIError verifies that applyTrustedCABundle leaves the pod
734+ // unchanged when the ConfigMap GET returns a non-NotFound error.
735+ func TestApplyTrustedCABundle_APIError (t * testing.T ) {
736+ fakeClient := fake .NewSimpleClientset ()
737+ fakeClient .PrependReactor ("get" , "configmaps" , func (action clienttesting.Action ) (bool , runtime.Object , error ) {
738+ return true , nil , fmt .Errorf ("connection refused" )
739+ })
740+ config .K8sClient = fakeClient
741+
742+ pod := & corev1.Pod {
743+ Spec : corev1.PodSpec {
744+ Containers : []corev1.Container {
745+ {Name : "ambient-code-runner" },
746+ },
747+ },
748+ }
749+
750+ applyTrustedCABundle (config .K8sClient , "session-ns" , pod )
751+
752+ if len (pod .Spec .Volumes ) != 0 {
753+ t .Errorf ("expected no volumes on API error, got %d" , len (pod .Spec .Volumes ))
754+ }
755+ if len (pod .Spec .Containers [0 ].VolumeMounts ) != 0 {
756+ t .Errorf ("expected no VolumeMounts on API error, got %d" , len (pod .Spec .Containers [0 ].VolumeMounts ))
757+ }
758+ }
759+
571760// TestDeleteAmbientVertexSecret_NilAnnotations tests handling of secret with nil annotations
572761func TestDeleteAmbientVertexSecret_NilAnnotations (t * testing.T ) {
573762 secret := & corev1.Secret {
0 commit comments