-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try to add the missing tests from CodeCov
- Loading branch information
1 parent
46e70c5
commit 8e8c7df
Showing
2 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
tests/unit/plugins/module_utils/test_hashi_vault_module.py
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,108 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2024 Mathijs Westerhof (@mathijswesterhof) | ||
# 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 | ||
|
||
import pytest | ||
|
||
from ansible_collections.community.hashi_vault.tests.unit.compat import mock | ||
from .....plugins.module_utils._hashi_vault_common import ( # pylint: disable=unused-import | ||
HashiVaultHVACError, | ||
HashiVaultHelper, | ||
) | ||
from .....plugins.module_utils._hashi_vault_module import HashiVaultModule | ||
|
||
|
||
@pytest.fixture | ||
def generate_argspec(): | ||
return HashiVaultModule.generate_argspec( | ||
want_exception=dict(type='bool'), | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def mock_hashi_vault_helper(): | ||
"""Fixture to mock HashiVaultHelper for testing""" | ||
with mock.patch('ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common.HashiVaultHelper') as mock_helper: | ||
yield mock_helper | ||
|
||
|
||
class TestHashiVaultModule: | ||
|
||
def test_init_success(self, generate_argspec, mock_hashi_vault_helper): | ||
"""Test successful initialization of HashiVaultModule.""" | ||
module = HashiVaultModule(argument_spec=generate_argspec, supports_check_mode=True) | ||
|
||
# Check if HashiVaultHelper was initialized | ||
mock_hashi_vault_helper.assert_called_once() | ||
|
||
# Ensure that the helper, adapter, connection options, and authenticator are set correctly | ||
assert module.helper is not None | ||
assert module.adapter is not None | ||
assert module.connection_options is not None | ||
assert module.authenticator is not None | ||
|
||
def test_init_hvac_error(self, generate_argspec): | ||
"""Test that HashiVaultHVACError triggers a call to fail_json.""" | ||
# Mock HashiVaultHelper to raise HashiVaultHVACError | ||
with mock.patch('ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common.HashiVaultHelper', | ||
side_effect=HashiVaultHVACError("Error occurred", "error_trace")): | ||
module = HashiVaultModule(argument_spec=generate_argspec, supports_check_mode=True) | ||
|
||
# Ensure that fail_json was called with the appropriate message and exception | ||
module.fail_json.assert_called_once_with( | ||
msg="Error occurred", | ||
exception="error_trace" | ||
) | ||
|
||
def test_generate_argspec(self): | ||
"""Test that generate_argspec correctly combines connection, authentication, and extra arguments.""" | ||
extra_args = {'extra_param': dict(type='str', required=False)} | ||
|
||
# Call the class method generate_argspec | ||
argspec = HashiVaultModule.generate_argspec(**extra_args) | ||
|
||
# Ensure the argument specification includes connection and authentication specs | ||
assert 'url' in argspec # From HashiVaultConnectionOptions.ARGSPEC | ||
assert 'auth_method' in argspec # From HashiVaultAuthenticator.ARGSPEC | ||
|
||
# Ensure extra parameters are included in the argument spec | ||
assert 'extra_param' in argspec | ||
|
||
def test_generate_retry_callback(self, generate_argspec): | ||
"""Test the _generate_retry_callback method and ensure it calls warn on retry action 'warn'.""" | ||
module = HashiVaultModule(argument_spec=generate_argspec, supports_check_mode=True) | ||
|
||
# Mock the `warn` method | ||
module.warn = mock.MagicMock() | ||
|
||
# Generate a retry callback with the 'warn' action | ||
retry_callback = module._generate_retry_callback('warn') | ||
|
||
# Create a mock retry object with retries remaining | ||
mock_retry_obj = mock.MagicMock(total=2) | ||
|
||
# Call the retry callback | ||
retry_callback(mock_retry_obj) | ||
|
||
# Ensure the warn method was called with the correct message | ||
module.warn.assert_called_once_with('community.hashi_vault: 2 retries remaining.') | ||
|
||
def test_generate_retry_callback_no_warn(self, generate_argspec): | ||
"""Test the _generate_retry_callback method when no warning is issued.""" | ||
module = HashiVaultModule(argument_spec=generate_argspec, supports_check_mode=True) | ||
|
||
# Mock the `warn` method | ||
module.warn = mock.MagicMock() | ||
|
||
# Generate a retry callback with no retry action | ||
retry_callback = module._generate_retry_callback('no_warn') | ||
|
||
# Create a mock retry object with retries remaining | ||
mock_retry_obj = mock.MagicMock(total=2) | ||
|
||
# Call the retry callback | ||
retry_callback(mock_retry_obj) | ||
|
||
# Ensure the warn method was not called | ||
module.warn.assert_not_called() |
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