11import re
22import time
33from test .integration .conftest import get_region
4- from test .integration .helpers import get_test_label
4+ from test .integration .helpers import get_test_label , wait_for_condition
55
66import pytest
77
88from linode_api4 import ApiError
99from linode_api4 .objects import ConfigInterface , ObjectStorageKeys , Region
1010
1111
12+ def is_tag_created (client , tag_label ):
13+ tags = client .tags ()
14+ tag_label_list = [i .label for i in tags ]
15+
16+ return tag_label in tag_label_list
17+
18+
1219@pytest .fixture (scope = "session" )
1320def setup_client_and_linode (test_linode_client , e2e_test_firewall ):
1421 client = test_linode_client
@@ -55,10 +62,11 @@ def test_fails_to_create_domain_without_soa_email(setup_client_and_linode):
5562
5663 timestamp = str (time .time_ns ())
5764 domain_addr = timestamp + "example.com"
58- try :
59- domain = client .domain_create (domain = domain_addr )
60- except ApiError as e :
61- assert e .status == 400
65+
66+ with pytest .raises (ApiError ) as exc_info :
67+ client .domain_create (domain = domain_addr )
68+
69+ assert exc_info .value .status == 400
6270
6371
6472@pytest .mark .smoke
@@ -90,11 +98,16 @@ def test_get_regions(test_linode_client):
9098def test_image_create (setup_client_and_linode ):
9199 client = setup_client_and_linode [0 ]
92100 linode = setup_client_and_linode [1 ]
93-
94101 label = get_test_label ()
95102 description = "Test description"
96103 tags = ["test" ]
97- usable_disk = [v for v in linode .disks if v .filesystem != "swap" ]
104+
105+ def linode_disks_are_ready (linode_instance ):
106+ linode_instance .invalidate ()
107+ disks = [d for d in linode_instance .disks if d .filesystem != "swap" ]
108+ return disks if disks else None
109+
110+ usable_disk = wait_for_condition (5 , 120 , linode_disks_are_ready , linode )
98111
99112 image = client .image_create (
100113 disk = usable_disk [0 ].id , label = label , description = description , tags = tags
@@ -116,23 +129,23 @@ def test_fails_to_create_image_with_non_existing_disk_id(
116129 description = "Test description"
117130 disk_id = 111111
118131
119- try :
132+ with pytest . raises ( ApiError ) as exc_info :
120133 client .image_create (disk = disk_id , label = label , description = description )
121- except ApiError as e :
122- assert 400 <= e .status < 500
134+
135+ # TODO: Specific status code may be used when defect is solved: ARB-7797
136+ assert 400 <= exc_info .value .status < 500
123137
124138
125139def test_fails_to_delete_predefined_images (setup_client_and_linode ):
126140 client = setup_client_and_linode [0 ]
127141
128142 images = client .images ()
129143
130- try :
144+ with pytest . raises ( ApiError , match = "Unauthorized" ) as exc_info :
131145 # new images go on top of the list thus choose last image
132146 images .last ().delete ()
133- except ApiError as e :
134- assert "Unauthorized" in str (e .json )
135- assert e .status == 403
147+
148+ assert exc_info .value .status == 403
136149
137150
138151def test_get_volume (test_linode_client , test_volume ):
@@ -150,11 +163,7 @@ def test_get_tag(test_linode_client, test_tag):
150163 client = test_linode_client
151164 label = test_tag .label
152165
153- tags = client .tags ()
154-
155- tag_label_list = [i .label for i in tags ]
156-
157- assert label in tag_label_list
166+ assert wait_for_condition (3 , 30 , is_tag_created , client , label )
158167
159168
160169def test_create_tag_with_id (
@@ -176,15 +185,10 @@ def test_create_tag_with_id(
176185 volumes = [volume .id , volume ],
177186 )
178187
179- # Get tags after creation
180- tags = client .tags ()
181-
182- tag_label_list = [i .label for i in tags ]
188+ assert wait_for_condition (3 , 30 , is_tag_created , client , label )
183189
184190 tag .delete ()
185191
186- assert label in tag_label_list
187-
188192
189193@pytest .mark .smoke
190194def test_create_tag_with_entities (
@@ -202,15 +206,10 @@ def test_create_tag_with_entities(
202206 label , entities = [linode , domain , nodebalancer , volume ]
203207 )
204208
205- # Get tags after creation
206- tags = client .tags ()
207-
208- tag_label_list = [i .label for i in tags ]
209+ assert wait_for_condition (3 , 30 , is_tag_created , client , label )
209210
210211 tag .delete ()
211212
212- assert label in tag_label_list
213-
214213
215214# AccountGroupTests
216215def test_get_account_settings (test_linode_client ):
@@ -345,16 +344,15 @@ def test_fails_to_create_cluster_with_invalid_version(test_linode_client):
345344 client = test_linode_client
346345 region = get_region (client , {"Kubernetes" }).id
347346
348- try :
349- cluster = client .lke .cluster_create (
347+ with pytest . raises ( ApiError , match = "not valid" ) as exc_info :
348+ client .lke .cluster_create (
350349 region ,
351350 "example-cluster" ,
352351 invalid_version ,
353352 {"type" : "g6-standard-1" , "count" : 3 },
354353 )
355- except ApiError as e :
356- assert "not valid" in str (e .json )
357- assert e .status == 400
354+
355+ assert exc_info .value .status == 400
358356
359357
360358# ObjectStorageGroupTests
0 commit comments