0

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?

  • Use fixtures. You can `yield` them, then do the cleanup after the test. See [Run code before and after each test in py.test?](https://stackoverflow.com/q/22627659/2745495) – Gino Mempin Sep 30 '21 at 00:41
  • @GinoMempin i understand that yield has to be used for cleaning but how can i use fixture- i want to remove this line assert delete_product and use fixture instaed, please help- you can see my updated question above – Sonal Kakkar Chawla Sep 30 '21 at 03:53

0 Answers0