forked from linode/linode_api4-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlke_test.py
More file actions
538 lines (451 loc) · 17.7 KB
/
lke_test.py
File metadata and controls
538 lines (451 loc) · 17.7 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
from datetime import datetime
from test.unit.base import ClientBaseCase
from unittest.mock import MagicMock
from linode_api4 import InstanceDiskEncryptionType
from linode_api4.objects import (
LKECluster,
LKEClusterControlPlaneACLAddressesOptions,
LKEClusterControlPlaneACLOptions,
LKEClusterControlPlaneOptions,
LKENodePool,
)
from linode_api4.objects.lke import LKENodePoolNode, LKENodePoolTaint
class LKETest(ClientBaseCase):
"""
Tests methods of the LKE class
"""
def test_get_cluster(self):
"""
Tests that the LKECluster object is properly generated.
"""
cluster = LKECluster(self.client, 18881)
self.assertEqual(cluster.id, 18881)
self.assertEqual(
cluster.created,
datetime(year=2021, month=2, day=10, hour=23, minute=54, second=21),
)
self.assertEqual(
cluster.updated,
datetime(year=2021, month=2, day=10, hour=23, minute=54, second=21),
)
self.assertEqual(cluster.label, "example-cluster")
self.assertEqual(cluster.tags, [])
self.assertEqual(cluster.region.id, "ap-west")
self.assertEqual(cluster.k8s_version.id, "1.19")
self.assertTrue(cluster.control_plane.high_availability)
self.assertTrue(cluster.apl_enabled)
def test_get_pool(self):
"""
Tests that the LKENodePool object is properly generated.
"""
pool = LKENodePool(self.client, 456, 18881)
assert pool.id == 456
assert pool.cluster_id == 18881
assert pool.type.id == "g6-standard-4"
assert pool.disk_encryption == InstanceDiskEncryptionType.enabled
assert pool.disks is not None
assert pool.nodes is not None
assert pool.autoscaler is not None
assert pool.tags is not None
assert pool.labels.foo == "bar"
assert pool.labels.bar == "foo"
assert isinstance(pool.taints[0], LKENodePoolTaint)
assert pool.taints[0].key == "foo"
assert pool.taints[0].value == "bar"
assert pool.taints[0].effect == "NoSchedule"
def test_cluster_dashboard_url_view(self):
"""
Tests that you can submit a correct cluster dashboard url api request.
"""
cluster = LKECluster(self.client, 18881)
with self.mock_get("/lke/clusters/18881/dashboard") as m:
result = cluster.cluster_dashboard_url_view()
self.assertEqual(m.call_url, "/lke/clusters/18881/dashboard")
self.assertEqual(result, "https://example.dashboard.linodelke.net")
def test_kubeconfig_delete(self):
"""
Tests that you can submit a correct kubeconfig delete api request.
"""
cluster = LKECluster(self.client, 18881)
with self.mock_delete() as m:
cluster.kubeconfig_delete()
self.assertEqual(m.call_url, "/lke/clusters/18881/kubeconfig")
def test_node_view(self):
"""
Tests that you can submit a correct node view api request.
"""
cluster = LKECluster(self.client, 18881)
with self.mock_get("/lke/clusters/18881/nodes/123456") as m:
node = cluster.node_view(123456)
self.assertEqual(m.call_url, "/lke/clusters/18881/nodes/123456")
self.assertIsNotNone(node)
self.assertEqual(node.id, "123456")
self.assertEqual(node.instance_id, 456)
self.assertEqual(node.status, "ready")
def test_node_delete(self):
"""
Tests that you can submit a correct node delete api request.
"""
cluster = LKECluster(self.client, 18881)
with self.mock_delete() as m:
cluster.node_delete(1234)
self.assertEqual(m.call_url, "/lke/clusters/18881/nodes/1234")
def test_node_recycle(self):
"""
Tests that you can submit a correct node recycle api request.
"""
cluster = LKECluster(self.client, 18881)
with self.mock_post({}) as m:
cluster.node_recycle(1234)
self.assertEqual(
m.call_url, "/lke/clusters/18881/nodes/1234/recycle"
)
def test_cluster_nodes_recycle(self):
"""
Tests that you can submit a correct cluster nodes recycle api request.
"""
cluster = LKECluster(self.client, 18881)
with self.mock_post({}) as m:
cluster.cluster_nodes_recycle()
self.assertEqual(m.call_url, "/lke/clusters/18881/recycle")
def test_cluster_regenerate(self):
"""
Tests that you can submit a correct cluster regenerate api request.
"""
cluster = LKECluster(self.client, 18881)
with self.mock_post({}) as m:
cluster.cluster_regenerate()
self.assertEqual(m.call_url, "/lke/clusters/18881/regenerate")
def test_service_token_delete(self):
"""
Tests that you can submit a correct service token delete api request.
"""
cluster = LKECluster(self.client, 18881)
with self.mock_delete() as m:
cluster.service_token_delete()
self.assertEqual(m.call_url, "/lke/clusters/18881/servicetoken")
def test_load_node_pool(self):
"""
Tests that an LKE Node Pool can be retrieved using LinodeClient.load(...)
"""
pool = self.client.load(LKENodePool, 456, 18881)
self.assertEqual(pool.id, 456)
self.assertEqual(pool.cluster_id, 18881)
self.assertEqual(pool.type.id, "g6-standard-4")
self.assertIsNotNone(pool.disks)
self.assertIsNotNone(pool.nodes)
self.assertIsNotNone(pool.autoscaler)
self.assertIsNotNone(pool.tags)
def test_cluster_get_acl(self):
"""
Tests that an LKE cluster can be created with a control plane ACL configuration.
"""
cluster = LKECluster(self.client, 18881)
with self.mock_get("lke/clusters/18881/control_plane_acl") as m:
_ = cluster.control_plane_acl
# Get the value again to pull from cache
acl = cluster.control_plane_acl
assert m.call_url == "/lke/clusters/18881/control_plane_acl"
assert m.method == "get"
# Ensure the endpoint was only called once
assert m.called == 1
assert acl.enabled
assert acl.addresses.ipv4 == ["10.0.0.1/32"]
assert acl.addresses.ipv6 == ["1234::5678"]
def test_cluster_put_acl(self):
"""
Tests that an LKE cluster can be created with a control plane ACL configuration.
"""
cluster = LKECluster(self.client, 18881)
with self.mock_put("lke/clusters/18881/control_plane_acl") as m:
acl = cluster.control_plane_acl_update(
LKEClusterControlPlaneACLOptions(
addresses=LKEClusterControlPlaneACLAddressesOptions(
ipv4=["10.0.0.2/32"],
)
)
)
# Make sure the cache was updated
assert cluster.control_plane_acl.dict == acl.dict
assert m.call_url == "/lke/clusters/18881/control_plane_acl"
assert m.method == "put"
assert m.call_data == {
"acl": {
"addresses": {
"ipv4": ["10.0.0.2/32"],
}
}
}
assert acl.enabled
assert acl.addresses.ipv4 == ["10.0.0.1/32"]
def test_cluster_delete_acl(self):
"""
Tests that an LKE cluster can be created with a control plane ACL configuration.
"""
cluster = LKECluster(self.client, 18881)
with self.mock_delete() as m:
cluster.control_plane_acl_delete()
# Make sure the cache was cleared
assert not hasattr(cluster, "_control_plane_acl")
assert m.call_url == "/lke/clusters/18881/control_plane_acl"
assert m.method == "delete"
# We expect a GET request to be made when accessing `control_plane_acl`
# because the cached value has been invalidated
with self.mock_get("lke/clusters/18881/control_plane_acl") as m:
cluster.control_plane_acl
assert m.call_url == "/lke/clusters/18881/control_plane_acl"
assert m.method == "get"
def test_lke_node_pool_update(self):
"""
Tests that an LKE Node Pool can be properly updated.
"""
pool = LKENodePool(self.client, 456, 18881)
pool.tags = ["foobar"]
pool.count = 5
pool.autoscaler = {
"enabled": True,
"min": 2,
"max": 10,
}
pool.labels = {"updated-key": "updated-value"}
pool.taints = [
LKENodePoolTaint(
key="updated-key", value="updated-value", effect="NoExecute"
)
]
with self.mock_put("lke/clusters/18881/pools/456") as m:
pool.save()
assert m.call_data == {
"tags": ["foobar"],
"count": 5,
"autoscaler": {
"enabled": True,
"min": 2,
"max": 10,
},
"labels": {
"updated-key": "updated-value",
},
"taints": [
{
"key": "updated-key",
"value": "updated-value",
"effect": "NoExecute",
}
],
}
def test_cluster_create_with_labels_and_taints(self):
"""
Tests that an LKE cluster can be created with labels and taints.
"""
with self.mock_post("lke/clusters") as m:
self.client.lke.cluster_create(
"us-mia",
"test-acl-cluster",
[
self.client.lke.node_pool(
"g6-nanode-1",
3,
labels={
"foo": "bar",
},
taints=[
LKENodePoolTaint(
key="a", value="b", effect="NoSchedule"
),
{"key": "b", "value": "a", "effect": "NoSchedule"},
],
)
],
"1.29",
)
assert m.call_data["node_pools"][0] == {
"type": "g6-nanode-1",
"count": 3,
"labels": {"foo": "bar"},
"taints": [
{"key": "a", "value": "b", "effect": "NoSchedule"},
{"key": "b", "value": "a", "effect": "NoSchedule"},
],
}
def test_cluster_create_with_apl(self):
"""
Tests that an LKE cluster can be created with APL enabled.
"""
with self.mock_post("lke/clusters") as m:
cluster = self.client.lke.cluster_create(
"us-mia",
"test-aapl-cluster",
[
self.client.lke.node_pool(
"g6-dedicated-4",
3,
)
],
"1.29",
apl_enabled=True,
control_plane=LKEClusterControlPlaneOptions(
high_availability=True,
),
)
assert m.call_data["apl_enabled"] == True
assert m.call_data["control_plane"]["high_availability"] == True
assert (
cluster.apl_console_url == "https://console.lke18881.akamai-apl.net"
)
assert (
cluster.apl_health_check_url
== "https://auth.lke18881.akamai-apl.net/ready"
)
def test_populate_with_taints(self):
"""
Tests that LKENodePool correctly handles a list of LKENodePoolTaint and Dict objects.
"""
self.client = MagicMock()
self.pool = LKENodePool(self.client, 456, 18881)
self.pool._populate(
{
"taints": [
LKENodePoolTaint(
key="wow", value="cool", effect="NoExecute"
),
{
"key": "foo",
"value": "bar",
"effect": "NoSchedule",
},
],
}
)
assert len(self.pool.taints) == 2
assert self.pool.taints[0].dict == {
"key": "wow",
"value": "cool",
"effect": "NoExecute",
}
assert self.pool.taints[1].dict == {
"key": "foo",
"value": "bar",
"effect": "NoSchedule",
}
def test_populate_with_node_objects(self):
"""
Tests that LKENodePool correctly handles a list of LKENodePoolNode objects.
"""
self.client = MagicMock()
self.pool = LKENodePool(self.client, 456, 18881)
node1 = LKENodePoolNode(
self.client, {"id": "node1", "instance_id": 101, "status": "active"}
)
node2 = LKENodePoolNode(
self.client,
{"id": "node2", "instance_id": 102, "status": "inactive"},
)
self.pool._populate({"nodes": [node1, node2]})
self.assertEqual(len(self.pool.nodes), 2)
self.assertIsInstance(self.pool.nodes[0], LKENodePoolNode)
self.assertIsInstance(self.pool.nodes[1], LKENodePoolNode)
self.assertEqual(self.pool.nodes[0].id, "node1")
self.assertEqual(self.pool.nodes[1].id, "node2")
def test_populate_with_node_dicts(self):
"""
Tests that LKENodePool correctly handles a list of node dictionaries.
"""
self.client = MagicMock()
self.pool = LKENodePool(self.client, 456, 18881)
node_dict1 = {"id": "node3", "instance_id": 103, "status": "pending"}
node_dict2 = {"id": "node4", "instance_id": 104, "status": "failed"}
self.pool._populate({"nodes": [node_dict1, node_dict2]})
assert len(self.pool.nodes) == 2
assert isinstance(self.pool.nodes[0], LKENodePoolNode)
assert isinstance(self.pool.nodes[1], LKENodePoolNode)
assert self.pool.nodes[0].id == "node3"
assert self.pool.nodes[1].id == "node4"
def test_populate_with_node_ids(self):
"""
Tests that LKENodePool correctly handles a list of node IDs.
"""
self.client = MagicMock()
self.pool = LKENodePool(self.client, 456, 18881)
node_id1 = "node5"
node_id2 = "node6"
# Mock instances creation
self.client.load = MagicMock(
side_effect=[
LKENodePoolNode(
self.client,
{"id": "node5", "instance_id": 105, "status": "active"},
),
LKENodePoolNode(
self.client,
{"id": "node6", "instance_id": 106, "status": "inactive"},
),
]
)
self.pool._populate({"nodes": [node_id1, node_id2]})
assert len(self.pool.nodes) == 2
assert isinstance(self.pool.nodes[0], LKENodePoolNode)
assert isinstance(self.pool.nodes[1], LKENodePoolNode)
assert self.pool.nodes[0].id == "node5"
assert self.pool.nodes[1].id == "node6"
def test_populate_with_mixed_types(self):
"""
Tests that LKENodePool correctly handles a mixed list of node objects, dicts, and IDs.
"""
self.client = MagicMock()
self.pool = LKENodePool(self.client, 456, 18881)
node1 = LKENodePoolNode(
self.client, {"id": "node7", "instance_id": 107, "status": "active"}
)
node_dict = {"id": "node8", "instance_id": 108, "status": "inactive"}
node_id = "node9"
# Mock instances creation
self.client.load = MagicMock(
side_effect=[
LKENodePoolNode(
self.client,
{"id": "node9", "instance_id": 109, "status": "pending"},
)
]
)
self.pool._populate({"nodes": [node1, node_dict, node_id]})
assert len(self.pool.nodes) == 3
assert isinstance(self.pool.nodes[0], LKENodePoolNode)
assert isinstance(self.pool.nodes[1], LKENodePoolNode)
assert isinstance(self.pool.nodes[2], LKENodePoolNode)
assert self.pool.nodes[0].id == "node7"
assert self.pool.nodes[1].id == "node8"
assert self.pool.nodes[2].id == "node9"
def test_cluster_create_acl_null_addresses(self):
with self.mock_post("lke/clusters") as m:
self.client.lke.cluster_create(
region="us-mia",
label="foobar",
kube_version="1.32",
node_pools=[self.client.lke.node_pool("g6-standard-1", 3)],
control_plane={
"acl": {
"enabled": False,
"addresses": None,
}
},
)
# Addresses should not be included in the API request if it's null
# See: TPT-3489
assert m.call_data["control_plane"] == {
"acl": {
"enabled": False,
}
}
def test_cluster_update_acl_null_addresses(self):
cluster = LKECluster(self.client, 18881)
with self.mock_put("lke/clusters/18881/control_plane_acl") as m:
cluster.control_plane_acl_update(
{
"enabled": True,
"addresses": None,
}
)
# Addresses should not be included in the API request if it's null
# See: TPT-3489
assert m.call_data == {"acl": {"enabled": True}}