Skip to content

Commit 0fab0e2

Browse files
committed
Add fake_open_factory to improve file handling in HAClusterValidator tests
1 parent 9d53980 commit 0fab0e2

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

tests/modules/get_pcmk_properties_test.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
Unit tests for the get_pcmk_properties module.
66
"""
77

8+
import io
89
import pytest
9-
from mock import mock_open
1010
from src.modules.get_pcmk_properties import HAClusterValidator, main
1111

1212
DUMMY_XML_RSC = """<rsc_defaults>
@@ -125,6 +125,22 @@
125125
}
126126

127127

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+
128144
class TestHAClusterValidator:
129145
"""
130146
Test cases for the HAClusterValidator class.
@@ -169,23 +185,11 @@ def mock_execute_command(*args, **kwargs):
169185
return DUMMY_OS_COMMAND
170186
return mock_xml_outputs.get(command[-1], "")
171187

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-
183188
monkeypatch.setattr(
184189
"src.module_utils.sap_automation_qa.SapAutomationQA.execute_command_subprocess",
185190
mock_execute_command,
186191
)
187-
monkeypatch.setattr("builtins.open", mock_open_file)
188-
192+
monkeypatch.setattr("builtins.open", fake_open_factory(DUMMY_GLOBAL_INI))
189193
return HAClusterValidator(
190194
os_type="REDHAT",
191195
os_version="9.2",

0 commit comments

Comments
 (0)