diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-18 05:52:35 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-18 05:52:35 +0000 |
commit | 7fec0b69a082aaeec72fee0612766aa42f6b1b4d (patch) | |
tree | efb569b86ca4da888717f5433e757145fa322e08 /ansible_collections/hetzner/hcloud/tests/unit | |
parent | Releasing progress-linux version 7.7.0+dfsg-3~progress7.99u1. (diff) | |
download | ansible-7fec0b69a082aaeec72fee0612766aa42f6b1b4d.tar.xz ansible-7fec0b69a082aaeec72fee0612766aa42f6b1b4d.zip |
Merging upstream version 9.4.0+dfsg.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ansible_collections/hetzner/hcloud/tests/unit')
-rw-r--r-- | ansible_collections/hetzner/hcloud/tests/unit/module_utils/test_hcloud.py | 125 | ||||
-rw-r--r-- | ansible_collections/hetzner/hcloud/tests/unit/requirements.txt | 2 |
2 files changed, 127 insertions, 0 deletions
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 new file mode 100644 index 000000000..c1a9ffb77 --- /dev/null +++ b/ansible_collections/hetzner/hcloud/tests/unit/module_utils/test_hcloud.py @@ -0,0 +1,125 @@ +from __future__ import annotations + +import traceback +from datetime import datetime, timezone +from unittest.mock import MagicMock + +from ansible_collections.hetzner.hcloud.plugins.module_utils.hcloud import AnsibleHCloud +from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud import ( + APIException, +) +from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud.actions import ( + Action, + ActionException, + ActionFailedException, + ActionTimeoutException, +) + + +def test_hcloud_fail_json_hcloud(): + module = MagicMock() + module.params = { + "api_token": "fake_token", + "api_endpoint": "https://api.hetzner.cloud/v1", + } + AnsibleHCloud.represent = "hcloud_test" + hcloud = AnsibleHCloud(module) + + try: + raise APIException( + code="invalid_input", + message="invalid input in fields 'server', 'home_location'", + details={ + "fields": [ + {"messages": ["either server or home_location must be provided"], "name": "server"}, + {"messages": ["either server or home_location must be provided"], "name": "home_location"}, + ] + }, + ) + except APIException as exception: + hcloud.fail_json_hcloud(exception) + + module.fail_json.assert_called_with( + msg="invalid input in fields 'server', 'home_location'", + exception=traceback.format_exc(), + failure={ + "message": "invalid input in fields 'server', 'home_location'", + "code": "invalid_input", + "details": { + "fields": [ + {"messages": ["either server or home_location must be provided"], "name": "server"}, + {"messages": ["either server or home_location must be provided"], "name": "home_location"}, + ] + }, + }, + ) + + try: + raise ActionFailedException( + action=Action( + **{ + "id": 1084730887, + "command": "change_server_type", + "status": "error", + "progress": 100, + "resources": [{"id": 34574042, "type": "server"}], + "error": {"code": "server_does_not_exist_anymore", "message": "Server does not exist anymore"}, + "started": "2023-07-06T14:52:42+00:00", + "finished": "2023-07-06T14:53:08+00:00", + } + ) + ) + except ActionException as exception: + hcloud.fail_json_hcloud(exception) + + module.fail_json.assert_called_with( + msg="The pending action failed: Server does not exist anymore", + exception=traceback.format_exc(), + failure={ + "action": { + "id": 1084730887, + "command": "change_server_type", + "status": "error", + "progress": 100, + "resources": [{"id": 34574042, "type": "server"}], + "error": {"code": "server_does_not_exist_anymore", "message": "Server does not exist anymore"}, + "started": datetime(2023, 7, 6, 14, 52, 42, tzinfo=timezone.utc), + "finished": datetime(2023, 7, 6, 14, 53, 8, tzinfo=timezone.utc), + } + }, + ) + + try: + raise ActionTimeoutException( + action=Action( + **{ + "id": 1084659545, + "command": "create_server", + "status": "running", + "progress": 50, + "started": "2023-07-06T13:58:38+00:00", + "finished": None, + "resources": [{"id": 34572291, "type": "server"}], + "error": None, + } + ) + ) + except ActionException as exception: + hcloud.fail_json_hcloud(exception) + + module.fail_json.assert_called_with( + msg="The pending action timed out", + exception=traceback.format_exc(), + failure={ + "action": { + "id": 1084659545, + "command": "create_server", + "status": "running", + "progress": 50, + "resources": [{"id": 34572291, "type": "server"}], + "error": None, + "started": datetime(2023, 7, 6, 13, 58, 38, tzinfo=timezone.utc), + "finished": None, + } + }, + ) diff --git a/ansible_collections/hetzner/hcloud/tests/unit/requirements.txt b/ansible_collections/hetzner/hcloud/tests/unit/requirements.txt new file mode 100644 index 000000000..bc314c3c2 --- /dev/null +++ b/ansible_collections/hetzner/hcloud/tests/unit/requirements.txt @@ -0,0 +1,2 @@ +python-dateutil +requests |