Repository navigation
Expand file tree
/
Copy pathtest_router.py
More file actions
244 lines (205 loc) · 7.18 KB
/
Copy pathtest_router.py
File metadata and controls
244 lines (205 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"""
Routers
=======
You can connect private networks to each other using routers, or connect
private networks to the internet through routers acting as internet gateways:
"""
import secrets
import pytest
from constants import PUBLIC_PING_TARGETS
from util import in_parallel, assert_reverse_pointer
def test_internet_gateway(
internet_gateway,
create_server,
create_jumphost,
private_network,
image,
):
""" Test to create an internet gateway. """
# Using a private network
subnet = private_network.add_subnet(
cidr="192.168.100.0/24",
gateway_address="192.168.100.1",
)
# Attach the internet_gateway to the private network
internet_gateway.add_interface(
network=private_network.uuid,
subnet=subnet.uuid,
address="192.168.100.1"
)
# Create jumpost
jumpost = create_jumphost(private_networks=[private_network])
# Create a server connected only to its own private network
private_server = create_server(
name="private_server",
image=image,
interfaces=[{"network": private_network.uuid}],
jump_host=jumpost,
)
# Ping a public IP: Verifies that the server cat access the internet
# Wait up to 30 seconds (wait * tries) for the gateway port to come UP
private_server.ping(PUBLIC_PING_TARGETS[4], tries=30, wait=1)
# Disable the internet gateway
internet_gateway.update(internet_gateway=False)
# Ping a public IP: Verifies that the server cannot access the internet
private_server.ping(
PUBLIC_PING_TARGETS[4],
tries=5,
wait=1,
expect_failure=True
)
# Re-enable the internet gateway
internet_gateway.update(internet_gateway=True)
# Ping a public IP: Verifies that the server can access the internet again
# Wait up to 30 seconds (wait * tries) for the gateway port to come UP
private_server.ping(PUBLIC_PING_TARGETS[4], tries=30, wait=1)
@pytest.mark.reverse_ptr
def test_reverse_ptr_record_of_router(internet_gateway):
# Verify initial reverse pointers were published
initial_public_addresses = internet_gateway.internet_gateway_addresses
assert_reverse_pointer(initial_public_addresses)
# Disable the internet gateway
internet_gateway.update(internet_gateway=False)
# Verify initial reverse pointers were reset
assert_reverse_pointer(initial_public_addresses)
# Rename the router to a FQDN, which gets published as a reverse pointer
secret = secrets.token_hex(4)
router_fqdn = f'router-at-{secret}.example.org'
internet_gateway.update(name=router_fqdn)
# Re-enable the internet gateway
internet_gateway.update(internet_gateway=True)
# Verify updated reverse pointers were published
internet_gateway.refresh()
updated_public_addresses = internet_gateway.internet_gateway_addresses
for address in updated_public_addresses:
assert router_fqdn == address["reverse_ptr"]
assert_reverse_pointer(updated_public_addresses)
def test_router_connected_private_networks(
router,
create_server,
create_jumphost,
create_private_network,
image,
):
""" Test to create a router between two private networks. """
# Create two private networks
private_network_a = create_private_network()
private_network_b = create_private_network()
subnet_a = private_network_a.add_subnet(
cidr="192.168.10.0/24",
gateway_address="192.168.10.1",
)
subnet_b = private_network_b.add_subnet(
cidr="192.168.11.0/24",
gateway_address="192.168.11.1",
)
# Attach the router to the private networks
router.add_interface(
network=private_network_a.uuid,
subnet=subnet_a.uuid,
address="192.168.10.1"
)
router.add_interface(
network=private_network_b.uuid,
subnet=subnet_b.uuid,
address="192.168.11.1"
)
# Create jumpost
jumphost = create_jumphost(private_networks=[
private_network_a,
private_network_b
])
# Create servers each connected only to its own private network
s1, s2 = in_parallel(create_server, instances=(
{
"name": "s1",
"image": image,
"interfaces": [{"network": private_network_a.uuid}],
"jump_host": jumphost,
},
{
"name": "s2",
"image": image,
"interfaces": [{"network": private_network_b.uuid}],
"jump_host": jumphost,
},
))
# Each server can ping the other over private IPv4
s1.ping(s2.ip("private", 4), tries=5, wait=1)
s2.ping(s1.ip("private", 4), tries=5, wait=1)
def test_peering_connected_private_networks(
create_router,
create_server,
create_jumphost,
create_private_network,
image,
):
""" Test to create two routers between three private networks. """
# Create a pair of routers
router_a = create_router(name='router-a')
router_b = create_router(name='router-b')
# Create three private networks
private_network_a = create_private_network()
private_network_b = create_private_network()
peering_network = create_private_network()
subnet_a = private_network_a.add_subnet(
cidr="10.10.10.0/24",
)
subnet_b = private_network_b.add_subnet(
cidr="10.10.20.0/24",
)
peering_subnet = peering_network.add_subnet(
cidr="10.10.0.0/24",
)
# Attach the routers to their respective private and peering subnets
router_a.add_interface(
network=private_network_a.uuid,
subnet=subnet_a.uuid,
address="10.10.10.1"
)
router_a.add_interface(
network=peering_network.uuid,
subnet=peering_subnet.uuid,
address="10.10.0.10"
)
router_b.add_interface(
network=private_network_b.uuid,
subnet=subnet_b.uuid,
address="10.10.20.1"
)
router_b.add_interface(
network=peering_network.uuid,
subnet=peering_subnet.uuid,
address="10.10.0.20"
)
# Add extra routes to correctly forward traffic
router_a.add_extra_route(
destination="10.10.20.0/24",
next_hop="10.10.0.20"
)
router_b.add_extra_route(
destination="10.10.10.0/24",
next_hop="10.10.0.10"
)
# Create servers each connected only to its own private network
server_a, server_b = in_parallel(create_server, instances=(
{
"name": "s1",
"image": image,
"interfaces": [{"network": "public"},
{"network": private_network_a.uuid}],
},
{
"name": "s2",
"image": image,
"interfaces": [{"network": "public"},
{"network": private_network_b.uuid}],
},
))
# The routes for the routers must set manually, since the public network
# already provides the default route
server_a.run("sudo ip route add 10.0.0.0/8 via 10.10.10.1")
server_b.run("sudo ip route add 10.0.0.0/8 via 10.10.20.1")
# Each server can ping the other over private IPv4
server_a.ping(server_b.ip("private", 4), tries=5, wait=1)
server_b.ping(server_a.ip("private", 4), tries=5, wait=1)