44import json
55import sys
66import os
7+ import time
78import pytest
89import logging
910
1920host_modules_path = os .path .join (sonic_host_service_path , "../host_modules" )
2021sys .path .insert (0 , sonic_host_service_path )
2122
23+ from host_modules .reboot import RebootStatus
24+
2225TIME = 1617811205
2326TEST_ACTIVE_RESPONSE_DATA = "{\" active\" : true, \" when\" : 1617811205, \" reason\" : \" testing reboot response\" }"
2427TEST_INACTIVE_RESPONSE_DATA = "{\" active\" : false, \" when\" : 0, \" reason\" : \" \" }"
@@ -54,6 +57,9 @@ def test_populate_reboot_status_flag(self):
5457 assert self .reboot_module .reboot_status_flag ["active" ] == False
5558 assert self .reboot_module .reboot_status_flag ["when" ] == 0
5659 assert self .reboot_module .reboot_status_flag ["reason" ] == ""
60+ assert self .reboot_module .reboot_status_flag ["count" ] == 0
61+ assert self .reboot_module .reboot_status_flag ["method" ] == ""
62+ assert self .reboot_module .reboot_status_flag ["status" ] == RebootStatus .STATUS_UNKNOWN
5763
5864 def test_validate_reboot_request_success_cold_boot_enum_method (self ):
5965 reboot_request = {"method" : REBOOT_METHOD_COLD_BOOT_ENUM , "reason" : "test reboot request reason" }
@@ -115,12 +121,12 @@ def test_is_container_running_success(self):
115121 mock_docker .return_value = mock_client
116122 mock_container = mock .Mock ()
117123 mock_container .name = "pmon"
118- mock_container .status = "running"
124+ mock_container .attrs = { 'State' : { 'Running' : True }}
119125 mock_client .containers .list .return_value = [mock_container ]
120126
121127 result = self .reboot_module .is_container_running ("pmon" )
122128 assert result is True
123- mock_client .containers .list .assert_called_once_with (filters = {"name" : "pmon" })
129+ mock_client .containers .list .assert_called_once_with (filters = {"name" : "^ pmon$ " })
124130
125131 def test_is_container_running_failure (self ):
126132 with mock .patch ("docker.from_env" ) as mock_docker :
@@ -130,7 +136,7 @@ def test_is_container_running_failure(self):
130136
131137 result = self .reboot_module .is_container_running ("pmon" )
132138 assert result is False
133- mock_client .containers .list .assert_called_once_with (filters = {"name" : "pmon" })
139+ mock_client .containers .list .assert_called_once_with (filters = {"name" : "^ pmon$ " })
134140
135141 def test_is_container_running_exception (self , caplog ):
136142 with mock .patch ("docker.from_env" , side_effect = Exception ("Docker error" )) as mock_docker , \
@@ -176,7 +182,7 @@ def test_execute_reboot_success(self):
176182 self .reboot_module .execute_reboot ("WARM" )
177183 mock_run_command .assert_called_once_with ("sudo warm-reboot" )
178184 mock_sleep .assert_called_once_with (260 )
179- mock_populate_reboot_status_flag .assert_called_once_with ()
185+ mock_populate_reboot_status_flag .assert_called_once_with (False , int ( time . time ()), "Reboot command failed to execute" , 'WARM' , RebootStatus . STATUS_FAILURE )
180186
181187 def test_execute_reboot_fail_unknown_reboot (self , caplog ):
182188 with caplog .at_level (logging .ERROR ):
@@ -196,7 +202,7 @@ def test_execute_reboot_fail_issue_reboot_command_cold_boot(self, caplog):
196202 "stdout: ['stdout: execute cold reboot'], stderr: "
197203 "['stderror: execute cold reboot']" )
198204 assert caplog .records [0 ].message == msg
199- mock_populate_reboot_status_flag .assert_called_once_with ()
205+ mock_populate_reboot_status_flag .assert_called_once_with (False , int ( time . time ()), "Failed to execute reboot command" , 1 , RebootStatus . STATUS_FAILURE )
200206
201207 def test_execute_reboot_fail_issue_reboot_command_halt (self , caplog ):
202208 with (
@@ -210,7 +216,7 @@ def test_execute_reboot_fail_issue_reboot_command_halt(self, caplog):
210216 "stdout: ['stdout: execute halt reboot'], stderr: "
211217 "['stderror: execute halt reboot']" )
212218 assert caplog .records [0 ].message == msg
213- mock_populate_reboot_status_flag .assert_called_once_with ()
219+ mock_populate_reboot_status_flag .assert_called_once_with (False , int ( time . time ()), "Failed to execute reboot command" , 3 , RebootStatus . STATUS_FAILURE )
214220
215221 def test_execute_reboot_success_halt (self ):
216222 with (
@@ -225,7 +231,7 @@ def test_execute_reboot_success_halt(self):
225231 mock_run_command .assert_called_once_with ("sudo reboot -p" )
226232 mock_is_halt_command_running .assert_called ()
227233 mock_is_container_running .assert_called_with ("pmon" )
228- mock_populate_reboot_status_flag .assert_not_called ( )
234+ mock_populate_reboot_status_flag .assert_called_once_with ( False , 0 , 'Halt reboot completed' , 3 , RebootStatus . STATUS_SUCCESS )
229235
230236 def test_execute_reboot_fail_halt_timeout (self , caplog ):
231237 with (
@@ -242,7 +248,7 @@ def test_execute_reboot_fail_halt_timeout(self, caplog):
242248 mock_sleep .assert_called_with (5 )
243249 mock_is_halt_command_running .assert_called ()
244250 assert any ("HALT reboot failed: Services are still running" in record .message for record in caplog .records )
245- mock_populate_reboot_status_flag .assert_called_once_with ()
251+ mock_populate_reboot_status_flag .assert_called_once_with (False , int ( time . time ()), 'Halt reboot did not complete' , 3 , RebootStatus . STATUS_FAILURE )
246252
247253 def test_execute_reboot_fail_issue_reboot_command_warm (self , caplog ):
248254 with (
@@ -256,7 +262,7 @@ def test_execute_reboot_fail_issue_reboot_command_warm(self, caplog):
256262 "stdout: ['stdout: execute WARM reboot'], stderr: "
257263 "['stderror: execute WARM reboot']" )
258264 assert caplog .records [0 ].message == msg
259- mock_populate_reboot_status_flag .assert_called_once_with ()
265+ mock_populate_reboot_status_flag .assert_called_once_with (False , int ( time . time ()), "Failed to execute reboot command" , 'WARM' , RebootStatus . STATUS_FAILURE )
260266
261267 def test_issue_reboot_success_cold_boot (self ):
262268 with (
@@ -355,22 +361,26 @@ def test_issue_reboot_fail_issue_reboot_thread(self):
355361
356362 def test_get_reboot_status_active (self ):
357363 MSG = "testing reboot response"
358- self .reboot_module .populate_reboot_status_flag (True , TIME , MSG )
364+ self .reboot_module .populate_reboot_status_flag (True , TIME , MSG , REBOOT_METHOD_COLD_BOOT_ENUM , RebootStatus . STATUS_SUCCESS . name )
359365 result = self .reboot_module .get_reboot_status ()
360366 assert result [0 ] == 0
361367 response_data = json .loads (result [1 ])
362368 assert response_data ["active" ] == True
363369 assert response_data ["when" ] == TIME
364370 assert response_data ["reason" ] == MSG
371+ assert response_data ["method" ] == REBOOT_METHOD_COLD_BOOT_ENUM
372+ assert response_data ["status" ] == RebootStatus .STATUS_SUCCESS .name
365373
366374 def test_get_reboot_status_inactive (self ):
367- self .reboot_module .populate_reboot_status_flag (False , 0 , "" )
375+ self .reboot_module .populate_reboot_status_flag (False , 0 , "" , REBOOT_METHOD_COLD_BOOT_ENUM , RebootStatus . STATUS_SUCCESS . name )
368376 result = self .reboot_module .get_reboot_status ()
369377 assert result [0 ] == 0
370378 response_data = json .loads (result [1 ])
371379 assert response_data ["active" ] == False
372380 assert response_data ["when" ] == 0
373381 assert response_data ["reason" ] == ""
382+ assert response_data ["method" ] == REBOOT_METHOD_COLD_BOOT_ENUM
383+ assert response_data ["status" ] == RebootStatus .STATUS_SUCCESS .name
374384
375385# assert result[1] == TEST_INACTIVE_RESPONSE_DATA
376386
0 commit comments