-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding testing for LinodeBaseModule (#422)
* implementation * formatting * fixing wording * adding env fallback * adding tests * removing unittest * fixing unit
- Loading branch information
1 parent
8437fc1
commit b5b735d
Showing
5 changed files
with
48 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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,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" |
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 |
---|---|---|
@@ -1,44 +1,43 @@ | ||
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 | ||
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) |
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