目前,如果测试因任何原因而失败,则在测试完成后,由于所有失败的断言停止脚本,在 AWS 服务目录(SC)中创建的对象可以保留在那里,因此在无法调用后清理几行。
代码示例:
product_name, result = launch_product(role, product, storagerole)
logger.info("Creating pod with storagerole: {}".format(storagerole))
assert result, 'Product ' + product + ' could not be launched by role ' + role + ' assuming role ' + storagerole
# Get part of pod unique name
for key in client.get_provisioned_product_outputs(ProvisionedProductName=product_name)["Outputs"]:
if key["OutputKey"] == 'SSH':
pod_unique_id = key["OutputValue"].split('-')[1]
# Pick up pod with selected unique name
querypod = "kubectl get po -n rstudio | grep " + pod_unique_id + " | awk 'END {print $1}'| tr -d '\n'"
launched_pod = subprocess.check_output(querypod, shell=True).decode()
logger.info("Checking pod: {}".format(launched_pod))
cmd = "kubectl -n rstudio exec " + launched_pod + " -- aws sts get-caller-identity"
try:
output = subprocess.check_output(cmd, shell=True).decode()
except subprocess.CalledProcessError as error:
logger.error("error: {}".format(error))
assert delete_product(role, product_name), 'Product ' + product_name + ' could not be deleted by role ' + role
assert False, error
try:
assert "assumed-role/" + storagerole + "/kiam-kiam" in output, 'Expected role ' + storagerole + ' was not assumed within container'
except AssertionError as error:
logger.error("error: {}".format(error))
assert delete_product(role, product_name), 'Product ' + product_name + ' could not be deleted by role ' + role
assert False, error
logger.info("All steps passed, deleting pod: {}".format(launched_pod))
assert delete_product(role, product_name), 'Product ' + product_name + ' could not be deleted by role ' + role
即使使用 pytest 固定装置的任何断言失败,我们如何才能找到清理残留物的解决方案?