1
0
Fork 0
mirror of https://github.com/ansible-collections/hetzner.hcloud.git synced 2026-02-04 08:01:49 +00:00
hetzner.hcloud/tests/unit/module_utils/test_hcloud.py
Jonas L 94190ae6cb
refactor: move module vars out of __init__ (#336)
##### SUMMARY

- move module vars out of `__init__`
- add modules type hints
- catch hcloud exception for api calls
2023-09-26 09:41:01 +02:00

123 lines
4.5 KiB
Python

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",
"endpoint": "https://api.hetzner.cloud/v1",
}
hcloud = AnsibleHCloud(module, "hcloud_test")
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,
}
},
)