1
0
Fork 0
mirror of https://github.com/ansible-collections/hetzner.hcloud.git synced 2026-02-04 08:01:49 +00:00

chore: add fail_on_invalid_params helper (#470)

##### SUMMARY

Add a small helper to validate parameters while executing the module.

---------

Co-authored-by: Justin Jeffery <justin.jeffery@ipfabric.io>
This commit is contained in:
Jonas L 2024-03-11 18:03:26 +01:00 committed by GitHub
parent 46717e2d65
commit e781f48f15
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 69 additions and 7 deletions

View file

@ -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)