Skip to content

Commit 4570296

Browse files
committed
Use finally: for credential cleanup.
Signed-off-by: Kurt Garloff <[email protected]>
1 parent 80beff4 commit 4570296

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

Tests/iaas/mandatory-services/mandatory-iaas-services.py

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -176,43 +176,45 @@ def check_for_s3_and_swift(conn: openstack.connection.Connection, s3_credentials
176176
)
177177
return 1
178178
# Get S3 endpoint (swift) and ec2 creds from OpenStack (keystone)
179-
ec2_cred = s3_from_ostack(s3_creds, conn, endpoint)
180-
# Overrides (var names are from libs3, in case you wonder)
181-
s3_from_env(s3_creds, "HOST", "S3_HOSTNAME", "https://")
182-
s3_from_env(s3_creds, "AK", "S3_ACCESS_KEY_ID")
183-
s3_from_env(s3_creds, "SK", "S3_SECRET_ACCESS_KEY")
184-
185-
# This is to be used for local debugging purposes ONLY
186-
# logger.info(f"using credentials {s3_creds}")
187-
188-
s3 = s3_conn(s3_creds, conn)
189-
s3_buckets = list_s3_buckets(s3) or create_bucket(s3, TESTCONTNAME)
190-
if not s3_buckets:
191-
raise RuntimeError("failed to create S3 bucket")
192-
193-
# If we got till here, s3 is working, now swift
194-
swift_containers = list_containers(conn)
195-
# if not swift_containers:
196-
# swift_containers = create_container(conn, TESTCONTNAME)
197-
result = 0
198-
# Compare number of buckets/containers
199-
# FIXME: Could compare list of sorted names
200-
if Counter(s3_buckets) != Counter(swift_containers):
201-
logger.error("S3 buckets and Swift Containers differ:\n"
202-
f"S3: {sorted(s3_buckets)}\nSW: {sorted(swift_containers)}")
203-
result = 1
204-
else:
205-
logger.info("SUCCESS: S3 and Swift exist and agree")
206-
# No need to clean up swift container, as we did not create one
207-
# (If swift and S3 agree, there will be a S3 bucket that we clean up with S3.)
208-
# if swift_containers == [TESTCONTNAME]:
209-
# del_container(conn, TESTCONTNAME)
210-
# Cleanup created S3 bucket
211-
if s3_buckets == [TESTCONTNAME]:
212-
del_bucket(s3, TESTCONTNAME)
213-
# Clean up ec2 cred IF we created one
214-
if ec2_cred:
215-
conn.identity.delete_credential(ec2_cred)
179+
try:
180+
ec2_cred = s3_from_ostack(s3_creds, conn, endpoint)
181+
# Overrides (var names are from libs3, in case you wonder)
182+
s3_from_env(s3_creds, "HOST", "S3_HOSTNAME", "https://")
183+
s3_from_env(s3_creds, "AK", "S3_ACCESS_KEY_ID")
184+
s3_from_env(s3_creds, "SK", "S3_SECRET_ACCESS_KEY")
185+
186+
# This is to be used for local debugging purposes ONLY
187+
# logger.info(f"using credentials {s3_creds}")
188+
189+
s3 = s3_conn(s3_creds, conn)
190+
s3_buckets = list_s3_buckets(s3) or create_bucket(s3, TESTCONTNAME)
191+
if not s3_buckets:
192+
raise RuntimeError("failed to create S3 bucket")
193+
194+
# If we got till here, s3 is working, now swift
195+
swift_containers = list_containers(conn)
196+
# if not swift_containers:
197+
# swift_containers = create_container(conn, TESTCONTNAME)
198+
result = 0
199+
# Compare number of buckets/containers
200+
# FIXME: Could compare list of sorted names
201+
if Counter(s3_buckets) != Counter(swift_containers):
202+
logger.error("S3 buckets and Swift Containers differ:\n"
203+
f"S3: {sorted(s3_buckets)}\nSW: {sorted(swift_containers)}")
204+
result = 1
205+
else:
206+
logger.info("SUCCESS: S3 and Swift exist and agree")
207+
# No need to clean up swift container, as we did not create one
208+
# (If swift and S3 agree, there will be a S3 bucket that we clean up with S3.)
209+
# if swift_containers == [TESTCONTNAME]:
210+
# del_container(conn, TESTCONTNAME)
211+
# Cleanup created S3 bucket
212+
if s3_buckets == [TESTCONTNAME]:
213+
del_bucket(s3, TESTCONTNAME)
214+
# Clean up ec2 cred IF we created one
215+
finally:
216+
if ec2_cred:
217+
conn.identity.delete_credential(ec2_cred)
216218
return result
217219

218220

0 commit comments

Comments
 (0)