@@ -99,16 +99,24 @@ func createTestClient(t *testing.T) (*AWSClient, *MockAWSHealthClient, *fake.Cli
9999 _ , err := fakeK8sClient .CoreV1 ().Nodes ().Create (context .Background (), node , metav1.CreateOptions {})
100100 assert .NoError (t , err )
101101
102+ nodeInformer := & NodeInformer {
103+ k8sClient : fakeK8sClient ,
104+ instanceIDs : map [string ]string {
105+ testInstanceID : testNodeName ,
106+ },
107+ }
108+
102109 client := & AWSClient {
103110 config : config.AWSConfig {
104111 Region : testRegion ,
105112 PollingIntervalSeconds : 60 ,
106113 Enabled : true ,
107114 },
108- awsClient : mockAWSClient ,
109- k8sClient : fakeK8sClient ,
110- normalizer : & eventpkg.AWSNormalizer {},
111- clusterName : "test-cluster" ,
115+ awsClient : mockAWSClient ,
116+ k8sClient : fakeK8sClient ,
117+ normalizer : & eventpkg.AWSNormalizer {},
118+ clusterName : "test-cluster" ,
119+ nodeInformer : nodeInformer ,
112120 }
113121
114122 return client , mockAWSClient , fakeK8sClient
@@ -167,12 +175,9 @@ func TestHandleMaintenanceEvents(t *testing.T) {
167175 }, nil )
168176 // Setup test channel and test instance IDs
169177 eventChan := make (chan model.MaintenanceEvent , 10 )
170- instanceIDs := map [string ]string {
171- testInstanceID : testNodeName ,
172- }
173178
174179 // Call the function being tested
175- err := client .handleMaintenanceEvents (context .Background (), instanceIDs , eventChan , pollStartTime )
180+ err := client .handleMaintenanceEvents (context .Background (), eventChan , pollStartTime )
176181 assert .NoError (t , err )
177182
178183 // Verify we received an event
@@ -202,12 +207,9 @@ func TestNoMaintenanceEvents(t *testing.T) {
202207
203208 // Setup test channel and test instance IDs
204209 eventChan := make (chan model.MaintenanceEvent , 10 )
205- instanceIDs := map [string ]string {
206- testInstanceID : testNodeName ,
207- }
208210
209211 // Call the function being tested
210- err := client .handleMaintenanceEvents (context .Background (), instanceIDs , eventChan , pollStartTime )
212+ err := client .handleMaintenanceEvents (context .Background (), eventChan , pollStartTime )
211213 assert .NoError (t , err )
212214
213215 mockAWSClient .AssertNotCalled (t , "DescribeAffectedEntities" , mock .Anything , mock .Anything )
@@ -304,14 +306,15 @@ func TestMultipleAffectedEntities(t *testing.T) {
304306
305307 // Setup test channel and instance IDs
306308 eventChan := make (chan model.MaintenanceEvent , 10 )
307- instanceIDs := map [string ]string {
309+
310+ client .nodeInformer .instanceIDs = map [string ]string {
308311 testInstanceID : testNodeName ,
309312 testInstanceID1 : testNodeName1 ,
310313 testInstanceID2 : testNodeName2 ,
311314 }
312315
313316 // Call the function being tested
314- err := client .handleMaintenanceEvents (context .Background (), instanceIDs , eventChan , pollStartTime )
317+ err := client .handleMaintenanceEvents (context .Background (), eventChan , pollStartTime )
315318 assert .NoError (t , err )
316319
317320 // Verify we received events for all affected instances
@@ -427,14 +430,14 @@ func TestCompletedEvent(t *testing.T) {
427430
428431 // Setup test channel and instance IDs
429432 eventChan := make (chan model.MaintenanceEvent , 10 )
430- instanceIDs : = map [string ]string {
433+ client . nodeInformer . instanceIDs = map [string ]string {
431434 testInstanceID : testNodeName ,
432435 testInstanceID1 : testNodeName1 ,
433436 testInstanceID2 : testNodeName2 ,
434437 }
435438
436439 // Call the function being tested
437- err := client .handleMaintenanceEvents (context .Background (), instanceIDs , eventChan , pollStartTime )
440+ err := client .handleMaintenanceEvents (context .Background (), eventChan , pollStartTime )
438441 assert .NoError (t , err )
439442
440443 // Verify we received a completed event
@@ -476,12 +479,12 @@ func TestErrorScenario(t *testing.T) {
476479
477480 // Setup test channel and test instance IDs
478481 eventChan := make (chan model.MaintenanceEvent , 10 )
479- instanceIDs : = map [string ]string {
482+ client . nodeInformer . instanceIDs = map [string ]string {
480483 testInstanceID : testNodeName ,
481484 }
482485
483486 // Call the function being tested - should not panic but return error
484- err := client .handleMaintenanceEvents (context .Background (), instanceIDs , eventChan , pollStartTime )
487+ err := client .handleMaintenanceEvents (context .Background (), eventChan , pollStartTime )
485488 assert .Error (t , err )
486489
487490 mockAWSClient .AssertNotCalled (t , "DescribeAffectedEntities" , mock .Anything , mock .Anything )
@@ -520,12 +523,12 @@ func TestTimeWindowFiltering(t *testing.T) {
520523
521524 // Setup test channel and test instance IDs
522525 eventChan := make (chan model.MaintenanceEvent , 10 )
523- instanceIDs : = map [string ]string {
526+ client . nodeInformer . instanceIDs = map [string ]string {
524527 testInstanceID : testNodeName ,
525528 }
526529
527530 // Call the function being tested
528- err := client .handleMaintenanceEvents (context .Background (), instanceIDs , eventChan , pollStartTime )
531+ err := client .handleMaintenanceEvents (context .Background (), eventChan , pollStartTime )
529532 assert .NoError (t , err )
530533
531534 // Verify no events were received (as our filter should exclude the old event)
@@ -608,11 +611,11 @@ func TestInstanceFiltering(t *testing.T) {
608611
609612 // Setup test channel with our cluster's instance IDs only
610613 eventChan := make (chan model.MaintenanceEvent , 10 )
611- instanceIDs : = map [string ]string {
614+ client . nodeInformer . instanceIDs = map [string ]string {
612615 testInstanceID : testNodeName ,
613616 }
614617
615- err := client .handleMaintenanceEvents (context .Background (), instanceIDs , eventChan , pollStartTime )
618+ err := client .handleMaintenanceEvents (context .Background (), eventChan , pollStartTime )
616619
617620 assert .Error (t , err )
618621 assert .Contains (t , err .Error (), "instance ID not found in node map" )
@@ -709,12 +712,12 @@ func TestInvalidEntityData(t *testing.T) {
709712
710713 // Setup test channel and test instance IDs
711714 eventChan := make (chan model.MaintenanceEvent , 10 )
712- instanceIDs : = map [string ]string {
715+ client . nodeInformer . instanceIDs = map [string ]string {
713716 testInstanceID : testNodeName ,
714717 }
715718
716719 // Call the function - should handle nil values without panicking but return errors
717- err := client .handleMaintenanceEvents (context .Background (), instanceIDs , eventChan , pollStartTime )
720+ err := client .handleMaintenanceEvents (context .Background (), eventChan , pollStartTime )
718721 // Should return error because 2 entities have invalid/nil values
719722 assert .Error (t , err )
720723 assert .Contains (t , err .Error (), "entity with nil EntityValue" )
@@ -786,12 +789,12 @@ func TestInstanceRebootEvent(t *testing.T) {
786789 }, nil )
787790 // Setup test channel and test instance IDs
788791 eventChan := make (chan model.MaintenanceEvent , 10 )
789- instanceIDs : = map [string ]string {
792+ client . nodeInformer . instanceIDs = map [string ]string {
790793 testInstanceID : testNodeName ,
791794 }
792795
793796 // Call the function being tested
794- err := client .handleMaintenanceEvents (context .Background (), instanceIDs , eventChan , pollStartTime )
797+ err := client .handleMaintenanceEvents (context .Background (), eventChan , pollStartTime )
795798 assert .NoError (t , err )
796799
797800 // Verify we received a maintenance event with correct type
@@ -808,11 +811,9 @@ func TestInstanceRebootEvent(t *testing.T) {
808811 }
809812}
810813
811- // Test that ignores instance retirement events
812814func TestIgnoredEventTypes (t * testing.T ) {
813815 client , mockAWSClient , _ := createTestClient (t )
814816
815- // Setup test data for events that should be ignored
816817 startTime := time .Now ().Add (24 * time .Hour )
817818 endTime := startTime .Add (2 * time .Hour )
818819 testInstanceIDIgnored := "i-0123456789abcdef1"
@@ -829,7 +830,6 @@ func TestIgnoredEventTypes(t *testing.T) {
829830 testRegion , testService , MAINTENANCE_SCHEDULED ,
830831 )
831832
832- // Setup AWS Health API mock with events to be ignored
833833 mockAWSClient .On ("DescribeEvents" , mock .Anything , mock .Anything ).Return (& health.DescribeEventsOutput {
834834 Events : []types.Event {
835835 {
@@ -886,15 +886,13 @@ func TestIgnoredEventTypes(t *testing.T) {
886886 },
887887 }, nil )
888888
889- // Setup test channel and test instance IDs
890889 eventChan := make (chan model.MaintenanceEvent , 10 )
891- instanceIDs : = map [string ]string {
890+ client . nodeInformer . instanceIDs = map [string ]string {
892891 testInstanceID : testNodeName ,
893892 testInstanceIDIgnored : testNodeNameIgnored ,
894893 }
895894
896- // Call the function being tested
897- err := client .handleMaintenanceEvents (context .Background (), instanceIDs , eventChan , pollStartTime )
895+ err := client .handleMaintenanceEvents (context .Background (), eventChan , pollStartTime )
898896 assert .NoError (t , err )
899897
900898 // Verify no events were received (as these should be filtered out)
@@ -905,6 +903,5 @@ func TestIgnoredEventTypes(t *testing.T) {
905903 assert .Equal (t , testInstanceIDIgnored , event .ResourceID )
906904 assert .Equal (t , model .StatusDetected , event .Status )
907905 default :
908- // This is expected, no events should be present
909906 }
910907}
0 commit comments