@@ -22,6 +22,7 @@ import (
2222 "github.com/stretchr/testify/require"
2323 appsv1 "k8s.io/api/apps/v1"
2424 corev1 "k8s.io/api/core/v1"
25+ k8sapierr "k8s.io/apimachinery/pkg/api/errors"
2526 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627 "k8s.io/apimachinery/pkg/types"
2728 "k8s.io/client-go/tools/record"
@@ -852,13 +853,18 @@ configuration:
852853}
853854
854855func TestConfigurationValuesFrom (t * testing.T ) {
856+ commonExpectedData := map [string ]string {"PODINFO_UI_MESSAGE" : "this is a new message" , "PODINFO_UI_COLOR" : "bittersweet" }
857+
855858 testCases := []struct {
856- name string
857- configuration func (source client.Object ) * v1alpha1.Configuration
858- setup func () client.Object
859+ name string
860+ expectedError error
861+ expectedConfig map [string ]string
862+ configuration func (source client.Object ) * v1alpha1.Configuration
863+ setup func () client.Object
859864 }{
860865 {
861- name : "configuration values from GitRepository" ,
866+ name : "configuration values from GitRepository" ,
867+ expectedConfig : commonExpectedData ,
862868 configuration : func (source client.Object ) * v1alpha1.Configuration {
863869 configuration := DefaultConfiguration .DeepCopy ()
864870 configuration .Status .SnapshotName = "configuration-snapshot"
@@ -889,7 +895,8 @@ func TestConfigurationValuesFrom(t *testing.T) {
889895 },
890896 },
891897 {
892- name : "configuration values from ConfigMap" ,
898+ name : "configuration values from ConfigMap" ,
899+ expectedConfig : commonExpectedData ,
893900 configuration : func (source client.Object ) * v1alpha1.Configuration {
894901 configuration := DefaultConfiguration .DeepCopy ()
895902 configuration .Status .SnapshotName = "configuration-snapshot"
@@ -922,6 +929,57 @@ func TestConfigurationValuesFrom(t *testing.T) {
922929 return configMap
923930 },
924931 },
932+ {
933+ name : "configuration values from optional missing ConfigMap" ,
934+ expectedConfig : map [string ]string {"PODINFO_UI_MESSAGE" : "Hello, world!" , "PODINFO_UI_COLOR" : "red" },
935+ configuration : func (client.Object ) * v1alpha1.Configuration {
936+ configuration := DefaultConfiguration .DeepCopy ()
937+ configuration .Status .SnapshotName = "configuration-snapshot"
938+ configuration .Spec .ValuesFrom = & v1alpha1.ValuesSource {
939+ ConfigMapSource : & v1alpha1.ConfigMapSource {
940+ SourceRef : meta.LocalObjectReference {
941+ Name : "test-config-data-does-not-exist" ,
942+ },
943+ Key : "values.yaml" ,
944+ SubPath : "test.backend" ,
945+ Optional : true ,
946+ },
947+ }
948+ configuration .Spec .Values = nil
949+
950+ return configuration
951+ },
952+ setup : func () client.Object {
953+ return nil
954+ },
955+ },
956+ {
957+ name : "configuration values from missing ConfigMap" ,
958+ expectedError : & k8sapierr.StatusError {
959+ ErrStatus : metav1.Status {
960+ Reason : metav1 .StatusReasonNotFound ,
961+ },
962+ },
963+ configuration : func (client.Object ) * v1alpha1.Configuration {
964+ configuration := DefaultConfiguration .DeepCopy ()
965+ configuration .Status .SnapshotName = "configuration-snapshot"
966+ configuration .Spec .ValuesFrom = & v1alpha1.ValuesSource {
967+ ConfigMapSource : & v1alpha1.ConfigMapSource {
968+ SourceRef : meta.LocalObjectReference {
969+ Name : "test-config-data-does-not-exist" ,
970+ },
971+ Key : "values.yaml" ,
972+ SubPath : "test.backend" ,
973+ },
974+ }
975+ configuration .Spec .Values = nil
976+
977+ return configuration
978+ },
979+ setup : func () client.Object {
980+ return nil
981+ },
982+ },
925983 }
926984
927985 for _ , tc := range testCases {
@@ -960,8 +1018,12 @@ func TestConfigurationValuesFrom(t *testing.T) {
9601018 },
9611019 }
9621020
1021+ objs := []client.Object {cv , cd , resource }
9631022 configSource := tc .setup ()
964- objs := []client.Object {cv , cd , resource , configSource }
1023+ if configSource != nil {
1024+ objs = append (objs , configSource )
1025+
1026+ }
9651027
9661028 configuration := tc .configuration (configSource )
9671029 configuration .Spec .SourceRef = source
@@ -1017,35 +1079,39 @@ func TestConfigurationValuesFrom(t *testing.T) {
10171079 }, snapshotOutput )
10181080
10191081 t .Log ("check if target snapshot has been created and cache was called" )
1020- require .NoError (t , err )
1021-
1022- t .Log ("extracting the passed in data and checking if the configuration worked" )
1023- args := cache .PushDataCallingArgumentsOnCall (0 )
1024- assert .Equal (t , "sha-13092443426051895747" , args .Name )
1025- assert .Equal (t , "1.0.0" , args .Version )
1026- sourceFile := extractFileFromTarGz (t , io .NopCloser (bytes .NewBuffer ([]byte (args .Content ))), "configmap.yaml" )
1027- configMap := corev1.ConfigMap {}
1028- assert .NoError (t , yaml .Unmarshal (sourceFile , & configMap ))
1029- assert .Equal (t , map [string ]string {"PODINFO_UI_MESSAGE" : "this is a new message" , "PODINFO_UI_COLOR" : "bittersweet" }, configMap .Data )
1030-
1031- err = client .Get (context .Background (), types.NamespacedName {
1032- Namespace : configuration .Namespace ,
1033- Name : configuration .Name ,
1034- }, configuration )
1035- require .NoError (t , err )
1036-
1037- assert .True (t , conditions .IsTrue (configuration , meta .ReadyCondition ))
1038-
10391082 close (recorder .Events )
10401083 event := ""
1041- for e := range recorder .Events {
1042- if strings .Contains (e , "Reconciliation finished, next run in" ) {
1043- event = e
1084+ if tc .expectedError == nil {
1085+ require .NoError (t , err )
1086+ t .Log ("extracting the passed in data and checking if the configuration worked" )
1087+ args := cache .PushDataCallingArgumentsOnCall (0 )
1088+ assert .Equal (t , "sha-13092443426051895747" , args .Name )
1089+ assert .Equal (t , "1.0.0" , args .Version )
1090+ sourceFile := extractFileFromTarGz (t , io .NopCloser (bytes .NewBuffer ([]byte (args .Content ))), "configmap.yaml" )
1091+ configMap := corev1.ConfigMap {}
1092+ assert .NoError (t , yaml .Unmarshal (sourceFile , & configMap ))
1093+ assert .Equal (t , tc .expectedConfig , configMap .Data )
1094+
1095+ err = client .Get (context .Background (), types.NamespacedName {
1096+ Namespace : configuration .Namespace ,
1097+ Name : configuration .Name ,
1098+ }, configuration )
1099+ require .NoError (t , err )
10441100
1045- break
1101+ assert .True (t , conditions .IsTrue (configuration , meta .ReadyCondition ))
1102+
1103+ for e := range recorder .Events {
1104+ if strings .Contains (e , "Reconciliation finished, next run in" ) {
1105+ event = e
1106+
1107+ break
1108+ }
10461109 }
1110+ assert .Contains (t , event , "Reconciliation finished, next run in" )
1111+ } else {
1112+ assert .Equal (t , k8sapierr .ReasonForError (tc .expectedError ), k8sapierr .ReasonForError (err ))
10471113 }
1048- assert . Contains ( t , event , "Reconciliation finished, next run in" )
1114+
10491115 })
10501116 }
10511117}
0 commit comments