blob: cf8cc1f355c1e1ff0ac3a8f81aa25dc19377e4b4 [file] [log] [blame]
Filip Pytlouna1898572016-01-25 22:35:13 +01001#!/usr/bin/env bash
Dennis Dmitriev4bf87622017-07-06 21:07:27 +03002###
Oleksii Molchanove465aeb2020-05-18 23:47:10 +03003# Script source: https://gerrit.mcp.mirantis.com/#/admin/projects/salt-formulas/cookiecutter-salt-formula
Dennis Dmitriev4bf87622017-07-06 21:07:27 +03004# Script requirments:
5#apt-get install -y python-yaml virtualenv git
Oleksii Molchanove465aeb2020-05-18 23:47:10 +03006__ScriptVersion="2019.01.07"
Dennis Dmitriev4bf87622017-07-06 21:07:27 +03007__ScriptName="run_tests.sh"
8__ScriptFullName="$0"
9__ScriptArgs="$*"
Filip Pytlouna1898572016-01-25 22:35:13 +010010set -e
11[ -n "$DEBUG" ] && set -x
Filip Pytlouna1898572016-01-25 22:35:13 +010012CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
13METADATA=${CURDIR}/../metadata.yml
14FORMULA_NAME=$(cat $METADATA | python -c "import sys,yaml; print yaml.load(sys.stdin)['name']")
Dennis Dmitriev4bf87622017-07-06 21:07:27 +030015FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
Filip Pytlouna1898572016-01-25 22:35:13 +010016## Overrideable parameters
17PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
18BUILDDIR=${BUILDDIR:-${CURDIR}/build}
19VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
Dennis Dmitriev4bf87622017-07-06 21:07:27 +030020MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
Filip Pytlouna1898572016-01-25 22:35:13 +010021DEPSDIR=${BUILDDIR}/deps
Dennis Dmitriev4bf87622017-07-06 21:07:27 +030022SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"}
Filip Pytlouna1898572016-01-25 22:35:13 +010023SALT_FILE_DIR=${SALT_FILE_DIR:-${BUILDDIR}/file_root}
24SALT_PILLAR_DIR=${SALT_PILLAR_DIR:-${BUILDDIR}/pillar_root}
25SALT_CONFIG_DIR=${SALT_CONFIG_DIR:-${BUILDDIR}/salt}
26SALT_CACHE_DIR=${SALT_CACHE_DIR:-${SALT_CONFIG_DIR}/cache}
Dennis Dmitriev4bf87622017-07-06 21:07:27 +030027SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods}
Dennis Dmitriev4bf87622017-07-06 21:07:27 +030028SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
Dennis Dmitriev4bf87622017-07-06 21:07:27 +030029IGNORE_MODELVALIDATE_MASK=${IGNORE_MODELVALIDATE_MASK:-"novalidate"}
Filip Pytlouna1898572016-01-25 22:35:13 +010030if [ "x${SALT_VERSION}" != "x" ]; then
31 PIP_SALT_VERSION="==${SALT_VERSION}"
32fi
Filip Pytlouna1898572016-01-25 22:35:13 +010033## Functions
34log_info() {
Dennis Dmitriev4bf87622017-07-06 21:07:27 +030035 echo -e "[INFO] $*"
Filip Pytlouna1898572016-01-25 22:35:13 +010036}
Filip Pytlouna1898572016-01-25 22:35:13 +010037log_err() {
Dennis Dmitriev4bf87622017-07-06 21:07:27 +030038 echo -e "[ERROR] $*" >&2
Filip Pytlouna1898572016-01-25 22:35:13 +010039}
Filip Pytlouna1898572016-01-25 22:35:13 +010040setup_virtualenv() {
41 log_info "Setting up Python virtualenv"
Dennis Dmitriev4bf87622017-07-06 21:07:27 +030042 dependency_check virtualenv
Filip Pytlouna1898572016-01-25 22:35:13 +010043 virtualenv $VENV_DIR
44 source ${VENV_DIR}/bin/activate
Jakub Josef8e994f92018-02-08 13:22:17 +010045 python -m pip install salt${PIP_SALT_VERSION}
Dennis Dmitriev4bf87622017-07-06 21:07:27 +030046 if [[ -f ${CURDIR}/test-requirements.txt ]]; then
47 python -m pip install -r ${CURDIR}/test-requirements.txt
48 fi
49}
Dennis Dmitriev4bf87622017-07-06 21:07:27 +030050setup_mock_bin() {
51 # If some state requires a binary, a lightweight replacement for
52 # such binary can be put into MOCK_BIN_DIR for test purposes
53 if [ -d "${MOCK_BIN_DIR}" ]; then
54 PATH="${MOCK_BIN_DIR}:$PATH"
55 export PATH
56 fi
Filip Pytlouna1898572016-01-25 22:35:13 +010057}
Filip Pytlouna1898572016-01-25 22:35:13 +010058setup_pillar() {
59 [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR}
60 echo "base:" > ${SALT_PILLAR_DIR}/top.sls
61 for pillar in ${PILLARDIR}/*; do
Dennis Dmitriev4bf87622017-07-06 21:07:27 +030062 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Filip Pytlouna1898572016-01-25 22:35:13 +010063 state_name=$(basename ${pillar%.sls})
64 echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
65 done
Oleksii Molchanove465aeb2020-05-18 23:47:10 +030066 for pillar in $(find $PILLARDIR -mindepth 2 -type f -iname *.sls); do
67 state_name=$(basename "${pillar%*.sls}")
68 os_release=$(echo $pillar | rev | cut -d'/' -f2 | rev)
69 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
70 echo -e " ${os_release}_${state_name}:\n - ${os_release}.${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
71 done
Filip Pytlouna1898572016-01-25 22:35:13 +010072}
Filip Pytlouna1898572016-01-25 22:35:13 +010073setup_salt() {
74 [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR}
75 [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR}
76 [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR}
Dennis Dmitriev4bf87622017-07-06 21:07:27 +030077 [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR}
Filip Pytlouna1898572016-01-25 22:35:13 +010078 echo "base:" > ${SALT_FILE_DIR}/top.sls
79 for pillar in ${PILLARDIR}/*.sls; do
Dennis Dmitriev4bf87622017-07-06 21:07:27 +030080 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Filip Pytlouna1898572016-01-25 22:35:13 +010081 state_name=$(basename ${pillar%.sls})
82 echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
83 done
Oleksii Molchanove465aeb2020-05-18 23:47:10 +030084 for pillar in $(find $PILLARDIR -mindepth 2 -type f -iname *.sls); do
85 state_name=$(basename "${pillar%*.sls}")
86 os_release=$(echo $pillar | rev | cut -d'/' -f2 | rev)
87 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
88 echo -e " ${os_release}_${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
89 done
Filip Pytlouna1898572016-01-25 22:35:13 +010090 cat << EOF > ${SALT_CONFIG_DIR}/minion
91file_client: local
92cachedir: ${SALT_CACHE_DIR}
Dennis Dmitriev4bf87622017-07-06 21:07:27 +030093extension_modules: ${SALT_CACHE_EXTMODS_DIR}
Filip Pytlouna1898572016-01-25 22:35:13 +010094verify_env: False
Dennis Dmitriev4bf87622017-07-06 21:07:27 +030095minion_id_caching: False
Filip Pytlouna1898572016-01-25 22:35:13 +010096file_roots:
97 base:
98 - ${SALT_FILE_DIR}
99 - ${CURDIR}/..
Filip Pytlouna1898572016-01-25 22:35:13 +0100100pillar_roots:
101 base:
102 - ${SALT_PILLAR_DIR}
103 - ${PILLARDIR}
104EOF
105}
Filip Pytlouna1898572016-01-25 22:35:13 +0100106fetch_dependency() {
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300107 # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux"
Filip Pytloun35a38332016-04-14 11:43:20 +0200108 dep_name="$(echo $1|cut -d : -f 1)"
Filip Pytloun0aaf5af2016-04-14 16:39:19 +0200109 dep_source="$(echo $1|cut -d : -f 2-)"
Filip Pytloun35a38332016-04-14 11:43:20 +0200110 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
Filip Pytlouna1898572016-01-25 22:35:13 +0100111 dep_metadata="${dep_root}/metadata.yml"
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300112 dependency_check git
113 [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; }
Filip Pytloun35a38332016-04-14 11:43:20 +0200114 log_info "Fetching dependency $dep_name"
Filip Pytlouna1898572016-01-25 22:35:13 +0100115 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
Filip Pytloun9f3a3912016-04-14 12:46:30 +0200116 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
Filip Pytlouna1898572016-01-25 22:35:13 +0100117 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
Filip Pytlouna1898572016-01-25 22:35:13 +0100118 METADATA="${dep_metadata}" install_dependencies
119}
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300120link_modules(){
121 # Link modules *.py files to temporary salt-root
122 local SALT_ROOT=${1:-$SALT_FILE_DIR}
123 local SALT_ENV=${2:-$DEPSDIR}
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300124 mkdir -p "${SALT_ROOT}/_modules/"
125 # from git, development versions
126 find ${SALT_ENV} -maxdepth 3 -mindepth 3 -path '*_modules*' -iname "*.py" -type f -print0 | while read -d $'\0' file; do
127 ln -fs $(readlink -e ${file}) "$SALT_ROOT"/_modules/$(basename ${file}) ;
128 done
129 salt_run saltutil.sync_all
130}
Filip Pytlouna1898572016-01-25 22:35:13 +0100131install_dependencies() {
132 grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
133 (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
134import sys,yaml
135for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
Filip Pytloun35a38332016-04-14 11:43:20 +0200136 print '%s:%s' % (dep["name"], dep["source"])
Filip Pytlouna1898572016-01-25 22:35:13 +0100137EOF
138}
Filip Pytlouna1898572016-01-25 22:35:13 +0100139clean() {
140 log_info "Cleaning up ${BUILDDIR}"
141 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
142}
Filip Pytlouna1898572016-01-25 22:35:13 +0100143salt_run() {
Jakub Josefd9fffba2017-12-15 16:58:30 +0100144 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
Jakub Josef8e994f92018-02-08 13:22:17 +0100145 python $(which salt-call) ${SALT_OPTS} $*
Filip Pytlouna1898572016-01-25 22:35:13 +0100146}
Filip Pytlouna1898572016-01-25 22:35:13 +0100147prepare() {
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300148 if [[ -f ${BUILDDIR}/.prepare_done ]]; then
149 log_info "${BUILDDIR}/.prepare_done exist, not rebuilding BUILDDIR"
150 return
151 fi
152 [[ -d ${BUILDDIR} ]] && mkdir -p ${BUILDDIR}
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300153 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
154 setup_mock_bin
Filip Pytlouna1898572016-01-25 22:35:13 +0100155 setup_pillar
156 setup_salt
157 install_dependencies
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300158 link_modules
159 touch ${BUILDDIR}/.prepare_done
160}
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300161lint_releasenotes() {
162 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
163 source ${VENV_DIR}/bin/activate
164 reno lint ${CURDIR}/../
165}
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300166lint() {
167# lint_releasenotes
168 log_err "TODO: lint_releasenotes"
Filip Pytlouna1898572016-01-25 22:35:13 +0100169}
Filip Pytlouna1898572016-01-25 22:35:13 +0100170run() {
171 for pillar in ${PILLARDIR}/*.sls; do
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300172 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Filip Pytlouna1898572016-01-25 22:35:13 +0100173 state_name=$(basename ${pillar%.sls})
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300174 salt_run grains.set 'noservices' False force=True
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300175 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
Filip Pytlouna1898572016-01-25 22:35:13 +0100176 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300177 # Check that all files in 'meta' folder can be rendered using any valid pillar
178 for meta in `find ${FORMULA_META_DIR} -type f`; do
179 meta_name=$(basename ${meta})
180 echo "Checking meta ${meta_name} ..."
181 salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
182 || { log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1; }
183 cat ${SALT_CACHE_DIR}/${meta_name}
184 done
Filip Pytlouna1898572016-01-25 22:35:13 +0100185 done
186}
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300187real_run() {
188 for pillar in ${PILLARDIR}/*.sls; do
189 state_name=$(basename ${pillar%.sls})
190 salt_run --id=${state_name} state.sls ${FORMULA_NAME} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1; }
191 done
192}
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300193run_model_validate(){
194 # Run modelschema.model_validate validation.
195 # TEST iterateble, run for `each formula ROLE against each ROLE_PILLARNAME`
196 # Pillars should be named in conviend ROLE_XXX.sls or ROLE.sls
197 # Example:
198 # client.sls client_auth.sls server.sls server_auth.sls
199 if [ -d ${SCHEMARDIR} ]; then
200 # model validator require py modules
201 fetch_dependency "salt:https://github.com/salt-formulas/salt-formula-salt"
202 link_modules
203 salt_run saltutil.clear_cache; salt_run saltutil.refresh_pillar; salt_run saltutil.sync_all;
Oleksii Molchanove465aeb2020-05-18 23:47:10 +0300204 for role in $(find $SCHEMARDIR/* -maxdepth 0 -type f -iname *.yaml); do
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300205 role_name=$(basename "${role%*.yaml}")
206 for pillar in $(ls pillar/${role_name}*.sls | grep -v ${IGNORE_MODELVALIDATE_MASK} ); do
207 pillar_name=$(basename "${pillar%*.sls}")
208 local _message="FORMULA:${FORMULA_NAME} ROLE:${role_name} against PILLAR:${pillar_name}"
209 log_info "model_validate ${_message}"
210 # Rendered Example:
211 # python $(which salt-call) --local -c /test1/maas/tests/build/salt --id=maas_cluster modelschema.model_validate maas cluster
212 salt_run -m ${DEPSDIR}/salt-formula-salt --id=${pillar_name} modelschema.model_validate ${FORMULA_NAME} ${role_name} || { log_err "Execution of model_validate ${_message} failed"; exit 1 ; }
213 done
214 done
Oleksii Molchanove465aeb2020-05-18 23:47:10 +0300215 for schema in $(find $SCHEMARDIR -mindepth 2 -type f -iname *.yaml); do
216 role_name=$(basename "${schema%*.yaml}")
217 os_release=$(echo $schema | rev | cut -d'/' -f2 | rev)
218 local _message="FORMULA:${FORMULA_NAME} ROLE:${role_name} against PILLAR:${role_name}"
219 log_info "model_validate ${_message}"
220 salt_run -m ${DEPSDIR}/salt-formula-salt --id=${os_release}_${role_name} modelschema.model_validate ${FORMULA_NAME} ${role_name} ${os_release} || { log_err "Execution of model_validate ${_message} failed"; exit 1 ; }
221 done
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300222 else
223 log_info "${SCHEMARDIR} not found!";
224 fi
225}
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300226dependency_check() {
227 local DEPENDENCY_COMMANDS=$*
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300228 for DEPENDENCY_COMMAND in $DEPENDENCY_COMMANDS; do
229 which $DEPENDENCY_COMMAND > /dev/null || ( log_err "Command \"$DEPENDENCY_COMMAND\" can not be found in default path."; exit 1; )
230 done
231}
Filip Pytlouna1898572016-01-25 22:35:13 +0100232_atexit() {
233 RETVAL=$?
234 trap true INT TERM EXIT
Filip Pytlouna1898572016-01-25 22:35:13 +0100235 if [ $RETVAL -ne 0 ]; then
236 log_err "Execution failed"
237 else
238 log_info "Execution successful"
239 fi
240 return $RETVAL
241}
Filip Pytlouna1898572016-01-25 22:35:13 +0100242## Main
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300243log_info "Running version: ${__ScriptVersion}"
244log_info "Command line: '${__ScriptFullName} ${__ScriptArgs}'"
Filip Pytlouna1898572016-01-25 22:35:13 +0100245trap _atexit INT TERM EXIT
Filip Pytlouna1898572016-01-25 22:35:13 +0100246case $1 in
247 clean)
248 clean
249 ;;
250 prepare)
251 prepare
252 ;;
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300253 lint)
254 lint
255 ;;
Filip Pytlouna1898572016-01-25 22:35:13 +0100256 run)
257 run
258 ;;
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300259 real-run)
260 real_run
261 ;;
262 model-validate)
263 prepare
264 run_model_validate
265 ;;
Filip Pytlouna1898572016-01-25 22:35:13 +0100266 *)
267 prepare
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300268# lint
Filip Pytlouna1898572016-01-25 22:35:13 +0100269 run
Dennis Dmitriev4bf87622017-07-06 21:07:27 +0300270 run_model_validate
Filip Pytlouna1898572016-01-25 22:35:13 +0100271 ;;
272esac