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
352 lines (285 loc) · 11.1 KB
/
Copy pathlinode_test.py
File metadata and controls
352 lines (285 loc) · 11.1 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
from datetime import datetime
from test.base import ClientBaseCase
from linode_api4.objects import Config, Disk, Image, Instance, Type
from linode_api4.objects.base import MappedObject
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")
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')
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,
})
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(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.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
},
"label": "NewLinodeLabel",
"group": "new_group",
"tags": ["something"],
})
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')
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/debian10")
self.assertEqual(m.call_url, "/linode/instances/123/disks")
print(m.call_data)
self.assertEqual(m.call_data, {
"size": 1234,
"label": "test",
"root_pass": gen_pass,
"image": "linode/debian10",
"authorized_users": ["test"],
"read_only": False,
})
assert disk.id == 12345
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'
},
{
'purpose': 'vlan',
'label': 'cool-vlan'
}
]
config.interfaces = new_interfaces
config.save()
self.assertEqual(m.call_url, '/linode/instances/123/configs/456789')
self.assertEqual(m.call_data.get('interfaces'), new_interfaces)
class TypeTest(ClientBaseCase):
def test_get_types(self):
"""
Tests that Linode types can be returned
"""
types = self.client.linode.types()
self.assertEqual(len(types), 4)
for t in types:
self.assertTrue(t._populated)
self.assertIsNotNone(t.id)
self.assertIsNotNone(t.label)
self.assertIsNotNone(t.disk)
self.assertIsNotNone(t.type_class)
self.assertIsNotNone(t.gpus)
self.assertIsNone(t.successor)
def test_get_type_by_id(self):
"""
Tests that a Linode type is loaded correctly by ID
"""
t = Type(self.client, 'g5-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')
def test_get_type_gpu(self):
"""
Tests that gpu types load up right
"""
t = Type(self.client, "g5-gpu-2")
self.assertEqual(t._populated, False)
self.assertEqual(t.gpus, 1)
self.assertEqual(t._populated, True)