Skip to content

Commit f410291

Browse files
Change from print to logging
Signed-off-by: Markus Hentsch <[email protected]>
1 parent 51d839b commit f410291

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

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

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818
import time
1919
import typing
20+
import logging
2021

2122
import openstack
2223

@@ -80,7 +81,7 @@ def test_backup(conn: openstack.connection.Connection,
8081

8182
# CREATE VOLUME
8283
volume_name = f"{prefix}volume"
83-
print(f"Creating volume '{volume_name}' ...")
84+
logging.info(f"Creating volume '{volume_name}' ...")
8485
volume = conn.block_storage.create_volume(
8586
name=volume_name,
8687
size=1
@@ -95,7 +96,7 @@ def test_backup(conn: openstack.connection.Connection,
9596
f"Retrieving initial volume by ID '{volume_id}' failed"
9697
)
9798

98-
print(
99+
logging.info(
99100
f"↳ waiting for volume with ID '{volume_id}' to reach status "
100101
f"'available' ..."
101102
)
@@ -109,10 +110,10 @@ def test_backup(conn: openstack.connection.Connection,
109110
f"'available' (volume id: {volume_id}) after {seconds_waited} "
110111
f"seconds"
111112
)
112-
print("Create empty volume: PASS")
113+
logging.info("Create empty volume: PASS")
113114

114115
# CREATE BACKUP
115-
print("Creating backup from volume ...")
116+
logging.info("Creating backup from volume ...")
116117
backup = conn.block_storage.create_backup(
117118
name=f"{prefix}volume-backup",
118119
volume_id=volume_id
@@ -127,7 +128,7 @@ def test_backup(conn: openstack.connection.Connection,
127128
"Retrieving backup by ID failed"
128129
)
129130

130-
print(f"↳ waiting for backup '{backup_id}' to become available ...")
131+
logging.info(f"↳ waiting for backup '{backup_id}' to become available ...")
131132
seconds_waited = 0
132133
while conn.block_storage.get_backup(backup_id).status != "available":
133134
time.sleep(1.0)
@@ -138,17 +139,17 @@ def test_backup(conn: openstack.connection.Connection,
138139
f"'available' (backup id: {backup_id}) after {seconds_waited} "
139140
f"seconds"
140141
)
141-
print("Create backup from volume: PASS")
142+
logging.info("Create backup from volume: PASS")
142143

143144
# RESTORE BACKUP
144145
restored_volume_name = f"{prefix}restored-backup"
145-
print(f"Restoring backup to volume '{restored_volume_name}' ...")
146+
logging.info(f"Restoring backup to volume '{restored_volume_name}' ...")
146147
conn.block_storage.restore_backup(
147148
backup_id,
148149
name=restored_volume_name
149150
)
150151

151-
print(
152+
logging.info(
152153
f"↳ waiting for restoration target volume '{restored_volume_name}' "
153154
f"to be created ..."
154155
)
@@ -163,7 +164,7 @@ def test_backup(conn: openstack.connection.Connection,
163164
f"seconds"
164165
)
165166
# wait for the volume restoration to finish
166-
print(
167+
logging.info(
167168
f"↳ waiting for restoration target volume '{restored_volume_name}' "
168169
f"to reach 'available' status ..."
169170
)
@@ -177,7 +178,7 @@ def test_backup(conn: openstack.connection.Connection,
177178
f"'available' (volume id: {volume_id}) after {seconds_waited} "
178179
f"seconds"
179180
)
180-
print("Restore volume from backup: PASS")
181+
logging.info("Restore volume from backup: PASS")
181182

182183

183184
def cleanup(conn: openstack.connection.Connection, prefix=DEFAULT_PREFIX,
@@ -204,8 +205,8 @@ def wait_for_resource(resource_type: str, resource_id: str,
204205
f"seconds"
205206
)
206207

207-
print(f"\nPerforming cleanup for resources with the "
208-
f"'{prefix}' prefix ...")
208+
logging.info(f"Performing cleanup for resources with the "
209+
f"'{prefix}' prefix ...")
209210

210211
cleanup_was_successful = True
211212
backups = conn.block_storage.backups()
@@ -218,9 +219,9 @@ def wait_for_resource(resource_type: str, resource_id: str,
218219
# its own in the meantime ignore it
219220
continue
220221
except ConformanceTestException as e:
221-
print("WARNING:", str(e))
222+
logging.warning(str(e))
222223
else:
223-
print(f"↳ deleting volume backup '{backup.id}' ...")
224+
logging.info(f"↳ deleting volume backup '{backup.id}' ...")
224225
conn.block_storage.delete_backup(backup.id)
225226

226227
# wait for all backups to be cleaned up before attempting to remove volumes
@@ -233,9 +234,8 @@ def wait_for_resource(resource_type: str, resource_id: str,
233234
seconds_waited += 1
234235
if seconds_waited >= timeout:
235236
cleanup_was_successful = False
236-
print(
237-
"WARNING:"
238-
"Timeout reached while waiting for all backups with prefix "
237+
logging.warning(
238+
f"Timeout reached while waiting for all backups with prefix "
239239
f"'{prefix}' to finish deletion during cleanup after "
240240
f"{seconds_waited} seconds"
241241
)
@@ -251,10 +251,10 @@ def wait_for_resource(resource_type: str, resource_id: str,
251251
# its own in the meantime ignore it
252252
continue
253253
except ConformanceTestException as e:
254-
print("WARNING:", str(e))
254+
logging.warning(str(e))
255255
cleanup_was_successful = False
256256
else:
257-
print(f"↳ deleting volume '{volume.id}' ...")
257+
logging.info(f"↳ deleting volume '{volume.id}' ...")
258258
conn.block_storage.delete_volume(volume.id)
259259

260260
return cleanup_was_successful
@@ -299,6 +299,10 @@ def main():
299299
)
300300
args = parser.parse_args()
301301
openstack.enable_logging(debug=args.debug)
302+
logging.basicConfig(
303+
format="%(levelname)s: %(message)s",
304+
level=logging.DEBUG if args.debug else logging.INFO,
305+
)
302306

303307
# parse cloud name for lookup in clouds.yaml
304308
cloud = os.environ.get("OS_CLOUD", None)
@@ -325,7 +329,7 @@ def main():
325329
test_backup(conn, prefix=args.prefix, timeout=args.timeout)
326330
finally:
327331
if not cleanup(conn, prefix=args.prefix, timeout=args.timeout):
328-
print(
332+
logging.info(
329333
f"There may be leftover resources with the "
330334
f"'{args.prefix}' prefix that could not be cleaned up!"
331335
)

0 commit comments

Comments
 (0)