Fix check_worfklow_status to add additional condition regarding status.phase

Change-Id: I539ca5506cd44dad791844c1f5611ac530a5fbbf
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
diff --git a/osm_lcm/odu_libs/workflows.py b/osm_lcm/odu_libs/workflows.py
index 1b5d5cc..84a2b9e 100644
--- a/osm_lcm/odu_libs/workflows.py
+++ b/osm_lcm/odu_libs/workflows.py
@@ -26,7 +26,8 @@
     if not workflow_name:
         return False, "Workflow was not launched"
     try:
-        return await self.readiness_loop(
+        # First check if the workflow ends successfully
+        completed, message = await self.readiness_loop(
             item="workflow",
             name=workflow_name,
             namespace="osm-workflows",
@@ -37,8 +38,23 @@
             deleted=False,
             timeout=300,
         )
+        if completed:
+            # Then check if the workflow has a failed task
+            return await self.readiness_loop(
+                item="workflow",
+                name=workflow_name,
+                namespace="osm-workflows",
+                condition={
+                    "jsonpath_filter": "status.phase",
+                    "value": "Succeeded",
+                },
+                deleted=False,
+                timeout=0,
+            )
+        else:
+            return False, f"Workflow was not completed: {message}"
     except Exception as e:
-        return False, f"Unexpected exception: {e}"
+        return False, f"Workflow could not be completed. Unexpected exception: {e}"
 
 
 async def readiness_loop(
@@ -90,6 +106,8 @@
     counter = 1
     retry_time = self._odu_checkloop_retry_time
     max_iterations = ceil(timeout / retry_time)
+    if max_iterations < 1:
+        max_iterations = 1
     api_group = item_api_map[item]["api_group"]
     api_plural = item_api_map[item]["api_plural"]
     api_version = item_api_map[item]["api_version"]
@@ -150,7 +168,8 @@
                     )
         except Exception as e:
             self.logger.error(f"Exception: {e}")
-        await asyncio.sleep(retry_time)
+        if counter < max_iterations:
+            await asyncio.sleep(retry_time)
         counter += 1
     return (
         False,