@@ -38,6 +38,7 @@ import (
38
38
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
39
39
"k8s.io/apimachinery/pkg/types"
40
40
"k8s.io/apimachinery/pkg/util/rand"
41
+ "k8s.io/apimachinery/pkg/util/validation"
41
42
"k8s.io/client-go/tools/record"
42
43
ctrl "sigs.k8s.io/controller-runtime"
43
44
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -314,6 +315,47 @@ func TestImageUpdateAutomationReconciler_Reconcile(t *testing.T) {
314
315
checker .WithT (g ).CheckErr (ctx , obj )
315
316
})
316
317
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
+
317
359
t .Run ("non-existing gitrepo results in failure" , func (t * testing.T ) {
318
360
g := NewWithT (t )
319
361
@@ -1434,11 +1476,13 @@ func Test_getPolicies(t *testing.T) {
1434
1476
name string
1435
1477
namespace string
1436
1478
latestImage string
1479
+ labels map [string ]string
1437
1480
}
1438
1481
1439
1482
tests := []struct {
1440
1483
name string
1441
1484
listNamespace string
1485
+ selector * metav1.LabelSelector
1442
1486
policies []policyArgs
1443
1487
wantPolicies []string
1444
1488
}{
@@ -1453,6 +1497,21 @@ func Test_getPolicies(t *testing.T) {
1453
1497
},
1454
1498
wantPolicies : []string {"p1" , "p2" },
1455
1499
},
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
+ },
1456
1515
{
1457
1516
name : "no policies in empty namespace" ,
1458
1517
listNamespace : testNS2 ,
@@ -1475,13 +1534,14 @@ func Test_getPolicies(t *testing.T) {
1475
1534
aPolicy .Status = imagev1_reflect.ImagePolicyStatus {
1476
1535
LatestImage : p .latestImage ,
1477
1536
}
1537
+ aPolicy .Labels = p .labels
1478
1538
testObjects = append (testObjects , aPolicy )
1479
1539
}
1480
1540
kClient := fakeclient .NewClientBuilder ().
1481
1541
WithScheme (testEnv .GetScheme ()).
1482
1542
WithObjects (testObjects ... ).Build ()
1483
1543
1484
- result , err := getPolicies (context .TODO (), kClient , tt .listNamespace )
1544
+ result , err := getPolicies (context .TODO (), kClient , tt .listNamespace , tt . selector )
1485
1545
g .Expect (err ).ToNot (HaveOccurred ())
1486
1546
1487
1547
// Extract policy name from the result and compare with the expected
0 commit comments