diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-05 16:18:41 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-05 16:18:41 +0000 |
commit | b643c52cf29ce5bbab738b43290af3556efa1ca9 (patch) | |
tree | 21d5c53d7a9b696627a255777cefdf6f78968824 /ansible_collections/hetzner/hcloud/tests/unit | |
parent | Releasing progress-linux version 9.5.1+dfsg-1~progress7.99u1. (diff) | |
download | ansible-b643c52cf29ce5bbab738b43290af3556efa1ca9.tar.xz ansible-b643c52cf29ce5bbab738b43290af3556efa1ca9.zip |
Merging upstream version 10.0.0+dfsg.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ansible_collections/hetzner/hcloud/tests/unit')
3 files changed, 117 insertions, 7 deletions
diff --git a/ansible_collections/hetzner/hcloud/tests/unit/conftest.py b/ansible_collections/hetzner/hcloud/tests/unit/conftest.py new file mode 100644 index 000000000..af891afc2 --- /dev/null +++ b/ansible_collections/hetzner/hcloud/tests/unit/conftest.py @@ -0,0 +1,15 @@ +from __future__ import annotations + +from unittest.mock import MagicMock + +import pytest + + +@pytest.fixture() +def module(): + obj = MagicMock() + obj.params = { + "api_token": "dummy", + "api_endpoint": "https://api.hetzner.cloud/v1", + } + return obj diff --git a/ansible_collections/hetzner/hcloud/tests/unit/inventory/test_hcloud.py b/ansible_collections/hetzner/hcloud/tests/unit/inventory/test_hcloud.py new file mode 100644 index 000000000..b5d3b777b --- /dev/null +++ b/ansible_collections/hetzner/hcloud/tests/unit/inventory/test_hcloud.py @@ -0,0 +1,75 @@ +from __future__ import annotations + +import json +from unittest.mock import MagicMock + +from plugins.inventory.hcloud import InventoryModule, first_ipv6_address +from plugins.module_utils.vendor.hcloud.servers import BoundServer + + +def test_first_ipv6_address(): + found = first_ipv6_address("2001:db8::/64") + assert isinstance(found, str) + assert found == "2001:db8::1" + + +def test_build_inventory_server(): + client = MagicMock() + inventory = InventoryModule() + inventory.get_option = MagicMock() + inventory.get_option.return_value = None + + server = BoundServer( + client, + { + "id": 45921624, + "name": "my-server", + "labels": {}, + "status": "running", + "public_net": { + "ipv4": { + "id": 56583278, + "ip": "127.0.0.1", + "blocked": False, + "dns_ptr": "static.1.0.0.127.clients.your-server.de", + }, + "ipv6": {"id": 56583279, "ip": "2001:db8::/64", "blocked": False, "dns_ptr": []}, + "floating_ips": [], + "firewalls": [], + }, + "private_net": [], + "server_type": {"id": 1, "name": "cx11", "architecture": "x86"}, + "datacenter": { + "id": 3, + "name": "hel1-dc2", + "location": {"id": 3, "name": "hel1"}, + }, + "image": {"id": 114690387, "name": "debian-12", "os_flavor": "debian", "os_version": "12"}, + }, + ) + # pylint: disable=protected-access + variables = inventory._build_inventory_server(server) + + # Ensure the host_vars are json serializable + json.dumps(variables) + + assert variables == { + "id": 45921624, + "name": "my-server", + "status": "running", + "type": "cx11", + "server_type": "cx11", + "architecture": "x86", + "location": "hel1", + "datacenter": "hel1-dc2", + "labels": {}, + "ipv4": "127.0.0.1", + "ipv6": "2001:db8::1", + "ipv6_network": "2001:db8::", + "ipv6_network_mask": "64", + "private_networks": [], + "image_id": 114690387, + "image_name": "debian-12", + "image_os_flavor": "debian", + "ansible_host": None, + } diff --git a/ansible_collections/hetzner/hcloud/tests/unit/module_utils/test_hcloud.py b/ansible_collections/hetzner/hcloud/tests/unit/module_utils/test_hcloud.py index c1a9ffb77..2f5e7509f 100644 --- a/ansible_collections/hetzner/hcloud/tests/unit/module_utils/test_hcloud.py +++ b/ansible_collections/hetzner/hcloud/tests/unit/module_utils/test_hcloud.py @@ -2,8 +2,8 @@ from __future__ import annotations import traceback from datetime import datetime, timezone -from unittest.mock import MagicMock +import pytest from ansible_collections.hetzner.hcloud.plugins.module_utils.hcloud import AnsibleHCloud from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud import ( APIException, @@ -16,12 +16,7 @@ from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud.actio ) -def test_hcloud_fail_json_hcloud(): - module = MagicMock() - module.params = { - "api_token": "fake_token", - "api_endpoint": "https://api.hetzner.cloud/v1", - } +def test_hcloud_fail_json_hcloud(module): AnsibleHCloud.represent = "hcloud_test" hcloud = AnsibleHCloud(module) @@ -123,3 +118,28 @@ def test_hcloud_fail_json_hcloud(): } }, ) + + +@pytest.mark.parametrize( + ("kwargs", "msg"), + [ + ({"required": ["key1"]}, None), + ({"required": ["missing"]}, "missing required arguments: missing"), + ({"required_one_of": [["key1", "missing"]]}, None), + ({"required_one_of": [["missing1", "missing2"]]}, "one of the following is required: missing1, missing2"), + ], +) +def test_hcloud_fail_on_invalid_params(module, kwargs, msg): + AnsibleHCloud.represent = "hcloud_test" + hcloud = AnsibleHCloud(module) + + module.params = { + "key1": "value", + "key2": "value", + } + + hcloud.fail_on_invalid_params(**kwargs) + if msg is None: + module.fail_json.assert_not_called() + else: + module.fail_json.assert_called_with(msg=msg) |