generated from ansible-collections/collection_template
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added unit tests and updated version_utils.py
- Loading branch information
Kilian Soltermann
committed
Nov 9, 2023
1 parent
a2a62f4
commit 24f0362
Showing
4 changed files
with
190 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright: (c) 2023, Puzzle ITC | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
"""Tests for the plugins.module_utils.opnsense_utils module.""" | ||
|
||
from __future__ import (absolute_import, division, print_function) | ||
|
||
__metaclass__ = type | ||
|
||
import subprocess | ||
from unittest.mock import patch, MagicMock | ||
from ansible_collections.puzzle.opnsense.plugins.module_utils import opnsense_utils | ||
|
||
|
||
@patch("subprocess.run") | ||
def test_run_function(mock_subprocess_run: MagicMock): | ||
""" | ||
Test the `run_function` utility which executes a PHP function with optional parameters. | ||
This unit test mocks the `subprocess.run` method to simulate the execution of a PHP script | ||
that includes specific PHP files and calls a given function with parameters, if any. | ||
The purpose is to ensure that the `run_function` utility correctly constructs the PHP command, | ||
executes it, and returns the standard output. | ||
The mock is set up to ensure that `subprocess.run` behaves as if the PHP script was executed | ||
successfully, returning an output as expected. | ||
Mocks: | ||
- mock_subprocess_run (MagicMock): A mock for `subprocess.run` to prevent the actual execution | ||
of the PHP command during testing. It is configured to simulate a successful execution with | ||
a predetermined standard output. | ||
Assertions: | ||
- Asserts that `run_function` returns the standard output correctly processed as a string. | ||
Raises: | ||
- AssertionError: If the `run_function` does not return the expected standard output string. | ||
""" | ||
|
||
# Mock the subprocess.run to return a predefined output | ||
mock_output = b"Function executed successfully" | ||
mock_subprocess_run.return_value.stdout = mock_output | ||
|
||
# Define the PHP requirements and the function with parameters to be tested | ||
php_requirements = ["/usr/local/etc/inc/config.inc", "/usr/local/etc/inc/util.inc"] | ||
configure_function = 'plugins_configure' | ||
configure_params = ['dns', 'true'] | ||
|
||
# Call run_function with the test parameters | ||
result = opnsense_utils.run_function(php_requirements, configure_function, configure_params) | ||
|
||
# Assert the result matches the mocked standard output | ||
assert result.decode() == mock_output.decode() | ||
|
||
# Assert the subprocess.run was called with the expected command | ||
expected_command = [ | ||
"php", | ||
"-r", | ||
"require '/usr/local/etc/inc/config.inc'; " | ||
"require '/usr/local/etc/inc/util.inc'; " | ||
"plugins_configure(dns,true);" | ||
] | ||
|
||
mock_subprocess_run.assert_called_with( | ||
expected_command, | ||
stdout=subprocess.PIPE, | ||
check=True | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright: (c) 2023, Puzzle ITC | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
"""Tests for the plugins.module_utils.version_utils module.""" | ||
|
||
from __future__ import (absolute_import, division, print_function) | ||
|
||
__metaclass__ = type | ||
|
||
from unittest.mock import patch, MagicMock | ||
|
||
from ansible_collections.puzzle.opnsense.plugins.module_utils import version_utils | ||
|
||
|
||
@patch("subprocess.check_output", return_value=" OPNsense 23.1 ") | ||
def test_version_utils(mock_object: MagicMock): | ||
""" | ||
Test the retrieval of the OPNsense version using the version_utils module. | ||
This unit test mocks the `subprocess.check_output` method to simulate the system's response | ||
for the OPNsense version check. It ensures that the `get_opnsense_version` function | ||
correctly processes the output from the subprocess call, trimming any extraneous whitespace, | ||
and returns the exact version string. | ||
The mock is configured to return a string with leading and trailing spaces around the version | ||
number, which mimics the real subprocess output behavior. The test checks that the function | ||
under test extracts the version number accurately, without any surrounding whitespace. | ||
Mocks: | ||
- mock_subprocess_check_output (MagicMock): A mock for `subprocess.check_output` to avoid | ||
executing the actual command line call during testing. | ||
Assertions: | ||
- Asserts that the `get_opnsense_version` function returns "OPNsense 23.1" exactly, ensuring | ||
that any preprocessing of the output is handled correctly. | ||
Raises: | ||
- AssertionError: If the `get_opnsense_version` does not return the expected version string. | ||
""" | ||
|
||
assert version_utils.get_opnsense_version() == "OPNsense 23.1" |