-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathlinode_interface_test.py
More file actions
332 lines (262 loc) · 9.85 KB
/
Copy pathlinode_interface_test.py
File metadata and controls
332 lines (262 loc) · 9.85 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
from datetime import datetime
from test.unit.base import ClientBaseCase
from linode_api4 import (
LinodeInterface,
LinodeInterfaceDefaultRouteOptions,
LinodeInterfaceOptions,
LinodeInterfacePublicIPv4AddressOptions,
LinodeInterfacePublicIPv4Options,
LinodeInterfacePublicIPv6Options,
LinodeInterfacePublicIPv6RangeOptions,
LinodeInterfacePublicOptions,
LinodeInterfaceVLANOptions,
LinodeInterfaceVPCIPv4AddressOptions,
LinodeInterfaceVPCIPv4Options,
LinodeInterfaceVPCIPv4RangeOptions,
LinodeInterfaceVPCIPv6SLAACOptions,
LinodeInterfaceVPCOptions,
)
def build_interface_options_public():
return LinodeInterfaceOptions(
firewall_id=123,
default_route=LinodeInterfaceDefaultRouteOptions(
ipv4=True,
ipv6=True,
),
public=LinodeInterfacePublicOptions(
ipv4=LinodeInterfacePublicIPv4Options(
addresses=[
LinodeInterfacePublicIPv4AddressOptions(
address="172.30.0.50", primary=True
)
],
),
ipv6=LinodeInterfacePublicIPv6Options(
ranges=[
LinodeInterfacePublicIPv6RangeOptions(
range="2600:3c09:e001:59::/64"
)
]
),
),
)
def build_interface_options_vpc():
return LinodeInterfaceOptions(
firewall_id=123,
default_route=LinodeInterfaceDefaultRouteOptions(
ipv4=True,
),
vpc=LinodeInterfaceVPCOptions(
subnet_id=123,
ipv4=LinodeInterfaceVPCIPv4Options(
addresses=[
LinodeInterfaceVPCIPv4AddressOptions(
address="192.168.22.3",
primary=True,
nat_1_1_address="any",
)
],
ranges=[
LinodeInterfaceVPCIPv4RangeOptions(range="192.168.22.16/28")
],
),
),
)
def build_interface_options_vlan():
return LinodeInterfaceOptions(
vlan=LinodeInterfaceVLANOptions(
vlan_label="my_vlan", ipam_address="10.0.0.1/24"
),
)
class LinodeInterfaceTest(ClientBaseCase):
"""
Tests methods of the LinodeInterface class
"""
@staticmethod
def assert_linode_124_interface_123(iface: LinodeInterface):
assert iface.id == 123
assert isinstance(iface.created, datetime)
assert isinstance(iface.updated, datetime)
assert iface.default_route.ipv4
assert iface.default_route.ipv6
assert iface.mac_address == "22:00:AB:CD:EF:01"
assert iface.version == 1
assert iface.vlan is None
assert iface.vpc is None
# public.ipv4 assertions
assert iface.public.ipv4.addresses[0].address == "172.30.0.50"
assert iface.public.ipv4.addresses[0].primary
assert iface.public.ipv4.shared[0].address == "172.30.0.51"
assert iface.public.ipv4.shared[0].linode_id == 125
# public.ipv6 assertions
assert iface.public.ipv6.ranges[0].range == "2600:3c09:e001:59::/64"
assert (
iface.public.ipv6.ranges[0].route_target
== "2600:3c09::ff:feab:cdef"
)
assert iface.public.ipv6.ranges[1].range == "2600:3c09:e001:5a::/64"
assert (
iface.public.ipv6.ranges[1].route_target
== "2600:3c09::ff:feab:cdef"
)
assert iface.public.ipv6.shared[0].range == "2600:3c09:e001:2a::/64"
assert iface.public.ipv6.shared[0].route_target is None
assert iface.public.ipv6.slaac[0].address == "2600:3c09::ff:feab:cdef"
assert iface.public.ipv6.slaac[0].prefix == 64
@staticmethod
def assert_linode_124_interface_456(iface: LinodeInterface):
assert iface.id == 456
assert isinstance(iface.created, datetime)
assert isinstance(iface.updated, datetime)
assert iface.default_route.ipv4
assert not iface.default_route.ipv6
assert iface.mac_address == "22:00:AB:CD:EF:01"
assert iface.version == 1
assert iface.vlan is None
assert iface.public is None
# vpc assertions
assert iface.vpc.vpc_id == 123456
assert iface.vpc.subnet_id == 789
assert iface.vpc.ipv4.addresses[0].address == "192.168.22.3"
assert iface.vpc.ipv4.addresses[0].primary
assert iface.vpc.ipv4.ranges[0].range == "192.168.22.16/28"
assert iface.vpc.ipv4.ranges[1].range == "192.168.22.32/28"
assert iface.vpc.ipv6.is_public
assert iface.vpc.ipv6.slaac[0].range == "1234::/64"
assert iface.vpc.ipv6.slaac[0].address == "1234::5678"
assert iface.vpc.ipv6.ranges[0].range == "4321::/64"
@staticmethod
def assert_linode_124_interface_789(iface: LinodeInterface):
assert iface.id == 789
assert isinstance(iface.created, datetime)
assert isinstance(iface.updated, datetime)
assert iface.default_route.ipv4 is None
assert iface.default_route.ipv6 is None
assert iface.mac_address == "22:00:AB:CD:EF:01"
assert iface.version == 1
assert iface.public is None
assert iface.vpc is None
# vlan assertions
assert iface.vlan.vlan_label == "my_vlan"
assert iface.vlan.ipam_address == "10.0.0.1/24"
def test_get_public(self):
iface = LinodeInterface(self.client, 123, 124)
self.assert_linode_124_interface_123(iface)
iface.invalidate()
self.assert_linode_124_interface_123(iface)
def test_get_vpc(self):
iface = LinodeInterface(self.client, 456, 124)
self.assert_linode_124_interface_456(iface)
iface.invalidate()
self.assert_linode_124_interface_456(iface)
def test_get_vlan(self):
iface = LinodeInterface(self.client, 789, 124)
self.assert_linode_124_interface_789(iface)
iface.invalidate()
self.assert_linode_124_interface_789(iface)
def test_update_public(self):
iface = LinodeInterface(self.client, 123, 124)
self.assert_linode_124_interface_123(iface)
iface.default_route.ipv4 = False
iface.default_route.ipv6 = False
iface.public.ipv4.addresses = [
LinodeInterfacePublicIPv4AddressOptions(
address="172.30.0.51",
primary=False,
)
]
iface.public.ipv6.ranges = [
LinodeInterfacePublicIPv6RangeOptions(
range="2600:3c09:e001:58::/64"
)
]
with self.mock_put("/linode/instances/124/interfaces/123") as m:
iface.save()
assert m.called
assert m.call_data == {
"default_route": {
"ipv4": False,
"ipv6": False,
},
"public": {
"ipv4": {
"addresses": [
{
"address": "172.30.0.51",
"primary": False,
},
]
},
"ipv6": {
"ranges": [
{
"range": "2600:3c09:e001:58::/64",
}
]
},
},
}
def test_update_vpc(self):
iface = LinodeInterface(self.client, 456, 124)
self.assert_linode_124_interface_456(iface)
iface.default_route.ipv4 = False
iface.vpc.subnet_id = 456
iface.vpc.ipv4.addresses = [
LinodeInterfaceVPCIPv4AddressOptions(
address="192.168.22.4", primary=False, nat_1_1_address="auto"
)
]
iface.vpc.ipv4.ranges = [
LinodeInterfaceVPCIPv4RangeOptions(
range="192.168.22.17/28",
)
]
iface.vpc.ipv6.is_public = False
iface.vpc.ipv6.slaac = [
LinodeInterfaceVPCIPv6SLAACOptions(
range="1233::/64",
)
]
iface.vpc.ipv6.ranges = [
LinodeInterfacePublicIPv6RangeOptions(range="9876::/64")
]
with self.mock_put("/linode/instances/124/interfaces/456") as m:
iface.save()
assert m.called
assert m.call_data == {
"default_route": {
"ipv4": False,
},
"vpc": {
"subnet_id": 456,
"ipv4": {
"addresses": [
{
"address": "192.168.22.4",
"primary": False,
"nat_1_1_address": "auto",
},
],
"ranges": [{"range": "192.168.22.17/28"}],
},
"ipv6": {
"is_public": False,
"slaac": [{"range": "1233::/64"}],
"ranges": [{"range": "9876::/64"}],
},
},
}
def test_delete(self):
iface = LinodeInterface(self.client, 123, 124)
with self.mock_delete() as m:
iface.delete()
assert m.called
def test_firewalls(self):
iface = LinodeInterface(self.client, 123, 124)
firewalls = iface.firewalls()
assert len(firewalls) == 1
assert firewalls[0].id == 123
# Check a few fields to make sure the Firewall object was populated
assert firewalls[0].label == "firewall123"
assert firewalls[0].rules.inbound[0].action == "ACCEPT"
assert firewalls[0].status == "enabled"