@@ -3,6 +3,7 @@ package crds
33import (
44 "context"
55 "testing"
6+ "time"
67
78 "github.com/aws/amazon-vpc-resource-controller-k8s/apis/vpcresources/v1alpha1"
89 mock_api "github.com/aws/amazon-vpc-resource-controller-k8s/mocks/amazon-vcp-resource-controller-k8s/pkg/aws/ec2/api"
@@ -12,6 +13,7 @@ import (
1213 "github.com/aws/amazon-vpc-resource-controller-k8s/pkg/aws/ec2/api/cleanup"
1314 "github.com/aws/amazon-vpc-resource-controller-k8s/pkg/config"
1415 "github.com/golang/mock/gomock"
16+ "github.com/prometheus/client_golang/prometheus/testutil"
1517 "github.com/stretchr/testify/assert"
1618 corev1 "k8s.io/api/core/v1"
1719 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -24,7 +26,8 @@ import (
2426)
2527
2628type CNINodeMock struct {
27- Reconciler CNINodeReconciler
29+ Reconciler CNINodeReconciler
30+ initialStaleCount float64
2831}
2932
3033var (
@@ -163,6 +166,50 @@ func TestCNINodeReconcile(t *testing.T) {
163166 assert .Equal (t , res , reconcile.Result {})
164167 },
165168 },
169+ {
170+ name : "verify stale CNINode metric is incremented when deletion timestamp is older than 15 minutes" ,
171+ args : args {
172+ mockNode : nil ,
173+ mockCNINode : & v1alpha1.CNINode {
174+ ObjectMeta : metav1.ObjectMeta {
175+ Name : mockName ,
176+ Labels : map [string ]string {
177+ config .NodeLabelOS : config .OSLinux ,
178+ },
179+ Finalizers : []string {config .NodeTerminationFinalizer },
180+ DeletionTimestamp : & metav1.Time {Time : metav1 .Now ().Add (- 20 * time .Minute )}, // 20 minutes ago
181+ },
182+ Spec : v1alpha1.CNINodeSpec {
183+ Tags : map [string ]string {
184+ config .NetworkInterfaceNodeIDKey : "i-1234567890" ,
185+ },
186+ },
187+ },
188+ },
189+ prepare : func (f * fields ) {
190+ // Get initial metric value
191+ initialValue := testutil .ToFloat64 (staleCNINodeCount )
192+
193+ f .mockCNINode .Reconciler .newResourceCleaner = func (nodeID string , eC2Wrapper ec2API.EC2Wrapper , vpcID string ) cleanup.ResourceCleaner {
194+ return f .mockResourceCleaner
195+ }
196+ f .mockResourceCleaner .EXPECT ().DeleteLeakedResources ().Times (1 ).Return (nil )
197+ f .mockFinalizerManager .EXPECT ().
198+ RemoveFinalizers (gomock .Any (), gomock .Any (), config .NodeTerminationFinalizer ).
199+ Return (nil )
200+
201+ // Store initial value for comparison in asserts
202+ f .mockCNINode .initialStaleCount = initialValue
203+ },
204+ asserts : func (res reconcile.Result , err error , cniNode * v1alpha1.CNINode ) {
205+ assert .NoError (t , err )
206+ assert .Equal (t , res , reconcile.Result {})
207+
208+ // Verify the stale metric was incremented
209+ finalValue := testutil .ToFloat64 (staleCNINodeCount )
210+ assert .Greater (t , finalValue ,float64 (0 ))
211+ },
212+ },
166213 }
167214 for _ , tt := range tests {
168215 t .Run (tt .name , func (t * testing.T ) {
0 commit comments