blob: 4665d343bcfa6848f6af73f799e2b52127c8983e [file] [log] [blame]
Martin Polreich08ae6d22018-07-18 11:14:05 +02001#!/usr/bin/env bash
2
3###
Ivan Berezovskiy5b2759c2019-02-21 21:07:27 +04004# Script source: https://gerrit.mcp.mirantis.com/#/admin/projects/salt-formulas/cookiecutter-salt-formula
Martin Polreich08ae6d22018-07-18 11:14:05 +02005# Script requirments:
6#apt-get install -y python-yaml virtualenv git
7
Ivan Berezovskiy5b2759c2019-02-21 21:07:27 +04008__ScriptVersion="2019.01.07"
9__ScriptName="run_tests.sh"
10__ScriptFullName="$0"
11__ScriptArgs="$*"
12
Martin Polreich08ae6d22018-07-18 11:14:05 +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']")
19FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
20
21## Overrideable parameters
22PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
23BUILDDIR=${BUILDDIR:-${CURDIR}/build}
24VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
25MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
26DEPSDIR=${BUILDDIR}/deps
27SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"}
28
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}
33SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods}
34
35SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
36
Ivan Berezovskiy5b2759c2019-02-21 21:07:27 +040037IGNORE_MODELVALIDATE_MASK=${IGNORE_MODELVALIDATE_MASK:-"novalidate"}
38
Martin Polreich08ae6d22018-07-18 11:14:05 +020039if [ "x${SALT_VERSION}" != "x" ]; then
40 PIP_SALT_VERSION="==${SALT_VERSION}"
41fi
42
43## Functions
44log_info() {
45 echo -e "[INFO] $*"
46}
47
48log_err() {
49 echo -e "[ERROR] $*" >&2
50}
51
52setup_virtualenv() {
53 log_info "Setting up Python virtualenv"
54 dependency_check virtualenv
55 virtualenv $VENV_DIR
56 source ${VENV_DIR}/bin/activate
57 python -m pip install salt${PIP_SALT_VERSION}
Ivan Berezovskiy5b2759c2019-02-21 21:07:27 +040058 if [[ -f ${CURDIR}/test-requirements.txt ]]; then
59 python -m pip install -r ${CURDIR}/test-requirements.txt
Martin Polreich08ae6d22018-07-18 11:14:05 +020060 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
70}
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
76 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
77 state_name=$(basename ${pillar%.sls})
78 echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
79 done
Ivan Berezovskiy5b2759c2019-02-21 21:07:27 +040080 for pillar in $(find $PILLARDIR -mindepth 2 -type f -iname *.sls); do
81 state_name=$(basename "${pillar%*.sls}")
82 os_release=$(echo $pillar | rev | cut -d'/' -f2 | rev)
83 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
84 echo -e " ${os_release}_${state_name}:\n - ${os_release}.${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
85 done
Martin Polreich08ae6d22018-07-18 11:14:05 +020086}
87
88setup_salt() {
89 [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR}
90 [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR}
91 [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR}
92 [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR}
93
94 echo "base:" > ${SALT_FILE_DIR}/top.sls
95 for pillar in ${PILLARDIR}/*.sls; do
96 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
97 state_name=$(basename ${pillar%.sls})
98 echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
99 done
Ivan Berezovskiy5b2759c2019-02-21 21:07:27 +0400100 for pillar in $(find $PILLARDIR -mindepth 2 -type f -iname *.sls); do
101 state_name=$(basename "${pillar%*.sls}")
102 os_release=$(echo $pillar | rev | cut -d'/' -f2 | rev)
103 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
104 echo -e " ${os_release}_${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
105 done
Martin Polreich08ae6d22018-07-18 11:14:05 +0200106
107 cat << EOF > ${SALT_CONFIG_DIR}/minion
108file_client: local
109cachedir: ${SALT_CACHE_DIR}
110extension_modules: ${SALT_CACHE_EXTMODS_DIR}
111verify_env: False
112minion_id_caching: False
113
114file_roots:
115 base:
116 - ${SALT_FILE_DIR}
117 - ${CURDIR}/..
118
119pillar_roots:
120 base:
121 - ${SALT_PILLAR_DIR}
122 - ${PILLARDIR}
123EOF
124}
125
126fetch_dependency() {
127 # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux"
128 dep_name="$(echo $1|cut -d : -f 1)"
129 dep_source="$(echo $1|cut -d : -f 2-)"
130 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
131 dep_metadata="${dep_root}/metadata.yml"
132
133 dependency_check git
134 [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; }
135
136 log_info "Fetching dependency $dep_name"
137 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
138 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
139 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
140
141 METADATA="${dep_metadata}" install_dependencies
142}
143
144link_modules(){
145 # Link modules *.py files to temporary salt-root
146 local SALT_ROOT=${1:-$SALT_FILE_DIR}
147 local SALT_ENV=${2:-$DEPSDIR}
148
149 mkdir -p "${SALT_ROOT}/_modules/"
150 # from git, development versions
151 find ${SALT_ENV} -maxdepth 3 -mindepth 3 -path '*_modules*' -iname "*.py" -type f -print0 | while read -d $'\0' file; do
152 ln -fs $(readlink -e ${file}) "$SALT_ROOT"/_modules/$(basename ${file}) ;
153 done
154 salt_run saltutil.sync_all
155}
156
157install_dependencies() {
158 grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
159 (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
160import sys,yaml
161for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
162 print '%s:%s' % (dep["name"], dep["source"])
163EOF
164}
165
166clean() {
167 log_info "Cleaning up ${BUILDDIR}"
168 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
169}
170
171salt_run() {
172 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
173 python $(which salt-call) ${SALT_OPTS} $*
174}
175
176prepare() {
Ivan Berezovskiy5b2759c2019-02-21 21:07:27 +0400177 if [[ -f ${BUILDDIR}/.prepare_done ]]; then
178 log_info "${BUILDDIR}/.prepare_done exist, not rebuilding BUILDDIR"
179 return
180 fi
181 [[ -d ${BUILDDIR} ]] && mkdir -p ${BUILDDIR}
Martin Polreich08ae6d22018-07-18 11:14:05 +0200182
183 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
184 setup_mock_bin
185 setup_pillar
186 setup_salt
187 install_dependencies
188 link_modules
Ivan Berezovskiy5b2759c2019-02-21 21:07:27 +0400189 touch ${BUILDDIR}/.prepare_done
Martin Polreich08ae6d22018-07-18 11:14:05 +0200190}
191
192lint_releasenotes() {
193 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
194 source ${VENV_DIR}/bin/activate
Martin Polreich08ae6d22018-07-18 11:14:05 +0200195 reno lint ${CURDIR}/../
196}
197
198lint() {
199# lint_releasenotes
200 log_err "TODO: lint_releasenotes"
201}
202
203run() {
204 for pillar in ${PILLARDIR}/*.sls; do
205 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
206 state_name=$(basename ${pillar%.sls})
207 salt_run grains.set 'noservices' False force=True
208
209 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
210 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
211
212 # Check that all files in 'meta' folder can be rendered using any valid pillar
213 for meta in `find ${FORMULA_META_DIR} -type f`; do
214 meta_name=$(basename ${meta})
215 echo "Checking meta ${meta_name} ..."
216 salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
217 || { log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1; }
218 cat ${SALT_CACHE_DIR}/${meta_name}
219 done
220 done
221}
222
223real_run() {
224 for pillar in ${PILLARDIR}/*.sls; do
225 state_name=$(basename ${pillar%.sls})
226 salt_run --id=${state_name} state.sls ${FORMULA_NAME} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1; }
227 done
228}
229
230run_model_validate(){
Ivan Berezovskiy5b2759c2019-02-21 21:07:27 +0400231 # Run modelschema.model_validate validation.
232 # TEST iterateble, run for `each formula ROLE against each ROLE_PILLARNAME`
233 # Pillars should be named in conviend ROLE_XXX.sls or ROLE.sls
234 # Example:
235 # client.sls client_auth.sls server.sls server_auth.sls
236 if [ -d ${SCHEMARDIR} ]; then
237 # model validator require py modules
238 fetch_dependency "salt:https://github.com/salt-formulas/salt-formula-salt"
239 link_modules
240 salt_run saltutil.clear_cache; salt_run saltutil.refresh_pillar; salt_run saltutil.sync_all;
241 for role in $(find $SCHEMARDIR/* -maxdepth 0 -type f -iname *.yaml); do
242 role_name=$(basename "${role%*.yaml}")
243 for pillar in $(ls pillar/${role_name}*.sls | grep -v ${IGNORE_MODELVALIDATE_MASK} ); do
244 pillar_name=$(basename "${pillar%*.sls}")
245 local _message="FORMULA:${FORMULA_NAME} ROLE:${role_name} against PILLAR:${pillar_name}"
246 log_info "model_validate ${_message}"
247 # Rendered Example:
248 # python $(which salt-call) --local -c /test1/maas/tests/build/salt --id=maas_cluster modelschema.model_validate maas cluster
249 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 ; }
Martin Polreich08ae6d22018-07-18 11:14:05 +0200250 done
Ivan Berezovskiy5b2759c2019-02-21 21:07:27 +0400251 done
252 for schema in $(find $SCHEMARDIR -mindepth 2 -type f -iname *.yaml); do
253 role_name=$(basename "${schema%*.yaml}")
254 os_release=$(echo $schema | rev | cut -d'/' -f2 | rev)
255 local _message="FORMULA:${FORMULA_NAME} ROLE:${role_name} against PILLAR:${role_name}"
256 log_info "model_validate ${_message}"
257 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 ; }
258 done
259 else
260 log_info "${SCHEMARDIR} not found!";
261 fi
Martin Polreich08ae6d22018-07-18 11:14:05 +0200262}
263
264dependency_check() {
265 local DEPENDENCY_COMMANDS=$*
266
267 for DEPENDENCY_COMMAND in $DEPENDENCY_COMMANDS; do
268 which $DEPENDENCY_COMMAND > /dev/null || ( log_err "Command \"$DEPENDENCY_COMMAND\" can not be found in default path."; exit 1; )
269 done
270}
271
272_atexit() {
273 RETVAL=$?
274 trap true INT TERM EXIT
275
276 if [ $RETVAL -ne 0 ]; then
277 log_err "Execution failed"
278 else
279 log_info "Execution successful"
280 fi
281 return $RETVAL
282}
283
284## Main
Ivan Berezovskiy5b2759c2019-02-21 21:07:27 +0400285
286log_info "Running version: ${__ScriptVersion}"
287log_info "Command line: '${__ScriptFullName} ${__ScriptArgs}'"
288
Martin Polreich08ae6d22018-07-18 11:14:05 +0200289trap _atexit INT TERM EXIT
290
291case $1 in
292 clean)
293 clean
294 ;;
295 prepare)
296 prepare
297 ;;
298 lint)
299 lint
300 ;;
301 run)
302 run
303 ;;
304 real-run)
305 real_run
306 ;;
307 model-validate)
308 prepare
309 run_model_validate
310 ;;
311 *)
312 prepare
313# lint
314 run
315 run_model_validate
316 ;;
Ivan Berezovskiy5b2759c2019-02-21 21:07:27 +0400317esac