@@ -2,6 +2,7 @@ package controller
22
33import (
44 "context"
5+ "encoding/json"
56 "testing"
67
78 batchv1 "k8s.io/api/batch/v1"
@@ -15,6 +16,8 @@ import (
1516
1617 dataflowv1 "github.com/dataflow-operator/dataflow/api/v1"
1718 "github.com/dataflow-operator/dataflow/pkg/k8snames"
19+ "github.com/stretchr/testify/assert"
20+ "github.com/stretchr/testify/require"
1821)
1922
2023func TestDataFlowCronReconcile_CreatesConfigMapAndCronJob (t * testing.T ) {
@@ -152,3 +155,74 @@ func TestDataFlowCronReconcile_CreatesFirstTriggerJobAfterProcessor(t *testing.T
152155 t .Fatalf ("expected first trigger job to be created after processor" )
153156 }
154157}
158+
159+ func TestDataFlowCronReconcile_ResolvesSecretsInConfigMap (t * testing.T ) {
160+ scheme := runtime .NewScheme ()
161+ require .NoError (t , dataflowv1 .AddToScheme (scheme ))
162+ require .NoError (t , clientgoscheme .AddToScheme (scheme ))
163+
164+ const wantDSN = "postgres://user:pass@host:5432/db"
165+ secret := & corev1.Secret {
166+ ObjectMeta : metav1.ObjectMeta {Name : "db-env-vars" , Namespace : "default" },
167+ Data : map [string ][]byte {
168+ "pg-source" : []byte (wantDSN ),
169+ "pg-sink" : []byte (wantDSN ),
170+ },
171+ }
172+
173+ dfc := & dataflowv1.DataFlowCron {
174+ ObjectMeta : metav1.ObjectMeta {Name : "cron-secrets" , Namespace : "default" },
175+ Spec : dataflowv1.DataFlowCronSpec {
176+ Schedule : "0 0 * * *" ,
177+ DataFlowSpec : dataflowv1.DataFlowSpec {
178+ Source : dataflowv1.SourceSpec {
179+ Type : "postgresql" ,
180+ Config : mustConfig (dataflowv1.PostgreSQLSourceSpec {
181+ Table : "price.price" ,
182+ ConnectionStringSecretRef : & dataflowv1.SecretRef {
183+ Name : "db-env-vars" ,
184+ Key : "pg-source" ,
185+ },
186+ }),
187+ },
188+ Sink : dataflowv1.SinkSpec {
189+ Type : "postgresql" ,
190+ Config : mustConfig (dataflowv1.PostgreSQLSinkSpec {
191+ Table : "public.price_target" ,
192+ ConnectionStringSecretRef : & dataflowv1.SecretRef {
193+ Name : "db-env-vars" ,
194+ Key : "pg-sink" ,
195+ },
196+ }),
197+ },
198+ },
199+ },
200+ }
201+
202+ c := fake .NewClientBuilder ().
203+ WithScheme (scheme ).
204+ WithStatusSubresource (& dataflowv1.DataFlowCron {}).
205+ WithObjects (secret , dfc ).
206+ Build ()
207+ r := NewDataFlowCronReconciler (c , scheme )
208+ _ , err := r .Reconcile (context .Background (), ctrl.Request {
209+ NamespacedName : types.NamespacedName {Name : "cron-secrets" , Namespace : "default" },
210+ })
211+ require .NoError (t , err )
212+
213+ var cm corev1.ConfigMap
214+ require .NoError (t , c .Get (context .Background (), types.NamespacedName {
215+ Name : k8snames .CronSpecConfigMap ("cron-secrets" ), Namespace : "default" ,
216+ }, & cm ))
217+
218+ var spec dataflowv1.DataFlowSpec
219+ require .NoError (t , json .Unmarshal ([]byte (cm .Data ["spec.json" ]), & spec ))
220+
221+ sourceCfg , err := spec .Source .GetPostgreSQLConfig ()
222+ require .NoError (t , err )
223+ assert .Equal (t , wantDSN , sourceCfg .ConnectionString )
224+
225+ sinkCfg , err := spec .Sink .GetPostgreSQLConfig ()
226+ require .NoError (t , err )
227+ assert .Equal (t , wantDSN , sinkCfg .ConnectionString )
228+ }
0 commit comments