@@ -3,6 +3,7 @@ package reporter
33import (
44 "context"
55 "encoding/json"
6+ "k8s.io/apimachinery/pkg/watch"
67 "net/http"
78 "testing"
89 "time"
@@ -536,3 +537,112 @@ func TestGetParentAppIdentityWithinControllerNs(t *testing.T) {
536537 assert .Equal (t , expectedName , res .name )
537538 assert .Equal (t , expectedNamespace , res .namespace )
538539}
540+
541+ func TestShouldSendApplicationEvent (t * testing.T ) {
542+ eventReporter := fakeReporter ()
543+
544+ t .Run ("should send because cache is missing" , func (t * testing.T ) {
545+ app := v1alpha1.Application {
546+ ObjectMeta : metav1.ObjectMeta {
547+ Labels : map [string ]string {
548+ "sdsds" : "sdsd" ,
549+ },
550+ },
551+ }
552+
553+ shouldSend , _ := eventReporter .ShouldSendApplicationEvent (& v1alpha1.ApplicationWatchEvent {
554+ Type : watch .Modified ,
555+ Application : app ,
556+ })
557+ assert .True (t , shouldSend )
558+ })
559+
560+ t .Run ("should send because labels changed" , func (t * testing.T ) {
561+ appCache := v1alpha1.Application {
562+ ObjectMeta : metav1.ObjectMeta {
563+ Labels : map [string ]string {
564+ "data" : "old value" ,
565+ },
566+ },
567+ }
568+
569+ err := eventReporter .cache .SetLastApplicationEvent (& appCache , time .Second * 5 )
570+ assert .NoError (t , err )
571+
572+ app := v1alpha1.Application {
573+ ObjectMeta : metav1.ObjectMeta {
574+ Labels : map [string ]string {
575+ "data" : "new value" ,
576+ },
577+ },
578+ }
579+
580+ shouldSend , _ := eventReporter .ShouldSendApplicationEvent (& v1alpha1.ApplicationWatchEvent {
581+ Type : watch .Modified ,
582+ Application : app ,
583+ })
584+ assert .True (t , shouldSend )
585+ })
586+
587+ t .Run ("should send because annotations changed" , func (t * testing.T ) {
588+ appCache := v1alpha1.Application {
589+ ObjectMeta : metav1.ObjectMeta {
590+ Annotations : map [string ]string {
591+ "data" : "old value" ,
592+ },
593+ },
594+ }
595+
596+ err := eventReporter .cache .SetLastApplicationEvent (& appCache , time .Second * 5 )
597+ assert .NoError (t , err )
598+
599+ app := v1alpha1.Application {
600+ ObjectMeta : metav1.ObjectMeta {
601+ Annotations : map [string ]string {
602+ "data" : "new value" ,
603+ },
604+ },
605+ }
606+
607+ shouldSend , _ := eventReporter .ShouldSendApplicationEvent (& v1alpha1.ApplicationWatchEvent {
608+ Type : watch .Modified ,
609+ Application : app ,
610+ })
611+ assert .True (t , shouldSend )
612+ })
613+
614+ t .Run ("should ignore some changed metadata fields" , func (t * testing.T ) {
615+ appCache := v1alpha1.Application {
616+ ObjectMeta : metav1.ObjectMeta {
617+ ResourceVersion : "1" ,
618+ Generation : 1 ,
619+ GenerateName : "first" ,
620+ ManagedFields : []metav1.ManagedFieldsEntry {},
621+ Annotations : map [string ]string {
622+ "kubectl.kubernetes.io/last-applied-configuration" : "first" ,
623+ },
624+ },
625+ }
626+
627+ err := eventReporter .cache .SetLastApplicationEvent (& appCache , time .Second * 5 )
628+ assert .NoError (t , err )
629+
630+ app := v1alpha1.Application {
631+ ObjectMeta : metav1.ObjectMeta {
632+ ResourceVersion : "2" ,
633+ Generation : 2 ,
634+ GenerateName : "changed" ,
635+ ManagedFields : []metav1.ManagedFieldsEntry {{Manager : "changed" }},
636+ Annotations : map [string ]string {
637+ "kubectl.kubernetes.io/last-applied-configuration" : "changed" ,
638+ },
639+ },
640+ }
641+
642+ shouldSend , _ := eventReporter .ShouldSendApplicationEvent (& v1alpha1.ApplicationWatchEvent {
643+ Type : watch .Modified ,
644+ Application : app ,
645+ })
646+ assert .False (t , shouldSend )
647+ })
648+ }
0 commit comments