mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-08-06 06:04:24 -07:00
changes
This commit is contained in:
parent
ed5e9de244
commit
d088911ed8
1 changed files with 42 additions and 13 deletions
|
@ -1,7 +1,5 @@
|
||||||
# SPDX-FileCopyrightText: (c) 2025, Samaneh Yousefnezhad <s-yousefenzhad@um.ac.ir>
|
# SPDX-FileCopyrightText: (c) 2025, Samaneh Yousefnezhad <s-yousefenzhad@um.ac.ir>
|
||||||
|
|
||||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
from __future__ import absolute_import, division, print_function
|
from __future__ import absolute_import, division, print_function
|
||||||
|
@ -10,13 +8,15 @@ __metaclass__ = type
|
||||||
try:
|
try:
|
||||||
from unittest.mock import mock_open, patch, MagicMock
|
from unittest.mock import mock_open, patch, MagicMock
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
|
||||||
from mock import mock_open, patch, MagicMock
|
from mock import mock_open, patch, MagicMock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
|
||||||
# Add plugins/modules to path for direct import
|
|
||||||
sys.path.insert(0, os.path.abspath(
|
sys.path.insert(0, os.path.abspath(
|
||||||
os.path.join(os.path.dirname(__file__), '../../../../plugins/modules')
|
os.path.join(os.path.dirname(__file__), '../../../../plugins/modules')
|
||||||
))
|
))
|
||||||
|
@ -33,15 +33,33 @@ def fake_exports_content():
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def calculate_expected_digests(content_string):
|
||||||
|
content_bytes = content_string.encode('utf-8')
|
||||||
|
digests = {}
|
||||||
|
hash_algorithms = ['sha256', 'sha1', 'md5']
|
||||||
|
for algo in hash_algorithms:
|
||||||
|
try:
|
||||||
|
hasher = hashlib.new(algo)
|
||||||
|
hasher.update(content_bytes)
|
||||||
|
digests[algo] = hasher.hexdigest()
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
return digests
|
||||||
|
|
||||||
|
|
||||||
def test_get_exports_ips_per_share(fake_exports_content):
|
def test_get_exports_ips_per_share(fake_exports_content):
|
||||||
mock_module = MagicMock()
|
mock_module = MagicMock()
|
||||||
mock_module.digest_from_file.return_value = "fake_sha1_digest"
|
mock_module.file_exists.return_value = True
|
||||||
|
mock_module.warn.return_value = None
|
||||||
|
mock_module.fail_json.side_effect = Exception("fail_json called")
|
||||||
|
|
||||||
|
|
||||||
patch_target = "builtins.open" if sys.version_info[0] == 3 else "__builtin__.open"
|
patch_target = "builtins.open" if sys.version_info[0] == 3 else "__builtin__.open"
|
||||||
with patch(patch_target, mock_open(read_data=fake_exports_content)):
|
|
||||||
|
with patch(patch_target, mock_open(read_data=fake_exports_content.encode('utf-8'))):
|
||||||
result = get_exports(mock_module, "ips_per_share")
|
result = get_exports(mock_module, "ips_per_share")
|
||||||
|
|
||||||
expected = {
|
expected_exports_info = {
|
||||||
'/srv/nfs1': [
|
'/srv/nfs1': [
|
||||||
{'ip': '192.168.1.10', 'options': ['rw', 'sync']},
|
{'ip': '192.168.1.10', 'options': ['rw', 'sync']},
|
||||||
{'ip': '192.168.1.20', 'options': ['ro', 'sync']}
|
{'ip': '192.168.1.20', 'options': ['ro', 'sync']}
|
||||||
|
@ -51,19 +69,28 @@ def test_get_exports_ips_per_share(fake_exports_content):
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
assert result['exports_info'] == expected
|
expected_file_digests = calculate_expected_digests(fake_exports_content)
|
||||||
assert result['file_digest'] == "fake_sha1_digest"
|
|
||||||
|
assert result['exports_info'] == expected_exports_info
|
||||||
|
assert result['file_digest'] == expected_file_digests
|
||||||
|
|
||||||
|
|
||||||
def test_get_exports_shares_per_ip(fake_exports_content):
|
def test_get_exports_shares_per_ip(fake_exports_content):
|
||||||
mock_module = MagicMock()
|
mock_module = MagicMock()
|
||||||
mock_module.digest_from_file.return_value = "fake_sha1_digest"
|
|
||||||
|
|
||||||
|
|
||||||
|
mock_module.file_exists.return_value = True
|
||||||
|
mock_module.warn.return_value = None
|
||||||
|
mock_module.fail_json.side_effect = Exception("fail_json called")
|
||||||
|
|
||||||
|
|
||||||
patch_target = "builtins.open" if sys.version_info[0] == 3 else "__builtin__.open"
|
patch_target = "builtins.open" if sys.version_info[0] == 3 else "__builtin__.open"
|
||||||
with patch(patch_target, mock_open(read_data=fake_exports_content)):
|
|
||||||
|
with patch(patch_target, mock_open(read_data=fake_exports_content.encode('utf-8'))):
|
||||||
result = get_exports(mock_module, "shares_per_ip")
|
result = get_exports(mock_module, "shares_per_ip")
|
||||||
|
|
||||||
expected = {
|
expected_exports_info = {
|
||||||
'192.168.1.10': [
|
'192.168.1.10': [
|
||||||
{'folder': '/srv/nfs1', 'options': ['rw', 'sync']}
|
{'folder': '/srv/nfs1', 'options': ['rw', 'sync']}
|
||||||
],
|
],
|
||||||
|
@ -75,5 +102,7 @@ def test_get_exports_shares_per_ip(fake_exports_content):
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
assert result['exports_info'] == expected
|
expected_file_digests = calculate_expected_digests(fake_exports_content)
|
||||||
assert result['file_digest'] == "fake_sha1_digest"
|
|
||||||
|
assert result['exports_info'] == expected_exports_info
|
||||||
|
assert result['file_digest'] == expected_file_digests
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue