@@ -325,32 +325,35 @@ def test_mgmtiface_event(self):
325325 ]
326326 mocked_check_output .assert_has_calls (expected )
327327
328- def test_memory_statistics_event (self ):
329- MockConfigDb .set_config_db (HOSTCFG_DAEMON_CFG_DB )
330- daemon = hostcfgd .HostConfigDaemon ()
331- daemon .register_callbacks ()
328+ def test_memory_statistics_event (self , mock_config_db_connector ):
329+ # Mock the ConfigDBConnector instance methods
330+ mock_instance = mock_config_db_connector .return_value
331+ # Make sure get_table returns the correct nested structure
332+ mock_instance .get_table .return_value = HOSTCFG_DAEMON_CFG_DB ['MEMORY_STATISTICS' ]['memory_statistics' ]
333+
334+ # Mock the subprocess module to avoid real process callswith mock.patch('hostcfgd.subprocess') as mocked_subprocess:
335+ daemon = hostcfgd .HostConfigDaemon () # Create the daemon instance# Load config using the correct nested dictionary
336+ daemon .memory_statisticsCfg .load (HOSTCFG_DAEMON_CFG_DB ['MEMORY_STATISTICS' ]['memory_statistics' ])
337+
338+ # Mock the subprocess.Popen and check_call
339+ popen_mock = mock .Mock ()
340+ attrs = {'communicate.return_value' : ('output' , 'error' )}
341+ popen_mock .configure_mock (** attrs )
342+ mocked_subprocess .Popen .return_value = popen_mock
343+ mocked_subprocess .check_call = mock .Mock ()
344+
345+ # Trigger event handler
332346 MockConfigDb .event_queue = [('MEMORY_STATISTICS' , 'config' )]
347+ daemon .memory_statistics_handler ('enabled' , 'SET' , 'true' )
333348
334- with mock .patch ('hostcfgd.subprocess' ) as mocked_subprocess :
335- popen_mock = mock .Mock ()
336- attrs = {'communicate.return_value' : ('output' , 'error' )}
337- popen_mock .configure_mock (** attrs )
338- mocked_subprocess .Popen .return_value = popen_mock
339- mocked_subprocess .check_call = mock .Mock ()
340-
341- try :
342- daemon .start ()
343- except TimeoutError :
344- pass
345-
346- expected_calls = [
347- mock .call (['sonic-memory_statistics-config' , '--enable' ]),
348- mock .call (['sonic-memory_statistics-config' , '--retention_time' , '15 days' ]),
349- mock .call (['sonic-memory_statistics-config' , '--sampling_interval' , '5 minutes' ])
350- ]
351-
352- mocked_subprocess .check_call .assert_has_calls (expected_calls , any_order = True )
349+ # Define expected subprocess calls
350+ expected_calls = [
351+ mock .call (['/usr/bin/memorystatsd' ]),
352+ ]
353353
354+ # Check if subprocess check_call was made with correct arguments
355+ mocked_subprocess .Popen .assert_has_calls (expected_calls , any_order = True )
356+
354357 def test_dns_events (self ):
355358 MockConfigDb .set_config_db (HOSTCFG_DAEMON_CFG_DB )
356359 MockConfigDb .event_queue = [('DNS_NAMESERVER' , '1.1.1.1' )]
0 commit comments