diff --git a/tests/unit/base.py b/tests/unit/base.py new file mode 100644 index 00000000..82f5560e --- /dev/null +++ b/tests/unit/base.py @@ -0,0 +1,9 @@ +from ansible_collections.linode.cloud.plugins.module_utils.linode_common import LinodeModuleBase + +class TestModuleBase(LinodeModuleBase): + """ + Test module that represents an instance of LinodeModuleBase. + """ + def __init__(self): + self._client = None + pass \ No newline at end of file diff --git a/tests/unit/module_utils/test_linode_common.py b/tests/unit/module_utils/test_linode_common.py new file mode 100644 index 00000000..ae70ec79 --- /dev/null +++ b/tests/unit/module_utils/test_linode_common.py @@ -0,0 +1,21 @@ +from types import SimpleNamespace +import os + +from ansible_collections.linode.cloud.tests.unit.base import TestModuleBase + +class TestLinodeModuleBase(): + + def test_module_ca_path_override(self): + os.environ['LINODE_CA'] = "env_ca" + + mock_module = TestModuleBase() + mock_module.module = SimpleNamespace(params={ + "api_token":"testing", + "api_version": None, + "api_url":"/", + "ua_prefix": None, + "ca_path":"foobar" + }) + + client = mock_module.client + assert client.ca_path == "foobar" \ No newline at end of file diff --git a/tests/unit/module_utils/test_linode_common_info.py b/tests/unit/module_utils/test_linode_common_info.py index c3834a91..aca9e315 100644 --- a/tests/unit/module_utils/test_linode_common_info.py +++ b/tests/unit/module_utils/test_linode_common_info.py @@ -1,5 +1,3 @@ -import unittest - import pytest from ansible_collections.linode.cloud.plugins.module_utils.linode_common_info import InfoModule, InfoModuleResult, \ diff --git a/tests/unit/module_utils/test_linode_database_shared.py b/tests/unit/module_utils/test_linode_database_shared.py index 5b6e0539..76226b42 100644 --- a/tests/unit/module_utils/test_linode_database_shared.py +++ b/tests/unit/module_utils/test_linode_database_shared.py @@ -1,31 +1,30 @@ -import unittest - from linode_api4 import ApiError +import pytest from ansible_collections.linode.cloud.plugins.module_utils.linode_database_shared import validate_allow_list, validate_shared_db_input, call_protected_provisioning -class LinodeDatabaseSharedTest(unittest.TestCase): +class TestLinodeDatabaseShared(): def test_validate_allow_list_valid_cidr(self): # Valid CIDR format allow_list = {"192.168.0.1/24", "10.0.0.0/16", "172.16.0.0/12"} - self.assertIsNone(validate_allow_list(allow_list)) + assert not validate_allow_list(allow_list) def test_validate_allow_list_invalid_cidr(self): # Invalid CIDR format allow_list = {"192.168.0.1/24", "10.0.0.0", "172.16.0.0/12", "200.100.50.25"} - with self.assertRaises(ValueError): + with pytest.raises(ValueError): validate_allow_list(allow_list) def test_validate_shared_db_input_with_allow_list(self): # Valid allow_list params = {"allow_list": {"192.168.0.1/24", "10.0.0.0/16"}} - self.assertIsNone(validate_shared_db_input(params)) + assert not validate_shared_db_input(params) def test_validate_shared_db_input_without_allow_list(self): # No allow_list present in params params = {"other_field": "value"} - self.assertIsNone(validate_shared_db_input(params)) + assert not validate_shared_db_input(params) def test_call_protected_provisioning_with_exception(self): # Simulate an ApiError with status 400 @@ -33,12 +32,12 @@ def mock_provisioning_function(): raise ApiError(status=400, message="Api Error") result = call_protected_provisioning(mock_provisioning_function) - self.assertIsNone(result) + assert not result def test_call_protected_provisioning_with_other_exception(self): # Simulate an ApiError with status other than 400 def mock_provisioning_function(): raise ApiError(status=404, message="Not found") - - with self.assertRaises(ApiError): + + with pytest.raises(ApiError): call_protected_provisioning(mock_provisioning_function) diff --git a/tests/unit/module_utils/test_linode_helper.py b/tests/unit/module_utils/test_linode_helper.py index 0e609240..a5a93729 100644 --- a/tests/unit/module_utils/test_linode_helper.py +++ b/tests/unit/module_utils/test_linode_helper.py @@ -1,4 +1,4 @@ -import unittest +import pytest from ansible_collections.linode.cloud.plugins.module_utils.linode_helper import ( dict_select_spec, filter_null_values, @@ -7,7 +7,7 @@ ) -class LinodeHelperTest(unittest.TestCase): +class TestLinodeHelper(): def test_dict_select_spec(self): target = { @@ -27,7 +27,7 @@ def test_dict_select_spec(self): "key5": None, } result = dict_select_spec(target, spec) - self.assertEqual(result, expected_result) + assert result == expected_result def test_filter_null_values(self): input_dict = { @@ -45,7 +45,7 @@ def test_filter_null_values(self): "key6": "value6", } result = filter_null_values(input_dict) - self.assertEqual(result, expected_result) + assert result == expected_result def test_drop_empty_strings(self): input_dict = { @@ -59,17 +59,17 @@ def test_drop_empty_strings(self): "key4": "value4", } result = drop_empty_strings(input_dict) - self.assertEqual(result, expected_result) + assert result == expected_result def test_validate_required_with_missing_fields(self): required_fields = {"field1", "field2", "field3"} params = {"field1": "value1", "field4": "value4"} - with self.assertRaises(Exception) as context: + with pytest.raises(Exception) as context: validate_required(required_fields, params) - exception_fields = str(context.exception) + exception_fields = str(context.value) - self.assertTrue("field2" in exception_fields and "field3" in exception_fields) + assert "field2" in exception_fields and "field3" in exception_fields def test_validate_required_with_all_fields(self): required_fields = {"field1", "field2", "field3"} @@ -77,7 +77,4 @@ def test_validate_required_with_all_fields(self): try: validate_required(required_fields, params) except Exception as e: - self.fail(f"validate_required raised an unexpected exception: {e}") - - - + pytest.fail(f"validate_required raised an unexpected exception: {e}")