Debian Codegraph @

ansible_collections/community/aws/tests/unit/plugins/modules/test_directconnect_virtual_interface.py

raw
1 # (c) 2017 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 # Make coding more python3-ish
7 from __future__ import absolute_import
8 from __future__ import division
9 from __future__ import print_function
10
11 __metaclass__ = type
12
13 import pytest
14
15 from ansible_collections.amazon.aws.plugins.module_utils.botocore import HAS_BOTO3
16
17 from ansible_collections.community.aws.plugins.modules import directconnect_virtual_interface
18
19 if not HAS_BOTO3:
20 pytestmark = pytest.mark.skip(
21 "test_directconnect_confirm_connection.py requires the `boto3` and `botocore` modules"
22 )
23
24
25 class FailException(Exception):
26 pass
27
28
29 class FakeModule(object):
30 def __init__(self, **kwargs):
31 self.params = kwargs
32
33 def fail_json(self, *args, **kwargs):
34 self.exit_args = args
35 self.exit_kwargs = kwargs
36 raise FailException("FAIL")
37
38 def exit_json(self, *args, **kwargs):
39 self.exit_args = args
40 self.exit_kwargs = kwargs
41
42
43 def test_find_unique_vi_by_connection_id(placeboify, maybe_sleep):
44 client = placeboify.client("directconnect")
45 vi_id = directconnect_virtual_interface.find_unique_vi(client, "dxcon-aaaaaaaa", None, None)
46 assert vi_id == "dxvif-aaaaaaaa"
47
48
49 def test_find_unique_vi_by_vi_id(placeboify, maybe_sleep):
50 client = placeboify.client("directconnect")
51 vi_id = directconnect_virtual_interface.find_unique_vi(client, None, "dxvif-aaaaaaaaa", None)
52 assert vi_id == "dxvif-aaaaaaaa"
53
54
55 def test_find_unique_vi_by_name(placeboify, maybe_sleep):
56 client = placeboify.client("directconnect")
57 vi_id = directconnect_virtual_interface.find_unique_vi(client, None, None, "aaaaaaaa")
58 assert vi_id == "dxvif-aaaaaaaa"
59
60
61 def test_find_unique_vi_returns_multiple(placeboify, maybe_sleep):
62 client = placeboify.client("directconnect")
63 module = FakeModule(state="present", id_to_associate="dxcon-aaaaaaaa", public=False, name=None)
64 with pytest.raises(FailException):
65 directconnect_virtual_interface.ensure_state(client, module)
66 assert "Multiple virtual interfaces were found" in module.exit_kwargs["msg"]
67
68
69 def test_find_unique_vi_returns_missing_for_vi_id(placeboify, maybe_sleep):
70 client = placeboify.client("directconnect")
71 module = FakeModule(
72 state="present", id_to_associate=None, public=False, name=None, virtual_interface_id="dxvif-aaaaaaaa"
73 )
74 with pytest.raises(FailException):
75 directconnect_virtual_interface.ensure_state(client, module)
76 assert "The virtual interface dxvif-aaaaaaaa does not exist" in module.exit_kwargs["msg"]
77
78
79 def test_construct_public_vi():
80 module = FakeModule(
81 state="present",
82 id_to_associate=None,
83 public=True,
84 name="aaaaaaaa",
85 vlan=1,
86 bgp_asn=123,
87 authentication_key="aaaa",
88 customer_address="169.254.0.1/30",
89 amazon_address="169.254.0.2/30",
90 address_type="ipv4",
91 cidr=["10.88.0.0/30"],
92 virtual_gateway_id="xxxx",
93 direct_connect_gateway_id="yyyy",
94 )
95 vi = directconnect_virtual_interface.assemble_params_for_creating_vi(module.params)
96 assert vi == {
97 "virtualInterfaceName": "aaaaaaaa",
98 "vlan": 1,
99 "asn": 123,
100 "authKey": "aaaa",
101 "amazonAddress": "169.254.0.2/30",
102 "customerAddress": "169.254.0.1/30",
103 "addressFamily": "ipv4",
104 "routeFilterPrefixes": [{"cidr": "10.88.0.0/30"}],
105 }
106
107
108 def test_construct_private_vi_with_virtual_gateway_id():
109 module = FakeModule(
110 state="present",
111 id_to_associate=None,
112 public=False,
113 name="aaaaaaaa",
114 vlan=1,
115 bgp_asn=123,
116 authentication_key="aaaa",
117 customer_address="169.254.0.1/30",
118 amazon_address="169.254.0.2/30",
119 address_type="ipv4",
120 cidr=["10.88.0.0/30"],
121 virtual_gateway_id="xxxx",
122 direct_connect_gateway_id="yyyy",
123 )
124 vi = directconnect_virtual_interface.assemble_params_for_creating_vi(module.params)
125 assert vi == {
126 "virtualInterfaceName": "aaaaaaaa",
127 "vlan": 1,
128 "asn": 123,
129 "authKey": "aaaa",
130 "amazonAddress": "169.254.0.2/30",
131 "customerAddress": "169.254.0.1/30",
132 "addressFamily": "ipv4",
133 "virtualGatewayId": "xxxx",
134 }
135
136
137 def test_construct_private_vi_with_direct_connect_gateway_id():
138 module = FakeModule(
139 state="present",
140 id_to_associate=None,
141 public=False,
142 name="aaaaaaaa",
143 vlan=1,
144 bgp_asn=123,
145 authentication_key="aaaa",
146 customer_address="169.254.0.1/30",
147 amazon_address="169.254.0.2/30",
148 address_type="ipv4",
149 cidr=["10.88.0.0/30"],
150 virtual_gateway_id=None,
151 direct_connect_gateway_id="yyyy",
152 )
153 vi = directconnect_virtual_interface.assemble_params_for_creating_vi(module.params)
154 print(vi)
155 assert vi == {
156 "virtualInterfaceName": "aaaaaaaa",
157 "vlan": 1,
158 "asn": 123,
159 "authKey": "aaaa",
160 "amazonAddress": "169.254.0.2/30",
161 "customerAddress": "169.254.0.1/30",
162 "addressFamily": "ipv4",
163 "directConnectGatewayId": "yyyy",
164 }
165
166
167 def test_create_public_vi(placeboify, maybe_sleep):
168 client = placeboify.client("directconnect")
169 module = FakeModule(
170 state="present",
171 id_to_associate="dxcon-aaaaaaaa",
172 virtual_interface_id=None,
173 public=True,
174 name="aaaaaaaa",
175 vlan=1,
176 bgp_asn=123,
177 authentication_key="aaaa",
178 customer_address="169.254.0.1/30",
179 amazon_address="169.254.0.2/30",
180 address_type="ipv4",
181 cidr=["10.88.0.0/30"],
182 virtual_gateway_id="xxxx",
183 direct_connect_gateway_id="yyyy",
184 )
185 changed, latest_state = directconnect_virtual_interface.ensure_state(client, module)
186 assert changed is True
187 assert latest_state is not None
188
189
190 def test_create_private_vi(placeboify, maybe_sleep):
191 client = placeboify.client("directconnect")
192 module = FakeModule(
193 state="present",
194 id_to_associate="dxcon-aaaaaaaa",
195 virtual_interface_id=None,
196 public=False,
197 name="aaaaaaaa",
198 vlan=1,
199 bgp_asn=123,
200 authentication_key="aaaa",
201 customer_address="169.254.0.1/30",
202 amazon_address="169.254.0.2/30",
203 address_type="ipv4",
204 cidr=["10.88.0.0/30"],
205 virtual_gateway_id="xxxx",
206 direct_connect_gateway_id="yyyy",
207 )
208 changed, latest_state = directconnect_virtual_interface.ensure_state(client, module)
209 assert changed is True
210 assert latest_state is not None
211
212
213 def test_delete_vi(placeboify, maybe_sleep):
214 client = placeboify.client("directconnect")
215 module = FakeModule(
216 state="absent",
217 id_to_associate="dxcon-aaaaaaaa",
218 virtual_interface_id="dxvif-aaaaaaaa",
219 public=False,
220 name="aaaaaaaa",
221 vlan=1,
222 bgp_asn=123,
223 authentication_key="aaaa",
224 customer_address="169.254.0.1/30",
225 amazon_address="169.254.0.2/30",
226 address_type="ipv4",
227 cidr=["10.88.0.0/30"],
228 virtual_gateway_id=None,
229 direct_connect_gateway_id="yyyy",
230 )
231 changed, latest_state = directconnect_virtual_interface.ensure_state(client, module)
232 assert changed is True
233 assert latest_state == {}