blob: 461fd8b8b50aeae4ee1a8c2706d28f374746906d [file] [log] [blame]
Martin Polreich21f23142017-05-16 11:03:46 +02001#!/usr/bin/env bash
2
Martin Polreich4f41de52019-02-06 13:01:20 +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
Martin Polreich21f23142017-05-16 11:03:46 +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']")
Martin Polreich4f41de52019-02-06 13:01:20 +010019FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
Martin Polreich21f23142017-05-16 11:03:46 +020020
21## Overrideable parameters
22PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
23BUILDDIR=${BUILDDIR:-${CURDIR}/build}
24VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
Martin Polreich4f41de52019-02-06 13:01:20 +010025MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
Martin Polreich21f23142017-05-16 11:03:46 +020026DEPSDIR=${BUILDDIR}/deps
Martin Polreich4f41de52019-02-06 13:01:20 +010027SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"}
Martin Polreich21f23142017-05-16 11:03:46 +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}
Martin Polreich4f41de52019-02-06 13:01:20 +010033SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods}
Martin Polreich21f23142017-05-16 11:03:46 +020034
35SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
36
Martin Polreich4f41de52019-02-06 13:01:20 +010037IGNORE_MODELVALIDATE_MASK=${IGNORE_MODELVALIDATE_MASK:-"novalidate"}
38
Martin Polreich21f23142017-05-16 11:03:46 +020039if [ "x${SALT_VERSION}" != "x" ]; then
40 PIP_SALT_VERSION="==${SALT_VERSION}"
41fi
42
43## Functions
44log_info() {
Martin Polreich4f41de52019-02-06 13:01:20 +010045 echo -e "[INFO] $*"
Martin Polreich21f23142017-05-16 11:03:46 +020046}
47
48log_err() {
Martin Polreich4f41de52019-02-06 13:01:20 +010049 echo -e "[ERROR] $*" >&2
Martin Polreich21f23142017-05-16 11:03:46 +020050}
51
52setup_virtualenv() {
53 log_info "Setting up Python virtualenv"
Martin Polreich4f41de52019-02-06 13:01:20 +010054 dependency_check virtualenv
Martin Polreich21f23142017-05-16 11:03:46 +020055 virtualenv $VENV_DIR
56 source ${VENV_DIR}/bin/activate
Jakub Josef40845022018-02-08 13:22:13 +010057 python -m pip install salt${PIP_SALT_VERSION}
Martin Polreich4f41de52019-02-06 13:01:20 +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
Martin Polreich21f23142017-05-16 11:03:46 +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
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
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}
Martin Polreich4f41de52019-02-06 13:01:20 +010086 [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR}
Martin Polreich21f23142017-05-16 11:03:46 +020087
88 echo "base:" > ${SALT_FILE_DIR}/top.sls
89 for pillar in ${PILLARDIR}/*.sls; do
90 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
91 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}
Martin Polreich4f41de52019-02-06 13:01:20 +010098extension_modules: ${SALT_CACHE_EXTMODS_DIR}
Martin Polreich21f23142017-05-16 11:03:46 +020099verify_env: False
100minion_id_caching: False
101
102file_roots:
103 base:
104 - ${SALT_FILE_DIR}
105 - ${CURDIR}/..
Martin Polreich21f23142017-05-16 11:03:46 +0200106
107pillar_roots:
108 base:
109 - ${SALT_PILLAR_DIR}
110 - ${PILLARDIR}
111EOF
112}
113
114fetch_dependency() {
Martin Polreich4f41de52019-02-06 13:01:20 +0100115 # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux"
Martin Polreich21f23142017-05-16 11:03:46 +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
Martin Polreich4f41de52019-02-06 13:01:20 +0100121 dependency_check git
122 [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; }
Martin Polreich21f23142017-05-16 11:03:46 +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
Martin Polreich4f41de52019-02-06 13:01:20 +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
Martin Polreich21f23142017-05-16 11:03:46 +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() {
Jakub Josefdae63e72017-12-15 16:58:28 +0100160 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
Jakub Josef40845022018-02-08 13:22:13 +0100161 python $(which salt-call) ${SALT_OPTS} $*
Martin Polreich21f23142017-05-16 11:03:46 +0200162}
163
164prepare() {
Martin Polreich4f41de52019-02-06 13:01:20 +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}
Martin Polreich21f23142017-05-16 11:03:46 +0200170
Martin Polreich4f41de52019-02-06 13:01:20 +0100171 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
172 setup_mock_bin
Martin Polreich21f23142017-05-16 11:03:46 +0200173 setup_pillar
174 setup_salt
175 install_dependencies
Martin Polreich4f41de52019-02-06 13:01:20 +0100176 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"
Martin Polreich21f23142017-05-16 11:03:46 +0200189}
190
191run() {
192 for pillar in ${PILLARDIR}/*.sls; do
193 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
194 state_name=$(basename ${pillar%.sls})
Martin Polreich4f41de52019-02-06 13:01:20 +0100195 salt_run grains.set 'noservices' False force=True
196
197 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
Martin Polreich21f23142017-05-16 11:03:46 +0200198 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
Martin Polreich4f41de52019-02-06 13:01:20 +0100199
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
Martin Polreich21f23142017-05-16 11:03:46 +0200208 done
209}
210
Martin Polreich4f41de52019-02-06 13:01:20 +0100211real_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
Martin Polreich21f23142017-05-16 11:03:46 +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
Martin Polreich4f41de52019-02-06 13:01:20 +0100266
267log_info "Running version: ${__ScriptVersion}"
268log_info "Command line: '${__ScriptFullName} ${__ScriptArgs}'"
269
Martin Polreich21f23142017-05-16 11:03:46 +0200270trap _atexit INT TERM EXIT
271
272case $1 in
273 clean)
274 clean
275 ;;
276 prepare)
277 prepare
278 ;;
Martin Polreich4f41de52019-02-06 13:01:20 +0100279 lint)
280 lint
281 ;;
Martin Polreich21f23142017-05-16 11:03:46 +0200282 run)
283 run
284 ;;
Martin Polreich4f41de52019-02-06 13:01:20 +0100285 real-run)
286 real_run
287 ;;
288 model-validate)
289 prepare
290 run_model_validate
291 ;;
Martin Polreich21f23142017-05-16 11:03:46 +0200292 *)
293 prepare
Martin Polreich4f41de52019-02-06 13:01:20 +0100294# lint
Martin Polreich21f23142017-05-16 11:03:46 +0200295 run
Martin Polreich4f41de52019-02-06 13:01:20 +0100296 run_model_validate
Martin Polreich21f23142017-05-16 11:03:46 +0200297 ;;
298esac