@@ -28,12 +28,50 @@ import (
2828 corev1 "k8s.io/api/core/v1"
2929 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3030 "k8s.io/client-go/tools/record"
31+ ctrl "sigs.k8s.io/controller-runtime"
32+ "sigs.k8s.io/controller-runtime/pkg/client"
3133 "sigs.k8s.io/controller-runtime/pkg/client/fake"
3234
3335 imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta2"
3436 "github.com/fluxcd/image-reflector-controller/internal/policy"
3537)
3638
39+ func TestImagePolicyReconciler_deleteBeforeFinalizer (t * testing.T ) {
40+ g := NewWithT (t )
41+
42+ namespaceName := "imagepolicy-" + randStringRunes (5 )
43+ namespace := & corev1.Namespace {
44+ ObjectMeta : metav1.ObjectMeta {Name : namespaceName },
45+ }
46+ g .Expect (k8sClient .Create (ctx , namespace )).ToNot (HaveOccurred ())
47+ t .Cleanup (func () {
48+ g .Expect (k8sClient .Delete (ctx , namespace )).NotTo (HaveOccurred ())
49+ })
50+
51+ imagePolicy := & imagev1.ImagePolicy {}
52+ imagePolicy .Name = "test-imagepolicy"
53+ imagePolicy .Namespace = namespaceName
54+ imagePolicy .Spec = imagev1.ImagePolicySpec {
55+ ImageRepositoryRef : meta.NamespacedObjectReference {
56+ Name : "foo" ,
57+ },
58+ Policy : imagev1.ImagePolicyChoice {},
59+ }
60+ // Add a test finalizer to prevent the object from getting deleted.
61+ imagePolicy .SetFinalizers ([]string {"test-finalizer" })
62+ g .Expect (k8sClient .Create (ctx , imagePolicy )).NotTo (HaveOccurred ())
63+ // Add deletion timestamp by deleting the object.
64+ g .Expect (k8sClient .Delete (ctx , imagePolicy )).NotTo (HaveOccurred ())
65+
66+ r := & ImagePolicyReconciler {
67+ Client : k8sClient ,
68+ EventRecorder : record .NewFakeRecorder (32 ),
69+ }
70+ // NOTE: Only a real API server responds with an error in this scenario.
71+ _ , err := r .Reconcile (ctx , ctrl.Request {NamespacedName : client .ObjectKeyFromObject (imagePolicy )})
72+ g .Expect (err ).NotTo (HaveOccurred ())
73+ }
74+
3775func TestImagePolicyReconciler_getImageRepository (t * testing.T ) {
3876 testImageRepoName := "test-repo"
3977 testNamespace1 := "test-ns1" // Default namespace of ImagePolicy.
0 commit comments