| 1 |
# |
| 2 |
# (c) 2021 Red Hat Inc. |
| 3 |
# |
| 4 |
# This file is part of Ansible |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from unittest.mock import MagicMock |
| 8 |
from unittest.mock import call |
| 9 |
from unittest.mock import patch |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
from ansible_collections.amazon.aws.plugins.module_utils import elbv2 |
| 14 |
|
| 15 |
|
| 16 |
def createListener(**kwargs): |
| 17 |
result = { |
| 18 |
"Port": 80, |
| 19 |
"Protocol": "TCP", |
| 20 |
"DefaultActions": [ |
| 21 |
{ |
| 22 |
"Type": "fixed-response", |
| 23 |
"TargetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067", |
| 24 |
} |
| 25 |
], |
| 26 |
} |
| 27 |
if kwargs.get("port"): |
| 28 |
result["Port"] = kwargs.get("port") |
| 29 |
if kwargs.get("protocol"): |
| 30 |
result["Protocol"] = kwargs.get("protocol") |
| 31 |
if kwargs.get("certificate_arn") and kwargs.get("protocol") in ("TLS", "HTTPS"): |
| 32 |
result["Certificates"] = [{"CertificateArn": kwargs.get("certificate_arn")}] |
| 33 |
if kwargs.get("sslPolicy") and kwargs.get("protocol") in ("TLS", "HTTPS"): |
| 34 |
result["SslPolicy"] = kwargs.get("sslPolicy") |
| 35 |
if kwargs.get("alpnPolicy") and kwargs.get("protocol") == "TLS": |
| 36 |
result["AlpnPolicy"] = kwargs.get("alpnPolicy") |
| 37 |
if kwargs.get("default_actions"): |
| 38 |
result["DefaultActions"] = kwargs.get("default_actions") |
| 39 |
return result |
| 40 |
|
| 41 |
|
| 42 |
@pytest.mark.parametrize("current_protocol", ["TCP", "TLS", "UDP"]) |
| 43 |
@pytest.mark.parametrize( |
| 44 |
"current_alpn,new_alpn", |
| 45 |
[ |
| 46 |
(None, "None"), |
| 47 |
(None, "HTTP1Only"), |
| 48 |
("HTTP1Only", "HTTP2Only"), |
| 49 |
("HTTP1Only", "HTTP1Only"), |
| 50 |
], |
| 51 |
) |
| 52 |
def test__compare_listener_alpn_policy(current_protocol, current_alpn, new_alpn): |
| 53 |
current_listener = createListener(protocol=current_protocol, alpnPolicy=[current_alpn]) |
| 54 |
new_listener = createListener(protocol="TLS", alpnPolicy=[new_alpn]) |
| 55 |
result = {} |
| 56 |
if current_protocol != "TLS": |
| 57 |
result["Protocol"] = "TLS" |
| 58 |
if new_alpn and any((current_protocol != "TLS", not current_alpn, current_alpn and current_alpn != new_alpn)): |
| 59 |
result["AlpnPolicy"] = [new_alpn] |
| 60 |
|
| 61 |
assert result == elbv2._compare_listener(current_listener, new_listener) |
| 62 |
|
| 63 |
|
| 64 |
@pytest.mark.parametrize( |
| 65 |
"current_protocol,new_protocol", |
| 66 |
[ |
| 67 |
("TCP", "TCP"), |
| 68 |
("TLS", "HTTPS"), |
| 69 |
("HTTPS", "HTTPS"), |
| 70 |
("TLS", "TLS"), |
| 71 |
("HTTPS", "TLS"), |
| 72 |
("HTTPS", "TCP"), |
| 73 |
("TLS", "TCP"), |
| 74 |
], |
| 75 |
) |
| 76 |
@pytest.mark.parametrize( |
| 77 |
"current_ssl,new_ssl", |
| 78 |
[ |
| 79 |
(None, "ELBSecurityPolicy-TLS-1-0-2015-04"), |
| 80 |
("ELBSecurityPolicy-TLS13-1-2-Ext2-2021-06", "ELBSecurityPolicy-TLS-1-0-2015-04"), |
| 81 |
("ELBSecurityPolicy-TLS-1-0-2015-04", None), |
| 82 |
("ELBSecurityPolicy-TLS-1-0-2015-04", "ELBSecurityPolicy-TLS-1-0-2015-04"), |
| 83 |
], |
| 84 |
) |
| 85 |
def test__compare_listener_sslpolicy(current_protocol, new_protocol, current_ssl, new_ssl): |
| 86 |
current_listener = createListener(protocol=current_protocol, sslPolicy=current_ssl) |
| 87 |
|
| 88 |
new_listener = createListener(protocol=new_protocol, sslPolicy=new_ssl) |
| 89 |
|
| 90 |
expected = {} |
| 91 |
if new_protocol != current_protocol: |
| 92 |
expected["Protocol"] = new_protocol |
| 93 |
if new_protocol in ("HTTPS", "TLS") and new_ssl and new_ssl != current_ssl: |
| 94 |
expected["SslPolicy"] = new_ssl |
| 95 |
assert expected == elbv2._compare_listener(current_listener, new_listener) |
| 96 |
|
| 97 |
|
| 98 |
@pytest.mark.parametrize( |
| 99 |
"current_protocol,new_protocol", |
| 100 |
[ |
| 101 |
("TCP", "TCP"), |
| 102 |
("TLS", "HTTPS"), |
| 103 |
("HTTPS", "HTTPS"), |
| 104 |
("TLS", "TLS"), |
| 105 |
("HTTPS", "TLS"), |
| 106 |
("HTTPS", "TCP"), |
| 107 |
("TLS", "TCP"), |
| 108 |
], |
| 109 |
) |
| 110 |
@pytest.mark.parametrize( |
| 111 |
"current_certificate,new_certificate", |
| 112 |
[ |
| 113 |
(None, "arn:aws:iam::012345678901:server-certificate/ansible-test-1"), |
| 114 |
( |
| 115 |
"arn:aws:iam::012345678901:server-certificate/ansible-test-1", |
| 116 |
"arn:aws:iam::012345678901:server-certificate/ansible-test-2", |
| 117 |
), |
| 118 |
("arn:aws:iam::012345678901:server-certificate/ansible-test-1", None), |
| 119 |
( |
| 120 |
"arn:aws:iam::012345678901:server-certificate/ansible-test-1", |
| 121 |
"arn:aws:iam::012345678901:server-certificate/ansible-test-1", |
| 122 |
), |
| 123 |
], |
| 124 |
) |
| 125 |
def test__compare_listener_certificates(current_protocol, new_protocol, current_certificate, new_certificate): |
| 126 |
current_listener = createListener(protocol=current_protocol, certificate_arn=current_certificate) |
| 127 |
|
| 128 |
new_listener = createListener(protocol=new_protocol, certificate_arn=new_certificate) |
| 129 |
|
| 130 |
expected = {} |
| 131 |
if new_protocol != current_protocol: |
| 132 |
expected["Protocol"] = new_protocol |
| 133 |
if new_protocol in ("HTTPS", "TLS") and new_certificate and new_certificate != current_certificate: |
| 134 |
expected["Certificates"] = [{"CertificateArn": new_certificate}] |
| 135 |
assert expected == elbv2._compare_listener(current_listener, new_listener) |
| 136 |
|
| 137 |
|
| 138 |
@pytest.mark.parametrize( |
| 139 |
"current_actions,new_actions,expected_listener", |
| 140 |
[ |
| 141 |
# When Order field differs between current and new actions, modification is expected |
| 142 |
# because Order is now included in the comparison logic |
| 143 |
( |
| 144 |
[ |
| 145 |
{"TargetGroupArn": "ansible1", "Type": "a", "Order": 1}, |
| 146 |
{"TargetGroupArn": "ansible0", "Type": "b", "Order": 2}, |
| 147 |
{"TargetGroupArn": "ansible0", "Type": "a"}, |
| 148 |
], |
| 149 |
[ |
| 150 |
{"TargetGroupArn": "ansible1", "Type": "a"}, |
| 151 |
{"TargetGroupArn": "ansible0", "Type": "b"}, |
| 152 |
{"TargetGroupArn": "ansible0", "Type": "a"}, |
| 153 |
], |
| 154 |
{ |
| 155 |
"DefaultActions": [ |
| 156 |
{"TargetGroupArn": "ansible1", "Type": "a"}, |
| 157 |
{"TargetGroupArn": "ansible0", "Type": "b"}, |
| 158 |
{"TargetGroupArn": "ansible0", "Type": "a"}, |
| 159 |
] |
| 160 |
}, |
| 161 |
), |
| 162 |
( |
| 163 |
[ |
| 164 |
{"TargetGroupArn": "ansible0", "Type": "b", "Order": 1}, |
| 165 |
{"TargetGroupArn": "ansible1", "Type": "a", "Order": 2}, |
| 166 |
{"TargetGroupArn": "ansible0", "Type": "a"}, |
| 167 |
], |
| 168 |
[ |
| 169 |
{"TargetGroupArn": "ansible1", "Type": "a"}, |
| 170 |
{"TargetGroupArn": "ansible0", "Type": "b"}, |
| 171 |
{"TargetGroupArn": "ansible0", "Type": "a"}, |
| 172 |
], |
| 173 |
{ |
| 174 |
"DefaultActions": [ |
| 175 |
{"TargetGroupArn": "ansible1", "Type": "a"}, |
| 176 |
{"TargetGroupArn": "ansible0", "Type": "b"}, |
| 177 |
{"TargetGroupArn": "ansible0", "Type": "a"}, |
| 178 |
] |
| 179 |
}, |
| 180 |
), |
| 181 |
( |
| 182 |
[{"TargetGroupArn": "ansible1", "Type": "a", "Order": 1}], |
| 183 |
[{"TargetGroupArn": "ansible1", "Type": "a"}], |
| 184 |
{"DefaultActions": [{"TargetGroupArn": "ansible1", "Type": "a"}]}, |
| 185 |
), |
| 186 |
# When actions are identical (no Order difference), no modification expected |
| 187 |
( |
| 188 |
[{"TargetGroupArn": "ansible1", "Type": "a"}], |
| 189 |
[{"TargetGroupArn": "ansible1", "Type": "a"}], |
| 190 |
{}, |
| 191 |
), |
| 192 |
( |
| 193 |
[{"TargetGroupArn": "ansible1", "Type": "a", "Order": 1}], |
| 194 |
[{"TargetGroupArn": "ansible2", "Type": "a"}], |
| 195 |
{"DefaultActions": [{"TargetGroupArn": "ansible2", "Type": "a"}]}, |
| 196 |
), |
| 197 |
( |
| 198 |
[{"TargetGroupArn": "ansible1", "Type": "a"}, {"TargetGroupArn": "ansible2", "Type": "a"}], |
| 199 |
[{"TargetGroupArn": "ansible1", "Type": "a"}, {"TargetGroupArn": "ansible2", "Type": "b"}], |
| 200 |
{ |
| 201 |
"DefaultActions": [ |
| 202 |
{"TargetGroupArn": "ansible1", "Type": "a"}, |
| 203 |
{"TargetGroupArn": "ansible2", "Type": "b"}, |
| 204 |
] |
| 205 |
}, |
| 206 |
), |
| 207 |
], |
| 208 |
) |
| 209 |
def test__compare_listener_default_actions(current_actions, new_actions, expected_listener): |
| 210 |
current_listener = createListener(default_actions=current_actions) |
| 211 |
new_listener = createListener(default_actions=new_actions) |
| 212 |
assert expected_listener == elbv2._compare_listener(current_listener, new_listener) |
| 213 |
|
| 214 |
|
| 215 |
@pytest.mark.parametrize( |
| 216 |
"current_listeners,new_listeners,expected", |
| 217 |
[ |
| 218 |
( |
| 219 |
[ |
| 220 |
{ |
| 221 |
"Port": 80, |
| 222 |
"Protocol": "TCP", |
| 223 |
"ListenerArn": "arn80", |
| 224 |
"DefaultActions": [{"TargetGroupArn": "arn1", "Type": "forward", "Order": 1}], |
| 225 |
}, |
| 226 |
{ |
| 227 |
"Port": 90, |
| 228 |
"Protocol": "UDP", |
| 229 |
"ListenerArn": "arn90", |
| 230 |
"DefaultActions": [{"TargetGroupArn": "arn1", "Type": "forward", "Order": 1}], |
| 231 |
}, |
| 232 |
{ |
| 233 |
"Port": 100, |
| 234 |
"Protocol": "TLS", |
| 235 |
"ListenerArn": "arn100", |
| 236 |
"DefaultActions": [{"TargetGroupArn": "arn1", "Type": "forward", "Order": 1}], |
| 237 |
}, |
| 238 |
], |
| 239 |
[ |
| 240 |
{"Port": 80, "Protocol": "TCP", "DefaultActions": [{"TargetGroupArn": "arn1", "Type": "forward"}]}, |
| 241 |
{"Port": 90, "Protocol": "HTTPS", "DefaultActions": [{"TargetGroupArn": "arn1", "Type": "forward"}]}, |
| 242 |
{"Port": 101, "Protocol": "UDP", "DefaultActions": [{"TargetGroupArn": "arn1", "Type": "forward"}]}, |
| 243 |
], |
| 244 |
{ |
| 245 |
"add": [ |
| 246 |
{"Port": 101, "Protocol": "UDP", "DefaultActions": [{"TargetGroupArn": "arn1", "Type": "forward"}]} |
| 247 |
], |
| 248 |
# Port 80: DefaultActions Order changed (current has Order:1, new doesn't) |
| 249 |
# Port 90: Protocol changed (UDP->HTTPS) AND DefaultActions Order changed |
| 250 |
"modify": [ |
| 251 |
{ |
| 252 |
"DefaultActions": [{"TargetGroupArn": "arn1", "Type": "forward"}], |
| 253 |
"Port": 80, |
| 254 |
"ListenerArn": "arn80", |
| 255 |
}, |
| 256 |
{ |
| 257 |
"Protocol": "HTTPS", |
| 258 |
"DefaultActions": [{"TargetGroupArn": "arn1", "Type": "forward"}], |
| 259 |
"Port": 90, |
| 260 |
"ListenerArn": "arn90", |
| 261 |
}, |
| 262 |
], |
| 263 |
"delete": ["arn100"], |
| 264 |
}, |
| 265 |
) |
| 266 |
], |
| 267 |
) |
| 268 |
def test__group_listeners(current_listeners, new_listeners, expected): |
| 269 |
to_add, to_modify, to_delete = elbv2._group_listeners(current_listeners, new_listeners) |
| 270 |
assert to_add == expected.get("add", []) |
| 271 |
assert to_modify == expected.get("modify", []) |
| 272 |
assert to_delete == expected.get("delete", []) |
| 273 |
|
| 274 |
|
| 275 |
def test__prepare_listeners__no_listeners(): |
| 276 |
module = MagicMock() |
| 277 |
connection = MagicMock() |
| 278 |
assert elbv2._prepare_listeners(connection, module, None) == [] |
| 279 |
|
| 280 |
|
| 281 |
def test__prepare_listeners__scrub_none_parameters(): |
| 282 |
module = MagicMock() |
| 283 |
connection = MagicMock() |
| 284 |
listeners = [ |
| 285 |
{ |
| 286 |
"Port": 123, |
| 287 |
"Protocol": "TCP", |
| 288 |
"SslPolicy": None, |
| 289 |
"Rules": None, |
| 290 |
"DefaultActions": [{"TargetGroupArn": "arn1", "Type": "forward"}], |
| 291 |
} |
| 292 |
] |
| 293 |
assert elbv2._prepare_listeners(connection, module, listeners) == [ |
| 294 |
{"Port": 123, "Protocol": "TCP", "DefaultActions": [{"TargetGroupArn": "arn1", "Type": "forward"}]} |
| 295 |
] |
| 296 |
|
| 297 |
|
| 298 |
def test__prepare_listeners__alpn_policy(): |
| 299 |
module = MagicMock() |
| 300 |
connection = MagicMock() |
| 301 |
listeners = [ |
| 302 |
{"Port": 123, "AlpnPolicy": "MyPolicy1", "DefaultActions": [{"TargetGroupArn": "arn1", "Type": "forward"}]} |
| 303 |
] |
| 304 |
assert elbv2._prepare_listeners(connection, module, listeners) == [ |
| 305 |
{"Port": 123, "AlpnPolicy": ["MyPolicy1"], "DefaultActions": [{"TargetGroupArn": "arn1", "Type": "forward"}]} |
| 306 |
] |
| 307 |
|
| 308 |
|
| 309 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.convert_tg_name_to_arn") |
| 310 |
def test__prepare_listeners__target_group_name_unique_name(m_convert_tg_name_to_arn): |
| 311 |
module = MagicMock() |
| 312 |
connection = MagicMock() |
| 313 |
m_convert_tg_name_to_arn.return_value = MagicMock() |
| 314 |
target_group_name = MagicMock() |
| 315 |
listeners = [ |
| 316 |
{ |
| 317 |
"DefaultActions": [ |
| 318 |
{"TargetGroupName": target_group_name, "Type": "forward"}, |
| 319 |
{"TargetGroupName": target_group_name, "Type": "redirect"}, |
| 320 |
] |
| 321 |
} |
| 322 |
] |
| 323 |
assert elbv2._prepare_listeners(connection, module, listeners) == [ |
| 324 |
{ |
| 325 |
"DefaultActions": [ |
| 326 |
{"TargetGroupArn": m_convert_tg_name_to_arn.return_value, "Type": "forward"}, |
| 327 |
{"TargetGroupArn": m_convert_tg_name_to_arn.return_value, "Type": "redirect"}, |
| 328 |
] |
| 329 |
} |
| 330 |
] |
| 331 |
m_convert_tg_name_to_arn.assert_called_once_with(connection, module, target_group_name) |
| 332 |
|
| 333 |
|
| 334 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.convert_tg_name_to_arn") |
| 335 |
def test__prepare_listeners__target_group_name_multiple_name(m_convert_tg_name_to_arn): |
| 336 |
module = MagicMock() |
| 337 |
connection = MagicMock() |
| 338 |
tg_name1 = MagicMock() |
| 339 |
tg_name2 = MagicMock() |
| 340 |
arn_values = { |
| 341 |
tg_name1: MagicMock(), |
| 342 |
tg_name2: MagicMock(), |
| 343 |
} |
| 344 |
m_convert_tg_name_to_arn.side_effect = lambda conn, module, name: arn_values.get(name) |
| 345 |
|
| 346 |
listeners = [ |
| 347 |
{ |
| 348 |
"DefaultActions": [ |
| 349 |
{"TargetGroupName": tg_name1, "Type": "forward"}, |
| 350 |
{"TargetGroupName": tg_name2, "Type": "redirect"}, |
| 351 |
] |
| 352 |
} |
| 353 |
] |
| 354 |
assert elbv2._prepare_listeners(connection, module, listeners) == [ |
| 355 |
{ |
| 356 |
"DefaultActions": [ |
| 357 |
{"TargetGroupArn": arn_values.get(tg_name1), "Type": "forward"}, |
| 358 |
{"TargetGroupArn": arn_values.get(tg_name2), "Type": "redirect"}, |
| 359 |
] |
| 360 |
} |
| 361 |
] |
| 362 |
m_convert_tg_name_to_arn.assert_has_calls([call(connection, module, tg_name1), call(connection, module, tg_name2)]) |
| 363 |
|
| 364 |
|
| 365 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_listeners") |
| 366 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2._prepare_listeners") |
| 367 |
def testELBListenersInit( |
| 368 |
m__prepare_listeners, |
| 369 |
m_describe_listeners, |
| 370 |
): |
| 371 |
connection = MagicMock() |
| 372 |
module = MagicMock() |
| 373 |
purge_listeners = MagicMock() |
| 374 |
elb_arn = MagicMock() |
| 375 |
listeners = MagicMock() |
| 376 |
module.params = {"listeners": listeners, "purge_listeners": purge_listeners} |
| 377 |
|
| 378 |
m__prepare_listeners.return_value = MagicMock() |
| 379 |
m_describe_listeners.return_value = MagicMock() |
| 380 |
|
| 381 |
elb_listener = elbv2.ELBListeners(connection, module, elb_arn) |
| 382 |
|
| 383 |
m__prepare_listeners.assert_called_once_with(connection, module, listeners) |
| 384 |
m_describe_listeners.assert_called_once_with(connection, load_balancer_arn=elb_arn) |
| 385 |
|
| 386 |
assert elb_listener.listeners == m__prepare_listeners.return_value |
| 387 |
assert elb_listener.current_listeners == m_describe_listeners.return_value |
| 388 |
assert elb_listener.purge_listeners == purge_listeners |
| 389 |
assert elb_listener.elb_arn == elb_arn |
| 390 |
assert elb_listener.module == module |
| 391 |
assert elb_listener.changed is False |
| 392 |
|
| 393 |
|
| 394 |
class TestBuildApplicationLoadBalancerDescription: |
| 395 |
"""Tests for build_application_load_balancer_description function""" |
| 396 |
|
| 397 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.normalize_application_load_balancer") |
| 398 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_rules") |
| 399 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_listeners") |
| 400 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_load_balancer_attributes") |
| 401 |
def test_with_all_includes( |
| 402 |
self, |
| 403 |
m_describe_attributes, |
| 404 |
m_describe_listeners, |
| 405 |
m_describe_rules, |
| 406 |
m_normalize, |
| 407 |
): |
| 408 |
"""Test with all include flags set to True""" |
| 409 |
connection = MagicMock() |
| 410 |
load_balancer = { |
| 411 |
"LoadBalancerArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/test/123", |
| 412 |
"LoadBalancerName": "test-alb", |
| 413 |
} |
| 414 |
|
| 415 |
# Mock return values |
| 416 |
mock_attributes = [{"Key": "deletion_protection.enabled", "Value": "true"}] |
| 417 |
mock_listeners = [ |
| 418 |
{ |
| 419 |
"ListenerArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/app/test/123/abc", |
| 420 |
"Port": 80, |
| 421 |
} |
| 422 |
] |
| 423 |
mock_rules = [{"RuleArn": "arn:aws:...rule/1", "Priority": "1"}] |
| 424 |
mock_normalized = {"load_balancer_name": "test-alb"} |
| 425 |
|
| 426 |
m_describe_attributes.return_value = mock_attributes |
| 427 |
m_describe_listeners.return_value = mock_listeners |
| 428 |
m_describe_rules.return_value = mock_rules |
| 429 |
m_normalize.return_value = mock_normalized |
| 430 |
|
| 431 |
result = elbv2.build_application_load_balancer_description( |
| 432 |
connection, |
| 433 |
load_balancer, |
| 434 |
include_attributes=True, |
| 435 |
include_listeners=True, |
| 436 |
include_listener_rules=True, |
| 437 |
) |
| 438 |
|
| 439 |
# Verify all describe functions were called |
| 440 |
m_describe_attributes.assert_called_once_with(connection, load_balancer["LoadBalancerArn"]) |
| 441 |
m_describe_listeners.assert_called_once_with(connection, load_balancer_arn=load_balancer["LoadBalancerArn"]) |
| 442 |
m_describe_rules.assert_called_once_with(connection, ListenerArn=mock_listeners[0]["ListenerArn"]) |
| 443 |
|
| 444 |
# Verify normalize was called with the enriched ALB |
| 445 |
call_args = m_normalize.call_args[0][0] |
| 446 |
assert call_args["Attributes"] == mock_attributes |
| 447 |
assert call_args["Listeners"] == mock_listeners |
| 448 |
assert call_args["Listeners"][0]["Rules"] == mock_rules |
| 449 |
|
| 450 |
assert result == mock_normalized |
| 451 |
|
| 452 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.normalize_application_load_balancer") |
| 453 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_rules") |
| 454 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_listeners") |
| 455 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_load_balancer_attributes") |
| 456 |
def test_without_attributes( |
| 457 |
self, |
| 458 |
m_describe_attributes, |
| 459 |
m_describe_listeners, |
| 460 |
m_describe_rules, |
| 461 |
m_normalize, |
| 462 |
): |
| 463 |
"""Test with include_attributes=False""" |
| 464 |
connection = MagicMock() |
| 465 |
load_balancer = { |
| 466 |
"LoadBalancerArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/test/123", |
| 467 |
"LoadBalancerName": "test-alb", |
| 468 |
} |
| 469 |
|
| 470 |
mock_listeners = [{"ListenerArn": "arn:aws:...listener/1", "Port": 80}] |
| 471 |
mock_rules = [{"RuleArn": "arn:aws:...rule/1", "Priority": "1"}] |
| 472 |
mock_normalized = {"load_balancer_name": "test-alb"} |
| 473 |
|
| 474 |
m_describe_listeners.return_value = mock_listeners |
| 475 |
m_describe_rules.return_value = mock_rules |
| 476 |
m_normalize.return_value = mock_normalized |
| 477 |
|
| 478 |
result = elbv2.build_application_load_balancer_description( |
| 479 |
connection, |
| 480 |
load_balancer, |
| 481 |
include_attributes=False, |
| 482 |
include_listeners=True, |
| 483 |
include_listener_rules=True, |
| 484 |
) |
| 485 |
|
| 486 |
# Verify attributes were not fetched |
| 487 |
m_describe_attributes.assert_not_called() |
| 488 |
|
| 489 |
# Verify listeners and rules were fetched |
| 490 |
m_describe_listeners.assert_called_once() |
| 491 |
m_describe_rules.assert_called_once() |
| 492 |
|
| 493 |
# Verify normalize was called without Attributes |
| 494 |
call_args = m_normalize.call_args[0][0] |
| 495 |
assert "Attributes" not in call_args |
| 496 |
assert result == mock_normalized |
| 497 |
|
| 498 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.normalize_application_load_balancer") |
| 499 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_rules") |
| 500 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_listeners") |
| 501 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_load_balancer_attributes") |
| 502 |
def test_without_listeners( |
| 503 |
self, |
| 504 |
m_describe_attributes, |
| 505 |
m_describe_listeners, |
| 506 |
m_describe_rules, |
| 507 |
m_normalize, |
| 508 |
): |
| 509 |
"""Test with include_listeners=False and include_listener_rules=False""" |
| 510 |
connection = MagicMock() |
| 511 |
load_balancer = { |
| 512 |
"LoadBalancerArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/test/123", |
| 513 |
"LoadBalancerName": "test-alb", |
| 514 |
} |
| 515 |
|
| 516 |
mock_attributes = [{"Key": "deletion_protection.enabled", "Value": "true"}] |
| 517 |
mock_normalized = {"load_balancer_name": "test-alb"} |
| 518 |
|
| 519 |
m_describe_attributes.return_value = mock_attributes |
| 520 |
m_normalize.return_value = mock_normalized |
| 521 |
|
| 522 |
result = elbv2.build_application_load_balancer_description( |
| 523 |
connection, |
| 524 |
load_balancer, |
| 525 |
include_attributes=True, |
| 526 |
include_listeners=False, |
| 527 |
include_listener_rules=False, |
| 528 |
) |
| 529 |
|
| 530 |
# Verify listeners and rules were not fetched |
| 531 |
m_describe_listeners.assert_not_called() |
| 532 |
m_describe_rules.assert_not_called() |
| 533 |
|
| 534 |
# Verify attributes were fetched |
| 535 |
m_describe_attributes.assert_called_once() |
| 536 |
|
| 537 |
# Verify normalize was called without Listeners |
| 538 |
call_args = m_normalize.call_args[0][0] |
| 539 |
assert "Listeners" not in call_args |
| 540 |
assert result == mock_normalized |
| 541 |
|
| 542 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.normalize_application_load_balancer") |
| 543 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_rules") |
| 544 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_listeners") |
| 545 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_load_balancer_attributes") |
| 546 |
def test_with_listeners_but_without_rules( |
| 547 |
self, |
| 548 |
m_describe_attributes, |
| 549 |
m_describe_listeners, |
| 550 |
m_describe_rules, |
| 551 |
m_normalize, |
| 552 |
): |
| 553 |
"""Test with include_listeners=True but include_listener_rules=False""" |
| 554 |
connection = MagicMock() |
| 555 |
load_balancer = { |
| 556 |
"LoadBalancerArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/test/123", |
| 557 |
"LoadBalancerName": "test-alb", |
| 558 |
} |
| 559 |
|
| 560 |
mock_listeners = [{"ListenerArn": "arn:aws:...listener/1", "Port": 80}] |
| 561 |
mock_normalized = {"load_balancer_name": "test-alb"} |
| 562 |
|
| 563 |
m_describe_listeners.return_value = mock_listeners |
| 564 |
m_normalize.return_value = mock_normalized |
| 565 |
|
| 566 |
result = elbv2.build_application_load_balancer_description( |
| 567 |
connection, |
| 568 |
load_balancer, |
| 569 |
include_attributes=False, |
| 570 |
include_listeners=True, |
| 571 |
include_listener_rules=False, |
| 572 |
) |
| 573 |
|
| 574 |
# Verify listeners were fetched but rules were not |
| 575 |
m_describe_listeners.assert_called_once() |
| 576 |
m_describe_rules.assert_not_called() |
| 577 |
|
| 578 |
# Verify normalize was called with listeners but without rules |
| 579 |
call_args = m_normalize.call_args[0][0] |
| 580 |
assert call_args["Listeners"] == mock_listeners |
| 581 |
assert "Rules" not in call_args["Listeners"][0] |
| 582 |
assert result == mock_normalized |
| 583 |
|
| 584 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.normalize_application_load_balancer") |
| 585 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_rules") |
| 586 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_listeners") |
| 587 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_load_balancer_attributes") |
| 588 |
def test_with_multiple_listeners( |
| 589 |
self, |
| 590 |
m_describe_attributes, |
| 591 |
m_describe_listeners, |
| 592 |
m_describe_rules, |
| 593 |
m_normalize, |
| 594 |
): |
| 595 |
"""Test that rules are fetched for each listener""" |
| 596 |
connection = MagicMock() |
| 597 |
load_balancer = { |
| 598 |
"LoadBalancerArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/test/123", |
| 599 |
"LoadBalancerName": "test-alb", |
| 600 |
} |
| 601 |
|
| 602 |
mock_listeners = [ |
| 603 |
{"ListenerArn": "arn:aws:...listener/1", "Port": 80}, |
| 604 |
{"ListenerArn": "arn:aws:...listener/2", "Port": 443}, |
| 605 |
] |
| 606 |
mock_rules = [{"RuleArn": "arn:aws:...rule/1", "Priority": "1"}] |
| 607 |
mock_normalized = {"load_balancer_name": "test-alb"} |
| 608 |
|
| 609 |
m_describe_listeners.return_value = mock_listeners |
| 610 |
m_describe_rules.return_value = mock_rules |
| 611 |
m_normalize.return_value = mock_normalized |
| 612 |
|
| 613 |
result = elbv2.build_application_load_balancer_description( |
| 614 |
connection, |
| 615 |
load_balancer, |
| 616 |
include_attributes=False, |
| 617 |
include_listeners=True, |
| 618 |
include_listener_rules=True, |
| 619 |
) |
| 620 |
|
| 621 |
# Verify describe_rules was called for each listener |
| 622 |
assert m_describe_rules.call_count == 2 |
| 623 |
m_describe_rules.assert_any_call(connection, ListenerArn="arn:aws:...listener/1") |
| 624 |
m_describe_rules.assert_any_call(connection, ListenerArn="arn:aws:...listener/2") |
| 625 |
|
| 626 |
# Verify both listeners have rules attached |
| 627 |
call_args = m_normalize.call_args[0][0] |
| 628 |
assert call_args["Listeners"][0]["Rules"] == mock_rules |
| 629 |
assert call_args["Listeners"][1]["Rules"] == mock_rules |
| 630 |
assert result == mock_normalized |
| 631 |
|
| 632 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.normalize_application_load_balancer") |
| 633 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_rules") |
| 634 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_listeners") |
| 635 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_load_balancer_attributes") |
| 636 |
def test_include_listener_rules_implies_listeners( |
| 637 |
self, |
| 638 |
m_describe_attributes, |
| 639 |
m_describe_listeners, |
| 640 |
m_describe_rules, |
| 641 |
m_normalize, |
| 642 |
): |
| 643 |
"""Test that include_listener_rules=True implies fetching listeners even if include_listeners=False""" |
| 644 |
connection = MagicMock() |
| 645 |
load_balancer = { |
| 646 |
"LoadBalancerArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/test/123", |
| 647 |
"LoadBalancerName": "test-alb", |
| 648 |
} |
| 649 |
|
| 650 |
mock_listeners = [{"ListenerArn": "arn:aws:...listener/1", "Port": 80}] |
| 651 |
mock_rules = [{"RuleArn": "arn:aws:...rule/1", "Priority": "1"}] |
| 652 |
mock_normalized = {"load_balancer_name": "test-alb"} |
| 653 |
|
| 654 |
m_describe_listeners.return_value = mock_listeners |
| 655 |
m_describe_rules.return_value = mock_rules |
| 656 |
m_normalize.return_value = mock_normalized |
| 657 |
|
| 658 |
result = elbv2.build_application_load_balancer_description( |
| 659 |
connection, |
| 660 |
load_balancer, |
| 661 |
include_attributes=False, |
| 662 |
include_listeners=False, |
| 663 |
include_listener_rules=True, |
| 664 |
) |
| 665 |
|
| 666 |
# Verify listeners were still fetched because rules were requested |
| 667 |
m_describe_listeners.assert_called_once() |
| 668 |
m_describe_rules.assert_called_once() |
| 669 |
|
| 670 |
assert result == mock_normalized |
| 671 |
|
| 672 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.normalize_application_load_balancer") |
| 673 |
@patch("ansible_collections.amazon.aws.plugins.module_utils.elbv2.describe_load_balancer_attributes") |
| 674 |
def test_does_not_modify_original_load_balancer( |
| 675 |
self, |
| 676 |
m_describe_attributes, |
| 677 |
m_normalize, |
| 678 |
): |
| 679 |
"""Test that the original load_balancer dict is not modified""" |
| 680 |
connection = MagicMock() |
| 681 |
load_balancer = { |
| 682 |
"LoadBalancerArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/test/123", |
| 683 |
"LoadBalancerName": "test-alb", |
| 684 |
} |
| 685 |
original_load_balancer = load_balancer.copy() |
| 686 |
|
| 687 |
mock_attributes = [{"Key": "deletion_protection.enabled", "Value": "true"}] |
| 688 |
mock_normalized = {"load_balancer_name": "test-alb"} |
| 689 |
|
| 690 |
m_describe_attributes.return_value = mock_attributes |
| 691 |
m_normalize.return_value = mock_normalized |
| 692 |
|
| 693 |
elbv2.build_application_load_balancer_description( |
| 694 |
connection, |
| 695 |
load_balancer, |
| 696 |
include_attributes=True, |
| 697 |
include_listeners=False, |
| 698 |
include_listener_rules=False, |
| 699 |
) |
| 700 |
|
| 701 |
# Verify the original dict was not modified |
| 702 |
assert load_balancer == original_load_balancer |
| 703 |
assert "Attributes" not in load_balancer |