From 389a773d49664eb7518c690ac4795f3a149e8755 Mon Sep 17 00:00:00 2001
From: Matthew Mahoney <mmahoney@redhat.com>
Date: Wed, 3 Apr 2024 07:48:58 -0400
Subject: [PATCH] RHOAIENG-2519 Service Mesh Test Case (#1336)

* RHOAIENG-2519 - New test case for Service Mesh Integration

* RHOAIENG-2519 - Moved Reusable Keywords to commmon resouces

* RHOAIENG-2519 - Extend wait for Unmanaged

* RHOAIENG-2519 - Addressed review feedback

* RHOAIENG-2519 - changed tag to Tier1
---
 ods_ci/tests/Resources/OCP.resource           | 25 +++++
 ods_ci/tests/Resources/ODS.robot              | 16 +++-
 .../139__trusted_ca_bundles.robot             | 39 +-------
 .../140__service_mesh.robot                   | 96 +++++++++++++++++++
 4 files changed, 138 insertions(+), 38 deletions(-)
 create mode 100644 ods_ci/tests/Tests/100__deploy/130__operators/130__rhods_operator/140__service_mesh.robot

diff --git a/ods_ci/tests/Resources/OCP.resource b/ods_ci/tests/Resources/OCP.resource
index c91ced17c..f76f0d736 100644
--- a/ods_ci/tests/Resources/OCP.resource
+++ b/ods_ci/tests/Resources/OCP.resource
@@ -200,3 +200,28 @@ Wait Until CRD Exists
     [Arguments]    ${crd_fullname}
     Wait Until Keyword Succeeds    15 times    5s
     ...    Oc Get    kind=CustomResourceDefinition    name=${crd_fullname}
+
+Is Resource Present
+    [Documentation]    Check If Resource Is Present In Namespace
+    [Arguments]    ${resource}    ${resource_name}    ${namespace}    ${expected_result}
+    ${rc}=    Run And Return Rc
+    ...    oc get ${resource} ${resource_name} -n ${namespace}
+    Should Be Equal    "${rc}"    "${expected_result}"    msg=${resource} does not exist in ${namespace}
+
+Create Namespace In Openshift
+    [Documentation]    Create a new namespace if it does not already exist
+    [Arguments]    ${namespace}
+    ${rc}   ${output}=    Run And Return Rc And Output    oc get project ${namespace}
+    IF    ${rc} != 0
+        ${rc}=     Run And Return Rc    oc new-project ${namespace}
+        Should Be Equal    "${rc}"    "0"   msg=${output}
+    END
+
+Delete Namespace From Openshift
+    [Documentation]    Delete namespace from opneshift if it exists
+    [Arguments]    ${namespace}
+    ${rc}   ${output}=    Run And Return Rc And Output    oc get project ${namespace}
+    IF    ${rc} == 0
+        ${rc}   ${output}=    Run And Return Rc And Output    oc delete project ${namespace}
+        Should Be Equal    "${rc}"    "0"   msg=${output}
+    END
diff --git a/ods_ci/tests/Resources/ODS.robot b/ods_ci/tests/Resources/ODS.robot
index 14122fedf..557f3360d 100644
--- a/ods_ci/tests/Resources/ODS.robot
+++ b/ods_ci/tests/Resources/ODS.robot
@@ -102,7 +102,7 @@ Set Access Groups Settings
     [Documentation]    Changes the rhods-groups config map to set the new access configuration
     [Arguments]     ${admins_group}   ${users_group}
     ${return_code}    ${output}    Run And Return Rc And Output    oc patch OdhDashboardConfig odh-dashboard-config -n ${APPLICATIONS_NAMESPACE} --type=merge -p '{"spec": {"groupsConfig": {"adminGroups": "${admins_group}","allowedGroups": "${users_group}"}}}'   #robocop:disable
-    Should Be Equal As Integers	${return_code}	 0    msg=Patch to group settings failed
+    Should Be Equal As Integers        ${return_code}   0    msg=Patch to group settings failed
 
 Set Default Access Groups Settings
     [Documentation]    Restores the default rhods-groups config map
@@ -389,3 +389,17 @@ Delete RHODS Config Map
     ...                corresponding argument
     [Arguments]     ${name}  ${namespace}=${APPLICATIONS_NAMESPACE}
     OpenShiftLibrary.Oc Delete    kind=ConfigMap  name=${name}  namespace=${namespace}
+
+Wait Until Operator Ready
+    [Documentation]    Wait for operator to be available/ready
+    [Arguments]    ${operator_name}    ${namespace}    ${wait_time}=2m
+    ${rc}   ${output}=    Run And Return Rc And Output
+    ...    oc wait --timeout=${wait_time} --for condition=available -n ${namespace} deploy/${operator_name}
+    Should Be Equal    "${rc}"    "0"    msg=${output}
+
+Wait For DSCI Ready State
+    [Documentation]    Wait for DSCI to reconciled to be complete
+    [Arguments]    ${dsci}    ${namespace}    ${wait_time}=2m
+    ${rc}   ${output}=    Run And Return Rc And Output
+    ...    oc wait --timeout=${wait_time} --for jsonpath='{.status.conditions[].reason}'=ReconcileCompleted -n ${namespace} dsci ${dsci}
+    Should Be Equal    "${rc}"    "0"     msg=${output}
diff --git a/ods_ci/tests/Tests/100__deploy/130__operators/130__rhods_operator/139__trusted_ca_bundles.robot b/ods_ci/tests/Tests/100__deploy/130__operators/130__rhods_operator/139__trusted_ca_bundles.robot
index 0ba8d82d5..e1b20a61c 100644
--- a/ods_ci/tests/Tests/100__deploy/130__operators/130__rhods_operator/139__trusted_ca_bundles.robot
+++ b/ods_ci/tests/Tests/100__deploy/130__operators/130__rhods_operator/139__trusted_ca_bundles.robot
@@ -1,7 +1,8 @@
 *** Settings ***
 Documentation    Test Cases to verify Trusted CA Bundle support
 Library    Collections
-Resource    ../../../../Resources/Page/OCPDashboard/OCPDashboard.resource
+Resource       ../../../../Resources/OCP.resource
+Resource       ../../../../Resources/ODS.robot
 Suite Setup    Suite Setup
 Suite Teardown    Suite Teardown
 
@@ -87,20 +88,6 @@ Restore DSCI Trusted CA Bundle Settings
     Set Custom CA Bundle Value In DSCI    ${DSCI_NAME}   ''    ${OPERATOR_NS}
     Set Trusted CA Bundle Management State    ${DSCI_NAME}    Managed    ${OPERATOR_NS}
 
-Wait Until Operator Ready
-    [Documentation]    Checks if operator is available/ready
-    [Arguments]    ${operator_name}    ${namespace}
-    ${rc}   ${output}=    Run And Return Rc And Output
-    ...    oc wait --timeout=2m --for condition=available -n ${namespace} deploy/${operator_name}
-    Should Be Equal    "${rc}"    "0"    msg=${output}
-
-Is Resource Present
-    [Documentation]    Check if resource is present in namespace
-    [Arguments]       ${resource}     ${resource_name}    ${namespace}    ${expected_result}
-    ${rc}   ${output}=    Run And Return Rc And Output
-    ...  oc get ${resource} ${resource_name} -n ${namespace}
-    Should Be Equal    "${rc}"    "${expected_result}"    msg=${output}
-
 Is CA Bundle Value Present
     [Documentation]    Check if the ConfigtMap contains Custom CA Bundle value
     [Arguments]    ${config_map}    ${custom_ca_bundle_value}    ${namespace}        ${expected_result}
@@ -108,13 +95,6 @@ Is CA Bundle Value Present
     ...    oc get configmap ${config_map} -n ${namespace} -o yaml | grep ${custom_ca_bundle_value}
     Should Be Equal    "${rc}"    "${expected_result}"    msg=${output}
 
-Wait For DSCI Ready State
-    [Documentation]    Checks that DSCI reconciled succesfully
-    [Arguments]    ${dsci}    ${namespace}
-    ${rc}   ${output}=    Run And Return Rc And Output
-    ...    oc wait --timeout=3m --for jsonpath='{.status.conditions[].reason}'=ReconcileCompleted -n ${namespace} dsci ${dsci}
-    Should Be Equal    "${rc}"    "0"     msg=${output}
-
 Check ConfigMap Contains CA Bundle Key
     [Documentation]    Checks that ConfigMap contains CA Bundle
     [Arguments]    ${config_map}    ${ca_bundle_name}    ${namespace}
@@ -122,21 +102,6 @@ Check ConfigMap Contains CA Bundle Key
     ...    oc get configmap ${config_map} -n ${namespace} -o yaml | grep ${ca_bundle_name}
     Should Be Equal    "${rc}"    "0"     msg=${output}
 
-Create Namespace In Openshift
-    [Documentation]    Create a new namespace if it does not already exist
-    [Arguments]    ${namespace}
-    ${rc}   ${output}=    Run And Return Rc And Output    oc get project ${namespace}
-    IF    ${rc} != 0
-        ${rc}=     Run And Return Rc    oc new-project ${namespace}
-        Should Be Equal    "${rc}"    "0"   msg=${output}
-    END
-
-Delete Namespace From Openshift
-    [Documentation]    Delete namespace from opneshift
-    [Arguments]    ${namespace}
-    ${rc}   ${output}=    Run And Return Rc And Output    oc delete project ${namespace}
-    Should Be Equal    "${rc}"    "0"   msg=${output}
-
 Set Custom CA Bundle Value In DSCI
     [Documentation]    Set Custom CA Bundle value in DSCI
     [Arguments]    ${DSCI}    ${custom_ca_bundle_value}    ${namespace}
diff --git a/ods_ci/tests/Tests/100__deploy/130__operators/130__rhods_operator/140__service_mesh.robot b/ods_ci/tests/Tests/100__deploy/130__operators/130__rhods_operator/140__service_mesh.robot
new file mode 100644
index 000000000..e1ae229e8
--- /dev/null
+++ b/ods_ci/tests/Tests/100__deploy/130__operators/130__rhods_operator/140__service_mesh.robot
@@ -0,0 +1,96 @@
+*** Settings ***
+Documentation       Test Cases to verify Service Mesh integration
+
+Library             Collections
+Resource            ../../../../Resources/OCP.resource
+Resource            ../../../../Resources/ODS.robot
+Suite Setup         Suite Setup
+Suite Teardown      Suite Teardown
+
+
+*** Variables ***
+${OPERATOR_NS}                              ${OPERATOR_NAMESPACE}
+${RHOAI_OPERATOR_DEPLOYMENT_NAME}           rhods-operator
+${DSCI_NAME}                                default-dsci
+${SERVICE_MESH_OPERATOR_NS}                 openshift-operators
+${SERVICE_MESH_OPERATOR_DEPLOYMENT_NAME}    istio-operator
+${SERVICE_MESH_CR_NS}                       istio-system
+${SERVICE_MESH_CR_NAME}                     data-science-smcp
+
+${IS_PRESENT}                           0
+${IS_NOT_PRESENT}                       1
+
+
+*** Test Cases ***
+Validate Service Mesh State Managed
+    [Documentation]    The purpose of this Test Case is to validate Service Mesh integration
+    [Tags]    Operator    Tier1    ODS-2526    ServiceMesh-Managed
+
+    Is Resource Present    ServiceMeshControlPlane    ${SERVICE_MESH_CR_NAME}    ${SERVICE_MESH_CR_NS}    ${IS_PRESENT}
+    Check If Pod Exists    istiod    ${SERVICE_MESH_CR_NS}
+
+Validate Service Mesh State Unmanaged
+    [Documentation]    The purpose of this Test Case is to validate Service Mesh state 'Unmanaged'.
+    ...                The operator will not recreate/update the Service Mesh CR if removed or changed.
+    [Tags]    Operator    Tier1    ODS-2526    ServiceMesh-Unmanaged
+
+    Set Service Mesh Management State    Unmanaged    ${OPERATOR_NS}
+    Delete Service Mesh Control Plane    ${SERVICE_MESH_CR_NS}
+    Wait Until Keyword Succeeds    2 min    0 sec
+    ...    Is Resource Present    ServiceMeshControlPlane    ${SERVICE_MESH_CR_NAME}     ${SERVICE_MESH_CR_NS}    ${IS_NOT_PRESENT}
+
+    [Teardown]    Set Service Mesh State To Managed And Wait For CR Ready    ${SERVICE_MESH_CR_NAME}    ${SERVICE_MESH_CR_NS}    ${OPERATOR_NS}
+
+Validate Service Mesh State Removed
+    [Documentation]    The purpose of this Test Case is to validate Service Mesh state 'Removed'.
+    ...                The operator will Delete the Service Mesh CR, when state is Removed.
+    ...                Test will fail until RHOAIENG-2209 is fixed
+    [Tags]    ServiceMesh-Removed
+
+    Set Service Mesh Management State    Removed    ${OPERATOR_NS}
+    Wait Until Keyword Succeeds    2 min    0 sec
+    ...    Is Resource Present    ServiceMeshControlPlane    ${SERVICE_MESH_CR_NAME}    ${SERVICE_MESH_CR_NS}    ${IS_NOT_PRESENT}
+
+    [Teardown]    Set Service Mesh State To Managed And Wait For CR Ready    ${SERVICE_MESH_CONTROL_PLANE_NAME}    ${SERVICE_MESH_CR_NS}    ${OPERATOR_NS}
+
+
+*** Keywords ***
+Suite Setup
+    [Documentation]    Suite Setup
+    RHOSi Setup
+    Wait Until Operator Ready    ${SERVICE_MESH_OPERATOR_DEPLOYMENT_NAME}    ${SERVICE_MESH_OPERATOR_NS}
+    Wait Until Operator Ready    ${RHOAI_OPERATOR_DEPLOYMENT_NAME}    ${OPERATOR_NS}
+    Wait For DSCI Ready State    ${DSCI_NAME}    ${OPERATOR_NS}
+
+Suite Teardown
+    [Documentation]    Suite Teardown
+    RHOSi Teardown
+
+Delete Service Mesh Control Plane
+    [Documentation]    Delete Service Mesh Control Plane
+    [Arguments]    ${namespace}        ${reconsile_wait_time}=15s
+    ${rc}   ${output}=    Run And Return Rc And Output
+    ...    oc delete ServiceMeshControlPlane data-science-smcp -n ${namespace}
+    Should Be Equal    "${rc}"    "0"   msg=${output}
+    # Allow operator time to reconsile
+    Sleep    ${reconsile_wait_time}
+
+Set Service Mesh Management State
+    [Documentation]    Change DSCI Management State to one of Managed/Unmanaged/Removed
+    [Arguments]    ${management_state}    ${namespace}
+    ${rc}   ${output}=    Run And Return Rc And Output
+    ...    oc patch DSCInitialization/default-dsci -n ${namespace} -p '{"spec":{"serviceMesh":{"managementState":"${management_state}"}}}' --type merge
+    Should Be Equal    "${rc}"    "0"   msg=${output}
+
+Set Service Mesh State To Managed And Wait For CR Ready
+    [Documentation]    Restore Service Mesh State and Wait for Service Mesh CR to be Ready
+    [Arguments]    ${smcp_name}    ${smcp_ns}    ${dsci_ns}    ${timeout}=2m
+
+    Set Service Mesh Management State    Managed    ${dsci_ns}
+
+    Wait Until Keyword Succeeds    2 min    0 sec
+    ...    Is Resource Present    ServiceMeshControlPlane    ${smcp_name}     ${smcp_ns}    ${IS_PRESENT}
+
+    ${rc}   ${output}=    Run And Return Rc And Output
+    ...    oc wait ServiceMeshControlPlane/${smcp_name} --for condition=Ready -n ${smcp_ns} --timeout ${timeout}
+    Should Be Equal    "${rc}"    "0"   msg=${output}