Add support for packaging test framework
This change implements functionality to package the dependencies
needed to use the test framework in an offline/air gapped environment.
A new script named package.sh and a library named package-lib.sh are added
which setup the necessary environment to package the selected test stack and
calls package.sh from the respective stack's test repository to produce
archive file to use for offline deployment.
Signed-off-by: eprasad <prasad.mukhedkar@est.tech>
Change-Id: Ibc3b987e0a6ae7d120c6912ac07322c18d4c776e
Signed-off-by: eprasad <prasad.mukhedkar@est.tech>
diff --git a/test-requirements.txt b/test-requirements.txt
index 13213c7..fb93f50 100644
--- a/test-requirements.txt
+++ b/test-requirements.txt
@@ -8,3 +8,4 @@
ansible-lint==4.1.0
yamllint==1.19.0
shellcheck-py==0.7.0.1
+ansible==2.9.6
diff --git a/test/inventory/group_vars/all/global.yaml b/test/inventory/group_vars/all/global.yaml
index f3b3456..22906cc 100644
--- a/test/inventory/group_vars/all/global.yaml
+++ b/test/inventory/group_vars/all/global.yaml
@@ -17,41 +17,66 @@
# SPDX-License-Identifier: Apache-2.0
# ============LICENSE_END=========================================================
-# NOTE(fdegir): This file is used for translating environment variables to Ansible
+# NOTE(eprasad): This file is used for translating environment variables to Ansible
# variables. Defaults for most of the variables below are set in test/library/test-vars.sh
# file. An important point to highlight here is that some of the variables could
# be overriden by users using the arguments to package.sh or testt.sh scripts
-# NOTE (fdegir): path to test repository
+# NOTE (eprasad): path to test repository
test_path: "{{ lookup('env', 'TEST_PATH') }}"
-# NOTE (fdegir): path to test work directory
+# NOTE (eprasad): path to test work directory
# default is /opt/test
test_workspace: "{{ lookup('env', 'TEST_WORKSPACE') }}"
-# NOTE (fdegir): path to test cache directory where files generated during
+# NOTE (eprasad): path to test cache directory where files generated during
# runtime is placed in
# default is /opt/test/.cache
test_cache: "{{ lookup('env', 'TEST_CACHE') }}"
-# NOTE (fdegir) path to test python virtualenv created during runtime
+# NOTE (eprasad) path to test python virtualenv created during runtime
# default is /opt/test/.venv
test_venv: "{{ lookup('env', 'TEST_VENV') }}"
-# NOTE (fdegir) path to test config folder where the configuration files
+# NOTE (eprasad) path to test config folder where the configuration files
# are generated or copied in during runtime
# default is /opt/test/.cache/config
config_path: "{{ test_cache }}/config"
-# NOTE (fdegir): path to test inventory folder where the initial and
+# NOTE (eprasad): path to test inventory folder where the initial and
# generated Ansible inventory files are located
inventory_path: "{{ test_path }}/test/inventory"
-# NOTE (fdegir): path to test variables file that is generated
+# NOTE (eprasad): path to test variables file that is generated
# during runtime
test_vars_file: "{{ inventory_path }}/group_vars/all/test.yaml"
-# NOTE (fdegir): ci_loop is needed for test frameworks
+# NOTE (eprasad): ci_loop is needed for test frameworks
ci_loop: "{{ lookup('env', 'CI_LOOP') }}"
+# NOTE (eprasad): Test stack version
+stack_test_version: "{{ lookup('env', 'STACK_TEST_VERSION') }}"
+
+# NOTE (eprasad): path to the directory where the files for packaging are
+# downloaded into
+# default is /tmp/offline-package
+offline_pkg_folder: "{{ lookup('env', 'OFFLINE_PKG_FOLDER') }}"
+
+# NOTE (eprasad): path to the archive file which is created using offline_pkg_folder
+# default is /tmp/offline-package.tgz
+offline_pkg_file: "{{ lookup('env', 'OFFLINE_PKG_FILE') }}"
+
+# NOTE (eprasad): fqdn of the server where the artifacts are served from during
+# offline deployments
+server_fqdn: "engine.local"
+
+# NOTE (eprasad): variable to configure the packaging profile
+# please note that this is a Temporary variable and can be taken into use in the future
+packaging_profile: "{{ lookup('env', 'PACKAGING_PROFILE') | default('full', true) }}"
+
+# TODO (eprasad): the name of the stack installer file. see library/engine-vars.sh for
+# how it gets set
+offline_installer_file: "{{ lookup('env', 'OFFLINE_INSTALLER_FILE') }}"
+
+
# vim: set ts=2 sw=2 expandtab:
diff --git a/test/library/package-lib.sh b/test/library/package-lib.sh
new file mode 100755
index 0000000..4014063
--- /dev/null
+++ b/test/library/package-lib.sh
@@ -0,0 +1,164 @@
+#!/bin/bash
+
+# ============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=========================================================
+
+set -o pipefail
+
+# TODO: ignoring SC2015 for the timebeing so we don't break things
+# shellcheck disable=SC2015
+# Avoid double sourcing the file
+[[ -n ${PACKAGE_LIB_SOURCED:-} ]] && return 0 || export PACKAGE_LIB_SOURCED=1
+
+#-------------------------------------------------------------------------------
+# Print the help message which includes the usage, the expected parameters
+# and their default values if they are not specified
+#-------------------------------------------------------------------------------
+function usage() {
+
+ # NOTE: shellcheck complains quoting in the example so SC2086 is disabled
+ # shellcheck disable=SC2086
+ cat <<EOF
+
+Usage: $(basename ${0}) [-s <test stack to package>] [-b <test stack version/branch>] [-p <packaging profile>] [-d <distro>] [-v] [-h]
+
+ -s: Test stack to package (Default kubernetes)
+ -b: Test stack version (Default master)
+ -p: Packaging profile. (Default full)
+ -d: Distro. (Default ubuntu1804)
+ -v: Increase verbosity and keep logs for troubleshooting. (Default false)
+ -h: This message.
+
+EOF
+ exit 0
+
+}
+
+#-------------------------------------------------------------------------------
+# Parse the arguments that are passed to the script
+# If an argument is not specified, default values for those are set
+#
+# The priority order is
+# - arguments: overrides the default values and values set as environment
+# values. highest prio.
+# - env vars: overrides the default values but not the values set from command
+# line.
+# - default values: only takes effect if the user doesn't specify the value
+# of an argument either as an env var or from command line. lowest prio.
+#-------------------------------------------------------------------------------
+function parse_cmdline_opts() {
+
+ # set variables to the values set in env - otherwise, set them to defaults
+ STACK_TYPE=${STACK_TYPE:-kubernetes}
+ STACK_TEST_VERSION=${STACK_TEST_VERSION:-master}
+ PACKAGING_PROFILE=${PACKAGING_PROFILE:-full}
+ DISTRO=${DISTRO:-ubuntu1804}
+ VERBOSITY=${VERBOSITY:-false}
+
+ # TODO (fdegir): This function is left here so we can introduce additional
+ # arguments when we introduce support for other stacks than Kubernetes only
+ # get values passed as command line arguments, overriding the defaults or
+ # the ones set by using env variables
+ while getopts ":hs:b:p:d::v" o; do
+ case "${o}" in
+ h) usage ;;
+ s) STACK_TYPE="${OPTARG}" ;;
+ b) STACK_TEST_VERSION="${OPTARG}" ;;
+ p) PACKAGING_PROFILE="${OPTARG}" ;;
+ d) DISTRO="${OPTARG}" ;;
+ v) VERBOSITY="true" ;;
+ *) echo "ERROR: Invalid option '-${OPTARG}'"; usage ;;
+ esac
+ done
+
+ # Do all the exports
+ export STACK_TYPE="${STACK_TYPE}"
+ export STACK_TEST_VERSION="${STACK_TEST_VERSION}"
+ export PACKAGING_PROFILE="${PACKAGING_PROFILE}"
+ export DISTRO="${DISTRO}"
+ export VERBOSITY="${VERBOSITY}"
+ export EXECUTION_MODE="packaging"
+
+ log_summary
+
+}
+
+#-------------------------------------------------------------------------------
+# Remove leftovers of previous runs
+# Leftovers of previous run might result in failures, especially within CI/CD
+# so this function is important to use but it is not executed by default.
+#-------------------------------------------------------------------------------
+function cleanup() {
+
+ echo "Info : Remove leftovers of previous run"
+
+ # remove engine venv, cache and .ansible
+ sudo /bin/rm -rf "$TEST_VENV" "$TEST_CACHE" "$HOME/.ansible" \
+ "$OFFLINE_PKG_FOLDER" "$OFFLINE_PKG_FILE"
+
+ # stop docker service since docker registry keeps creating
+ # $TEST_CACHE/certs folder, making engine prep to fail due
+ # to ownership issus
+ redirect_cmd sudo systemctl stop docker || true
+
+}
+
+#-------------------------------------------------------------------------------
+# Log summary & parameters to console
+#-------------------------------------------------------------------------------
+function log_summary() {
+
+ echo
+ echo "#---------------------------------------------------#"
+ echo "# Environment #"
+ echo "#---------------------------------------------------#"
+ echo "User : $USER"
+ echo "Hostname : $HOSTNAME"
+ echo "Host OS : $(source /etc/os-release &> /dev/null || source /usr/lib/os-release &> /dev/null; echo "${PRETTY_NAME}")"
+ echo "IP : $(hostname -I | cut -d' ' -f1)"
+ echo "#---------------------------------------------------#"
+ echo "# Execution Started #"
+ echo "#---------------------------------------------------#"
+ echo "Date & Time : $(date -u '+%F %T UTC')"
+ echo "Execution Mode : $EXECUTION_MODE"
+ echo "Test stack Type : $STACK_TYPE"
+ echo "Test stack Version: $STACK_TEST_VERSION"
+ echo "Distro : $DISTRO"
+ echo "Packaging Profile : $PACKAGING_PROFILE"
+ echo "Verbosity : $VERBOSITY"
+ echo "#---------------------------------------------------#"
+ echo
+
+}
+
+#-------------------------------------------------------------------------------
+# Log elapsed time to console
+#-------------------------------------------------------------------------------
+function log_elapsed_time() {
+
+ elapsed_time=$SECONDS
+ echo "#---------------------------------------------------#"
+ echo "# Execution Completed #"
+ echo "#---------------------------------------------------#"
+ echo "Date & Time : $(date -u '+%F %T UTC')"
+ echo "Elapsed : $((elapsed_time / 60)) minutes and $((elapsed_time % 60)) seconds"
+ echo "#---------------------------------------------------#"
+
+}
+
+# vim: set ts=2 sw=2 expandtab:
diff --git a/test/package.sh b/test/package.sh
new file mode 100755
index 0000000..bac46ee
--- /dev/null
+++ b/test/package.sh
@@ -0,0 +1,89 @@
+#!/bin/bash
+# ============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=========================================================
+
+set -o errexit
+set -o nounset
+set -o pipefail
+
+#-------------------------------------------------------------------------------
+# Find/set where we are
+#-------------------------------------------------------------------------------
+TEST_PATH="$(git rev-parse --show-toplevel)"
+export TEST_PATH
+
+#-------------------------------------------------------------------------------
+# Source test and engine libraries
+#-------------------------------------------------------------------------------
+# shellcheck source=test/library/package-lib.sh
+source "${TEST_PATH}/test/library/package-lib.sh"
+# shellcheck source=test/library/engine-lib.sh
+source "${TEST_PATH}/test/library/engine-lib.sh"
+
+#-------------------------------------------------------------------------------
+# Parse command line options
+#-------------------------------------------------------------------------------
+parse_cmdline_opts "$@"
+
+#-------------------------------------------------------------------------------
+# Check prerequisites before doing anything else to see if we should continue
+#-------------------------------------------------------------------------------
+check_prerequisites
+
+#-------------------------------------------------------------------------------
+# Bootstrap environment for Cloud Infra Tests
+#-------------------------------------------------------------------------------
+bootstrap_environment
+
+#-------------------------------------------------------------------------------
+# Cleanup leftovers of previous run if it is explicitly set
+#-------------------------------------------------------------------------------
+cleanup
+
+#-------------------------------------------------------------------------------
+# Install Ansible
+#-------------------------------------------------------------------------------
+install_ansible
+
+#-------------------------------------------------------------------------------
+# Bootstrap test software configuration
+#-------------------------------------------------------------------------------
+echo "Info : Bootstrap test software configuration"
+echo "-------------------------------------------------------------------------"
+cd "${TEST_PATH}"
+ansible-playbook "${TEST_ANSIBLE_PARAMS[@]}" \
+ -i "${TEST_PATH}/test/inventory/localhost.ini" \
+ test/playbooks/bootstrap-swconfig.yaml
+echo "-------------------------------------------------------------------------"
+
+#-------------------------------------------------------------------------------
+# Execute stack test script
+# NOTE (eprasad): Everything for the testing is pushed in to stack test
+# script as test repo itself does not have logic specific to stacks themselves because
+# the test steps may be different for different stack.
+#-------------------------------------------------------------------------------
+# NOTE: shellcheck SC1090 is disabled since stack becomes available during runtime
+# shellcheck disable=SC1090
+source "${TEST_PATH}/test/stack/${STACK_TYPE}/package.sh"
+
+#-------------------------------------------------------------------------------
+# Log total time it took to finish to console
+#-------------------------------------------------------------------------------
+log_elapsed_time
+
+# vim: set ts=2 sw=2 expandtab: