Skip to content

Commit d637f58

Browse files
Attempt to continue cleanup even after timeouts
Signed-off-by: Markus Hentsch <[email protected]>
1 parent 8a68539 commit d637f58

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

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

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,12 @@ def test_backup(conn: openstack.connection.Connection,
181181

182182

183183
def cleanup(conn: openstack.connection.Connection, prefix=DEFAULT_PREFIX,
184-
timeout=WAIT_TIMEOUT):
184+
timeout=WAIT_TIMEOUT) -> bool:
185185
"""
186186
Looks up volume and volume backup resources matching the given prefix and
187187
deletes them.
188+
Returns False if there were any errors during cleanup which might leave
189+
resources behind. Otherwise returns True to indicate cleanup success.
188190
"""
189191

190192
def wait_for_resource(resource_type: str, resource_id: str,
@@ -205,6 +207,7 @@ def wait_for_resource(resource_type: str, resource_id: str,
205207
print(f"\nPerforming cleanup for resources with the "
206208
f"'{prefix}' prefix ...")
207209

210+
cleanup_was_successful = True
208211
backups = conn.block_storage.backups()
209212
for backup in backups:
210213
if backup.name.startswith(prefix):
@@ -214,8 +217,11 @@ def wait_for_resource(resource_type: str, resource_id: str,
214217
# if the resource has vanished on
215218
# its own in the meantime ignore it
216219
continue
217-
print(f"↳ deleting volume backup '{backup.id}' ...")
218-
conn.block_storage.delete_backup(backup.id)
220+
except ConformanceTestException as e:
221+
print("WARNING:", str(e))
222+
else:
223+
print(f"↳ deleting volume backup '{backup.id}' ...")
224+
conn.block_storage.delete_backup(backup.id)
219225

220226
# wait for all backups to be cleaned up before attempting to remove volumes
221227
seconds_waited = 0
@@ -225,11 +231,17 @@ def wait_for_resource(resource_type: str, resource_id: str,
225231
) > 0:
226232
time.sleep(1.0)
227233
seconds_waited += 1
228-
ensure(
229-
seconds_waited < timeout,
230-
f"Timeout reached while waiting for all backups with prefix "
231-
f"'{prefix}' to finish deletion"
232-
)
234+
try:
235+
ensure(
236+
seconds_waited < timeout,
237+
f"Timeout reached while waiting for all backups with prefix "
238+
f"'{prefix}' to finish deletion during cleanup after "
239+
f"{seconds_waited} seconds"
240+
)
241+
except ConformanceTestException as e:
242+
print("WARNING:", str(e))
243+
cleanup_was_successful = False
244+
break
233245

234246
volumes = conn.block_storage.volumes()
235247
for volume in volumes:
@@ -240,8 +252,14 @@ def wait_for_resource(resource_type: str, resource_id: str,
240252
# if the resource has vanished on
241253
# its own in the meantime ignore it
242254
continue
243-
print(f"↳ deleting volume '{volume.id}' ...")
244-
conn.block_storage.delete_volume(volume.id)
255+
except ConformanceTestException as e:
256+
print("WARNING:", str(e))
257+
cleanup_was_successful = False
258+
else:
259+
print(f"↳ deleting volume '{volume.id}' ...")
260+
conn.block_storage.delete_volume(volume.id)
261+
262+
return cleanup_was_successful
245263

246264

247265
def main():
@@ -298,13 +316,21 @@ def main():
298316
password=getpass.getpass("Enter password: ") if args.ask else None
299317
)
300318
if args.cleanup_only:
301-
cleanup(conn, prefix=args.prefix, timeout=args.timeout)
319+
if not cleanup(conn, prefix=args.prefix, timeout=args.timeout):
320+
raise Exception(
321+
f"Cleanup was not successful, there may be leftover resources "
322+
f"with the '{args.prefix}' prefix"
323+
)
302324
else:
303325
cleanup(conn, prefix=args.prefix, timeout=args.timeout)
304326
try:
305327
test_backup(conn, prefix=args.prefix, timeout=args.timeout)
306328
finally:
307-
cleanup(conn, prefix=args.prefix, timeout=args.timeout)
329+
if not cleanup(conn, prefix=args.prefix, timeout=args.timeout):
330+
print(
331+
f"There may be leftover resources with the "
332+
f"'{args.prefix}' prefix that could not be cleaned up!"
333+
)
308334

309335

310336
if __name__ == "__main__":

0 commit comments

Comments
 (0)