blob: 461fd8b8b50aeae4ee1a8c2706d28f374746906d [file] [log] [blame]
jan kaufman35d74f62016-01-25 22:56:05 +01001#!/usr/bin/env bash
2
azvyagintsev6631f372018-11-21 18:36:00 +02003###
4# Script source: https://gerrit.mcp.mirantis.com/#/admin/projects/salt-formulas/cookiecutter-salt-formula
5# Script requirments:
6#apt-get install -y python-yaml virtualenv git
7
8__ScriptVersion="2018.11.21"
9__ScriptName="run_tests.sh"
10__ScriptFullName="$0"
11__ScriptArgs="$*"
12
jan kaufman35d74f62016-01-25 22:56:05 +010013set -e
14[ -n "$DEBUG" ] && set -x
15
16CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
17METADATA=${CURDIR}/../metadata.yml
18FORMULA_NAME=$(cat $METADATA | python -c "import sys,yaml; print yaml.load(sys.stdin)['name']")
Dennis Dmitrievbb2eff82017-07-06 21:07:27 +030019FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
jan kaufman35d74f62016-01-25 22:56:05 +010020
21## Overrideable parameters
22PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
23BUILDDIR=${BUILDDIR:-${CURDIR}/build}
24VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
Dennis Dmitrievbb2eff82017-07-06 21:07:27 +030025MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
jan kaufman35d74f62016-01-25 22:56:05 +010026DEPSDIR=${BUILDDIR}/deps
azvyagintsev6631f372018-11-21 18:36:00 +020027SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"}
jan kaufman35d74f62016-01-25 22:56:05 +010028
29SALT_FILE_DIR=${SALT_FILE_DIR:-${BUILDDIR}/file_root}
30SALT_PILLAR_DIR=${SALT_PILLAR_DIR:-${BUILDDIR}/pillar_root}
31SALT_CONFIG_DIR=${SALT_CONFIG_DIR:-${BUILDDIR}/salt}
32SALT_CACHE_DIR=${SALT_CACHE_DIR:-${SALT_CONFIG_DIR}/cache}
azvyagintsev6631f372018-11-21 18:36:00 +020033SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods}
jan kaufman35d74f62016-01-25 22:56:05 +010034
Dennis Dmitrievbb2eff82017-07-06 21:07:27 +030035SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
jan kaufman35d74f62016-01-25 22:56:05 +010036
azvyagintsev6631f372018-11-21 18:36:00 +020037IGNORE_MODELVALIDATE_MASK=${IGNORE_MODELVALIDATE_MASK:-"novalidate"}
38
jan kaufman35d74f62016-01-25 22:56:05 +010039if [ "x${SALT_VERSION}" != "x" ]; then
40 PIP_SALT_VERSION="==${SALT_VERSION}"
41fi
42
43## Functions
44log_info() {
azvyagintsev6631f372018-11-21 18:36:00 +020045 echo -e "[INFO] $*"
jan kaufman35d74f62016-01-25 22:56:05 +010046}
47
48log_err() {
azvyagintsev6631f372018-11-21 18:36:00 +020049 echo -e "[ERROR] $*" >&2
jan kaufman35d74f62016-01-25 22:56:05 +010050}
51
52setup_virtualenv() {
53 log_info "Setting up Python virtualenv"
azvyagintsev6631f372018-11-21 18:36:00 +020054 dependency_check virtualenv
jan kaufman35d74f62016-01-25 22:56:05 +010055 virtualenv $VENV_DIR
56 source ${VENV_DIR}/bin/activate
Jakub Josef2840f312018-02-08 13:22:14 +010057 python -m pip install salt${PIP_SALT_VERSION}
azvyagintsev6631f372018-11-21 18:36:00 +020058 if [[ -f ${CURDIR}/test-requirements.txt ]]; then
59 python -m pip install -r ${CURDIR}/test-requirements.txt
60 fi
jan kaufman35d74f62016-01-25 22:56:05 +010061}
62
Dennis Dmitrievbb2eff82017-07-06 21:07:27 +030063setup_mock_bin() {
64 # If some state requires a binary, a lightweight replacement for
65 # such binary can be put into MOCK_BIN_DIR for test purposes
66 if [ -d "${MOCK_BIN_DIR}" ]; then
67 PATH="${MOCK_BIN_DIR}:$PATH"
68 export PATH
69 fi
70}
71
jan kaufman35d74f62016-01-25 22:56:05 +010072setup_pillar() {
73 [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR}
74 echo "base:" > ${SALT_PILLAR_DIR}/top.sls
75 for pillar in ${PILLARDIR}/*; do
Dennis Dmitrievbb2eff82017-07-06 21:07:27 +030076 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
jan kaufman35d74f62016-01-25 22:56:05 +010077 state_name=$(basename ${pillar%.sls})
78 echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
79 done
80}
81
82setup_salt() {
83 [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR}
84 [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR}
85 [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR}
azvyagintsev6631f372018-11-21 18:36:00 +020086 [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR}
jan kaufman35d74f62016-01-25 22:56:05 +010087
88 echo "base:" > ${SALT_FILE_DIR}/top.sls
89 for pillar in ${PILLARDIR}/*.sls; do
Dennis Dmitrievbb2eff82017-07-06 21:07:27 +030090 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
jan kaufman35d74f62016-01-25 22:56:05 +010091 state_name=$(basename ${pillar%.sls})
92 echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
93 done
94
95 cat << EOF > ${SALT_CONFIG_DIR}/minion
96file_client: local
97cachedir: ${SALT_CACHE_DIR}
azvyagintsev6631f372018-11-21 18:36:00 +020098extension_modules: ${SALT_CACHE_EXTMODS_DIR}
jan kaufman35d74f62016-01-25 22:56:05 +010099verify_env: False
Dennis Dmitrievbb2eff82017-07-06 21:07:27 +0300100minion_id_caching: False
jan kaufman35d74f62016-01-25 22:56:05 +0100101
102file_roots:
103 base:
104 - ${SALT_FILE_DIR}
105 - ${CURDIR}/..
106
107pillar_roots:
108 base:
109 - ${SALT_PILLAR_DIR}
110 - ${PILLARDIR}
111EOF
112}
113
114fetch_dependency() {
azvyagintsev6631f372018-11-21 18:36:00 +0200115 # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux"
Filip Pytloun6eef1cb2016-04-14 11:48:39 +0200116 dep_name="$(echo $1|cut -d : -f 1)"
Filip Pytloune3527e72016-04-14 16:38:42 +0200117 dep_source="$(echo $1|cut -d : -f 2-)"
Filip Pytloun6eef1cb2016-04-14 11:48:39 +0200118 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
jan kaufman35d74f62016-01-25 22:56:05 +0100119 dep_metadata="${dep_root}/metadata.yml"
120
azvyagintsev6631f372018-11-21 18:36:00 +0200121 dependency_check git
122 [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; }
jan kaufman35d74f62016-01-25 22:56:05 +0100123
Filip Pytloun6eef1cb2016-04-14 11:48:39 +0200124 log_info "Fetching dependency $dep_name"
jan kaufman35d74f62016-01-25 22:56:05 +0100125 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
Filip Pytlounec0a6d72016-04-14 12:46:30 +0200126 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
jan kaufman35d74f62016-01-25 22:56:05 +0100127 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
128
129 METADATA="${dep_metadata}" install_dependencies
130}
131
azvyagintsev6631f372018-11-21 18:36:00 +0200132link_modules(){
133 # Link modules *.py files to temporary salt-root
134 local SALT_ROOT=${1:-$SALT_FILE_DIR}
135 local SALT_ENV=${2:-$DEPSDIR}
136
137 mkdir -p "${SALT_ROOT}/_modules/"
138 # from git, development versions
139 find ${SALT_ENV} -maxdepth 3 -mindepth 3 -path '*_modules*' -iname "*.py" -type f -print0 | while read -d $'\0' file; do
140 ln -fs $(readlink -e ${file}) "$SALT_ROOT"/_modules/$(basename ${file}) ;
141 done
142 salt_run saltutil.sync_all
143}
144
jan kaufman35d74f62016-01-25 22:56:05 +0100145install_dependencies() {
146 grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
147 (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
148import sys,yaml
149for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
Filip Pytloun6eef1cb2016-04-14 11:48:39 +0200150 print '%s:%s' % (dep["name"], dep["source"])
jan kaufman35d74f62016-01-25 22:56:05 +0100151EOF
152}
153
154clean() {
155 log_info "Cleaning up ${BUILDDIR}"
156 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
157}
158
159salt_run() {
Jakub Joseff88eb972017-12-15 16:58:29 +0100160 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
Jakub Josef2840f312018-02-08 13:22:14 +0100161 python $(which salt-call) ${SALT_OPTS} $*
jan kaufman35d74f62016-01-25 22:56:05 +0100162}
163
164prepare() {
azvyagintsev6631f372018-11-21 18:36:00 +0200165 if [[ -f ${BUILDDIR}/.prepare_done ]]; then
166 log_info "${BUILDDIR}/.prepare_done exist, not rebuilding BUILDDIR"
167 return
168 fi
169 [[ -d ${BUILDDIR} ]] && mkdir -p ${BUILDDIR}
jan kaufman35d74f62016-01-25 22:56:05 +0100170
azvyagintsev6631f372018-11-21 18:36:00 +0200171 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
Dennis Dmitrievbb2eff82017-07-06 21:07:27 +0300172 setup_mock_bin
jan kaufman35d74f62016-01-25 22:56:05 +0100173 setup_pillar
174 setup_salt
175 install_dependencies
azvyagintsev6631f372018-11-21 18:36:00 +0200176 link_modules
177 touch ${BUILDDIR}/.prepare_done
178}
179
180lint_releasenotes() {
181 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
182 source ${VENV_DIR}/bin/activate
183 reno lint ${CURDIR}/../
184}
185
186lint() {
187# lint_releasenotes
188 log_err "TODO: lint_releasenotes"
jan kaufman35d74f62016-01-25 22:56:05 +0100189}
190
191run() {
192 for pillar in ${PILLARDIR}/*.sls; do
Dennis Dmitrievbb2eff82017-07-06 21:07:27 +0300193 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
jan kaufman35d74f62016-01-25 22:56:05 +0100194 state_name=$(basename ${pillar%.sls})
Dennis Dmitrievbb2eff82017-07-06 21:07:27 +0300195 salt_run grains.set 'noservices' False force=True
196
197 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
jan kaufman35d74f62016-01-25 22:56:05 +0100198 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
Dennis Dmitrievbb2eff82017-07-06 21:07:27 +0300199
200 # Check that all files in 'meta' folder can be rendered using any valid pillar
201 for meta in `find ${FORMULA_META_DIR} -type f`; do
202 meta_name=$(basename ${meta})
203 echo "Checking meta ${meta_name} ..."
204 salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
azvyagintsev6631f372018-11-21 18:36:00 +0200205 || { log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1; }
Dennis Dmitrievbb2eff82017-07-06 21:07:27 +0300206 cat ${SALT_CACHE_DIR}/${meta_name}
207 done
208 done
209}
210
211real_run() {
212 for pillar in ${PILLARDIR}/*.sls; do
213 state_name=$(basename ${pillar%.sls})
azvyagintsev6631f372018-11-21 18:36:00 +0200214 salt_run --id=${state_name} state.sls ${FORMULA_NAME} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1; }
jan kaufman35d74f62016-01-25 22:56:05 +0100215 done
216}
217
azvyagintsev6631f372018-11-21 18:36:00 +0200218run_model_validate(){
219 # Run modelschema.model_validate validation.
220 # TEST iterateble, run for `each formula ROLE against each ROLE_PILLARNAME`
221 # Pillars should be named in conviend ROLE_XXX.sls or ROLE.sls
222 # Example:
223 # client.sls client_auth.sls server.sls server_auth.sls
224 if [ -d ${SCHEMARDIR} ]; then
225 # model validator require py modules
226 fetch_dependency "salt:https://github.com/salt-formulas/salt-formula-salt"
227 link_modules
228 salt_run saltutil.clear_cache; salt_run saltutil.refresh_pillar; salt_run saltutil.sync_all;
229 for role in ${SCHEMARDIR}/*.yaml; do
230 role_name=$(basename "${role%*.yaml}")
231 for pillar in $(ls pillar/${role_name}*.sls | grep -v ${IGNORE_MODELVALIDATE_MASK} ); do
232 pillar_name=$(basename "${pillar%*.sls}")
233 local _message="FORMULA:${FORMULA_NAME} ROLE:${role_name} against PILLAR:${pillar_name}"
234 log_info "model_validate ${_message}"
235 # Rendered Example:
236 # python $(which salt-call) --local -c /test1/maas/tests/build/salt --id=maas_cluster modelschema.model_validate maas cluster
237 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 ; }
238 done
239 done
240 else
241 log_info "${SCHEMARDIR} not found!";
242 fi
243}
244
245dependency_check() {
246 local DEPENDENCY_COMMANDS=$*
247
248 for DEPENDENCY_COMMAND in $DEPENDENCY_COMMANDS; do
249 which $DEPENDENCY_COMMAND > /dev/null || ( log_err "Command \"$DEPENDENCY_COMMAND\" can not be found in default path."; exit 1; )
250 done
251}
252
jan kaufman35d74f62016-01-25 22:56:05 +0100253_atexit() {
254 RETVAL=$?
255 trap true INT TERM EXIT
256
257 if [ $RETVAL -ne 0 ]; then
258 log_err "Execution failed"
259 else
260 log_info "Execution successful"
261 fi
262 return $RETVAL
263}
264
265## Main
azvyagintsev6631f372018-11-21 18:36:00 +0200266
267log_info "Running version: ${__ScriptVersion}"
268log_info "Command line: '${__ScriptFullName} ${__ScriptArgs}'"
269
jan kaufman35d74f62016-01-25 22:56:05 +0100270trap _atexit INT TERM EXIT
271
272case $1 in
273 clean)
274 clean
275 ;;
276 prepare)
277 prepare
278 ;;
azvyagintsev6631f372018-11-21 18:36:00 +0200279 lint)
280 lint
281 ;;
jan kaufman35d74f62016-01-25 22:56:05 +0100282 run)
283 run
284 ;;
Dennis Dmitrievbb2eff82017-07-06 21:07:27 +0300285 real-run)
286 real_run
287 ;;
azvyagintsev6631f372018-11-21 18:36:00 +0200288 model-validate)
289 prepare
290 run_model_validate
291 ;;
jan kaufman35d74f62016-01-25 22:56:05 +0100292 *)
293 prepare
azvyagintsev6631f372018-11-21 18:36:00 +0200294# lint
jan kaufman35d74f62016-01-25 22:56:05 +0100295 run
azvyagintsev6631f372018-11-21 18:36:00 +0200296 run_model_validate
jan kaufman35d74f62016-01-25 22:56:05 +0100297 ;;
298esac