Skip to content

Commit

Permalink
Adding testing for LinodeBaseModule (#422)
Browse files Browse the repository at this point in the history
* implementation

* formatting

* fixing wording

* adding env fallback

* adding tests

* removing unittest

* fixing unit
  • Loading branch information
amisiorek-akamai authored Oct 6, 2023
1 parent 8437fc1 commit b5b735d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
9 changes: 9 additions & 0 deletions tests/unit/base.py
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
21 changes: 21 additions & 0 deletions tests/unit/module_utils/test_linode_common.py
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"
2 changes: 0 additions & 2 deletions tests/unit/module_utils/test_linode_common_info.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import unittest

import pytest

from ansible_collections.linode.cloud.plugins.module_utils.linode_common_info import InfoModule, InfoModuleResult, \
Expand Down
19 changes: 9 additions & 10 deletions tests/unit/module_utils/test_linode_database_shared.py
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)
21 changes: 9 additions & 12 deletions tests/unit/module_utils/test_linode_helper.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -7,7 +7,7 @@
)


class LinodeHelperTest(unittest.TestCase):
class TestLinodeHelper():

def test_dict_select_spec(self):
target = {
Expand All @@ -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 = {
Expand All @@ -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 = {
Expand All @@ -59,25 +59,22 @@ 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"}
params = {"field1": "value1", "field2": "value2", "field3": "value3"}
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}")

0 comments on commit b5b735d

Please sign in to comment.