blob: 461fd8b8b50aeae4ee1a8c2706d28f374746906d [file] [log] [blame]
Jiri Broulik634ea8a2017-05-31 17:10:03 +02001#!/usr/bin/env bash
2
azvyagintsevd7d89552018-08-20 15:15:00 +03003###
Pavel Cizinskyf5141b42019-02-22 14:08:28 +01004# Script source: https://gerrit.mcp.mirantis.com/#/admin/projects/salt-formulas/cookiecutter-salt-formula
azvyagintsevd7d89552018-08-20 15:15:00 +03005# Script requirments:
6#apt-get install -y python-yaml virtualenv git
7
Pavel Cizinskyf5141b42019-02-22 14:08:28 +01008__ScriptVersion="2018.11.21"
azvyagintsevd7d89552018-08-20 15:15:00 +03009__ScriptName="run_tests.sh"
10__ScriptFullName="$0"
11__ScriptArgs="$*"
12
Jiri Broulik634ea8a2017-05-31 17:10:03 +020013set -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']")
azvyagintsevd7d89552018-08-20 15:15:00 +030019FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
Jiri Broulik634ea8a2017-05-31 17:10:03 +020020
21## Overrideable parameters
22PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
23BUILDDIR=${BUILDDIR:-${CURDIR}/build}
24VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
azvyagintsevd7d89552018-08-20 15:15:00 +030025MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
Jiri Broulik634ea8a2017-05-31 17:10:03 +020026DEPSDIR=${BUILDDIR}/deps
azvyagintsevd7d89552018-08-20 15:15:00 +030027SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"}
Jiri Broulik634ea8a2017-05-31 17:10:03 +020028
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}
azvyagintsevd7d89552018-08-20 15:15:00 +030033SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods}
Jiri Broulik634ea8a2017-05-31 17:10:03 +020034
azvyagintsevd7d89552018-08-20 15:15:00 +030035SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
36
37IGNORE_MODELVALIDATE_MASK=${IGNORE_MODELVALIDATE_MASK:-"novalidate"}
Jiri Broulik634ea8a2017-05-31 17:10:03 +020038
39if [ "x${SALT_VERSION}" != "x" ]; then
40 PIP_SALT_VERSION="==${SALT_VERSION}"
41fi
42
43## Functions
44log_info() {
azvyagintsevd7d89552018-08-20 15:15:00 +030045 echo -e "[INFO] $*"
Jiri Broulik634ea8a2017-05-31 17:10:03 +020046}
47
48log_err() {
azvyagintsevd7d89552018-08-20 15:15:00 +030049 echo -e "[ERROR] $*" >&2
Jiri Broulik634ea8a2017-05-31 17:10:03 +020050}
51
52setup_virtualenv() {
53 log_info "Setting up Python virtualenv"
azvyagintsevd7d89552018-08-20 15:15:00 +030054 dependency_check virtualenv
Jiri Broulik634ea8a2017-05-31 17:10:03 +020055 virtualenv $VENV_DIR
56 source ${VENV_DIR}/bin/activate
azvyagintsevd7d89552018-08-20 15:15:00 +030057 python -m pip install salt${PIP_SALT_VERSION}
58 if [[ -f ${CURDIR}/test-requirements.txt ]]; then
59 python -m pip install -r ${CURDIR}/test-requirements.txt
60 fi
61}
62
63setup_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
Jiri Broulik634ea8a2017-05-31 17:10:03 +020070}
71
72setup_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
azvyagintsevd7d89552018-08-20 15:15:00 +030076 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Jiri Broulik634ea8a2017-05-31 17:10:03 +020077 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}
azvyagintsevd7d89552018-08-20 15:15:00 +030086 [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR}
Jiri Broulik634ea8a2017-05-31 17:10:03 +020087
88 echo "base:" > ${SALT_FILE_DIR}/top.sls
89 for pillar in ${PILLARDIR}/*.sls; do
azvyagintsevd7d89552018-08-20 15:15:00 +030090 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Jiri Broulik634ea8a2017-05-31 17:10:03 +020091 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}
azvyagintsevd7d89552018-08-20 15:15:00 +030098extension_modules: ${SALT_CACHE_EXTMODS_DIR}
Jiri Broulik634ea8a2017-05-31 17:10:03 +020099verify_env: False
azvyagintsevd7d89552018-08-20 15:15:00 +0300100minion_id_caching: False
Jiri Broulik634ea8a2017-05-31 17:10:03 +0200101
102file_roots:
103 base:
104 - ${SALT_FILE_DIR}
105 - ${CURDIR}/..
Jiri Broulik634ea8a2017-05-31 17:10:03 +0200106
107pillar_roots:
108 base:
109 - ${SALT_PILLAR_DIR}
110 - ${PILLARDIR}
111EOF
112}
113
114fetch_dependency() {
azvyagintsevd7d89552018-08-20 15:15:00 +0300115 # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux"
Jiri Broulik634ea8a2017-05-31 17:10:03 +0200116 dep_name="$(echo $1|cut -d : -f 1)"
117 dep_source="$(echo $1|cut -d : -f 2-)"
118 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
119 dep_metadata="${dep_root}/metadata.yml"
120
azvyagintsevd7d89552018-08-20 15:15:00 +0300121 dependency_check git
122 [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; }
Jiri Broulik634ea8a2017-05-31 17:10:03 +0200123
124 log_info "Fetching dependency $dep_name"
125 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
126 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
127 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
128
129 METADATA="${dep_metadata}" install_dependencies
130}
131
azvyagintsevd7d89552018-08-20 15:15:00 +0300132link_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
Jiri Broulik634ea8a2017-05-31 17:10:03 +0200145install_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']:
150 print '%s:%s' % (dep["name"], dep["source"])
151EOF
152}
153
154clean() {
155 log_info "Cleaning up ${BUILDDIR}"
156 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
157}
158
159salt_run() {
azvyagintsevd7d89552018-08-20 15:15:00 +0300160 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
161 python $(which salt-call) ${SALT_OPTS} $*
Jiri Broulik634ea8a2017-05-31 17:10:03 +0200162}
163
164prepare() {
azvyagintsevd7d89552018-08-20 15:15:00 +0300165 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}
Jiri Broulik634ea8a2017-05-31 17:10:03 +0200170
azvyagintsevd7d89552018-08-20 15:15:00 +0300171 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
172 setup_mock_bin
Jiri Broulik634ea8a2017-05-31 17:10:03 +0200173 setup_pillar
174 setup_salt
175 install_dependencies
azvyagintsevd7d89552018-08-20 15:15:00 +0300176 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"
Jiri Broulik634ea8a2017-05-31 17:10:03 +0200189}
190
191run() {
192 for pillar in ${PILLARDIR}/*.sls; do
azvyagintsevd7d89552018-08-20 15:15:00 +0300193 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Jiri Broulik634ea8a2017-05-31 17:10:03 +0200194 state_name=$(basename ${pillar%.sls})
azvyagintsevd7d89552018-08-20 15:15:00 +0300195 salt_run grains.set 'noservices' False force=True
196
197 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
Jiri Broulik634ea8a2017-05-31 17:10:03 +0200198 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
azvyagintsevd7d89552018-08-20 15:15:00 +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} \
205 || { log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1; }
206 cat ${SALT_CACHE_DIR}/${meta_name}
207 done
Jiri Broulik634ea8a2017-05-31 17:10:03 +0200208 done
209}
210
azvyagintsevd7d89552018-08-20 15:15:00 +0300211real_run() {
212 for pillar in ${PILLARDIR}/*.sls; do
213 state_name=$(basename ${pillar%.sls})
214 salt_run --id=${state_name} state.sls ${FORMULA_NAME} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1; }
215 done
216}
217
218run_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
Jiri Broulik634ea8a2017-05-31 17:10:03 +0200253_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
azvyagintsevd7d89552018-08-20 15:15:00 +0300266
267log_info "Running version: ${__ScriptVersion}"
268log_info "Command line: '${__ScriptFullName} ${__ScriptArgs}'"
269
Jiri Broulik634ea8a2017-05-31 17:10:03 +0200270trap _atexit INT TERM EXIT
271
272case $1 in
273 clean)
274 clean
275 ;;
276 prepare)
277 prepare
278 ;;
azvyagintsevd7d89552018-08-20 15:15:00 +0300279 lint)
280 lint
281 ;;
Jiri Broulik634ea8a2017-05-31 17:10:03 +0200282 run)
283 run
284 ;;
azvyagintsevd7d89552018-08-20 15:15:00 +0300285 real-run)
286 real_run
287 ;;
288 model-validate)
289 prepare
290 run_model_validate
291 ;;
Jiri Broulik634ea8a2017-05-31 17:10:03 +0200292 *)
293 prepare
azvyagintsevd7d89552018-08-20 15:15:00 +0300294# lint
Jiri Broulik634ea8a2017-05-31 17:10:03 +0200295 run
azvyagintsevd7d89552018-08-20 15:15:00 +0300296 run_model_validate
Jiri Broulik634ea8a2017-05-31 17:10:03 +0200297 ;;
298esac