Skip to content

Commit 09b66c2

Browse files
Replace assert statements
Signed-off-by: Markus Hentsch <[email protected]>
1 parent 128b404 commit 09b66c2

File tree

1 file changed

+47
-20
lines changed

1 file changed

+47
-20
lines changed

Tests/iaas/volume-backup/volume-backup-tester.py

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@
2929
WAIT_TIMEOUT = 60
3030

3131

32+
class ConformanceTestException(Exception):
33+
pass
34+
35+
36+
def ensure(condition: bool, error_message: str):
37+
"""
38+
Custom replacement for the `assert` statement that is not removed by the
39+
-O optimization parameter.
40+
If the condition does not evaluate to `True`, a ConformanceTestException
41+
will be raised containing the specified error_message string.
42+
"""
43+
if not condition:
44+
raise ConformanceTestException(error_message)
45+
46+
3247
def connect(cloud_name: str, password: typing.Optional[str] = None
3348
) -> openstack.connection.Connection:
3449
"""Create a connection to an OpenStack cloud
@@ -64,17 +79,20 @@ def test_backup(conn: openstack.connection.Connection,
6479
"""
6580

6681
# CREATE VOLUME
67-
print("Creating volume ...")
82+
volume_name = f"{prefix}volume"
83+
print(f"Creating volume '{volume_name}' ...")
6884
volume = conn.block_storage.create_volume(
69-
name=f"{prefix}volume",
85+
name=volume_name,
7086
size=1
7187
)
72-
assert volume is not None, (
73-
"Initial volume creation failed"
88+
ensure(
89+
volume is not None,
90+
f"Creation of initial volume '{volume_name}' failed"
7491
)
7592
volume_id = volume.id
76-
assert conn.block_storage.get_volume(volume_id) is not None, (
77-
"Retrieving initial volume by ID failed"
93+
ensure(
94+
conn.block_storage.get_volume(volume_id) is not None,
95+
f"Retrieving initial volume by ID '{volume_id}' failed"
7896
)
7997

8098
print(
@@ -85,7 +103,8 @@ def test_backup(conn: openstack.connection.Connection,
85103
while conn.block_storage.get_volume(volume_id).status != "available":
86104
time.sleep(1.0)
87105
seconds_waited += 1
88-
assert seconds_waited < timeout, (
106+
ensure(
107+
seconds_waited < timeout,
89108
f"Timeout reached while waiting for volume to reach status "
90109
f"'available' (volume id: {volume_id}) after {seconds_waited} "
91110
f"seconds"
@@ -98,11 +117,13 @@ def test_backup(conn: openstack.connection.Connection,
98117
name=f"{prefix}volume-backup",
99118
volume_id=volume_id
100119
)
101-
assert backup is not None, (
120+
ensure(
121+
backup is not None,
102122
"Backup creation failed"
103123
)
104124
backup_id = backup.id
105-
assert conn.block_storage.get_backup(backup_id) is not None, (
125+
ensure(
126+
conn.block_storage.get_backup(backup_id) is not None,
106127
"Retrieving backup by ID failed"
107128
)
108129

@@ -111,16 +132,17 @@ def test_backup(conn: openstack.connection.Connection,
111132
while conn.block_storage.get_backup(backup_id).status != "available":
112133
time.sleep(1.0)
113134
seconds_waited += 1
114-
assert seconds_waited < timeout, (
135+
ensure(
136+
seconds_waited < timeout,
115137
f"Timeout reached while waiting for backup to reach status "
116138
f"'available' (backup id: {backup_id}) after {seconds_waited} "
117139
f"seconds"
118140
)
119141
print("Create backup from volume: PASS")
120142

121143
# RESTORE BACKUP
122-
print("Restoring backup to volume ...")
123144
restored_volume_name = f"{prefix}restored-backup"
145+
print(f"Restoring backup to volume '{restored_volume_name}' ...")
124146
conn.block_storage.restore_backup(
125147
backup_id,
126148
name=restored_volume_name
@@ -134,7 +156,8 @@ def test_backup(conn: openstack.connection.Connection,
134156
while conn.block_storage.find_volume(restored_volume_name) is None:
135157
time.sleep(1.0)
136158
seconds_waited += 1
137-
assert seconds_waited < timeout, (
159+
ensure(
160+
seconds_waited < timeout,
138161
f"Timeout reached while waiting for restored volume to be created "
139162
f"(volume name: {restored_volume_name}) after {seconds_waited} "
140163
f"seconds"
@@ -148,7 +171,8 @@ def test_backup(conn: openstack.connection.Connection,
148171
while conn.block_storage.get_volume(volume_id).status != "available":
149172
time.sleep(1.0)
150173
seconds_waited += 1
151-
assert seconds_waited < timeout, (
174+
ensure(
175+
seconds_waited < timeout,
152176
f"Timeout reached while waiting for restored volume reach status "
153177
f"'available' (volume id: {volume_id}) after {seconds_waited} "
154178
f"seconds"
@@ -170,9 +194,10 @@ def wait_for_resource(resource_type: str, resource_id: str,
170194
while get_func(resource_id).status not in expected_status:
171195
time.sleep(1.0)
172196
seconds_waited += 1
173-
assert seconds_waited < timeout, (
197+
ensure(
198+
seconds_waited < timeout,
174199
f"Timeout reached while waiting for {resource_type} during "
175-
f"cleanup to be in status '{expected_status}' "
200+
f"cleanup to be in status {expected_status} "
176201
f"({resource_type} id: {resource_id}) after {seconds_waited} "
177202
f"seconds"
178203
)
@@ -200,7 +225,8 @@ def wait_for_resource(resource_type: str, resource_id: str,
200225
) > 0:
201226
time.sleep(1.0)
202227
seconds_waited += 1
203-
assert seconds_waited < timeout, (
228+
ensure(
229+
seconds_waited < timeout,
204230
f"Timeout reached while waiting for all backups with prefix "
205231
f"'{prefix}' to finish deletion"
206232
)
@@ -262,10 +288,11 @@ def main():
262288
cloud = os.environ.get("OS_CLOUD", None)
263289
if args.os_cloud:
264290
cloud = args.os_cloud
265-
assert cloud, (
266-
"You need to have the OS_CLOUD environment variable set to your "
267-
"cloud name or pass it via --os-cloud"
268-
)
291+
if not cloud:
292+
raise Exception(
293+
"You need to have the OS_CLOUD environment variable set to your "
294+
"cloud name or pass it via --os-cloud"
295+
)
269296
conn = connect(
270297
cloud,
271298
password=getpass.getpass("Enter password: ") if args.ask else None

0 commit comments

Comments
 (0)