forked from linode/linode_api4-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinode_test.py
More file actions
1171 lines (954 loc) · 37.7 KB
/
linode_test.py
File metadata and controls
1171 lines (954 loc) · 37.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from datetime import datetime
from test.unit.base import ClientBaseCase
from test.unit.objects.linode_interface_test import (
LinodeInterfaceTest,
build_interface_options_public,
build_interface_options_vlan,
build_interface_options_vpc,
)
from linode_api4 import (
InstanceDiskEncryptionType,
InterfaceGeneration,
NetworkInterface,
)
from linode_api4.objects import (
Config,
ConfigInterface,
ConfigInterfaceIPv4,
ConfigInterfaceIPv6,
ConfigInterfaceIPv6Options,
ConfigInterfaceIPv6Range,
ConfigInterfaceIPv6RangeOptions,
ConfigInterfaceIPv6SLAAC,
ConfigInterfaceIPv6SLAACOptions,
Disk,
Image,
Instance,
StackScript,
Type,
VPCSubnet,
)
class LinodeTest(ClientBaseCase):
"""
Tests methods of the Linode class
"""
def test_get_linode(self):
"""
Tests that a client is loaded correctly by ID
"""
linode = Instance(self.client, 123)
self.assertEqual(linode._populated, False)
self.assertEqual(linode.label, "linode123")
self.assertEqual(linode.group, "test")
self.assertTrue(isinstance(linode.image, Image))
self.assertEqual(linode.image.label, "Ubuntu 17.04")
self.assertEqual(
linode.host_uuid, "3a3ddd59d9a78bb8de041391075df44de62bfec8"
)
self.assertEqual(linode.watchdog_enabled, True)
self.assertEqual(
linode.disk_encryption, InstanceDiskEncryptionType.disabled
)
self.assertEqual(linode.lke_cluster_id, None)
self.assertEqual(linode.maintenance_policy, "linode/migrate")
json = linode._raw_json
self.assertIsNotNone(json)
self.assertEqual(json["id"], 123)
self.assertEqual(json["label"], "linode123")
self.assertEqual(json["group"], "test")
# test that the _raw_json stored on the object is sufficient to populate
# a new object
linode2 = Instance(self.client, json["id"], json=json)
self.assertTrue(linode2._populated)
self.assertEqual(linode2.id, linode.id)
self.assertEqual(linode2.label, linode.label)
self.assertEqual(linode2.group, linode.group)
self.assertEqual(linode2._raw_json, linode._raw_json)
def test_transfer(self):
"""
Tests that you can get transfer
"""
linode = Instance(self.client, 123)
transfer = linode.transfer
self.assertEqual(transfer.quota, 471)
self.assertEqual(transfer.billable, 0)
self.assertEqual(transfer.used, 10369075)
def test_rebuild(self):
"""
Tests that you can rebuild with an image
"""
linode = Instance(self.client, 123)
with self.mock_post("/linode/instances/123") as m:
pw = linode.rebuild(
"linode/debian9",
disk_encryption=InstanceDiskEncryptionType.enabled,
)
self.assertIsNotNone(pw)
self.assertTrue(isinstance(pw, str))
self.assertEqual(m.call_url, "/linode/instances/123/rebuild")
self.assertEqual(
m.call_data,
{
"image": "linode/debian9",
"root_pass": pw,
"disk_encryption": "enabled",
},
)
def test_available_backups(self):
"""
Tests that a Linode can retrieve its own backups
"""
linode = Instance(self.client, 123)
backups = linode.available_backups
# assert we got the correct number of automatic backups
self.assertEqual(len(backups.automatic), 3)
# examine one automatic backup
b = backups.automatic[0]
self.assertEqual(b.id, 12345)
self.assertEqual(b._populated, True)
self.assertEqual(b.status, "successful")
self.assertEqual(b.type, "auto")
self.assertEqual(
b.created,
datetime(year=2018, month=1, day=9, hour=0, minute=1, second=1),
)
self.assertEqual(
b.updated,
datetime(year=2018, month=1, day=9, hour=0, minute=1, second=1),
)
self.assertEqual(
b.finished,
datetime(year=2018, month=1, day=9, hour=0, minute=1, second=1),
)
self.assertEqual(b.region.id, "us-east-1a")
self.assertEqual(b.label, None)
self.assertEqual(b.message, None)
self.assertEqual(b.available, True)
self.assertEqual(len(b.disks), 2)
self.assertEqual(b.disks[0].size, 1024)
self.assertEqual(b.disks[0].label, "Debian 8.1 Disk")
self.assertEqual(b.disks[0].filesystem, "ext4")
self.assertEqual(b.disks[1].size, 0)
self.assertEqual(b.disks[1].label, "256MB Swap Image")
self.assertEqual(b.disks[1].filesystem, "swap")
self.assertEqual(len(b.configs), 1)
self.assertEqual(b.configs[0], "My Debian 8.1 Profile")
# assert that snapshots came back as expected
self.assertEqual(backups.snapshot.current, None)
self.assertEqual(backups.snapshot.in_progress, None)
def test_update_linode(self):
"""
Tests that a Linode can be updated
"""
with self.mock_put("linode/instances/123") as m:
linode = self.client.load(Instance, 123)
linode.label = "NewLinodeLabel"
linode.group = "new_group"
linode.maintenance_policy = "linode/power_off_on"
linode.save()
self.assertEqual(m.call_url, "/linode/instances/123")
self.assertEqual(
m.call_data,
{
"alerts": {
"cpu": 90,
"io": 5000,
"network_in": 5,
"network_out": 5,
"transfer_quota": 80,
},
"backups": {
"enabled": True,
"schedule": {"day": "Scheduling", "window": "W02"},
},
"label": "NewLinodeLabel",
"group": "new_group",
"tags": ["something"],
"watchdog_enabled": True,
"maintenance_policy": "linode/power_off_on",
},
)
def test_delete_linode(self):
"""
Tests that deleting a Linode creates the correct api request
"""
with self.mock_delete() as m:
linode = Instance(self.client, 123)
linode.delete()
self.assertEqual(m.call_url, "/linode/instances/123")
def test_reboot(self):
"""
Tests that you can submit a correct reboot api request
"""
linode = Instance(self.client, 123)
result = {}
with self.mock_post(result) as m:
linode.reboot()
self.assertEqual(m.call_url, "/linode/instances/123/reboot")
def test_shutdown(self):
"""
Tests that you can submit a correct shutdown api request
"""
linode = Instance(self.client, 123)
result = {}
with self.mock_post(result) as m:
linode.shutdown()
self.assertEqual(m.call_url, "/linode/instances/123/shutdown")
def test_boot(self):
"""
Tests that you can submit a correct boot api request
"""
linode = Instance(self.client, 123)
result = {}
with self.mock_post(result) as m:
linode.boot()
self.assertEqual(m.call_url, "/linode/instances/123/boot")
def test_resize(self):
"""
Tests that you can submit a correct resize api request
"""
linode = Instance(self.client, 123)
result = {}
with self.mock_post(result) as m:
linode.resize(new_type="g6-standard-1")
self.assertEqual(m.call_url, "/linode/instances/123/resize")
self.assertEqual(m.call_data["type"], "g6-standard-1")
def test_resize_with_class(self):
"""
Tests that you can submit a correct resize api request with a Base class type
"""
linode = Instance(self.client, 123)
ltype = Type(self.client, "g6-standard-2")
result = {}
with self.mock_post(result) as m:
linode.resize(new_type=ltype)
self.assertEqual(m.call_url, "/linode/instances/123/resize")
self.assertEqual(m.call_data["type"], "g6-standard-2")
def test_boot_with_config(self):
"""
Tests that you can submit a correct boot with a config api request
"""
linode = Instance(self.client, 123)
config = linode.configs[0]
result = {}
with self.mock_post(result) as m:
linode.boot(config=config)
self.assertEqual(m.call_url, "/linode/instances/123/boot")
def test_mutate(self):
"""
Tests that you can submit a correct mutate api request
"""
linode = Instance(self.client, 123)
result = {}
with self.mock_post(result) as m:
linode.mutate()
self.assertEqual(m.call_url, "/linode/instances/123/mutate")
self.assertEqual(m.call_data["allow_auto_disk_resize"], True)
def test_firewalls(self):
"""
Tests that you can submit a correct firewalls api request
"""
linode = Instance(self.client, 123)
with self.mock_get("/linode/instances/123/firewalls") as m:
result = linode.firewalls()
self.assertEqual(m.call_url, "/linode/instances/123/firewalls")
self.assertEqual(len(result), 1)
def test_apply_firewalls(self):
"""
Tests that you can submit a correct apply firewalls api request
"""
linode = Instance(self.client, 123)
with self.mock_post({}) as m:
result = linode.apply_firewalls()
self.assertEqual(
m.call_url, "/linode/instances/123/firewalls/apply"
)
self.assertEqual(result, True)
def test_volumes(self):
"""
Tests that you can submit a correct volumes api request
"""
linode = Instance(self.client, 123)
with self.mock_get("/linode/instances/123/volumes") as m:
result = linode.volumes()
self.assertEqual(m.call_url, "/linode/instances/123/volumes")
self.assertEqual(len(result), 1)
def test_nodebalancers(self):
"""
Tests that you can submit a correct nodebalancers api request
"""
linode = Instance(self.client, 123)
with self.mock_get("/linode/instances/123/nodebalancers") as m:
result = linode.nodebalancers()
self.assertEqual(m.call_url, "/linode/instances/123/nodebalancers")
self.assertEqual(len(result), 1)
def test_transfer_year_month(self):
"""
Tests that you can submit a correct transfer api request
"""
linode = Instance(self.client, 123)
with self.mock_get("/linode/instances/123/transfer/2023/4") as m:
linode.transfer_year_month(2023, 4)
self.assertEqual(
m.call_url, "/linode/instances/123/transfer/2023/4"
)
def test_lke_cluster(self):
"""
Tests that you can grab the parent LKE cluster from an instance node
"""
linode = Instance(self.client, 456)
assert linode.lke_cluster_id == 18881
assert linode.lke_cluster.id == linode.lke_cluster_id
def test_duplicate(self):
"""
Tests that you can submit a correct disk clone api request
"""
disk = Disk(self.client, 12345, 123)
with self.mock_post("/linode/instances/123/disks/12345/clone") as m:
disk.duplicate()
self.assertEqual(
m.call_url, "/linode/instances/123/disks/12345/clone"
)
assert disk.disk_encryption == InstanceDiskEncryptionType.disabled
def test_disk_password(self):
"""
Tests that you can submit a correct disk password reset api request
"""
disk = Disk(self.client, 12345, 123)
with self.mock_post({}) as m:
disk.reset_root_password()
self.assertEqual(
m.call_url, "/linode/instances/123/disks/12345/password"
)
def test_instance_password(self):
"""
Tests that you can submit a correct instance password reset api request
"""
instance = Instance(self.client, 123)
with self.mock_post({}) as m:
instance.reset_instance_root_password()
self.assertEqual(m.call_url, "/linode/instances/123/password")
def test_ips(self):
"""
Tests that you can submit a correct ips api request
"""
linode = Instance(self.client, 123)
ips = linode.ips
assert ips.ipv4 is not None
assert ips.ipv6 is not None
assert ips.ipv4.public is not None
assert ips.ipv4.private is not None
assert ips.ipv4.shared is not None
assert ips.ipv4.reserved is not None
assert ips.ipv4.vpc is not None
assert ips.ipv6.slaac is not None
assert ips.ipv6.link_local is not None
assert ips.ipv6.ranges is not None
vpc_ip = ips.ipv4.vpc[0]
assert vpc_ip.nat_1_1 == "172.233.179.133"
assert vpc_ip.address_range == None
assert vpc_ip.vpc_id == 39246
assert vpc_ip.subnet_id == 39388
assert vpc_ip.config_id == 59036295
assert vpc_ip.interface_id == 1186165
assert vpc_ip.active
def test_initiate_migration(self):
"""
Tests that you can initiate a pending migration
"""
linode = Instance(self.client, 123)
result = {}
with self.mock_post(result) as m:
linode.initiate_migration()
self.assertEqual(m.call_url, "/linode/instances/123/migrate")
def test_create_disk(self):
"""
Tests that disk_create behaves as expected
"""
linode = Instance(self.client, 123)
with self.mock_post("/linode/instances/123/disks/12345") as m:
disk, gen_pass = linode.disk_create(
1234,
label="test",
authorized_users=["test"],
image="linode/debian12",
)
self.assertEqual(m.call_url, "/linode/instances/123/disks")
self.assertEqual(
m.call_data,
{
"size": 1234,
"label": "test",
"root_pass": gen_pass,
"image": "linode/debian12",
"authorized_users": ["test"],
"read_only": False,
},
)
assert disk.id == 12345
assert disk.disk_encryption == InstanceDiskEncryptionType.disabled
def test_create_config_with_device_map(self):
"""
Tests that config_create passes through a raw device map unchanged.
"""
linode = Instance(self.client, 123)
devices = {
"sda": {"disk_id": 111},
"sdb": {"volume_id": 222},
"sdc": None,
}
with self.mock_post(
{"id": 456, "devices": devices, "interfaces": []}
) as m:
config = linode.config_create(label="test-config", devices=devices)
self.assertEqual(m.call_url, "/linode/instances/123/configs")
self.assertEqual(
m.call_data,
{
"label": "test-config",
"devices": devices,
"interfaces": [],
},
)
self.assertEqual(config.id, 456)
def test_create_config_without_devices_raises_error(self):
"""
Tests that config_create raises ValueError when no devices, disks, or volumes are specified.
"""
linode = Instance(self.client, 123)
with self.assertRaises(ValueError) as context:
linode.config_create(label="test-config")
assert "Must include at least one disk or volume!" in str(
context.exception
)
def test_get_placement_group(self):
"""
Tests that you can get the placement group for a Linode
"""
linode = Instance(self.client, 123)
pg = linode.placement_group
assert pg.id == 123
assert pg.label == "test"
assert pg.placement_group_type == "anti_affinity:local"
# Invalidate the instance and try again
# This makes sure the implicit refresh/cache logic works
# as expected
linode.invalidate()
pg = linode.placement_group
assert pg.id == 123
assert pg.label == "test"
assert pg.placement_group_type == "anti_affinity:local"
def test_get_interfaces(self):
# Local import to avoid circular dependency
from linode_interface_test import ( # pylint: disable=import-outside-toplevel
LinodeInterfaceTest,
)
instance = Instance(self.client, 124)
assert instance.interface_generation == InterfaceGeneration.LINODE
interfaces = instance.linode_interfaces
LinodeInterfaceTest.assert_linode_124_interface_123(
next(iface for iface in interfaces if iface.id == 123)
)
LinodeInterfaceTest.assert_linode_124_interface_456(
next(iface for iface in interfaces if iface.id == 456)
)
LinodeInterfaceTest.assert_linode_124_interface_789(
next(iface for iface in interfaces if iface.id == 789)
)
def test_get_interfaces_settings(self):
instance = Instance(self.client, 124)
iface_settings = instance.interfaces_settings
assert iface_settings.network_helper
assert iface_settings.default_route.ipv4_interface_id == 123
assert iface_settings.default_route.ipv4_eligible_interface_ids == [
123,
456,
789,
]
assert iface_settings.default_route.ipv6_interface_id == 456
assert iface_settings.default_route.ipv6_eligible_interface_ids == [
123,
456,
]
def test_update_interfaces_settings(self):
instance = Instance(self.client, 124)
iface_settings = instance.interfaces_settings
iface_settings.network_helper = False
iface_settings.default_route.ipv4_interface_id = 456
iface_settings.default_route.ipv6_interface_id = 123
with self.mock_put("/linode/instances/124/interfaces/settings") as m:
iface_settings.save()
assert m.call_data == {
"network_helper": False,
"default_route": {
"ipv4_interface_id": 456,
"ipv6_interface_id": 123,
},
}
def test_upgrade_interfaces(self):
# Local import to avoid circular dependency
from linode_interface_test import ( # pylint: disable=import-outside-toplevel
LinodeInterfaceTest,
)
instance = Instance(self.client, 124)
with self.mock_post("/linode/instances/124/upgrade-interfaces") as m:
result = instance.upgrade_interfaces(123)
assert m.called
assert m.call_data == {"config_id": 123, "dry_run": False}
assert result.config_id == 123
assert result.dry_run
LinodeInterfaceTest.assert_linode_124_interface_123(
result.interfaces[0]
)
LinodeInterfaceTest.assert_linode_124_interface_456(
result.interfaces[1]
)
LinodeInterfaceTest.assert_linode_124_interface_789(
result.interfaces[2]
)
def test_upgrade_interfaces_dry(self):
instance = Instance(self.client, 124)
with self.mock_post("/linode/instances/124/upgrade-interfaces") as m:
result = instance.upgrade_interfaces(123, dry_run=True)
assert m.called
assert m.call_data == {
"config_id": 123,
"dry_run": True,
}
assert result.config_id == 123
assert result.dry_run
# We don't use the assertion helpers here because dry runs return
# a MappedObject.
assert result.interfaces[0].id == 123
assert result.interfaces[0].public is not None
assert result.interfaces[1].id == 456
assert result.interfaces[1].vpc is not None
assert result.interfaces[2].id == 789
assert result.interfaces[2].vlan is not None
def test_create_interface_public(self):
instance = Instance(self.client, 124)
iface = build_interface_options_public()
with self.mock_post("/linode/instances/124/interfaces/123") as m:
result = instance.interface_create(**vars(iface))
assert m.call_data == {
"firewall_id": iface.firewall_id,
"default_route": iface.default_route._serialize(),
"public": iface.public._serialize(),
}
LinodeInterfaceTest.assert_linode_124_interface_123(result)
def test_create_interface_vpc(self):
instance = Instance(self.client, 124)
iface = build_interface_options_vpc()
with self.mock_post("/linode/instances/124/interfaces/456") as m:
result = instance.interface_create(**vars(iface))
assert m.call_data == {
"firewall_id": iface.firewall_id,
"default_route": iface.default_route._serialize(),
"vpc": iface.vpc._serialize(),
}
LinodeInterfaceTest.assert_linode_124_interface_456(result)
def test_create_interface_vlan(self):
instance = Instance(self.client, 124)
iface = build_interface_options_vlan()
with self.mock_post("/linode/instances/124/interfaces/789") as m:
result = instance.interface_create(**vars(iface))
assert m.call_data == {"vlan": iface.vlan._serialize()}
LinodeInterfaceTest.assert_linode_124_interface_789(result)
class DiskTest(ClientBaseCase):
"""
Tests for the Disk object
"""
def test_resize(self):
"""
Tests that a resize is submitted correctly
"""
disk = Disk(self.client, 12345, 123)
with self.mock_post({}) as m:
r = disk.resize(1000)
self.assertTrue(r)
self.assertEqual(
m.call_url, "/linode/instances/123/disks/12345/resize"
)
self.assertEqual(m.call_data, {"size": 1000})
class ConfigTest(ClientBaseCase):
"""
Tests for the Config object
"""
def test_update_interfaces(self):
"""
Tests that a configs interfaces update correctly
"""
json = self.client.get("/linode/instances/123/configs/456789")
config = Config(self.client, 456789, 123, json=json)
with self.mock_put("/linode/instances/123/configs/456789") as m:
new_interfaces = [
{"purpose": "public", "primary": True},
ConfigInterface("vlan", label="cool-vlan"),
ConfigInterface(
"vpc",
vpc_id=18881,
subnet_id=123,
ipv4=ConfigInterfaceIPv4(vpc="10.0.0.4", nat_1_1="any"),
ipv6=ConfigInterfaceIPv6(
slaac=[
ConfigInterfaceIPv6SLAAC(
range="1234::5678/64", address="1234::5678"
)
],
ranges=[
ConfigInterfaceIPv6Range(range="1234::5678/64")
],
is_public=True,
),
),
]
config.interfaces = new_interfaces
config.save()
assert m.call_url == "/linode/instances/123/configs/456789"
assert m.call_data.get("interfaces") == [
{
"purpose": "public",
"primary": True,
},
{
"purpose": "vlan",
"label": "cool-vlan",
},
{
"purpose": "vpc",
"subnet_id": 123,
"ipv4": {
"vpc": "10.0.0.4",
"nat_1_1": "any",
},
"ipv6": {
"slaac": [
{
"range": "1234::5678/64",
# NOTE: Address is read-only so it shouldn't be specified here
}
],
"ranges": [
{
"range": "1234::5678/64",
}
],
"is_public": True,
},
},
]
def test_get_config(self):
json = self.client.get("/linode/instances/123/configs/456789")
config = Config(self.client, 456789, 123, json=json)
self.assertEqual(config.root_device, "/dev/sda")
self.assertEqual(config.comments, "")
self.assertIsNotNone(config.helpers)
self.assertEqual(config.label, "My Ubuntu 17.04 LTS Profile")
self.assertEqual(
config.created,
datetime(year=2014, month=10, day=7, hour=20, minute=4, second=0),
)
self.assertEqual(config.memory_limit, 0)
self.assertEqual(config.id, 456789)
self.assertIsNotNone(config.interfaces)
self.assertEqual(config.run_level, "default")
self.assertIsNone(config.initrd)
self.assertEqual(config.virt_mode, "paravirt")
self.assertIsNotNone(config.devices)
def test_interface_ipv4(self):
json = {"vpc": "10.0.0.1", "nat_1_1": "any"}
ipv4 = ConfigInterfaceIPv4.from_json(json)
self.assertEqual(ipv4.vpc, "10.0.0.1")
self.assertEqual(ipv4.nat_1_1, "any")
def test_interface_ipv6(self):
json = {
"slaac": [{"range": "1234::5678/64", "address": "1234::5678"}],
"ranges": [{"range": "1234::5678/64"}],
"is_public": True,
}
ipv6 = ConfigInterfaceIPv6.from_json(json)
assert len(ipv6.slaac) == 1
assert ipv6.slaac[0].range == "1234::5678/64"
assert ipv6.slaac[0].address == "1234::5678"
assert len(ipv6.ranges) == 1
assert ipv6.ranges[0].range == "1234::5678/64"
assert ipv6.is_public
def test_config_devices_unwrap(self):
"""
Tests that config devices can be successfully converted to a dict.
"""
inst = Instance(self.client, 123)
assert inst.configs[0].devices.dict.get("sda").get("id") == 12345
class StackScriptTest(ClientBaseCase):
"""
Tests the methods of the StackScript class.
"""
def test_get_stackscript(self):
"""
Tests that a stackscript is loaded correctly by ID
"""
stackscript = StackScript(self.client, 10079)
self.assertEqual(stackscript.id, 10079)
self.assertEqual(stackscript.deployments_active, 1)
self.assertEqual(stackscript.deployments_total, 12)
self.assertEqual(stackscript.rev_note, "Set up MySQL")
self.assertTrue(stackscript.mine)
self.assertTrue(stackscript.is_public)
self.assertIsNotNone(stackscript.user_defined_fields)
self.assertIsNotNone(stackscript.images)
class TypeTest(ClientBaseCase):
def test_get_type_by_id(self):
"""
Tests that a Linode type is loaded correctly by ID
"""
t = Type(self.client, "g6-nanode-1")
self.assertEqual(t._populated, False)
self.assertEqual(t.vcpus, 1)
self.assertEqual(t.gpus, 0)
self.assertEqual(t.label, "Linode 1024")
self.assertEqual(t.disk, 20480)
self.assertEqual(t.type_class, "nanode")
self.assertEqual(t.region_prices[0].id, "us-east")
def test_get_type_gpu(self):
"""
Tests that gpu types load up right
"""
t = Type(self.client, "g6-gpu-2")
self.assertEqual(t._populated, False)
self.assertEqual(t.gpus, 1)
self.assertEqual(t._populated, True)
def test_load_type(self):
"""
Tests that a type can be loaded using LinodeClient.load(...)
"""
t = self.client.load(Type, "g6-nanode-1")
self.assertEqual(t._populated, True)
self.assertEqual(t.type_class, "nanode")
def test_save_noforce(self):
"""
Tests that a client will only save if changes are detected
"""
linode = Instance(self.client, 123)
self.assertEqual(linode._populated, False)
self.assertEqual(linode.label, "linode123")
self.assertEqual(linode.group, "test")
assert not linode._changed
with self.mock_put("linode/instances") as m:
linode.save(force=False)
assert not m.called
linode.label = "blah"
assert linode._changed
with self.mock_put("linode/instances") as m:
linode.save(force=False)
assert m.called
assert m.call_url == "/linode/instances/123"
assert m.call_data["label"] == "blah"
assert not linode._changed
def test_save_force(self):
"""
Tests that a client will forcibly save by default
"""
linode = Instance(self.client, 123)
self.assertEqual(linode._populated, False)
self.assertEqual(linode.label, "linode123")
self.assertEqual(linode.group, "test")
assert not linode._changed
with self.mock_put("linode/instances") as m:
linode.save()
assert m.called
class ConfigInterfaceTest(ClientBaseCase):
def test_list(self):
config = Config(self.client, 456789, 123)
config._api_get()
assert {v.id for v in config.interfaces} == {123, 321, 456}
assert {v.purpose for v in config.interfaces} == {
"vlan",
"vpc",
"public",
}
def test_update(self):
config = Config(self.client, 456789, 123)
config._api_get()
config.interfaces = [
{"purpose": "public"},
ConfigInterface(
purpose="vlan", label="cool-vlan", ipam_address="10.0.0.4/32"
),
]
with self.mock_put("linode/instances/123/configs/456789") as m:
config.save()
assert m.call_url == "/linode/instances/123/configs/456789"
assert m.call_data["interfaces"] == [
{"purpose": "public"},
{
"purpose": "vlan",
"label": "cool-vlan",
"ipam_address": "10.0.0.4/32",
},
]
class TestNetworkInterface(ClientBaseCase):
def test_create_interface_public(self):
config = Config(self.client, 456789, 123)
config._api_get()
with self.mock_post(
"linode/instances/123/configs/456789/interfaces/456"
) as m:
interface = config.interface_create_public(primary=True)
assert m.called
assert (
m.call_url == "/linode/instances/123/configs/456789/interfaces"
)
assert m.method == "post"
assert m.call_data == {"purpose": "public", "primary": True}
assert interface.id == 456
assert interface.purpose == "public"
assert interface.primary
def test_create_interface_vlan(self):
config = Config(self.client, 456789, 123)
config._api_get()
with self.mock_post(
"linode/instances/123/configs/456789/interfaces/321"
) as m:
interface = config.interface_create_vlan(
"test-interface", ipam_address="10.0.0.2/32"