|
5 | 5 | Unit tests for the get_pcmk_properties module. |
6 | 6 | """ |
7 | 7 |
|
| 8 | +import io |
8 | 9 | import pytest |
9 | | -from mock import mock_open |
10 | 10 | from src.modules.get_pcmk_properties import HAClusterValidator, main |
11 | 11 |
|
12 | 12 | DUMMY_XML_RSC = """<rsc_defaults> |
|
125 | 125 | } |
126 | 126 |
|
127 | 127 |
|
| 128 | +def fake_open_factory(file_content): |
| 129 | + """ |
| 130 | + Factory function to create a fake open function that returns a StringIO object. |
| 131 | +
|
| 132 | + :param file_content: Content to be returned by the fake open function. |
| 133 | + :type file_content: str |
| 134 | + :return: Fake open function. |
| 135 | + :rtype: function |
| 136 | + """ |
| 137 | + |
| 138 | + def fake_open(*args, **kwargs): |
| 139 | + return io.StringIO("\n".join(file_content)) |
| 140 | + |
| 141 | + return fake_open |
| 142 | + |
| 143 | + |
128 | 144 | class TestHAClusterValidator: |
129 | 145 | """ |
130 | 146 | Test cases for the HAClusterValidator class. |
@@ -169,23 +185,11 @@ def mock_execute_command(*args, **kwargs): |
169 | 185 | return DUMMY_OS_COMMAND |
170 | 186 | return mock_xml_outputs.get(command[-1], "") |
171 | 187 |
|
172 | | - def mock_open_file(*args): |
173 | | - """ |
174 | | - Mock function to replace open. |
175 | | -
|
176 | | - :return: Mocked file object. |
177 | | - :rtype: mock_open |
178 | | - """ |
179 | | - if "global.ini" in str(args[0]): |
180 | | - return mock_open(read_data=DUMMY_GLOBAL_INI)() |
181 | | - return mock_open()() |
182 | | - |
183 | 188 | monkeypatch.setattr( |
184 | 189 | "src.module_utils.sap_automation_qa.SapAutomationQA.execute_command_subprocess", |
185 | 190 | mock_execute_command, |
186 | 191 | ) |
187 | | - monkeypatch.setattr("builtins.open", mock_open_file) |
188 | | - |
| 192 | + monkeypatch.setattr("builtins.open", fake_open_factory(DUMMY_GLOBAL_INI)) |
189 | 193 | return HAClusterValidator( |
190 | 194 | os_type="REDHAT", |
191 | 195 | os_version="9.2", |
|
0 commit comments