2
2
import os
3
3
import random
4
4
import time
5
+ from test .integration .helpers import (
6
+ get_test_label ,
7
+ send_request_when_resource_available ,
8
+ )
5
9
from typing import Optional , Set
6
10
7
11
import pytest
8
12
import requests
9
13
from requests .exceptions import ConnectionError , RequestException
10
14
11
- from linode_api4 import ApiError , PlacementGroupPolicy , PlacementGroupType
15
+ from linode_api4 import PlacementGroupPolicy , PlacementGroupType
12
16
from linode_api4 .linode_client import LinodeClient
13
17
from linode_api4 .objects import Region
14
18
@@ -27,13 +31,6 @@ def get_api_url():
27
31
return os .environ .get (ENV_API_URL_NAME , "https://api.linode.com/v4beta" )
28
32
29
33
30
- def get_random_label ():
31
- timestamp = str (time .time_ns ())[:- 5 ]
32
- label = "label_" + timestamp
33
-
34
- return label
35
-
36
-
37
34
def get_regions (
38
35
client : LinodeClient ,
39
36
capabilities : Optional [Set [str ]] = None ,
@@ -59,7 +56,7 @@ def get_regions(
59
56
60
57
61
58
def get_region (
62
- client : LinodeClient , capabilities : Set [str ] = None , site_type : str = None
59
+ client : LinodeClient , capabilities : Set [str ] = None , site_type : str = "core"
63
60
):
64
61
return random .choice (get_regions (client , capabilities , site_type ))
65
62
@@ -161,14 +158,12 @@ def create_inbound_rule(ipv4_address, ipv6_address):
161
158
def create_linode (test_linode_client , e2e_test_firewall ):
162
159
client = test_linode_client
163
160
164
- available_regions = client .regions ()
165
- chosen_region = available_regions [4 ]
166
- timestamp = str (time .time_ns ())
167
- label = "TestSDK-" + timestamp
161
+ region = get_region (client , {"Linodes" , "Cloud Firewall" }, site_type = "core" )
162
+ label = get_test_label (length = 8 )
168
163
169
164
linode_instance , password = client .linode .instance_create (
170
165
"g6-nanode-1" ,
171
- chosen_region ,
166
+ region ,
172
167
image = "linode/debian12" ,
173
168
label = label ,
174
169
firewall = e2e_test_firewall ,
@@ -183,14 +178,12 @@ def create_linode(test_linode_client, e2e_test_firewall):
183
178
def create_linode_for_pass_reset (test_linode_client , e2e_test_firewall ):
184
179
client = test_linode_client
185
180
186
- available_regions = client .regions ()
187
- chosen_region = available_regions [4 ]
188
- timestamp = str (time .time_ns ())
189
- label = "TestSDK-" + timestamp
181
+ region = get_region (client , {"Linodes" , "Cloud Firewall" }, site_type = "core" )
182
+ label = get_test_label (length = 8 )
190
183
191
184
linode_instance , password = client .linode .instance_create (
192
185
"g6-nanode-1" ,
193
- chosen_region ,
186
+ region ,
194
187
image = "linode/debian10" ,
195
188
label = label ,
196
189
firewall = e2e_test_firewall ,
@@ -271,65 +264,36 @@ def test_domain(test_linode_client):
271
264
@pytest .fixture (scope = "session" )
272
265
def test_volume (test_linode_client ):
273
266
client = test_linode_client
274
- timestamp = str (time .time_ns ())
275
- region = client .regions ()[4 ]
276
- label = "TestSDK-" + timestamp
267
+ region = get_region (client , {"Linodes" , "Cloud Firewall" }, site_type = "core" )
268
+ label = get_test_label (length = 8 )
277
269
278
270
volume = client .volume_create (label = label , region = region )
279
271
280
272
yield volume
281
273
282
- timeout = 100 # give 100s for volume to be detached before deletion
283
-
284
- start_time = time .time ()
285
-
286
- while time .time () - start_time < timeout :
287
- try :
288
- res = volume .delete ()
289
- if res :
290
- break
291
- else :
292
- time .sleep (3 )
293
- except ApiError as e :
294
- if time .time () - start_time > timeout :
295
- raise e
274
+ send_request_when_resource_available (timeout = 100 , func = volume .delete )
296
275
297
276
298
277
@pytest .fixture (scope = "session" )
299
278
def test_volume_with_encryption (test_linode_client ):
300
279
client = test_linode_client
301
- timestamp = str (time .time_ns ())
302
280
region = get_region (client , {"Block Storage Encryption" })
303
- label = "TestSDK-" + timestamp
281
+ label = get_test_label ( length = 8 )
304
282
305
283
volume = client .volume_create (
306
284
label = label , region = region , encryption = "enabled"
307
285
)
308
286
309
287
yield volume
310
288
311
- timeout = 100 # give 100s for volume to be detached before deletion
312
-
313
- start_time = time .time ()
314
-
315
- while time .time () - start_time < timeout :
316
- try :
317
- res = volume .delete ()
318
- if res :
319
- break
320
- else :
321
- time .sleep (3 )
322
- except ApiError as e :
323
- if time .time () - start_time > timeout :
324
- raise e
289
+ send_request_when_resource_available (timeout = 100 , func = volume .delete )
325
290
326
291
327
292
@pytest .fixture
328
293
def test_tag (test_linode_client ):
329
294
client = test_linode_client
330
295
331
- timestamp = str (time .time_ns ())
332
- label = "TestSDK-" + timestamp
296
+ label = get_test_label (length = 8 )
333
297
334
298
tag = client .tag_create (label = label )
335
299
@@ -342,11 +306,10 @@ def test_tag(test_linode_client):
342
306
def test_nodebalancer (test_linode_client ):
343
307
client = test_linode_client
344
308
345
- timestamp = str (time .time_ns ())
346
- label = "TestSDK-" + timestamp
309
+ label = get_test_label (length = 8 )
347
310
348
311
nodebalancer = client .nodebalancer_create (
349
- region = get_region (client ), label = label
312
+ region = get_region (client , capabilities = { "NodeBalancers" } ), label = label
350
313
)
351
314
352
315
yield nodebalancer
@@ -357,8 +320,7 @@ def test_nodebalancer(test_linode_client):
357
320
@pytest .fixture
358
321
def test_longview_client (test_linode_client ):
359
322
client = test_linode_client
360
- timestamp = str (time .time_ns ())
361
- label = "TestSDK-" + timestamp
323
+ label = get_test_label (length = 8 )
362
324
longview_client = client .longview .client_create (label = label )
363
325
364
326
yield longview_client
@@ -370,7 +332,8 @@ def test_longview_client(test_linode_client):
370
332
def test_sshkey (test_linode_client , ssh_key_gen ):
371
333
pub_key = ssh_key_gen [0 ]
372
334
client = test_linode_client
373
- key = client .profile .ssh_key_upload (pub_key , "IntTestSDK-sshkey" )
335
+ key_label = get_test_label (8 ) + "_key"
336
+ key = client .profile .ssh_key_upload (pub_key , key_label )
374
337
375
338
yield key
376
339
@@ -380,7 +343,7 @@ def test_sshkey(test_linode_client, ssh_key_gen):
380
343
@pytest .fixture
381
344
def access_keys_object_storage (test_linode_client ):
382
345
client = test_linode_client
383
- label = "TestSDK-obj-storage-key"
346
+ label = get_test_label ( length = 8 )
384
347
key = client .object_storage .keys_create (label )
385
348
386
349
yield key
@@ -398,8 +361,7 @@ def test_firewall(test_linode_client):
398
361
"inbound_policy" : "ACCEPT" ,
399
362
}
400
363
401
- timestamp = str (time .time_ns ())
402
- label = "firewall_" + timestamp
364
+ label = get_test_label (8 ) + "_firewall"
403
365
404
366
firewall = client .networking .firewall_create (
405
367
label = label , rules = rules , status = "enabled"
@@ -413,7 +375,7 @@ def test_firewall(test_linode_client):
413
375
@pytest .fixture
414
376
def test_oauth_client (test_linode_client ):
415
377
client = test_linode_client
416
- label = get_random_label ( ) + "_oauth"
378
+ label = get_test_label ( length = 8 ) + "_oauth"
417
379
418
380
oauth_client = client .account .oauth_client_create (
419
381
label , "https://localhost/oauth/callback"
@@ -428,10 +390,10 @@ def test_oauth_client(test_linode_client):
428
390
def create_vpc (test_linode_client ):
429
391
client = test_linode_client
430
392
431
- timestamp = str ( int ( time . time ()) )
393
+ label = get_test_label ( length = 10 )
432
394
433
395
vpc = client .vpcs .create (
434
- "pythonsdk-" + timestamp ,
396
+ label ,
435
397
get_region (test_linode_client , {"VPCs" }),
436
398
description = "test description" ,
437
399
)
@@ -455,8 +417,7 @@ def create_vpc_with_subnet_and_linode(
455
417
):
456
418
vpc , subnet = create_vpc_with_subnet
457
419
458
- timestamp = str (int (time .time ()))
459
- label = "TestSDK-" + timestamp
420
+ label = get_test_label (length = 8 )
460
421
461
422
instance , password = test_linode_client .linode .instance_create (
462
423
"g6-standard-1" ,
@@ -475,18 +436,18 @@ def create_vpc_with_subnet_and_linode(
475
436
def create_multiple_vpcs (test_linode_client ):
476
437
client = test_linode_client
477
438
478
- timestamp = str ( int ( time . time_ns () % 10 ** 10 ) )
439
+ label = get_test_label ( length = 10 )
479
440
480
- timestamp_2 = str ( int ( time . time_ns () % 10 ** 10 ) )
441
+ label_2 = get_test_label ( length = 10 )
481
442
482
443
vpc_1 = client .vpcs .create (
483
- "pythonsdk-" + timestamp ,
444
+ label ,
484
445
get_region (test_linode_client , {"VPCs" }),
485
446
description = "test description" ,
486
447
)
487
448
488
449
vpc_2 = client .vpcs .create (
489
- "pythonsdk-" + timestamp_2 ,
450
+ label_2 ,
490
451
get_region (test_linode_client , {"VPCs" }),
491
452
description = "test description" ,
492
453
)
@@ -502,10 +463,10 @@ def create_multiple_vpcs(test_linode_client):
502
463
def create_placement_group (test_linode_client ):
503
464
client = test_linode_client
504
465
505
- timestamp = str ( int ( time . time ()) )
466
+ label = get_test_label ( 10 )
506
467
507
468
pg = client .placement .group_create (
508
- "pythonsdk-" + timestamp ,
469
+ label ,
509
470
get_region (test_linode_client , {"Placement Group" }),
510
471
PlacementGroupType .anti_affinity_local ,
511
472
PlacementGroupPolicy .flexible ,
0 commit comments