| 1 |
# (c) 2024 Red Hat Inc. |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from unittest.mock import ANY |
| 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.modules import ec2_vpc_net |
| 14 |
|
| 15 |
module_name = "ansible_collections.amazon.aws.plugins.modules.ec2_vpc_net" |
| 16 |
|
| 17 |
|
| 18 |
@pytest.fixture(name="ansible_module") |
| 19 |
def fixture_ansible_module(): |
| 20 |
module = MagicMock() |
| 21 |
module.check_mode = False |
| 22 |
module.params = {} |
| 23 |
module.fail_json.side_effect = SystemExit(1) |
| 24 |
module.fail_json_aws.side_effect = SystemExit(1) |
| 25 |
|
| 26 |
return module |
| 27 |
|
| 28 |
|
| 29 |
@patch(module_name + ".ansible_dict_to_boto3_filter_list") |
| 30 |
@patch(module_name + ".describe_vpcs") |
| 31 |
def test_get_vpc_id_no_id(m_describe_vpcs, m_ansible_dict_to_boto3_filter_list, ansible_module): |
| 32 |
connection = MagicMock() |
| 33 |
cidr_block = MagicMock() |
| 34 |
vpc_id = MagicMock() |
| 35 |
|
| 36 |
m_ansible_dict_to_boto3_filter_list.return_value = MagicMock() |
| 37 |
ansible_module.params.update({"cidr_block": cidr_block}) |
| 38 |
m_describe_vpcs.return_value = [{"VpcId": vpc_id}] |
| 39 |
|
| 40 |
assert ec2_vpc_net.get_vpc_id(connection, ansible_module) == vpc_id |
| 41 |
m_ansible_dict_to_boto3_filter_list.assert_called_once_with( |
| 42 |
{"tag:Name": ansible_module.params.get("name"), "cidr-block": cidr_block} |
| 43 |
) |
| 44 |
m_describe_vpcs.assert_called_once_with(connection, Filters=m_ansible_dict_to_boto3_filter_list.return_value) |
| 45 |
|
| 46 |
|
| 47 |
@pytest.mark.parametrize("multi_ok", [True, False]) |
| 48 |
@patch(module_name + ".ansible_dict_to_boto3_filter_list") |
| 49 |
@patch(module_name + ".describe_vpcs") |
| 50 |
def test_get_vpc_id_multiple(m_describe_vpcs, m_ansible_dict_to_boto3_filter_list, ansible_module, multi_ok): |
| 51 |
connection = MagicMock() |
| 52 |
cidr_block = MagicMock() |
| 53 |
vpc_id = MagicMock() |
| 54 |
another_vpc_id = MagicMock() |
| 55 |
|
| 56 |
m_ansible_dict_to_boto3_filter_list.return_value = MagicMock() |
| 57 |
ansible_module.params.update({"cidr_block": cidr_block, "multi_ok": multi_ok}) |
| 58 |
m_describe_vpcs.return_value = [{"VpcId": vpc_id}, {"VpcId": another_vpc_id}] |
| 59 |
|
| 60 |
if multi_ok: |
| 61 |
assert not ec2_vpc_net.get_vpc_id(connection, ansible_module) |
| 62 |
else: |
| 63 |
with pytest.raises(SystemExit): |
| 64 |
ec2_vpc_net.get_vpc_id(connection, ansible_module) |
| 65 |
|
| 66 |
m_ansible_dict_to_boto3_filter_list.assert_called_once_with( |
| 67 |
{"tag:Name": ansible_module.params.get("name"), "cidr-block": cidr_block} |
| 68 |
) |
| 69 |
m_describe_vpcs.assert_called_once_with(connection, Filters=m_ansible_dict_to_boto3_filter_list.return_value) |
| 70 |
|
| 71 |
|
| 72 |
@patch(module_name + ".ansible_dict_to_boto3_filter_list") |
| 73 |
@patch(module_name + ".describe_vpcs") |
| 74 |
def test_get_vpc_id_cidr_block_list(m_describe_vpcs, m_ansible_dict_to_boto3_filter_list, ansible_module): |
| 75 |
connection = MagicMock() |
| 76 |
cidr_block = [MagicMock(), MagicMock()] |
| 77 |
vpc_id = MagicMock() |
| 78 |
vpc_filters = [MagicMock(), MagicMock()] |
| 79 |
|
| 80 |
m_ansible_dict_to_boto3_filter_list.side_effect = vpc_filters |
| 81 |
ansible_module.params.update({"cidr_block": cidr_block}) |
| 82 |
m_describe_vpcs.side_effect = [[], [{"VpcId": vpc_id}]] |
| 83 |
|
| 84 |
assert ec2_vpc_net.get_vpc_id(connection, ansible_module) == vpc_id |
| 85 |
m_ansible_dict_to_boto3_filter_list.assert_has_calls( |
| 86 |
[ |
| 87 |
call({"tag:Name": ansible_module.params.get("name"), "cidr-block": cidr_block}), |
| 88 |
call({"tag:Name": ansible_module.params.get("name"), "cidr-block": [cidr_block[0]]}), |
| 89 |
] |
| 90 |
) |
| 91 |
m_describe_vpcs.assert_has_calls( |
| 92 |
[ |
| 93 |
call(connection, Filters=vpc_filters[0]), |
| 94 |
call(connection, Filters=vpc_filters[1]), |
| 95 |
] |
| 96 |
) |
| 97 |
|
| 98 |
|
| 99 |
@pytest.mark.parametrize("cidr_block", [None, []]) |
| 100 |
@patch(module_name + ".disassociate_vpc_cidr_block") |
| 101 |
@patch(module_name + ".associate_vpc_cidr_block") |
| 102 |
def test_update_cidrs_no_cidr_block( |
| 103 |
m_associate_vpc_cidr_block, m_disassociate_vpc_cidr_block, ansible_module, cidr_block |
| 104 |
): |
| 105 |
connection = MagicMock() |
| 106 |
vpc_obj = {} |
| 107 |
changed, desired_cidrs = ec2_vpc_net.update_cidrs(connection, ansible_module, vpc_obj, cidr_block, ANY) |
| 108 |
assert not changed |
| 109 |
assert desired_cidrs is None |
| 110 |
|
| 111 |
m_associate_vpc_cidr_block.assert_not_called() |
| 112 |
m_disassociate_vpc_cidr_block.assert_not_called() |
| 113 |
|
| 114 |
|
| 115 |
@pytest.mark.parametrize("check_mode", [True, False]) |
| 116 |
@pytest.mark.parametrize( |
| 117 |
"associated_cidrs, cidr_block, purge_cidrs, add, remove, expected", |
| 118 |
[ |
| 119 |
( |
| 120 |
[ |
| 121 |
{ |
| 122 |
"AssociationId": "vpc-cidr-assoc-001", |
| 123 |
"CidrBlock": "10.1.0.0/24", |
| 124 |
"CidrBlockState": {"State": "associated"}, |
| 125 |
}, |
| 126 |
], |
| 127 |
["10.1.0.0/24"], |
| 128 |
True, |
| 129 |
[], |
| 130 |
[], |
| 131 |
(False, None), |
| 132 |
), |
| 133 |
( |
| 134 |
[ |
| 135 |
{ |
| 136 |
"AssociationId": "vpc-cidr-assoc-001", |
| 137 |
"CidrBlock": "10.1.0.0/24", |
| 138 |
"CidrBlockState": {"State": "associated"}, |
| 139 |
}, |
| 140 |
], |
| 141 |
["10.1.0.0/24"], |
| 142 |
False, |
| 143 |
[], |
| 144 |
[], |
| 145 |
(False, None), |
| 146 |
), |
| 147 |
( |
| 148 |
[ |
| 149 |
{ |
| 150 |
"AssociationId": "vpc-cidr-assoc-001", |
| 151 |
"CidrBlock": "10.1.0.0/24", |
| 152 |
"CidrBlockState": {"State": "associated"}, |
| 153 |
}, |
| 154 |
{ |
| 155 |
"AssociationId": "vpc-cidr-assoc-001", |
| 156 |
"CidrBlock": "10.2.0.0/24", |
| 157 |
"CidrBlockState": {"State": "failed"}, |
| 158 |
}, |
| 159 |
], |
| 160 |
["10.1.0.0/24"], |
| 161 |
True, |
| 162 |
[], |
| 163 |
[], |
| 164 |
(True, ["10.1.0.0/24"]), |
| 165 |
), |
| 166 |
( |
| 167 |
[ |
| 168 |
{ |
| 169 |
"AssociationId": "vpc-cidr-assoc-001", |
| 170 |
"CidrBlock": "10.1.0.0/24", |
| 171 |
"CidrBlockState": {"State": "associated"}, |
| 172 |
}, |
| 173 |
{ |
| 174 |
"AssociationId": "vpc-cidr-assoc-001", |
| 175 |
"CidrBlock": "10.2.0.0/24", |
| 176 |
"CidrBlockState": {"State": "failed"}, |
| 177 |
}, |
| 178 |
], |
| 179 |
["10.1.0.0/24"], |
| 180 |
False, |
| 181 |
[], |
| 182 |
[], |
| 183 |
(False, None), |
| 184 |
), |
| 185 |
( |
| 186 |
[ |
| 187 |
{ |
| 188 |
"AssociationId": "vpc-cidr-assoc-001", |
| 189 |
"CidrBlock": "10.1.0.0/24", |
| 190 |
"CidrBlockState": {"State": "associated"}, |
| 191 |
}, |
| 192 |
{ |
| 193 |
"AssociationId": "vpc-cidr-assoc-001", |
| 194 |
"CidrBlock": "10.2.0.0/24", |
| 195 |
"CidrBlockState": {"State": "disassociating"}, |
| 196 |
}, |
| 197 |
], |
| 198 |
["10.2.0.0/24"], |
| 199 |
False, |
| 200 |
["10.2.0.0/24"], |
| 201 |
[], |
| 202 |
(True, ["10.1.0.0/24", "10.2.0.0/24"]), |
| 203 |
), |
| 204 |
( |
| 205 |
[ |
| 206 |
{ |
| 207 |
"AssociationId": "vpc-cidr-assoc-001", |
| 208 |
"CidrBlock": "10.1.0.0/24", |
| 209 |
"CidrBlockState": {"State": "associated"}, |
| 210 |
}, |
| 211 |
{ |
| 212 |
"AssociationId": "vpc-cidr-assoc-001", |
| 213 |
"CidrBlock": "10.2.0.0/24", |
| 214 |
"CidrBlockState": {"State": "disassociating"}, |
| 215 |
}, |
| 216 |
], |
| 217 |
["10.2.0.0/24"], |
| 218 |
True, |
| 219 |
["10.2.0.0/24"], |
| 220 |
["vpc-cidr-assoc-001"], |
| 221 |
(True, ["10.2.0.0/24"]), |
| 222 |
), |
| 223 |
], |
| 224 |
) |
| 225 |
@patch(module_name + ".disassociate_vpc_cidr_block") |
| 226 |
@patch(module_name + ".associate_vpc_cidr_block") |
| 227 |
def test_update_cidrs_no_purge( |
| 228 |
m_associate_vpc_cidr_block, |
| 229 |
m_disassociate_vpc_cidr_block, |
| 230 |
ansible_module, |
| 231 |
check_mode, |
| 232 |
associated_cidrs, |
| 233 |
cidr_block, |
| 234 |
purge_cidrs, |
| 235 |
add, |
| 236 |
remove, |
| 237 |
expected, |
| 238 |
): |
| 239 |
connection = MagicMock() |
| 240 |
vpc_obj = {"VpcId": MagicMock(), "CidrBlockAssociationSet": associated_cidrs} |
| 241 |
ansible_module.check_mode = check_mode |
| 242 |
changed, desired_cidrs = ec2_vpc_net.update_cidrs(connection, ansible_module, vpc_obj, cidr_block, purge_cidrs) |
| 243 |
assert expected[0] == changed |
| 244 |
|
| 245 |
def sorted_list(x): |
| 246 |
return x if not x else sorted(x) |
| 247 |
|
| 248 |
assert sorted_list(expected[1]) == sorted_list(desired_cidrs) |
| 249 |
|
| 250 |
if not expected[0] or check_mode: |
| 251 |
m_associate_vpc_cidr_block.assert_not_called() |
| 252 |
m_disassociate_vpc_cidr_block.assert_not_called() |
| 253 |
else: |
| 254 |
if add: |
| 255 |
m_associate_vpc_cidr_block.assert_has_calls( |
| 256 |
[call(connection, vpc_id=vpc_obj["VpcId"], CidrBlock=cidr) for cidr in add], any_order=True |
| 257 |
) |
| 258 |
else: |
| 259 |
m_associate_vpc_cidr_block.assert_not_called() |
| 260 |
|
| 261 |
if remove: |
| 262 |
m_disassociate_vpc_cidr_block.assert_has_calls( |
| 263 |
[call(connection, association_id) for association_id in remove], any_order=False |
| 264 |
) |