@@ -38,6 +38,7 @@ import (
3838 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3939 "k8s.io/apimachinery/pkg/types"
4040 "k8s.io/apimachinery/pkg/util/rand"
41+ "k8s.io/apimachinery/pkg/util/validation"
4142 "k8s.io/client-go/tools/record"
4243 ctrl "sigs.k8s.io/controller-runtime"
4344 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -314,6 +315,47 @@ func TestImageUpdateAutomationReconciler_Reconcile(t *testing.T) {
314315 checker .WithT (g ).CheckErr (ctx , obj )
315316 })
316317
318+ t .Run ("invalid policy selector results in stalled" , func (t * testing.T ) {
319+ g := NewWithT (t )
320+
321+ namespace , err := testEnv .CreateNamespace (ctx , "test-update" )
322+ g .Expect (err ).ToNot (HaveOccurred ())
323+ defer func () { g .Expect (testEnv .Delete (ctx , namespace )).To (Succeed ()) }()
324+
325+ obj := & imagev1.ImageUpdateAutomation {}
326+ obj .Name = updateName
327+ obj .Namespace = namespace .Name
328+ obj .Spec = imagev1.ImageUpdateAutomationSpec {
329+ SourceRef : imagev1.CrossNamespaceSourceReference {
330+ Kind : "GitRepository" ,
331+ Name : "foo" ,
332+ },
333+ PolicySelector : & metav1.LabelSelector {
334+ MatchLabels : map [string ]string {
335+ "label-too-long-" + strings .Repeat ("0" , validation .LabelValueMaxLength ): "" ,
336+ },
337+ },
338+ }
339+ g .Expect (testEnv .Create (ctx , obj )).To (Succeed ())
340+ defer func () {
341+ g .Expect (deleteImageUpdateAutomation (ctx , testEnv , obj .Name , obj .Namespace )).To (Succeed ())
342+ }()
343+
344+ expectedConditions := []metav1.Condition {
345+ * conditions .TrueCondition (meta .StalledCondition , imagev1 .InvalidPolicySelectorReason , "failed to parse policy selector" ),
346+ * conditions .FalseCondition (meta .ReadyCondition , imagev1 .InvalidPolicySelectorReason , "failed to parse policy selector" ),
347+ }
348+ g .Eventually (func (g Gomega ) {
349+ g .Expect (testEnv .Get (ctx , client .ObjectKeyFromObject (obj ), obj )).To (Succeed ())
350+ g .Expect (obj .Status .Conditions ).To (conditions .MatchConditions (expectedConditions ))
351+ }).Should (Succeed ())
352+
353+ // Check if the object status is valid.
354+ condns := & conditionscheck.Conditions {NegativePolarity : imageUpdateAutomationNegativeConditions }
355+ checker := conditionscheck .NewChecker (testEnv .Client , condns )
356+ checker .WithT (g ).CheckErr (ctx , obj )
357+ })
358+
317359 t .Run ("non-existing gitrepo results in failure" , func (t * testing.T ) {
318360 g := NewWithT (t )
319361
@@ -1434,11 +1476,13 @@ func Test_getPolicies(t *testing.T) {
14341476 name string
14351477 namespace string
14361478 latestImage string
1479+ labels map [string ]string
14371480 }
14381481
14391482 tests := []struct {
14401483 name string
14411484 listNamespace string
1485+ selector * metav1.LabelSelector
14421486 policies []policyArgs
14431487 wantPolicies []string
14441488 }{
@@ -1453,6 +1497,21 @@ func Test_getPolicies(t *testing.T) {
14531497 },
14541498 wantPolicies : []string {"p1" , "p2" },
14551499 },
1500+ {
1501+ name : "lists policies with label selector in same namespace" ,
1502+ listNamespace : testNS1 ,
1503+ selector : & metav1.LabelSelector {
1504+ MatchLabels : map [string ]string {
1505+ "label" : "one" ,
1506+ },
1507+ },
1508+ policies : []policyArgs {
1509+ {name : "p1" , namespace : testNS1 , latestImage : "aaa:bbb" , labels : map [string ]string {"label" : "one" }},
1510+ {name : "p2" , namespace : testNS1 , latestImage : "ccc:ddd" , labels : map [string ]string {"label" : "false" }},
1511+ {name : "p3" , namespace : testNS2 , latestImage : "eee:fff" , labels : map [string ]string {"label" : "one" }},
1512+ },
1513+ wantPolicies : []string {"p1" },
1514+ },
14561515 {
14571516 name : "no policies in empty namespace" ,
14581517 listNamespace : testNS2 ,
@@ -1475,13 +1534,14 @@ func Test_getPolicies(t *testing.T) {
14751534 aPolicy .Status = imagev1_reflect.ImagePolicyStatus {
14761535 LatestImage : p .latestImage ,
14771536 }
1537+ aPolicy .Labels = p .labels
14781538 testObjects = append (testObjects , aPolicy )
14791539 }
14801540 kClient := fakeclient .NewClientBuilder ().
14811541 WithScheme (testEnv .GetScheme ()).
14821542 WithObjects (testObjects ... ).Build ()
14831543
1484- result , err := getPolicies (context .TODO (), kClient , tt .listNamespace )
1544+ result , err := getPolicies (context .TODO (), kClient , tt .listNamespace , tt . selector )
14851545 g .Expect (err ).ToNot (HaveOccurred ())
14861546
14871547 // Extract policy name from the result and compare with the expected
0 commit comments