5
5
"fmt"
6
6
"reflect"
7
7
"sort"
8
+ "time"
8
9
9
10
"testing"
10
11
@@ -21,8 +22,10 @@ import (
21
22
appsv1 "k8s.io/api/apps/v1"
22
23
v1 "k8s.io/api/core/v1"
23
24
policyv1beta1 "k8s.io/api/policy/v1beta1"
25
+ k8serrors "k8s.io/apimachinery/pkg/api/errors"
24
26
"k8s.io/apimachinery/pkg/api/resource"
25
27
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
+ "k8s.io/apimachinery/pkg/runtime/schema"
26
29
"k8s.io/apimachinery/pkg/types"
27
30
"k8s.io/client-go/kubernetes/fake"
28
31
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
@@ -640,8 +643,12 @@ func TestSecretVolume(t *testing.T) {
640
643
}
641
644
642
645
const (
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
645
652
)
646
653
647
654
type mockSecret struct {
@@ -653,8 +660,11 @@ type mockConfigMap struct {
653
660
}
654
661
655
662
func (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
+ }
656
666
if name != testPodEnvironmentSecretName {
657
- return nil , fmt . Errorf ( "Secret PodEnvironmentSecret not found" )
667
+ return nil , k8serrors . NewNotFound (schema. GroupResource { Group : "core" , Resource : "secret" }, name )
658
668
}
659
669
secret := & v1.Secret {}
660
670
secret .Name = testPodEnvironmentSecretName
@@ -723,7 +733,7 @@ func TestPodEnvironmentConfigMapVariables(t *testing.T) {
723
733
opConfig : config.Config {
724
734
Resources : config.Resources {
725
735
PodEnvironmentConfigMap : spec.NamespacedName {
726
- Name : "idonotexist" ,
736
+ Name : testPodEnvironmentObjectNotExists ,
727
737
},
728
738
},
729
739
},
@@ -774,6 +784,7 @@ func TestPodEnvironmentConfigMapVariables(t *testing.T) {
774
784
775
785
// Test if the keys of an existing secret are properly referenced
776
786
func TestPodEnvironmentSecretVariables (t * testing.T ) {
787
+ maxRetries := int (testResourceCheckTimeout / testResourceCheckInterval )
777
788
testName := "TestPodEnvironmentSecretVariables"
778
789
tests := []struct {
779
790
subTest string
@@ -789,16 +800,31 @@ func TestPodEnvironmentSecretVariables(t *testing.T) {
789
800
subTest : "Secret referenced by PodEnvironmentSecret does not exist" ,
790
801
opConfig : config.Config {
791
802
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 ),
793
817
},
794
818
},
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 " ),
796
820
},
797
821
{
798
822
subTest : "Pod environment vars reference all keys from secret configured by PodEnvironmentSecret" ,
799
823
opConfig : config.Config {
800
824
Resources : config.Resources {
801
- PodEnvironmentSecret : testPodEnvironmentSecretName ,
825
+ PodEnvironmentSecret : testPodEnvironmentSecretName ,
826
+ ResourceCheckInterval : time .Duration (testResourceCheckInterval ),
827
+ ResourceCheckTimeout : time .Duration (testResourceCheckTimeout ),
802
828
},
803
829
},
804
830
envVars : []v1.EnvVar {
0 commit comments