From 294d674544b0f2ac98ab0ea23f944e382087f502 Mon Sep 17 00:00:00 2001 From: Ania Misiorek Date: Wed, 27 Sep 2023 15:58:14 -0400 Subject: [PATCH 1/7] implementation --- plugins/module_utils/linode_common.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugins/module_utils/linode_common.py b/plugins/module_utils/linode_common.py index 851d80de..8bd03b15 100644 --- a/plugins/module_utils/linode_common.py +++ b/plugins/module_utils/linode_common.py @@ -80,6 +80,10 @@ "doc_hide": True, "fallback": (env_fallback, ["LINODE_UA_PREFIX"]), }, + "ca_path": { + "type": "str", + "description": "A path to a custom certificate authority that allows for alternate API testing.", + } } LINODE_TAG_ARGS = { @@ -241,6 +245,7 @@ def client(self) -> LinodeClient: api_token = self.module.params["api_token"] api_version = self.module.params["api_version"] api_url = self.module.params["api_url"] + ca_path = self.module.params["ca_path"] user_agent = COLLECTION_USER_AGENT @@ -256,6 +261,7 @@ def client(self) -> LinodeClient: retry_rate_limit_interval=RETRY_INTERVAL_SECONDS, retry_max=MAX_RETRIES, retry_statuses=RETRY_STATUSES, + ca_path=ca_path ) return self._client From 0ff16dc743c3675b24cb84469f209923937f5b32 Mon Sep 17 00:00:00 2001 From: Ania Misiorek Date: Wed, 27 Sep 2023 16:06:43 -0400 Subject: [PATCH 2/7] formatting --- plugins/module_utils/linode_common.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/module_utils/linode_common.py b/plugins/module_utils/linode_common.py index 8bd03b15..395762e3 100644 --- a/plugins/module_utils/linode_common.py +++ b/plugins/module_utils/linode_common.py @@ -83,7 +83,7 @@ "ca_path": { "type": "str", "description": "A path to a custom certificate authority that allows for alternate API testing.", - } + }, } LINODE_TAG_ARGS = { @@ -261,7 +261,7 @@ def client(self) -> LinodeClient: retry_rate_limit_interval=RETRY_INTERVAL_SECONDS, retry_max=MAX_RETRIES, retry_statuses=RETRY_STATUSES, - ca_path=ca_path + ca_path=ca_path, ) return self._client From dae6742e448e42015f72d921b41abde1a8df8044 Mon Sep 17 00:00:00 2001 From: Ania Misiorek Date: Wed, 27 Sep 2023 16:14:57 -0400 Subject: [PATCH 3/7] fixing wording --- plugins/module_utils/linode_common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/module_utils/linode_common.py b/plugins/module_utils/linode_common.py index 395762e3..1b445fe4 100644 --- a/plugins/module_utils/linode_common.py +++ b/plugins/module_utils/linode_common.py @@ -82,7 +82,7 @@ }, "ca_path": { "type": "str", - "description": "A path to a custom certificate authority that allows for alternate API testing.", + "description": "A path to a custom certificate authority for using alternate APIs.", }, } From 852c1bcfb9ba367388ce18600fb93fe9a0c6d3b4 Mon Sep 17 00:00:00 2001 From: Ania Misiorek Date: Thu, 28 Sep 2023 10:24:37 -0400 Subject: [PATCH 4/7] adding env fallback --- plugins/module_utils/linode_common.py | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/module_utils/linode_common.py b/plugins/module_utils/linode_common.py index 1b445fe4..b5466b3e 100644 --- a/plugins/module_utils/linode_common.py +++ b/plugins/module_utils/linode_common.py @@ -83,6 +83,7 @@ "ca_path": { "type": "str", "description": "A path to a custom certificate authority for using alternate APIs.", + "fallback": (env_fallback, ["LINODE_CA"]), }, } From 872de28db042053f29ef74de5d1dc1c1ea06311c Mon Sep 17 00:00:00 2001 From: Ania Misiorek Date: Tue, 3 Oct 2023 17:38:56 -0400 Subject: [PATCH 5/7] adding tests --- tests/unit/base.py | 9 ++++++++ tests/unit/module_utils/test_linode_common.py | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/unit/base.py create mode 100644 tests/unit/module_utils/test_linode_common.py 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..a6edd31b --- /dev/null +++ b/tests/unit/module_utils/test_linode_common.py @@ -0,0 +1,21 @@ +from types import SimpleNamespace + +from unittest import TestCase +import os + +from ansible_collections.linode.cloud.tests.unit.base import TestModuleBase + +class TestLinodeModuleBase(TestCase): + + def setUp(self, **kwargs): + mock_module = TestModuleBase() + mock_module.module = SimpleNamespace(params=kwargs) + + return mock_module + + def test_module_ca_path_override(self): + os.environ['LINODE_CA'] = "env_ca" + mock_module = self.setUp(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 From d36065e69bd266d790322d0adccb9608e4554a85 Mon Sep 17 00:00:00 2001 From: Ania Misiorek Date: Thu, 5 Oct 2023 09:57:46 -0400 Subject: [PATCH 6/7] removing unittest --- tests/unit/module_utils/test_linode_common.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/unit/module_utils/test_linode_common.py b/tests/unit/module_utils/test_linode_common.py index a6edd31b..ae70ec79 100644 --- a/tests/unit/module_utils/test_linode_common.py +++ b/tests/unit/module_utils/test_linode_common.py @@ -1,21 +1,21 @@ from types import SimpleNamespace - -from unittest import TestCase import os from ansible_collections.linode.cloud.tests.unit.base import TestModuleBase -class TestLinodeModuleBase(TestCase): - - def setUp(self, **kwargs): - mock_module = TestModuleBase() - mock_module.module = SimpleNamespace(params=kwargs) - - return mock_module +class TestLinodeModuleBase(): def test_module_ca_path_override(self): os.environ['LINODE_CA'] = "env_ca" - mock_module = self.setUp(api_token="testing", api_version=None, api_url="/", ua_prefix=None, ca_path="foobar") + + 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 From 6141705a1e6cdf8c1761aee3cc2146ca6b767e67 Mon Sep 17 00:00:00 2001 From: Ania Misiorek Date: Thu, 5 Oct 2023 10:43:00 -0400 Subject: [PATCH 7/7] fixing unit --- .../module_utils/test_linode_common_info.py | 2 -- .../test_linode_database_shared.py | 19 ++++++++--------- tests/unit/module_utils/test_linode_helper.py | 21 ++++++++----------- 3 files changed, 18 insertions(+), 24 deletions(-) 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}")