How to clean up the leftovers using pytest fixtures yield?

1.1k Views Asked by At

Currently if test will fail from any reason the objects which were created in AWS service catalog(SC) can stay there after test is finished due all failed asserts stop script, so clean few lines after cant be invoked.

Example of code:

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

how can we make a solution to clean remains even if any assertion is failed using pytest fixtures?

0

There are 0 best solutions below