Skip to content

Commit cfaf473

Browse files
committed
move to seperate function and add unit tests, ensuring coverage of change
1 parent 5106952 commit cfaf473

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

internal/controller/promotions/promotions.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,7 @@ func (r *reconciler) promote(
477477
}
478478
}
479479

480-
creator := "N/A"
481-
actorAnnotation, ok := promo.ObjectMeta.Annotations[kargoapi.AnnotationKeyCreateActor]
482-
if ok {
483-
substrings := strings.Split(actorAnnotation, ":")
484-
if len(substrings) == 2 {
485-
creator = substrings[1]
486-
}
487-
}
480+
creator := parseActorAnnotation(&promo)
488481

489482
promoCtx := directives.PromotionContext{
490483
UIBaseURL: r.cfg.APIServerBaseURL,
@@ -654,3 +647,15 @@ func (r *reconciler) terminatePromotion(
654647

655648
return nil
656649
}
650+
651+
func parseActorAnnotation(promo *kargoapi.Promotion) string {
652+
creator := "N/A"
653+
actorAnnotation, ok := promo.ObjectMeta.Annotations[kargoapi.AnnotationKeyCreateActor]
654+
if ok {
655+
substrings := strings.Split(actorAnnotation, ":")
656+
if len(substrings) == 2 {
657+
creator = substrings[1]
658+
}
659+
}
660+
return creator
661+
}

internal/controller/promotions/promotions_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,60 @@ func TestReconcile(t *testing.T) {
336336
}
337337
}
338338

339+
func Test_parseActorAnnotation(t *testing.T) {
340+
tests := []struct {
341+
name string
342+
promo *kargoapi.Promotion
343+
expectedCreator string
344+
}{
345+
{
346+
name: "basic case",
347+
promo: &kargoapi.Promotion{
348+
ObjectMeta: metav1.ObjectMeta{
349+
Name: "fake-promo",
350+
Namespace: "fake-namespace",
351+
Annotations: map[string]string{
352+
kargoapi.AnnotationKeyCreateActor: "email:fake-actor",
353+
},
354+
},
355+
},
356+
expectedCreator: "fake-actor",
357+
},
358+
{
359+
360+
name: "no annotation",
361+
promo: &kargoapi.Promotion{
362+
ObjectMeta: metav1.ObjectMeta{
363+
Name: "fake-promo",
364+
Namespace: "fake-namespace",
365+
},
366+
},
367+
expectedCreator: "N/A",
368+
},
369+
{
370+
name: "invalid",
371+
promo: &kargoapi.Promotion{
372+
ObjectMeta: metav1.ObjectMeta{
373+
Name: "fake-promo",
374+
Namespace: "fake-namespace",
375+
Annotations: map[string]string{
376+
kargoapi.AnnotationKeyCreateActor: "abc123",
377+
},
378+
},
379+
},
380+
expectedCreator: "N/A",
381+
},
382+
}
383+
384+
for _, tc := range tests {
385+
386+
t.Run(tc.name, func(t *testing.T) {
387+
result := parseActorAnnotation(tc.promo)
388+
require.Equal(t, tc.expectedCreator, result)
389+
})
390+
}
391+
}
392+
339393
func Test_reconciler_terminatePromotion(t *testing.T) {
340394
scheme := k8sruntime.NewScheme()
341395
require.NoError(t, kargoapi.SchemeBuilder.AddToScheme(scheme))

0 commit comments

Comments
 (0)