From a230e6e28e3f03f0c98cc2402388f0181d4cacd6 Mon Sep 17 00:00:00 2001 From: rkavitha-hcl Date: Fri, 29 Nov 2024 13:38:28 +0000 Subject: [PATCH] sonic-host-services changes for gNOI Warm Reboot --- host_modules/gnoi_reboot.py | 14 +++++--------- tests/gnoi_reboot_test.py | 34 +++++++++++++++++----------------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/host_modules/gnoi_reboot.py b/host_modules/gnoi_reboot.py index 0eae52d8..399e6cbd 100644 --- a/host_modules/gnoi_reboot.py +++ b/host_modules/gnoi_reboot.py @@ -12,13 +12,12 @@ # Both enum and string representations are supported REBOOTMETHOD_COLD_BOOT_VALUES = {1, "COLD"} REBOOTMETHOD_WARM_BOOT_VALUES = {4, "WARM"} -REBOOTMETHOD_NSF_VALUES = {5, "NSF"} # Timeout for SONiC Host Service to be killed during reboot REBOOT_TIMEOUT = 260 EXECUTE_COLD_REBOOT_COMMAND = "sudo reboot" -EXECUTE_NSF_REBOOT_COMMAND = "/etc/init.d/gpins-nsf-boot nsf-reboot" +EXECUTE_WARM_REBOOT_COMMAND = "/usr/local/bin/warm-reboot -v" logger = logging.getLogger(__name__) @@ -54,7 +53,7 @@ def validate_reboot_request(self, reboot_request): # Check whether reboot method is valid. rebootmethod = reboot_request["method"] valid_method = False - for values in [REBOOTMETHOD_COLD_BOOT_VALUES, REBOOTMETHOD_NSF_VALUES]: + for values in [REBOOTMETHOD_COLD_BOOT_VALUES, REBOOTMETHOD_WARM_BOOT_VALUES]: if rebootmethod in values: valid_method = True if not valid_method: @@ -70,13 +69,10 @@ def execute_reboot(self, rebootmethod): if rebootmethod in REBOOTMETHOD_COLD_BOOT_VALUES: command = EXECUTE_COLD_REBOOT_COMMAND - f = open("/tmp/hostlog.txt", "w") - f.write("Received reboot command ! ") - f.close() logger.warning("%s: Issuing cold reboot", MOD_NAME) - elif rebootmethod in REBOOTMETHOD_NSF_VALUES: - command = EXECUTE_NSF_REBOOT_COMMAND - logger.warning("%s: Issuing NSF reboot", MOD_NAME) + elif rebootmethod in REBOOTMETHOD_WARM_BOOT_VALUES: + command = EXECUTE_WARM_REBOOT_COMMAND + logger.warning("%s: Issuing WARM reboot", MOD_NAME) else: logger.error("%s: Invalid reboot method: %d", MOD_NAME, rebootmethod) return diff --git a/tests/gnoi_reboot_test.py b/tests/gnoi_reboot_test.py index 76ef0347..368412a1 100644 --- a/tests/gnoi_reboot_test.py +++ b/tests/gnoi_reboot_test.py @@ -23,13 +23,13 @@ REBOOTMETHOD_UNKNOWN_ENUM = 0 REBOOTMETHOD_COLD_BOOT_ENUM = 1 -REBOOTMETHOD_NSF_ENUM = 5 +REBOOTMETHOD_WARM_BOOT_ENUM = 4 TEST_TIMESTAMP = 1618942253.831912040 REPORT_CRITICAL_STATE_FULL_COMMAND = "redis-cli -n 6 HSET COMPONENT_STATE_TABLE|host state ERROR reason \"cold reboot has failed\" essential true timestamp \"2021-04-20 18:10:53\" timestamp-seconds 1618942253 timestamp-nanoseconds 831912040" VALID_REBOOT_REQUEST_COLD = "{\"method\": 1, \"message\": \"test reboot request reason\"}" -VALID_REBOOT_REQUEST_NSF = "{\"method\": \"NSF\", \"message\": \"test reboot request reason\"}" +VALID_REBOOT_REQUEST_WARM = "{\"method\": \"WARM\", \"message\": \"test reboot request reason\"}" INVALID_REBOOT_REQUEST = "\"method\": 1, \"message\": \"test reboot request reason\"" imp.load_source("host_service", host_modules_path + "/host_service.py") @@ -62,14 +62,14 @@ def test_validate_reboot_request_success_cold_boot_string_method(self): assert result[0] == 0 assert result[1] == "" - def test_validate_reboot_request_success_nsf_enum_method(self): - reboot_request = {"method": REBOOTMETHOD_NSF_ENUM, "reason": "test reboot request reason"} + def test_validate_reboot_request_success_warm_enum_method(self): + reboot_request = {"method": REBOOTMETHOD_WARM_BOOT_ENUM, "reason": "test reboot request reason"} result = self.gnoi_reboot_module.validate_reboot_request(reboot_request) assert result[0] == 0 assert result[1] == "" - def test_validate_reboot_request_success_nsf_enum_method(self): - reboot_request = {"method": "NSF", "reason": "test reboot request reason"} + def test_validate_reboot_request_success_WARM_enum_method(self): + reboot_request = {"method": "WARM", "reason": "test reboot request reason"} result = self.gnoi_reboot_module.validate_reboot_request(reboot_request) assert result[0] == 0 assert result[1] == "" @@ -98,9 +98,9 @@ def test_execute_reboot_success(self): mock.patch("time.sleep") as mock_sleep, mock.patch("gnoi_reboot.GnoiReboot.populate_reboot_status_flag") as mock_populate_reboot_status_flag, ): - mock_run_command.return_value = (0, ["stdout: execute NSF reboot"], ["stderror: execute NSF reboot"]) - self.gnoi_reboot_module.execute_reboot("NSF") - mock_run_command.assert_called_once_with("/etc/init.d/gpins-nsf-boot nsf-reboot") + mock_run_command.return_value = (0, ["stdout: execute WARM reboot"], ["stderror: execute WARM reboot"]) + self.gnoi_reboot_module.execute_reboot("WARM") + mock_run_command.assert_called_once_with("/usr/local/bin/warm-reboot -v") mock_sleep.assert_called_once_with(260) mock_populate_reboot_status_flag.assert_called_once_with() @@ -124,17 +124,17 @@ def test_execute_reboot_fail_issue_reboot_command_cold_boot(self, caplog): assert caplog.records[0].message == msg mock_populate_reboot_status_flag.assert_called_once_with() - def test_execute_reboot_fail_issue_reboot_command_nsf(self, caplog): + def test_execute_reboot_fail_issue_reboot_command_warm(self, caplog): with ( mock.patch("gnoi_reboot._run_command") as mock_run_command, mock.patch("gnoi_reboot.GnoiReboot.populate_reboot_status_flag") as mock_populate_reboot_status_flag, caplog.at_level(logging.ERROR), ): - mock_run_command.return_value = (1, ["stdout: execute NSF reboot"], ["stderror: execute NSF reboot"]) - self.gnoi_reboot_module.execute_reboot("NSF") + mock_run_command.return_value = (1, ["stdout: execute WARM reboot"], ["stderror: execute WARM reboot"]) + self.gnoi_reboot_module.execute_reboot("WARM") msg = ("gnoi_reboot: Reboot failed execution with " - "stdout: ['stdout: execute NSF reboot'], stderr: " - "['stderror: execute NSF reboot']") + "stdout: ['stdout: execute WARM reboot'], stderr: " + "['stderror: execute WARM reboot']") assert caplog.records[0].message == msg mock_populate_reboot_status_flag.assert_called_once_with() @@ -153,18 +153,18 @@ def test_issue_reboot_success_cold_boot(self): ) mock_thread.return_value.start.assert_called_once_with() - def test_issue_reboot_success_nsf(self): + def test_issue_reboot_success_warm(self): with ( mock.patch("threading.Thread") as mock_thread, mock.patch("gnoi_reboot.GnoiReboot.validate_reboot_request", return_value=(0, "")), ): self.gnoi_reboot_module.populate_reboot_status_flag() - result = self.gnoi_reboot_module.issue_reboot([VALID_REBOOT_REQUEST_NSF]) + result = self.gnoi_reboot_module.issue_reboot([VALID_REBOOT_REQUEST_WARM]) assert result[0] == 0 assert result[1] == "Successfully issued reboot" mock_thread.assert_called_once_with( target=self.gnoi_reboot_module.execute_reboot, - args=("NSF",), + args=("WARM",), ) mock_thread.return_value.start.assert_called_once_with()