| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright: (c) 2023, Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from ansible_collections.community.aws.plugins.modules.route53_wait import detect_task_results |
| 9 |
|
| 10 |
_SINGLE_RESULT_SUCCESS = { |
| 11 |
"changed": True, |
| 12 |
"diff": {}, |
| 13 |
"failed": False, |
| 14 |
"wait_id": None, |
| 15 |
} |
| 16 |
|
| 17 |
_SINGLE_RESULT_FAILED = { |
| 18 |
"changed": False, |
| 19 |
"failed": True, |
| 20 |
"msg": "value of type must be one of: A, AAAA, CAA, CNAME, MX, NS, PTR, SOA, SPF, SRV, TXT, got: bar", |
| 21 |
} |
| 22 |
|
| 23 |
_MULTI_RESULT_SUCCESS = { |
| 24 |
"ansible_loop_var": "item", |
| 25 |
"changed": True, |
| 26 |
"diff": {}, |
| 27 |
"failed": False, |
| 28 |
"invocation": { |
| 29 |
"module_args": { |
| 30 |
"access_key": "asdf", |
| 31 |
"alias": None, |
| 32 |
"alias_evaluate_target_health": False, |
| 33 |
"alias_hosted_zone_id": None, |
| 34 |
"aws_access_key": "asdf", |
| 35 |
"aws_ca_bundle": None, |
| 36 |
"aws_config": None, |
| 37 |
"aws_secret_key": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", |
| 38 |
"debug_botocore_endpoint_logs": False, |
| 39 |
"endpoint_url": None, |
| 40 |
"failover": None, |
| 41 |
"geo_location": None, |
| 42 |
"health_check": None, |
| 43 |
"hosted_zone_id": None, |
| 44 |
"identifier": None, |
| 45 |
"overwrite": True, |
| 46 |
"private_zone": False, |
| 47 |
"profile": None, |
| 48 |
"record": "foo.example.org", |
| 49 |
"region": None, |
| 50 |
"retry_interval": 500, |
| 51 |
"secret_key": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", |
| 52 |
"session_token": None, |
| 53 |
"state": "present", |
| 54 |
"ttl": 300, |
| 55 |
"type": "TXT", |
| 56 |
"validate_certs": True, |
| 57 |
"value": ["foo"], |
| 58 |
"vpc_id": None, |
| 59 |
"wait": False, |
| 60 |
"wait_timeout": 300, |
| 61 |
"weight": None, |
| 62 |
"zone": "example.org", |
| 63 |
}, |
| 64 |
}, |
| 65 |
"item": {"record": "foo.example.org", "value": "foo"}, |
| 66 |
"wait_id": None, |
| 67 |
} |
| 68 |
|
| 69 |
_MULTI_RESULT_FAILED = { |
| 70 |
"ansible_loop_var": "item", |
| 71 |
"changed": False, |
| 72 |
"failed": True, |
| 73 |
"invocation": { |
| 74 |
"module_args": { |
| 75 |
"access_key": "asdf", |
| 76 |
"alias": None, |
| 77 |
"alias_evaluate_target_health": False, |
| 78 |
"alias_hosted_zone_id": None, |
| 79 |
"aws_access_key": "asdf", |
| 80 |
"aws_ca_bundle": None, |
| 81 |
"aws_config": None, |
| 82 |
"aws_secret_key": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", |
| 83 |
"debug_botocore_endpoint_logs": False, |
| 84 |
"endpoint_url": None, |
| 85 |
"failover": None, |
| 86 |
"geo_location": None, |
| 87 |
"health_check": None, |
| 88 |
"hosted_zone_id": None, |
| 89 |
"identifier": None, |
| 90 |
"overwrite": True, |
| 91 |
"private_zone": False, |
| 92 |
"profile": None, |
| 93 |
"record": "foo.example.org", |
| 94 |
"region": None, |
| 95 |
"retry_interval": 500, |
| 96 |
"secret_key": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", |
| 97 |
"session_token": None, |
| 98 |
"state": "present", |
| 99 |
"ttl": 300, |
| 100 |
"type": "bar", |
| 101 |
"validate_certs": True, |
| 102 |
"value": ["foo"], |
| 103 |
"vpc_id": None, |
| 104 |
"wait": False, |
| 105 |
"wait_timeout": 300, |
| 106 |
"weight": None, |
| 107 |
"zone": "example.org", |
| 108 |
}, |
| 109 |
}, |
| 110 |
"item": {"record": "foo.example.org", "value": "foo"}, |
| 111 |
"msg": "value of type must be one of: A, AAAA, CAA, CNAME, MX, NS, PTR, SOA, SPF, SRV, TXT, got: bar", |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
DETECT_TASK_RESULTS_DATA = [ |
| 116 |
[ |
| 117 |
_SINGLE_RESULT_SUCCESS, |
| 118 |
[ |
| 119 |
( |
| 120 |
"", |
| 121 |
_SINGLE_RESULT_SUCCESS, |
| 122 |
), |
| 123 |
], |
| 124 |
], |
| 125 |
[ |
| 126 |
{ |
| 127 |
"changed": True, |
| 128 |
"msg": "All items completed", |
| 129 |
"results": [ |
| 130 |
_MULTI_RESULT_SUCCESS, |
| 131 |
], |
| 132 |
"skipped": False, |
| 133 |
}, |
| 134 |
[ |
| 135 |
( |
| 136 |
" for result #1", |
| 137 |
_MULTI_RESULT_SUCCESS, |
| 138 |
), |
| 139 |
], |
| 140 |
], |
| 141 |
[ |
| 142 |
_SINGLE_RESULT_FAILED, |
| 143 |
[ |
| 144 |
( |
| 145 |
"", |
| 146 |
_SINGLE_RESULT_FAILED, |
| 147 |
), |
| 148 |
], |
| 149 |
], |
| 150 |
[ |
| 151 |
{ |
| 152 |
"changed": False, |
| 153 |
"failed": True, |
| 154 |
"msg": "One or more items failed", |
| 155 |
"results": [ |
| 156 |
_MULTI_RESULT_FAILED, |
| 157 |
], |
| 158 |
"skipped": False, |
| 159 |
}, |
| 160 |
[ |
| 161 |
( |
| 162 |
" for result #1", |
| 163 |
_MULTI_RESULT_FAILED, |
| 164 |
), |
| 165 |
], |
| 166 |
], |
| 167 |
] |
| 168 |
|
| 169 |
|
| 170 |
@pytest.mark.parametrize( |
| 171 |
"input_value, expected", |
| 172 |
DETECT_TASK_RESULTS_DATA, |
| 173 |
) |
| 174 |
def test_detect_task_results(input_value, expected): |
| 175 |
assert list(detect_task_results(input_value)) == expected |
| 176 |
|
| 177 |
|
| 178 |
DETECT_TASK_RESULTS_FAIL_DATA = [ |
| 179 |
[ |
| 180 |
{}, |
| 181 |
"missing changed key", |
| 182 |
[], |
| 183 |
], |
| 184 |
[ |
| 185 |
{"changed": True}, |
| 186 |
"missing failed key", |
| 187 |
[], |
| 188 |
], |
| 189 |
[ |
| 190 |
{"results": None}, |
| 191 |
"missing changed key", |
| 192 |
[], |
| 193 |
], |
| 194 |
[ |
| 195 |
{"results": None, "changed": True, "msg": "foo"}, |
| 196 |
"missing skipped key", |
| 197 |
[], |
| 198 |
], |
| 199 |
[ |
| 200 |
{"results": None, "changed": True, "msg": "foo", "skipped": False}, |
| 201 |
"results is present, but not a list", |
| 202 |
[], |
| 203 |
], |
| 204 |
[ |
| 205 |
{"results": [None], "changed": True, "msg": "foo", "skipped": False}, |
| 206 |
"result 1 is not a dictionary", |
| 207 |
[], |
| 208 |
], |
| 209 |
[ |
| 210 |
{"results": [{}], "changed": True, "msg": "foo", "skipped": False}, |
| 211 |
"missing changed key for result 1", |
| 212 |
[], |
| 213 |
], |
| 214 |
[ |
| 215 |
{ |
| 216 |
"results": [{"changed": True, "failed": False, "ansible_loop_var": "item", "invocation": {}}, {}], |
| 217 |
"changed": True, |
| 218 |
"msg": "foo", |
| 219 |
"skipped": False, |
| 220 |
}, |
| 221 |
"missing changed key for result 2", |
| 222 |
[(" for result #1", {"changed": True, "failed": False, "ansible_loop_var": "item", "invocation": {}})], |
| 223 |
], |
| 224 |
] |
| 225 |
|
| 226 |
|
| 227 |
@pytest.mark.parametrize( |
| 228 |
"input_value, expected_exc, expected_result", |
| 229 |
DETECT_TASK_RESULTS_FAIL_DATA, |
| 230 |
) |
| 231 |
def test_detect_task_fail_results(input_value, expected_exc, expected_result): |
| 232 |
result = [] |
| 233 |
with pytest.raises(ValueError) as exc: |
| 234 |
for res in detect_task_results(input_value): |
| 235 |
result.append(res) |
| 236 |
|
| 237 |
print(exc.value.args[0]) |
| 238 |
assert expected_exc == exc.value.args[0] |
| 239 |
print(result) |
| 240 |
assert expected_result == result |