Adapt xtesting-onap to stack oriented framework

This commit adds an initial testing capability for onap with the following two testsuits
support.

1. infra-healthcheck
2. healthcheck

Signed-off-by: eprasad <prasad.mukhedkar@est.tech>
Change-Id: I1f669880ba7ae9301c8f86974072e114ae28951d
Signed-off-by: eprasad <prasad.mukhedkar@est.tech>
diff --git a/package.sh b/package.sh
index 5c26402..e2d3730 100755
--- a/package.sh
+++ b/package.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 # ============LICENSE_START=======================================================
-#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
+#  Copyright (C) 2020 The Nordix Foundation. All rights reserved.
 # ================================================================================
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
diff --git a/playbooks/prepare-testframework.yaml b/playbooks/prepare-testframework.yaml
new file mode 100644
index 0000000..42b72c9
--- /dev/null
+++ b/playbooks/prepare-testframework.yaml
@@ -0,0 +1,26 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2020 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+- hosts: jumphost
+  gather_facts: true
+
+  roles:
+    - role: prepare-testframework
+
+# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/prepare-testframework/tasks/main.yaml b/playbooks/roles/prepare-testframework/tasks/main.yaml
new file mode 100644
index 0000000..112aa80
--- /dev/null
+++ b/playbooks/roles/prepare-testframework/tasks/main.yaml
@@ -0,0 +1,33 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2020 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+- name: Set test framework container image name and version
+  set_fact:
+    testfw_image_name: "{{ framework[test_framework][test_suite].image_name }}"
+    testfw_image_version: "{{ framework[test_framework][test_suite].image_version }}"
+
+# Prepare infra-healthcheck test
+- include: "prepare-{{ test_framework }}-{{ test_suite }}.yaml"
+  when: test_suite == "infra-healthcheck"
+
+# Prepare healthcheck test
+- include: "prepare-{{ test_framework }}-{{ test_suite }}.yaml"
+  when: test_suite == "healthcheck"
+
+# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/prepare-testframework/tasks/prepare-xtesting-healthcheck.yaml b/playbooks/roles/prepare-testframework/tasks/prepare-xtesting-healthcheck.yaml
new file mode 100644
index 0000000..461c34c
--- /dev/null
+++ b/playbooks/roles/prepare-testframework/tasks/prepare-xtesting-healthcheck.yaml
@@ -0,0 +1,69 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2020 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+- name: Set test work and result directories
+  set_fact:
+    testfw_workdir: "{{ test_cache }}/{{ test_framework }}/{{ test_type }}"
+    testfw_configdir: "{{ config_path }}/{{ test_framework }}"
+    testfw_resultdir: "{{ test_cache }}/{{ test_framework }}/{{ test_type }}/results"
+
+- name: Ensure {{ testfw_workdir }} exists and empty
+  file:
+    path: "{{ testfw_workdir }}"
+    state: "{{ item }}"
+    mode: 0755
+  with_items:
+    - absent
+    - directory
+  become: true
+
+- name: Ensure {{ testfw_configdir }} exists and empty
+  file:
+    path: "{{ testfw_configdir }}"
+    state: "{{ item }}"
+    mode: 0755
+  with_items:
+    - absent
+    - directory
+  become: true
+
+- name: Ensure {{ testfw_resultdir }} exists and empty
+  file:
+    path: "{{ testfw_resultdir }}"
+    state: "{{ item }}"
+    mode: 0755
+  with_items:
+    - absent
+    - directory
+  become: true
+
+- name: Delete healthcheck job
+  k8s:
+    state: absent
+    api: batch/v1
+    kind: Job
+    namespace: onap
+    name: "xtesting-onap-{{ test_type }}"
+
+- name: Construct and Save healthcheck deployment to file
+  template:
+    src: "healthcheck_deployment.j2"
+    dest: "{{ testfw_configdir }}/healthcheck-{{ test_type }}.yaml"
+
+# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/prepare-testframework/tasks/prepare-xtesting-infra-healthcheck.yaml b/playbooks/roles/prepare-testframework/tasks/prepare-xtesting-infra-healthcheck.yaml
new file mode 100644
index 0000000..1400011
--- /dev/null
+++ b/playbooks/roles/prepare-testframework/tasks/prepare-xtesting-infra-healthcheck.yaml
@@ -0,0 +1,55 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2020 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+- name: Set test work and result directories
+  set_fact:
+    testfw_workdir: "{{ test_cache }}/{{ test_framework }}"
+    testfw_resultdir: "{{ test_cache }}/{{ test_framework }}/results"
+
+- name: Ensure {{ testfw_workdir }} exists and empty
+  file:
+    path: "{{ testfw_workdir }}"
+    state: "{{ item }}"
+    mode: 0755
+  with_items:
+    - absent
+    - directory
+  become: true
+
+- name: Prepare files necessary to run {{ test_framework }}
+  template:
+    src: "{{ item.src }}"
+    dest: "{{ item.dest }}"
+    mode: "{{ item.mode }}"
+  with_items:
+    - {src: "xtesting.env.j2", dest: "{{ testfw_workdir }}/xtesting.env", mode: "0644"}
+    - {src: "xtesting.sh.j2", dest: "{{ testfw_workdir }}/run-tests.sh", mode: "0755"}
+  become: true
+
+- name: Ensure {{ testfw_resultdir }} exists and empty
+  file:
+    path: "{{ testfw_resultdir }}"
+    state: "{{ item }}"
+    mode: 0755
+  with_items:
+    - absent
+    - directory
+  become: true
+
+# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/prepare-testframework/templates/healthcheck_deployment.j2 b/playbooks/roles/prepare-testframework/templates/healthcheck_deployment.j2
new file mode 100644
index 0000000..559a3c2
--- /dev/null
+++ b/playbooks/roles/prepare-testframework/templates/healthcheck_deployment.j2
@@ -0,0 +1,46 @@
+apiVersion: batch/v1
+kind: Job
+metadata:
+    name: "xtesting-onap-{{ test_type }}"
+    namespace: onap
+spec:
+    template:
+        spec:
+            containers:
+            -   env:
+                -   name: INSTALLER_TYPE
+                    value: kubespray-oom
+                -   name: DEPLOY_SCENARIO
+                    value: "{{ deploy_scenario }}"
+                -   name: NODE_NAME
+                    value: {{ lookup('env', 'POD_NAME') | default('noname', true) }}
+                -   name: TEST_DB_URL
+                    value: http://testresults.opnfv.org/onap/api/v1/results
+                -   name: BUILD_TAG
+                    value: "{{ lookup('env', 'BUILD_TAG') | default('notag', true) }}"
+                -   name: TAG
+                    value: "{{ test_type }}"
+                image: "{{ testfw_image_name }}@sha256:{{ testfw_image_version }}"
+                imagePullPolicy: Always
+                name: xtesting-onap
+                volumeMounts:
+                -   mountPath: /etc/localtime
+                    name: localtime
+                    readOnly: true
+                -   mountPath: /share/config
+                    name: robot-eteshare
+                -   mountPath: /var/lib/xtesting/results/
+                    name: robot-save-results
+            restartPolicy: Never
+            volumes:
+            -   hostPath:
+                    path: /etc/localtime
+                name: localtime
+            -   configMap:
+                    defaultMode: 493
+                    name: onapdep-robot-eteshare-configmap
+                name: robot-eteshare
+            -   hostPath:
+                  path: "{{ testfw_resultdir }}"
+                  type: DirectoryOrCreate
+                name: robot-save-results
diff --git a/playbooks/roles/prepare-testframework/templates/xtesting.env.j2 b/playbooks/roles/prepare-testframework/templates/xtesting.env.j2
new file mode 100644
index 0000000..ec7bf5e
--- /dev/null
+++ b/playbooks/roles/prepare-testframework/templates/xtesting.env.j2
@@ -0,0 +1,6 @@
+BUILD_TAG={{ lookup('env', 'BUILD_TAG') }}
+ENERGY_RECORDER_API_URL=http://energy.opnfv.fr/resources
+CI_LOOP=daily
+TEST_DB_URL=http://testresults.opnfv.org/onap/api/v1/results
+INSTALLER_TYPE=kubespray-oom
+NODE_NAME={{ lookup('env', 'POD_NAME') | default('noname', true) }}
diff --git a/playbooks/roles/prepare-testframework/templates/xtesting.sh.j2 b/playbooks/roles/prepare-testframework/templates/xtesting.sh.j2
new file mode 100755
index 0000000..226ba86
--- /dev/null
+++ b/playbooks/roles/prepare-testframework/templates/xtesting.sh.j2
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+declare -i retval=0
+
+sudo docker run --rm --env-file {{ testfw_workdir }}/xtesting.env \
+  -v $HOME/.kube/config:/root/.kube/config \
+  -v {{ testfw_resultdir }}:/var/lib/xtesting/results \
+  {{ testfw_image_name }}@sha256:{{ testfw_image_version }} \
+  run_tests -r -t {{ testcase }}
+  retval=$(($retval + $?))
+
+exit $retval
diff --git a/playbooks/roles/prepare-testframework/vars/main.yaml b/playbooks/roles/prepare-testframework/vars/main.yaml
new file mode 100644
index 0000000..3ddeaae
--- /dev/null
+++ b/playbooks/roles/prepare-testframework/vars/main.yaml
@@ -0,0 +1,19 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2020 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+testcase: all
diff --git a/playbooks/roles/run-tests/tasks/healthcheck.yaml b/playbooks/roles/run-tests/tasks/healthcheck.yaml
new file mode 100644
index 0000000..34df2ff
--- /dev/null
+++ b/playbooks/roles/run-tests/tasks/healthcheck.yaml
@@ -0,0 +1,54 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2020 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+- name: start healthcheck job
+  k8s:
+    state: present
+    src: "{{ config_path }}/xtesting/healthcheck-{{ test_type }}.yaml"
+
+- name: Get the name of pod created by healthcheck job
+  shell: kubectl get pods -l job-name=xtesting-onap-{{ test_type }} -o name --no-headers=true | sed 's/\<pod\>//g'  | sed 's/\///'
+  register: xtesting_pod
+
+- name: Wait for healthcheck job to complete
+  shell: "kubectl get pods {{ xtesting_pod.stdout }}"
+  register: healthcheck_pod
+  until: '" Completed "  in healthcheck_pod.stdout'
+  retries: 100
+  delay: 4
+
+- name: collect healthcheck testsuite logs
+  shell: "kubectl logs {{ xtesting_pod.stdout }}"
+  register: healthcheck_log
+
+- name: Log healthcheck testsuite output to console
+  debug:
+    var: healthcheck_log.stdout_lines
+
+- name: Save healthcheck testsuite output on jumphost
+  copy:
+    content: healthcheck_log.stdout_lines
+    dest: "{{ test_cache }}/xtesting/{{ test_type }}/results/xtesting.log"
+
+- name: Determine the test verdict
+  fail:
+    msg: "Tests failed!"
+  when: "'Result.EX_ERROR' in healthcheck_log.stdout"
+
+# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/run-tests/tasks/infra-healthcheck.yaml b/playbooks/roles/run-tests/tasks/infra-healthcheck.yaml
new file mode 100644
index 0000000..abd2767
--- /dev/null
+++ b/playbooks/roles/run-tests/tasks/infra-healthcheck.yaml
@@ -0,0 +1,35 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2020 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+- name: Run tests
+  command: "{{ test_cache }}/{{ test_framework }}/run-tests.sh"
+  changed_when: false
+  register: run_tests
+  ignore_errors: true
+
+- name: Log run-tests.sh output to console
+  debug:
+    msg: "{{ run_tests.stdout_lines }}"
+
+- name: Determine the test verdict
+  fail:
+    msg: "Tests failed!"
+  when: run_tests.rc != 0
+
+# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/run-tests/tasks/main.yaml b/playbooks/roles/run-tests/tasks/main.yaml
new file mode 100644
index 0000000..31d07ba
--- /dev/null
+++ b/playbooks/roles/run-tests/tasks/main.yaml
@@ -0,0 +1,25 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2020 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+# run tests
+- include: "{{ test_suite }}.yaml"
+  when: test_suite == "infra-healthcheck"
+
+- include: "{{ test_suite }}.yaml"
+  when: test_suite == "healthcheck"
diff --git a/playbooks/roles/run-tests/vars/main.yaml b/playbooks/roles/run-tests/vars/main.yaml
new file mode 100644
index 0000000..ed97d53
--- /dev/null
+++ b/playbooks/roles/run-tests/vars/main.yaml
@@ -0,0 +1 @@
+---
diff --git a/playbooks/run-tests.yaml b/playbooks/run-tests.yaml
new file mode 100644
index 0000000..82e12fb
--- /dev/null
+++ b/playbooks/run-tests.yaml
@@ -0,0 +1,26 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2020 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+- hosts: jumphost
+  gather_facts: true
+
+  roles:
+    - role: run-tests
+
+# vim: set ts=2 sw=2 expandtab:
diff --git a/test.sh b/test.sh
index 5c26402..748ccea 100755
--- a/test.sh
+++ b/test.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 # ============LICENSE_START=======================================================
-#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
+#  Copyright (C) 2020 The Nordix Foundation. All rights reserved.
 # ================================================================================
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -21,6 +21,32 @@
 set -o nounset
 set -o pipefail
 
-echo "Hello World!"
+#-------------------------------------------------------------------------------
+# Find and set where we are
+#-------------------------------------------------------------------------------
+STACK_ROOT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
+export STACK_ROOT_DIR
+
+#-------------------------------------------------------------------------------
+# Prepare test framework
+#-------------------------------------------------------------------------------
+echo "Info  : Preparing test framework"
+echo "-------------------------------------------------------------------------"
+cd "${STACK_ROOT_DIR}"
+ansible-playbook "${TEST_ANSIBLE_PARAMS[@]}" \
+  -i "${TEST_PATH}/test/inventory/inventory.ini" \
+  "${STACK_ROOT_DIR}/playbooks/prepare-testframework.yaml"
+echo "-------------------------------------------------------------------------"
+
+#-------------------------------------------------------------------------------
+# Run tests
+#-------------------------------------------------------------------------------
+echo "Info  : Running tests"
+echo "-------------------------------------------------------------------------"
+cd "${STACK_ROOT_DIR}"
+ansible-playbook "${TEST_ANSIBLE_PARAMS[@]}" \
+  -i "${TEST_PATH}/test/inventory/inventory.ini" \
+  "${STACK_ROOT_DIR}//playbooks/run-tests.yaml"
+echo "-------------------------------------------------------------------------"
 
 # vim: set ts=2 sw=2 expandtab:
diff --git a/vars/onap.yaml b/vars/onap.yaml
index c55258e..439d1ae 100644
--- a/vars/onap.yaml
+++ b/vars/onap.yaml
@@ -1,6 +1,6 @@
 ---
 # ============LICENSE_START=======================================================
-#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
+#  Copyright (C) 2020 The Nordix Foundation. All rights reserved.
 # ================================================================================
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -29,5 +29,19 @@
 #     <test suite>:
 #       image_name: <container image name>
 #       image_version: <container image sha256sum>
+framework:
+  xtesting:
+    infra-healthcheck:
+      image_name: nexus3.onap.org:10001/onap/xtesting-infra-healthcheck
+      # latest xtesting-infra-healthcheck image as on 2020-24-11
+      image_version: '75a85a5ef784e115186696ea9953dcbf0f2175401958c578aa708e2e894dea96'
+    healthcheck:
+      image_name: nexus3.onap.org:10001/onap/xtesting-healthcheck
+      # latest xtesting-healthcheck image as on 2020-24-11
+      image_version: '261eac403f0ecaa39194ac655042b7f1b87c2f9a57155f0f6ef5ed1eb53b766b'
+
+# test to peform with healthcheck testsuite. core, full, healthdist and postinstall tests
+#are supported by healthcheck testsuite.
+test_type: "{{ lookup('env','HEALTHCHECK_TEST_TYPE')| default('full', true) }}"
 
 # vim: set ts=2 sw=2 expandtab: