Skip to content

Commit 46be4fb

Browse files
committed
add basic test for templated label selectors
On-behalf-of: @SAP [email protected]
1 parent 7ca0303 commit 46be4fb

File tree

2 files changed

+78
-14
lines changed

2 files changed

+78
-14
lines changed

internal/sync/templating/templating.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ func Render(tpl string, data any) (string, error) {
4545

4646
func templateFuncMap() template.FuncMap {
4747
funcs := sprig.TxtFuncMap()
48-
funcs["join"] = strings.Join
4948
funcs["sha3sum"] = sha3sum
5049
funcs["sha3short"] = sha3short
5150

test/e2e/sync/related_test.go

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,9 @@ func TestSyncRelatedObjects(t *testing.T) {
9090
Kind: "Secret",
9191
Object: syncagentv1alpha1.RelatedResourceObject{
9292
RelatedResourceObjectSpec: syncagentv1alpha1.RelatedResourceObjectSpec{
93-
Reference: &syncagentv1alpha1.RelatedResourceObjectReference{
94-
Path: "metadata.name", // irrelevant
95-
Regex: &syncagentv1alpha1.RegularExpression{
96-
Replacement: "my-credentials",
97-
},
93+
Template: &syncagentv1alpha1.TemplateExpression{
94+
// same fixed value on both sides
95+
Template: "my-credentials",
9896
},
9997
},
10098
},
@@ -143,11 +141,9 @@ func TestSyncRelatedObjects(t *testing.T) {
143141
Kind: "Secret",
144142
Object: syncagentv1alpha1.RelatedResourceObject{
145143
RelatedResourceObjectSpec: syncagentv1alpha1.RelatedResourceObjectSpec{
146-
Reference: &syncagentv1alpha1.RelatedResourceObjectReference{
147-
Path: "metadata.name", // irrelevant
148-
Regex: &syncagentv1alpha1.RegularExpression{
149-
Replacement: "my-credentials",
150-
},
144+
Template: &syncagentv1alpha1.TemplateExpression{
145+
// same fixed value on both sides
146+
Template: "my-credentials",
151147
},
152148
},
153149
},
@@ -368,9 +364,9 @@ func TestSyncRelatedObjects(t *testing.T) {
368364
},
369365
},
370366
Rewrite: syncagentv1alpha1.RelatedResourceSelectorRewrite{
371-
// TODO: Use template instead of regex once that is implemented.
372-
Regex: &syncagentv1alpha1.RegularExpression{
373-
Replacement: "my-credentials",
367+
Template: &syncagentv1alpha1.TemplateExpression{
368+
// same fixed name on both sides
369+
Template: "my-credentials",
374370
},
375371
},
376372
},
@@ -405,6 +401,75 @@ func TestSyncRelatedObjects(t *testing.T) {
405401
Type: corev1.SecretTypeOpaque,
406402
},
407403
},
404+
405+
//////////////////////////////////////////////////////////////////////////////////////////////
406+
407+
{
408+
name: "find Secret based on templated label selector",
409+
workspace: "sync-templated-selected-secret-up",
410+
mainResource: crds.Crontab{
411+
ObjectMeta: metav1.ObjectMeta{
412+
Name: "my-crontab",
413+
Namespace: "default",
414+
},
415+
Spec: crds.CrontabSpec{
416+
CronSpec: "* * *",
417+
Image: "ubuntu:latest",
418+
},
419+
},
420+
relatedConfig: syncagentv1alpha1.RelatedResourceSpec{
421+
Identifier: "credentials",
422+
Origin: "service",
423+
Kind: "Secret",
424+
Object: syncagentv1alpha1.RelatedResourceObject{
425+
RelatedResourceObjectSpec: syncagentv1alpha1.RelatedResourceObjectSpec{
426+
Selector: &syncagentv1alpha1.RelatedResourceObjectSelector{
427+
LabelSelector: metav1.LabelSelector{
428+
MatchLabels: map[string]string{
429+
// include some nasty whitespace
430+
` {{ list "fi" "nd" | join "-" }} `: `
431+
{{ lower "ME" }}
432+
`,
433+
},
434+
},
435+
Rewrite: syncagentv1alpha1.RelatedResourceSelectorRewrite{
436+
Template: &syncagentv1alpha1.TemplateExpression{
437+
// same fixed name on both sides
438+
Template: "my-credentials",
439+
},
440+
},
441+
},
442+
},
443+
},
444+
},
445+
sourceRelatedObject: corev1.Secret{
446+
ObjectMeta: metav1.ObjectMeta{
447+
Name: "unknown-name",
448+
Namespace: "synced-default",
449+
Labels: map[string]string{
450+
"fi-nd": "me",
451+
},
452+
},
453+
Data: map[string][]byte{
454+
"password": []byte("hunter2"),
455+
},
456+
Type: corev1.SecretTypeOpaque,
457+
},
458+
459+
expectedSyncedRelatedObject: corev1.Secret{
460+
ObjectMeta: metav1.ObjectMeta{
461+
Name: "my-credentials",
462+
Namespace: "default",
463+
Labels: map[string]string{
464+
"fi-nd": "me",
465+
},
466+
},
467+
Data: map[string][]byte{
468+
"password": []byte("hunter2"),
469+
},
470+
Type: corev1.SecretTypeOpaque,
471+
},
472+
},
408473
}
409474

410475
for _, testcase := range testcases {

0 commit comments

Comments
 (0)