New test gitops02 for cluster registration, OKA and KSU in imported clusters

Change-Id: I5ab0daa91ef593ad613211951ba175f49c67dda2
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
diff --git a/robot-systest/lib/advanced_cluster_mgmt_lib.resource b/robot-systest/lib/advanced_cluster_mgmt_lib.resource
index dc1621d..23b3c02 100644
--- a/robot-systest/lib/advanced_cluster_mgmt_lib.resource
+++ b/robot-systest/lib/advanced_cluster_mgmt_lib.resource
@@ -25,6 +25,8 @@
 ${SUCCESS_RETURN_CODE}   0
 ${CLUSTER_LAUNCH_MAX_WAIT_TIME}   12min
 ${CLUSTER_LAUNCH_POL_TIME}   30sec
+${CLUSTER_REGISTER_MAX_WAIT_TIME}   7min
+${CLUSTER_REGISTER_POL_TIME}   30sec
 ${CLUSTER_DELETE_MAX_WAIT_TIME}   12min
 ${CLUSTER_DELETE_POL_TIME}   30sec
 ${OKA_OPERATION_MAX_WAIT_TIME}   5min
@@ -55,6 +57,31 @@
     Log   ${cluster_id}
     Check Cluster Age Keys   ${cluster_id}
     IF   ${wait_flag} == True
+        Wait Until Keyword Succeeds   ${CLUSTER_REGISTER_MAX_WAIT_TIME}   ${CLUSTER_REGISTER_POL_TIME}
+        ...   Check For Cluster To Be Ready   ${name}
+    END
+    RETURN   ${cluster_id}
+
+Register Cluster
+    [Documentation]   Register a Kubernetes cluster in OSM using a provided name and kubeconfig credentials.
+    [Arguments]   ${name}   ${vim_account}   ${creds}   ${description}   ${bootstrap_flag}=True   ${wait_flag}=True
+    ${command}=   Catenate
+    ...   osm cluster-register ${name}
+    ...   --vim-account ${vim_account}
+    ...   --creds ${creds}
+    ...   --description ${description}
+    IF   ${bootstrap_flag} == True
+        Catenate   ${command}   --bootstrap
+    ELSE
+        Catenate   ${command}   --no-bootstrap
+    END
+    ${rc}   ${stdout}=   Run And Return Rc And Output   ${command}
+    Log   ${rc},${stdout}
+    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}   msg=Cluster registration failed: ${stdout}
+    ${cluster_id}=   Set Variable   ${stdout}
+    Log   ${cluster_id}
+    Check Cluster Age Keys   ${cluster_id}
+    IF   ${wait_flag} == True
         Wait Until Keyword Succeeds   ${CLUSTER_LAUNCH_MAX_WAIT_TIME}   ${CLUSTER_LAUNCH_POL_TIME}
         ...   Check For Cluster To Be Ready   ${name}
     END
@@ -75,7 +102,7 @@
     Log   pubkey is ${stdout}
 
 Delete Cluster
-    [Documentation]   Unregister/delete a Kubernetes cluster from OSM.
+    [Documentation]   Delete a Kubernetes cluster from OSM.
     [Arguments]   ${cluster_name}   ${wait_flag}=True
     ${rc}   ${stdout}=   Run And Return Rc And Output   osm cluster-delete ${cluster_name}
     Log   ${rc},${stdout}
@@ -85,6 +112,17 @@
         ...    Check For Cluster Deletion Status   ${cluster_name}
     END
 
+Deregister Cluster
+    [Documentation]   Deregister a Kubernetes cluster from OSM.
+    [Arguments]   ${cluster_name}   ${wait_flag}=True
+    ${rc}   ${stdout}=   Run And Return Rc And Output   osm cluster-deregister ${cluster_name}
+    Log   ${rc},${stdout}
+    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}   msg=Cluster deregistration failed: ${stdout}
+    IF   ${wait_flag} == True
+        Wait Until Keyword Succeeds   ${CLUSTER_DELETE_MAX_WAIT_TIME}   ${CLUSTER_DELETE_POL_TIME}
+        ...    Check For Cluster Deletion Status   ${cluster_name}
+    END
+
 Check For Cluster Deletion Status
     [Documentation]   Check if a Kubernetes cluster identified by name is deleted or in error state.
     [Arguments]   ${cluster_name}
@@ -236,10 +274,12 @@
     Log   ${matches}
     IF   ${matches} != @{EMPTY}
        Log   ${ksu_name} is a valid UUID
-       ${rc}   ${stdout}=   Run And Return Rc And Output   osm ksu-list --filter _id="${ksu_name}" | grep ${ksu_name}
+       # ${rc}   ${stdout}=   Run And Return Rc And Output   osm ksu-list --filter _id="${ksu_name}" | grep ${ksu_name}
+       ${rc}   ${stdout}=   Run And Return Rc And Output   osm ksu-list --filter _id="${ksu_name}" -o jsonpath='[*].name'| grep -E "\^${ksu_name}\$""
     ELSE
        Log   ${ksu_name} is not a valid UUID, so it will be treated as a name
-       ${rc}   ${stdout}=   Run And Return Rc And Output   osm ksu-list --filter name="${ksu_name}" | grep ${ksu_name}
+       # ${rc}   ${stdout}=   Run And Return Rc And Output   osm ksu-list --filter name="${ksu_name}" | grep ${ksu_name}
+       ${rc}   ${stdout}=   Run And Return Rc And Output   osm ksu-list --filter name="${ksu_name}" -o jsonpath='[*].name'| grep -E "\^${ksu_name}\$"
     END
     Log   ${rc},${stdout}
     Should Be Empty   ${stdout}
diff --git a/robot-systest/testsuite/gitops_01-advanced_cluster_management.robot b/robot-systest/testsuite/gitops_01-declarative_cluster_creation_oka_ksu.robot
similarity index 97%
rename from robot-systest/testsuite/gitops_01-advanced_cluster_management.robot
rename to robot-systest/testsuite/gitops_01-declarative_cluster_creation_oka_ksu.robot
index 14c31af..a6bb19a 100644
--- a/robot-systest/testsuite/gitops_01-advanced_cluster_management.robot
+++ b/robot-systest/testsuite/gitops_01-declarative_cluster_creation_oka_ksu.robot
@@ -13,7 +13,7 @@
 
 
 *** Settings ***
-Documentation   [GITOPS-01] Advanced Cluster Management.
+Documentation   [GITOPS-01] Advanced Cluster Management. Cluster creation
 
 Library   OperatingSystem
 Library   String
@@ -24,7 +24,7 @@
 
 Test Tags   gitops_01   cluster_gitops
 
-# Suite Teardown   Run Keyword And Ignore Error   Suite Cleanup
+Suite Teardown   Run Keyword And Ignore Error   Suite Cleanup
 
 
 *** Variables ***
diff --git a/robot-systest/testsuite/gitops_01-advanced_cluster_management.robot b/robot-systest/testsuite/gitops_02-declarative_cluster_registration_oka_ksu.robot
similarity index 73%
copy from robot-systest/testsuite/gitops_01-advanced_cluster_management.robot
copy to robot-systest/testsuite/gitops_02-declarative_cluster_registration_oka_ksu.robot
index 14c31af..4f3a07a 100644
--- a/robot-systest/testsuite/gitops_01-advanced_cluster_management.robot
+++ b/robot-systest/testsuite/gitops_02-declarative_cluster_registration_oka_ksu.robot
@@ -13,7 +13,7 @@
 
 
 *** Settings ***
-Documentation   [GITOPS-01] Advanced Cluster Management.
+Documentation   [GITOPS-02] Advanced Cluster Management. Cluster registration
 
 Library   OperatingSystem
 Library   String
@@ -22,7 +22,7 @@
 
 Resource   ../lib/advanced_cluster_mgmt_lib.resource
 
-Test Tags   gitops_01   cluster_gitops
+Test Tags   gitops_02   cluster_gitops
 
 # Suite Teardown   Run Keyword And Ignore Error   Suite Cleanup
 
@@ -41,19 +41,16 @@
 ${OKA_NS_PROFILE}   app-profile
 
 # Cluster
-${CLUSTER_NAME}   gitops01
-${CLUSTER_DESCRIPTION}   "Cluster for gitops01 Robot test suite"
-${CLUSTER_VM_SIZE}   Standard_D2_v2
-${CLUSTER_NODES}   2
-${REGION_NAME}   northeurope
-${RESOURCE_GROUP}   OSM-CTIO
-${KUBERNETES_VERSION}   "1.30"
+${CLUSTER_NAME}   cl-madrid-norte1
+${CLUSTER_DESCRIPTION}   "Cluster for gitops02 Robot test suite"
 
 # KSUs
-${KSU1_NAME}   testacme
 ${KSU_NS_NAME}   jenkins-ns
+${KSU1_NAME}   testacme
 ${KSU2_NAME}   jenkins
-${KSU3_NAME}   jenkinsbis
+${KSU_NS_ID}   ${EMPTY}
+${KSU1_ID}   ${EMPTY}
+${KSU2_ID}   ${EMPTY}
 
 
 *** Test Cases ***
@@ -64,10 +61,10 @@
     Add OKA Package   ${OKA1_NAME}   '${OKA_FOLDER}/${OKA1_PKG}'   ${OKA1_PROFILE}
     Add OKA Package   ${OKA2_NAME}   '${OKA_FOLDER}/${OKA2_PKG}'   ${OKA2_PROFILE}
 
-Create Cluster For Testsuite in Azure
-    [Documentation]   Create K8s cluster for the testsuite.
+Register Cluster For Testsuite
+    [Documentation]   Register K8s cluster for the testsuite.
     [Tags]   cluster   create
-    ${id}=   Create Cluster   ${CLUSTER_NAME}   %{VIM_TARGET}   ${CLUSTER_DESCRIPTION}   ${CLUSTER_VM_SIZE}   ${KUBERNETES_VERSION}   ${CLUSTER_NODES}   ${REGION_NAME}   ${RESOURCE_GROUP}
+    ${id}=   Register Cluster   ${CLUSTER_NAME}   %{VIM_TARGET}   %{CLUSTER_KUBECONFIG_CREDENTIALS}   ${CLUSTER_DESCRIPTION}
     Set Suite Variable   ${CLUSTER_ID}   ${id}
     Log   ${CLUSTER_ID}
 
@@ -100,37 +97,40 @@
     Set Suite Variable   ${KSU2_ID}   ${id}
     Log   ${KSU2_ID}
 
-Create KSU3 With Params
-    [Documentation]   Create KSU based on ${OKA2_NAME} with same params as KSU2.
-    [Tags]   ksu   ksu3   create
-    ${id}=   Create KSU   ${KSU3_NAME}   ${KSU3_NAME}   ${CLUSTER_NAME}   app-profile   ${OKA2_NAME}   ${OKA_FOLDER}/apps/jenkins-params.yaml
-    Set Suite Variable   ${KSU3_ID}   ${id}
-    Log   ${KSU3_ID}
-
-Delete KSU3
-    [Documentation]   Delete KSU3 based on ${OKA2_NAME}.
-    [Tags]   cleanup   ksu   ksu3
-    Delete KSU   ${KSU3_ID}
-
 Delete KSU2
     [Documentation]   Delete KSU2 based on ${OKA2_NAME}.
     [Tags]   cleanup   ksu   ksu2
-    Delete KSU   ${KSU2_ID}
+    IF   '${KSU2_ID}' != ''
+       ${KSU_TO_BE_DELETED}=   Set Variable   ${KSU2_ID}
+    ELSE
+       ${KSU_TO_BE_DELETED}=   Set Variable   ${KSU2_NAME}
+    END
+    Delete KSU   ${KSU_TO_BE_DELETED}
 
 Delete KSU1
     [Documentation]   Delete KSU1 based on ${OKA1_NAME}.
     [Tags]   cleanup   ksu   ksu1
-    Delete KSU   ${KSU1_NAME}
+    IF   '${KSU1_ID}' != ''
+       ${KSU_TO_BE_DELETED}=   Set Variable   ${KSU1_ID}
+    ELSE
+       ${KSU_TO_BE_DELETED}=   Set Variable   ${KSU1_NAME}
+    END
+    Delete KSU   ${KSU_TO_BE_DELETED}
 
 Delete KSU Namespace
     [Documentation]   Delete KSU based on ${OKA_NS_NAME}.
     [Tags]   cleanup   ksu   ksu-ns
-    Delete KSU   ${KSU_NS_ID}
+    IF   '${KSU_NS_ID}' != ''
+       ${KSU_TO_BE_DELETED}=   Set Variable   ${KSU_NS_ID}
+    ELSE
+       ${KSU_TO_BE_DELETED}=   Set Variable   ${KSU_NS_NAME}
+    END
+    Delete KSU   ${KSU_TO_BE_DELETED}
 
-Delete Cluster For Testsuite
-    [Documentation]   Delete K8s cluster for the testsuite.
+Deregister Cluster For Testsuite
+    [Documentation]   Deregister K8s cluster for the testsuite.
     [Tags]   cleanup   cluster
-    Delete Cluster   ${CLUSTER_NAME}
+    Deregister Cluster   ${CLUSTER_NAME}
 
 Delete OKA Packages For The Testsuite
     [Documentation]   Delete OKA packages for the testsuite.
@@ -143,7 +143,7 @@
 *** Keywords ***
 Suite Cleanup
     [Documentation]   Test Suit Cleanup: Deleting cluster, KSUs, OKAs
-    Run Keyword If Any Tests Failed   Delete Cluster   ${CLUSTER_NAME}
+    Run Keyword If Any Tests Failed   Deregister Cluster   ${CLUSTER_NAME}
     Run Keyword If Any Tests Failed   Delete OKA Package   ${OKA_NS_NAME}
     Run Keyword If Any Tests Failed   Delete OKA Package   ${OKA1_NAME}
     Run Keyword If Any Tests Failed   Delete OKA Package   ${OKA2_NAME}