From 471811ae642c0ed50499fee34b0dd96dbbaea5a8 Mon Sep 17 00:00:00 2001
From: Utkarsh Bhatt <utkarsh.bhatt@canonical.com>
Date: Mon, 15 Apr 2024 15:30:08 +0530
Subject: [PATCH] Adds decision logic for action result code.

Signed-off-by: Utkarsh Bhatt <utkarsh.bhatt@canonical.com>
---
 .../charm_tests/ceph/rbd_mirror/tests.py      | 22 ++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/zaza/openstack/charm_tests/ceph/rbd_mirror/tests.py b/zaza/openstack/charm_tests/ceph/rbd_mirror/tests.py
index fc7be505f..ab8d143ab 100644
--- a/zaza/openstack/charm_tests/ceph/rbd_mirror/tests.py
+++ b/zaza/openstack/charm_tests/ceph/rbd_mirror/tests.py
@@ -172,6 +172,18 @@ def setup_rbd_mirror():
     )
 
 
+def get_action_result(result: dict) -> str:
+    """Get action result code."""
+    if result.get('Code', None):
+        return result['Code']
+    elif result.get('return-code', None):
+        return result['return-code']
+    else:
+        # default for non-zero return code.
+        logging.error("No suitable return code found in action result.")
+        return "-1"
+
+
 class CephRBDMirrorBase(test_utils.BaseCharmTest):
     """Base class for ``ceph-rbd-mirror`` tests."""
 
@@ -503,7 +515,7 @@ def execute_failover_juju_actions(self,
                 'pools': ','.join(primary_site_pools)
             })
         logging.info(result.results)
-        self.assertEqual(int(result.results['Code']), 0)
+        self.assertEqual(int(get_action_result(result.results)), 0)
 
         # Validate that the demoted pools count matches the total primary site
         # pools count.
@@ -537,7 +549,7 @@ def execute_failover_juju_actions(self,
                 'pools': ','.join(secondary_site_pools)
             })
         logging.info(result.results)
-        self.assertEqual(int(result.results['Code']), 0)
+        self.assertEqual(int(get_action_result(result.results)), 0)
 
         # Validate that the promoted pools count matches the total secondary
         # site pools count.
@@ -711,7 +723,7 @@ def test_203_juju_resync(self):
                 'i-really-mean-it': True,
             })
         logging.info(result.results)
-        self.assertEqual(int(result.results['Code']), 0)
+        self.assertEqual(int(get_action_result(result.results)), 0)
 
         # Validate that the Ceph images from site-b report 'up+replaying'
         # (which is reported by secondary Ceph images). And check that images
@@ -801,7 +813,7 @@ def test_100_forced_juju_failover(self):
             action_params={
                 'pools': ','.join(site_b_pools),
             })
-        self.assertEqual(int(result.results['Code']), 0)
+        self.assertEqual(int(get_action_result(result.results)), 0)
 
         # The action may not show up as 'failed' if there are no pools that
         # needed to be promoted.
@@ -816,7 +828,7 @@ def test_100_forced_juju_failover(self):
                 'force': True,
                 'pools': ','.join(site_b_pools),
             })
-        self.assertEqual(int(result.results['Code']), 0)
+        self.assertEqual(int(get_action_result(result.results)), 0)
 
         # Validate successful Juju action execution
         self.assertEqual(result.status, 'completed')