|
12 | 12 |
|
13 | 13 | import logging |
14 | 14 | import os |
| 15 | +from unittest.mock import mock_open |
15 | 16 |
|
16 | 17 | import pytest |
17 | 18 | import slurm_plugin |
18 | 19 | from assertpy import assert_that |
19 | | -from slurm_plugin.computemgtd import ComputemgtdConfig, _is_self_node_down, _self_terminate |
| 20 | +from slurm_plugin.computemgtd import ComputemgtdConfig, _is_self_node_down, _is_ubuntu2404, _self_terminate |
20 | 21 | from slurm_plugin.slurm_resources import DynamicNode |
21 | 22 |
|
22 | 23 |
|
@@ -103,13 +104,38 @@ def test_is_self_node_down(mock_node_info, expected_result, mocker): |
103 | 104 | assert_that(_is_self_node_down("queue1-st-c5xlarge-1")).is_equal_to(expected_result) |
104 | 105 |
|
105 | 106 |
|
106 | | -def test_self_terminate(mocker, caplog): |
| 107 | +@pytest.mark.parametrize( |
| 108 | + ("is_ubuntu2404", "expected_cmd"), |
| 109 | + [ |
| 110 | + (True, "sudo systemctl poweroff --force"), |
| 111 | + (False, "sudo shutdown -h now"), |
| 112 | + ], |
| 113 | +) |
| 114 | +def test_self_terminate(mocker, caplog, is_ubuntu2404, expected_cmd): |
107 | 115 | """Verify self-termination is implemented via a shutdown command rather than calling TerminateInstances.""" |
| 116 | + mocker.patch("slurm_plugin.computemgtd._is_ubuntu2404", return_value=is_ubuntu2404) |
108 | 117 | run_command_patch = mocker.patch("slurm_plugin.computemgtd.run_command") |
109 | 118 | sleep_patch = mocker.patch("slurm_plugin.computemgtd.time.sleep") |
110 | 119 | with caplog.at_level(logging.INFO): |
111 | 120 | _self_terminate() |
112 | 121 | assert_that(caplog.text).contains("Preparing to self terminate the instance in 10 seconds!") |
113 | 122 | assert_that(caplog.text).contains("Self terminating instance now!") |
114 | | - run_command_patch.assert_called_with("sudo shutdown -h now") |
| 123 | + run_command_patch.assert_called_with(expected_cmd) |
115 | 124 | sleep_patch.assert_called_with(10) |
| 125 | + |
| 126 | + |
| 127 | +@pytest.mark.parametrize( |
| 128 | + ("file_content", "expected"), |
| 129 | + [ |
| 130 | + ('ID=ubuntu\nVERSION_ID="24.04"\n', True), |
| 131 | + ('ID=ubuntu\nVERSION_ID="24.04.2"\n', True), |
| 132 | + ('ID=ubuntu\nVERSION_ID="22.04"\n', False), |
| 133 | + ('ID=rocky\nVERSION_ID="9.3"\n', False), |
| 134 | + ("ID=ubuntu\n", False), |
| 135 | + ], |
| 136 | +) |
| 137 | +def test_is_ubuntu2404(file_content, expected, mocker): |
| 138 | + m = mock_open(read_data=file_content) |
| 139 | + mocker.patch("builtins.open", m) |
| 140 | + |
| 141 | + assert _is_ubuntu2404() is expected |
0 commit comments