blob: 36c0c9798698c98e8f9d0464e8e03fa41ef5c800 [file] [log] [blame]
Ales Komarek16bd2532016-01-25 23:05:29 +01001#!/usr/bin/env bash
2
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +01003###
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
Ales Komarek16bd2532016-01-25 23:05:29 +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']")
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +010019FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
Ales Komarek16bd2532016-01-25 23:05:29 +010020
21## Overrideable parameters
22PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
23BUILDDIR=${BUILDDIR:-${CURDIR}/build}
24VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +010025MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
Ales Komarek16bd2532016-01-25 23:05:29 +010026DEPSDIR=${BUILDDIR}/deps
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +010027SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"}
Ales Komarek16bd2532016-01-25 23:05:29 +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}
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +010033SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods}
Ales Komarek16bd2532016-01-25 23:05:29 +010034
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +010035SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
36
37IGNORE_MODELVALIDATE_MASK=${IGNORE_MODELVALIDATE_MASK:-"novalidate"}
Ales Komarek16bd2532016-01-25 23:05:29 +010038
39if [ "x${SALT_VERSION}" != "x" ]; then
40 PIP_SALT_VERSION="==${SALT_VERSION}"
41fi
42
43## Functions
44log_info() {
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +010045 echo -e "[INFO] $*"
Ales Komarek16bd2532016-01-25 23:05:29 +010046}
47
48log_err() {
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +010049 echo -e "[ERROR] $*" >&2
Ales Komarek16bd2532016-01-25 23:05:29 +010050}
51
52setup_virtualenv() {
53 log_info "Setting up Python virtualenv"
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +010054 dependency_check virtualenv
Ales Komarek16bd2532016-01-25 23:05:29 +010055 virtualenv $VENV_DIR
56 source ${VENV_DIR}/bin/activate
Jakub Josef605e77d2018-02-08 13:22:20 +010057 python -m pip install salt${PIP_SALT_VERSION}
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +010058 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
Ales Komarek16bd2532016-01-25 23:05:29 +010070}
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
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +010076 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Ales Komarek16bd2532016-01-25 23:05:29 +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}
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +010086 [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR}
Ales Komarek16bd2532016-01-25 23:05:29 +010087
88 echo "base:" > ${SALT_FILE_DIR}/top.sls
89 for pillar in ${PILLARDIR}/*.sls; do
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +010090 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Ales Komarek16bd2532016-01-25 23:05:29 +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}
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +010098extension_modules: ${SALT_CACHE_EXTMODS_DIR}
Ales Komarek16bd2532016-01-25 23:05:29 +010099verify_env: False
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +0100100minion_id_caching: False
Ales Komarek16bd2532016-01-25 23:05:29 +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() {
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +0100115 # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux"
Filip Pytlouna09370f2016-04-14 11:51:09 +0200116 dep_name="$(echo $1|cut -d : -f 1)"
Filip Pytloune3b04922016-04-14 16:39:49 +0200117 dep_source="$(echo $1|cut -d : -f 2-)"
Filip Pytlouna09370f2016-04-14 11:51:09 +0200118 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
Ales Komarek16bd2532016-01-25 23:05:29 +0100119 dep_metadata="${dep_root}/metadata.yml"
120
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +0100121 dependency_check git
122 [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; }
Ales Komarek16bd2532016-01-25 23:05:29 +0100123
Filip Pytlouna09370f2016-04-14 11:51:09 +0200124 log_info "Fetching dependency $dep_name"
Ales Komarek16bd2532016-01-25 23:05:29 +0100125 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
Filip Pytloun29522992016-04-14 12:46:30 +0200126 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
Ales Komarek16bd2532016-01-25 23:05:29 +0100127 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
128
129 METADATA="${dep_metadata}" install_dependencies
130}
131
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +0100132link_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
Ales Komarek16bd2532016-01-25 23:05:29 +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 Pytlouna09370f2016-04-14 11:51:09 +0200150 print '%s:%s' % (dep["name"], dep["source"])
Ales Komarek16bd2532016-01-25 23:05:29 +0100151EOF
152}
153
154clean() {
155 log_info "Cleaning up ${BUILDDIR}"
156 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
157}
158
159salt_run() {
Jakub Josef6f2f0142017-12-15 16:58:30 +0100160 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
Jakub Josef605e77d2018-02-08 13:22:20 +0100161 python $(which salt-call) ${SALT_OPTS} $*
Ales Komarek16bd2532016-01-25 23:05:29 +0100162}
163
164prepare() {
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +0100165 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}
Ales Komarek16bd2532016-01-25 23:05:29 +0100170
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +0100171 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
172 setup_mock_bin
Ales Komarek16bd2532016-01-25 23:05:29 +0100173 setup_pillar
174 setup_salt
175 install_dependencies
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +0100176 link_modules
177 # Patch for load grains issue - https://github.com/saltstack/salt/commit/211c88bfbc8158b7ca16e429b366e403edfd39f3
178 sed -i 's/def find_module(self, module_name, package_path):/def find_module(self, module_name, package_path=None):/' ./build/virtualenv/lib/python2.7/site-packages/salt/__init__.py
179 touch ${BUILDDIR}/.prepare_done
180}
181
182lint_releasenotes() {
183 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
184 source ${VENV_DIR}/bin/activate
185 reno lint ${CURDIR}/../
186}
187
188lint() {
189# lint_releasenotes
190 log_err "TODO: lint_releasenotes"
Ales Komarek16bd2532016-01-25 23:05:29 +0100191}
192
193run() {
194 for pillar in ${PILLARDIR}/*.sls; do
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +0100195 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Ales Komarek16bd2532016-01-25 23:05:29 +0100196 state_name=$(basename ${pillar%.sls})
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +0100197 salt_run grains.set 'noservices' False force=True
198
199 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
Ales Komarek16bd2532016-01-25 23:05:29 +0100200 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +0100201
202 # Check that all files in 'meta' folder can be rendered using any valid pillar
203 for meta in `find ${FORMULA_META_DIR} -type f`; do
204 meta_name=$(basename ${meta})
205 echo "Checking meta ${meta_name} ..."
206 salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
207 || { log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1; }
208 cat ${SALT_CACHE_DIR}/${meta_name}
209 done
Ales Komarek16bd2532016-01-25 23:05:29 +0100210 done
211}
212
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +0100213real_run() {
214 for pillar in ${PILLARDIR}/*.sls; do
215 state_name=$(basename ${pillar%.sls})
216 salt_run --id=${state_name} state.sls ${FORMULA_NAME} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1; }
217 done
218}
219
220run_model_validate(){
221 # Run modelschema.model_validate validation.
222 # TEST iterateble, run for `each formula ROLE against each ROLE_PILLARNAME`
223 # Pillars should be named in conviend ROLE_XXX.sls or ROLE.sls
224 # Example:
225 # client.sls client_auth.sls server.sls server_auth.sls
226 if [ -d ${SCHEMARDIR} ]; then
227 # model validator require py modules
228 fetch_dependency "salt:https://github.com/salt-formulas/salt-formula-salt"
229 link_modules
230 salt_run saltutil.clear_cache; salt_run saltutil.refresh_pillar; salt_run saltutil.sync_all;
231 for role in ${SCHEMARDIR}/*.yaml; do
232 role_name=$(basename "${role%*.yaml}")
233 for pillar in $(ls pillar/${role_name}*.sls | grep -v ${IGNORE_MODELVALIDATE_MASK} ); do
234 pillar_name=$(basename "${pillar%*.sls}")
235 local _message="FORMULA:${FORMULA_NAME} ROLE:${role_name} against PILLAR:${pillar_name}"
236 log_info "model_validate ${_message}"
237 # Rendered Example:
238 # python $(which salt-call) --local -c /test1/maas/tests/build/salt --id=maas_cluster modelschema.model_validate maas cluster
239 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 ; }
240 done
241 done
242 else
243 log_info "${SCHEMARDIR} not found!";
244 fi
245}
246
247dependency_check() {
248 local DEPENDENCY_COMMANDS=$*
249
250 for DEPENDENCY_COMMAND in $DEPENDENCY_COMMANDS; do
251 which $DEPENDENCY_COMMAND > /dev/null || ( log_err "Command \"$DEPENDENCY_COMMAND\" can not be found in default path."; exit 1; )
252 done
253}
254
Ales Komarek16bd2532016-01-25 23:05:29 +0100255_atexit() {
256 RETVAL=$?
257 trap true INT TERM EXIT
258
259 if [ $RETVAL -ne 0 ]; then
260 log_err "Execution failed"
261 else
262 log_info "Execution successful"
263 fi
264 return $RETVAL
265}
266
267## Main
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +0100268
269log_info "Running version: ${__ScriptVersion}"
270log_info "Command line: '${__ScriptFullName} ${__ScriptArgs}'"
271
Ales Komarek16bd2532016-01-25 23:05:29 +0100272trap _atexit INT TERM EXIT
273
274case $1 in
275 clean)
276 clean
277 ;;
278 prepare)
279 prepare
280 ;;
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +0100281 lint)
282 lint
283 ;;
Ales Komarek16bd2532016-01-25 23:05:29 +0100284 run)
285 run
286 ;;
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +0100287 real-run)
288 real_run
289 ;;
290 model-validate)
291 prepare
292 run_model_validate
293 ;;
Ales Komarek16bd2532016-01-25 23:05:29 +0100294 *)
295 prepare
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +0100296# lint
Ales Komarek16bd2532016-01-25 23:05:29 +0100297 run
Dzmitry Stremkouski1ce1b352019-11-02 10:47:14 +0100298# run_model_validate
Ales Komarek16bd2532016-01-25 23:05:29 +0100299 ;;
300esac