From 788991a76da1fb32bf9dd5f563ca04f923cd72a5 Mon Sep 17 00:00:00 2001
From: Guilherme Costa <guilherme.costa@windriver.com>
Date: Tue, 30 Jan 2024 15:25:23 -0300
Subject: [PATCH] Adding semantic check on platform-integ-apps reapply
 operation

The reapply operation needs to do the same checks done on the apply
operation. These checks prevent reapplying the application on
some ceph or system scenarios.

Moreover, on the method pre_auto_apply_check, it is possible that
conductor_object._ceph (CephOperator) is not initialized yet, and trying
to access its methods will raise an AttributeError exception. As this is
a possible scenario, this case is now tested, and a
LifecycleSemanticCheckException is raised.

Test Plan:

    PASS: (AIO-DX) Check that platform-integ-apps is on uploaded state
    PASS: (AIO-DX) Unmanage ceph-mon
    PASS: (AIO-DX) Stop ceph-mon service
    PASS: (AIO-DX) Restart sysinv-conductor service
    PASS: (AIO-DX) Check on sysinv logs that the new Exception is
          being logged and the platform-integ-apps still trying to
          be applied
    PASS: (AIO-DX) Manage ceph-mon
    PASS: (AIO-DX) Start ceph-mon service
    PASS: (AIO-DX) After the Ceph cluster has become responsive,
          check that platform-integ-apps is being successfully
          applied.

    PASS: (AIO-DX) Check that platform-integ-apps is on applied state
    PASS: (AIO-DX) Unmanage ceph-mon
    PASS: (AIO-DX) Stop ceph-mon service
    PASS: (AIO-DX) Restart sysinv-conductor service
    PASS: (AIO-DX) Check on sysinv logs that the new Exception is
          being logged and the platform-integ-apps still trying
          to be applied.
    PASS: (AIO-DX) Wait until platform-integ-apps is on
          apply-failed state

Closes-bug: 2052315

Change-Id: I8c3318cc6f002671cb2e4bf81b14238ac4467131
Signed-off-by: Guilherme Costa <guilherme.costa@windriver.com>
---
 .../k8sapp_platform/lifecycle/lifecycle_platform.py      | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/python3-k8sapp-platform/k8sapp_platform/k8sapp_platform/lifecycle/lifecycle_platform.py b/python3-k8sapp-platform/k8sapp_platform/k8sapp_platform/lifecycle/lifecycle_platform.py
index 7a64055..29cae0a 100644
--- a/python3-k8sapp-platform/k8sapp_platform/k8sapp_platform/lifecycle/lifecycle_platform.py
+++ b/python3-k8sapp-platform/k8sapp_platform/k8sapp_platform/lifecycle/lifecycle_platform.py
@@ -38,7 +38,8 @@ class PlatformAppLifecycleOperator(base.AppLifecycleOperator):
         # Semantic checks
         if hook_info.lifecycle_type == constants.APP_LIFECYCLE_TYPE_SEMANTIC_CHECK:
             if hook_info.mode == constants.APP_LIFECYCLE_MODE_AUTO and \
-                    hook_info.operation == constants.APP_APPLY_OP and \
+                    hook_info.operation in [constants.APP_APPLY_OP,
+                                            constants.APP_EVALUATE_REAPPLY_OP] and \
                     hook_info.relative_timing == constants.APP_LIFECYCLE_TIMING_PRE:
                 return self.pre_auto_apply_check(conductor_obj)
 
@@ -83,6 +84,12 @@ class PlatformAppLifecycleOperator(base.AppLifecycleOperator):
         if not os.path.isfile(crushmap_flag_file):
             raise exception.LifecycleSemanticCheckException(
                 "Crush map not applied")
+        # conductor_obj._ceph (CephOperator) may not be initialized
+        # at this point, as it depends on ceph and system conditions
+        # to start the thread that initializes it
+        if conductor_obj._ceph is None:
+            raise exception.LifecycleSemanticCheckException(
+                "CephOperator is not initialized yet")
         if not conductor_obj._ceph.have_ceph_monitor_access():
             raise exception.LifecycleSemanticCheckException(
                 "Monitor access error")