55 "fmt"
66 "reflect"
77 "sort"
8+ "time"
89
910 "testing"
1011
@@ -21,8 +22,10 @@ import (
2122 appsv1 "k8s.io/api/apps/v1"
2223 v1 "k8s.io/api/core/v1"
2324 policyv1beta1 "k8s.io/api/policy/v1beta1"
25+ k8serrors "k8s.io/apimachinery/pkg/api/errors"
2426 "k8s.io/apimachinery/pkg/api/resource"
2527 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+ "k8s.io/apimachinery/pkg/runtime/schema"
2629 "k8s.io/apimachinery/pkg/types"
2730 "k8s.io/client-go/kubernetes/fake"
2831 v1core "k8s.io/client-go/kubernetes/typed/core/v1"
@@ -640,8 +643,12 @@ func TestSecretVolume(t *testing.T) {
640643}
641644
642645const (
643- testPodEnvironmentConfigMapName = "pod_env_cm"
644- testPodEnvironmentSecretName = "pod_env_sc"
646+ testPodEnvironmentConfigMapName = "pod_env_cm"
647+ testPodEnvironmentSecretName = "pod_env_sc"
648+ testPodEnvironmentObjectNotExists = "idonotexist"
649+ testPodEnvironmentSecretNameAPIError = "pod_env_sc_apierror"
650+ testResourceCheckInterval = 3
651+ testResourceCheckTimeout = 10
645652)
646653
647654type mockSecret struct {
@@ -653,8 +660,11 @@ type mockConfigMap struct {
653660}
654661
655662func (c * mockSecret ) Get (ctx context.Context , name string , options metav1.GetOptions ) (* v1.Secret , error ) {
663+ if name == testPodEnvironmentSecretNameAPIError {
664+ return nil , fmt .Errorf ("Secret PodEnvironmentSecret API error" )
665+ }
656666 if name != testPodEnvironmentSecretName {
657- return nil , fmt . Errorf ( "Secret PodEnvironmentSecret not found" )
667+ return nil , k8serrors . NewNotFound (schema. GroupResource { Group : "core" , Resource : "secret" }, name )
658668 }
659669 secret := & v1.Secret {}
660670 secret .Name = testPodEnvironmentSecretName
@@ -723,7 +733,7 @@ func TestPodEnvironmentConfigMapVariables(t *testing.T) {
723733 opConfig : config.Config {
724734 Resources : config.Resources {
725735 PodEnvironmentConfigMap : spec.NamespacedName {
726- Name : "idonotexist" ,
736+ Name : testPodEnvironmentObjectNotExists ,
727737 },
728738 },
729739 },
@@ -774,6 +784,7 @@ func TestPodEnvironmentConfigMapVariables(t *testing.T) {
774784
775785// Test if the keys of an existing secret are properly referenced
776786func TestPodEnvironmentSecretVariables (t * testing.T ) {
787+ maxRetries := int (testResourceCheckTimeout / testResourceCheckInterval )
777788 testName := "TestPodEnvironmentSecretVariables"
778789 tests := []struct {
779790 subTest string
@@ -789,16 +800,31 @@ func TestPodEnvironmentSecretVariables(t *testing.T) {
789800 subTest : "Secret referenced by PodEnvironmentSecret does not exist" ,
790801 opConfig : config.Config {
791802 Resources : config.Resources {
792- PodEnvironmentSecret : "idonotexist" ,
803+ PodEnvironmentSecret : testPodEnvironmentObjectNotExists ,
804+ ResourceCheckInterval : time .Duration (testResourceCheckInterval ),
805+ ResourceCheckTimeout : time .Duration (testResourceCheckTimeout ),
806+ },
807+ },
808+ err : fmt .Errorf ("could not read Secret PodEnvironmentSecretName: still failing after %d retries: secret.core %q not found" , maxRetries , testPodEnvironmentObjectNotExists ),
809+ },
810+ {
811+ subTest : "API error during PodEnvironmentSecret retrieval" ,
812+ opConfig : config.Config {
813+ Resources : config.Resources {
814+ PodEnvironmentSecret : testPodEnvironmentSecretNameAPIError ,
815+ ResourceCheckInterval : time .Duration (testResourceCheckInterval ),
816+ ResourceCheckTimeout : time .Duration (testResourceCheckTimeout ),
793817 },
794818 },
795- err : fmt .Errorf ("could not read Secret PodEnvironmentSecretName: Secret PodEnvironmentSecret not found " ),
819+ err : fmt .Errorf ("could not read Secret PodEnvironmentSecretName: Secret PodEnvironmentSecret API error " ),
796820 },
797821 {
798822 subTest : "Pod environment vars reference all keys from secret configured by PodEnvironmentSecret" ,
799823 opConfig : config.Config {
800824 Resources : config.Resources {
801- PodEnvironmentSecret : testPodEnvironmentSecretName ,
825+ PodEnvironmentSecret : testPodEnvironmentSecretName ,
826+ ResourceCheckInterval : time .Duration (testResourceCheckInterval ),
827+ ResourceCheckTimeout : time .Duration (testResourceCheckTimeout ),
802828 },
803829 },
804830 envVars : []v1.EnvVar {
0 commit comments