blob: 62a61bfb36d638dc7615b7f672d134c4b5f0df3d [file] [log] [blame]
marco53bc2992016-01-25 21:24:20 +01001#!/usr/bin/env bash
2
Roman Lubianyia6caca92020-03-04 11:26:23 +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="2019.01.07"
9__ScriptName="run_tests.sh"
10__ScriptFullName="$0"
11__ScriptArgs="$*"
12
marco53bc2992016-01-25 21:24:20 +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']")
Oleksandr Shyshko54786292018-08-01 13:43:02 +030019FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
marco53bc2992016-01-25 21:24:20 +010020
21## Overrideable parameters
22PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
23BUILDDIR=${BUILDDIR:-${CURDIR}/build}
24VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
Oleksandr Shyshko54786292018-08-01 13:43:02 +030025MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
marco53bc2992016-01-25 21:24:20 +010026DEPSDIR=${BUILDDIR}/deps
Roman Lubianyia6caca92020-03-04 11:26:23 +020027SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"}
marco53bc2992016-01-25 21:24:20 +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}
Roman Lubianyia6caca92020-03-04 11:26:23 +020033SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods}
marco53bc2992016-01-25 21:24:20 +010034
Lukas Zmrzlik4106bc82016-06-23 11:10:24 +020035SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
marco53bc2992016-01-25 21:24:20 +010036
Roman Lubianyia6caca92020-03-04 11:26:23 +020037IGNORE_MODELVALIDATE_MASK=${IGNORE_MODELVALIDATE_MASK:-"novalidate"}
38
marco53bc2992016-01-25 21:24:20 +010039if [ "x${SALT_VERSION}" != "x" ]; then
40 PIP_SALT_VERSION="==${SALT_VERSION}"
41fi
42
43## Functions
44log_info() {
Roman Lubianyia6caca92020-03-04 11:26:23 +020045 echo -e "[INFO] $*"
marco53bc2992016-01-25 21:24:20 +010046}
47
48log_err() {
Roman Lubianyia6caca92020-03-04 11:26:23 +020049 echo -e "[ERROR] $*" >&2
marco53bc2992016-01-25 21:24:20 +010050}
51
52setup_virtualenv() {
53 log_info "Setting up Python virtualenv"
Roman Lubianyia6caca92020-03-04 11:26:23 +020054 dependency_check virtualenv
marco53bc2992016-01-25 21:24:20 +010055 virtualenv $VENV_DIR
56 source ${VENV_DIR}/bin/activate
Jakub Josef1be97a62018-02-08 13:22:18 +010057 python -m pip install salt${PIP_SALT_VERSION}
Ksawery DziekoƄski2f433512020-05-15 11:53:15 +020058 python -m pip install 'msgpack<1' 'setuptools<45' jsonschema
Roman Lubianyia6caca92020-03-04 11:26:23 +020059 if [[ -f ${CURDIR}/test-requirements.txt ]]; then
60 python -m pip install -r ${CURDIR}/test-requirements.txt
61 fi
marco53bc2992016-01-25 21:24:20 +010062}
63
Oleksandr Shyshko54786292018-08-01 13:43:02 +030064setup_mock_bin() {
65 # If some state requires a binary, a lightweight replacement for
66 # such binary can be put into MOCK_BIN_DIR for test purposes
67 if [ -d "${MOCK_BIN_DIR}" ]; then
68 PATH="${MOCK_BIN_DIR}:$PATH"
69 export PATH
70 fi
71}
72
marco53bc2992016-01-25 21:24:20 +010073setup_pillar() {
74 [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR}
75 echo "base:" > ${SALT_PILLAR_DIR}/top.sls
76 for pillar in ${PILLARDIR}/*; do
Petr Michalec520a2a12017-03-13 08:47:43 +010077 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
marco53bc2992016-01-25 21:24:20 +010078 state_name=$(basename ${pillar%.sls})
79 echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
80 done
Roman Lubianyia6caca92020-03-04 11:26:23 +020081 for pillar in $(find $PILLARDIR -mindepth 2 -type f -iname *.sls); do
82 state_name=$(basename "${pillar%*.sls}")
83 os_release=$(echo $pillar | rev | cut -d'/' -f2 | rev)
84 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
85 echo -e " ${os_release}_${state_name}:\n - ${os_release}.${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
86 done
marco53bc2992016-01-25 21:24:20 +010087}
88
89setup_salt() {
90 [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR}
91 [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR}
92 [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR}
Roman Lubianyia6caca92020-03-04 11:26:23 +020093 [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR}
marco53bc2992016-01-25 21:24:20 +010094
95 echo "base:" > ${SALT_FILE_DIR}/top.sls
96 for pillar in ${PILLARDIR}/*.sls; do
Petr Michalec520a2a12017-03-13 08:47:43 +010097 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
marco53bc2992016-01-25 21:24:20 +010098 state_name=$(basename ${pillar%.sls})
99 echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
100 done
Roman Lubianyia6caca92020-03-04 11:26:23 +0200101 for pillar in $(find $PILLARDIR -mindepth 2 -type f -iname *.sls); do
102 state_name=$(basename "${pillar%*.sls}")
103 os_release=$(echo $pillar | rev | cut -d'/' -f2 | rev)
104 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
105 echo -e " ${os_release}_${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
106 done
marco53bc2992016-01-25 21:24:20 +0100107
108 cat << EOF > ${SALT_CONFIG_DIR}/minion
109file_client: local
110cachedir: ${SALT_CACHE_DIR}
Roman Lubianyia6caca92020-03-04 11:26:23 +0200111extension_modules: ${SALT_CACHE_EXTMODS_DIR}
marco53bc2992016-01-25 21:24:20 +0100112verify_env: False
Ondƙej NovĂœ23f1aba2016-04-23 17:16:13 +0200113minion_id_caching: False
marco53bc2992016-01-25 21:24:20 +0100114
115file_roots:
116 base:
117 - ${SALT_FILE_DIR}
118 - ${CURDIR}/..
119
120pillar_roots:
121 base:
122 - ${SALT_PILLAR_DIR}
123 - ${PILLARDIR}
124EOF
125}
126
127fetch_dependency() {
Roman Lubianyia6caca92020-03-04 11:26:23 +0200128 # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux"
Filip Pytlounad095892016-04-14 11:51:09 +0200129 dep_name="$(echo $1|cut -d : -f 1)"
130 dep_source="$(echo $1|cut -d : -f 2-)"
131 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
marco53bc2992016-01-25 21:24:20 +0100132 dep_metadata="${dep_root}/metadata.yml"
133
Roman Lubianyia6caca92020-03-04 11:26:23 +0200134 dependency_check git
135 [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; }
marco53bc2992016-01-25 21:24:20 +0100136
Filip Pytlounad095892016-04-14 11:51:09 +0200137 log_info "Fetching dependency $dep_name"
marco53bc2992016-01-25 21:24:20 +0100138 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
Filip Pytlounad095892016-04-14 11:51:09 +0200139 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
marco53bc2992016-01-25 21:24:20 +0100140 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
141
142 METADATA="${dep_metadata}" install_dependencies
143}
144
Roman Lubianyia6caca92020-03-04 11:26:23 +0200145link_modules(){
146 # Link modules *.py files to temporary salt-root
147 local SALT_ROOT=${1:-$SALT_FILE_DIR}
148 local SALT_ENV=${2:-$DEPSDIR}
149
150 mkdir -p "${SALT_ROOT}/_modules/"
151 # from git, development versions
152 find ${SALT_ENV} -maxdepth 3 -mindepth 3 -path '*_modules*' -iname "*.py" -type f -print0 | while read -d $'\0' file; do
153 ln -fs $(readlink -e ${file}) "$SALT_ROOT"/_modules/$(basename ${file}) ;
154 done
155 salt_run saltutil.sync_all
156}
157
marco53bc2992016-01-25 21:24:20 +0100158install_dependencies() {
159 grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
160 (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
161import sys,yaml
162for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
Filip Pytlounad095892016-04-14 11:51:09 +0200163 print '%s:%s' % (dep["name"], dep["source"])
marco53bc2992016-01-25 21:24:20 +0100164EOF
165}
166
167clean() {
168 log_info "Cleaning up ${BUILDDIR}"
169 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
170}
171
172salt_run() {
Jakub Josef60d9eed2017-12-15 16:58:30 +0100173 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
Jakub Josef1be97a62018-02-08 13:22:18 +0100174 python $(which salt-call) ${SALT_OPTS} $*
marco53bc2992016-01-25 21:24:20 +0100175}
176
177prepare() {
Roman Lubianyia6caca92020-03-04 11:26:23 +0200178 if [[ -f ${BUILDDIR}/.prepare_done ]]; then
179 log_info "${BUILDDIR}/.prepare_done exist, not rebuilding BUILDDIR"
180 return
181 fi
182 [[ -d ${BUILDDIR} ]] && mkdir -p ${BUILDDIR}
marco53bc2992016-01-25 21:24:20 +0100183
Roman Lubianyia6caca92020-03-04 11:26:23 +0200184 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
Oleksandr Shyshko54786292018-08-01 13:43:02 +0300185 setup_mock_bin
marco53bc2992016-01-25 21:24:20 +0100186 setup_pillar
187 setup_salt
188 install_dependencies
Roman Lubianyia6caca92020-03-04 11:26:23 +0200189 link_modules
190 touch ${BUILDDIR}/.prepare_done
191}
192
193lint_releasenotes() {
194 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
195 source ${VENV_DIR}/bin/activate
196 reno lint ${CURDIR}/../
197}
198
199lint() {
200# lint_releasenotes
201 log_err "TODO: lint_releasenotes"
marco53bc2992016-01-25 21:24:20 +0100202}
203
204run() {
205 for pillar in ${PILLARDIR}/*.sls; do
Petr Michalec520a2a12017-03-13 08:47:43 +0100206 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
marco53bc2992016-01-25 21:24:20 +0100207 state_name=$(basename ${pillar%.sls})
Oleksandr Shyshko54786292018-08-01 13:43:02 +0300208 salt_run grains.set 'noservices' False force=True
209
210 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
marco53bc2992016-01-25 21:24:20 +0100211 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
Oleksandr Shyshko54786292018-08-01 13:43:02 +0300212
213 # Check that all files in 'meta' folder can be rendered using any valid pillar
214 for meta in `find ${FORMULA_META_DIR} -type f`; do
215 meta_name=$(basename ${meta})
216 echo "Checking meta ${meta_name} ..."
217 salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
Roman Lubianyia6caca92020-03-04 11:26:23 +0200218 || { log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1; }
Oleksandr Shyshko54786292018-08-01 13:43:02 +0300219 cat ${SALT_CACHE_DIR}/${meta_name}
220 done
221 done
222}
223
224real_run() {
225 for pillar in ${PILLARDIR}/*.sls; do
226 state_name=$(basename ${pillar%.sls})
Roman Lubianyia6caca92020-03-04 11:26:23 +0200227 salt_run --id=${state_name} state.sls ${FORMULA_NAME} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1; }
marco53bc2992016-01-25 21:24:20 +0100228 done
229}
230
Roman Lubianyia6caca92020-03-04 11:26:23 +0200231run_model_validate(){
232 # Run modelschema.model_validate validation.
233 # TEST iterateble, run for `each formula ROLE against each ROLE_PILLARNAME`
234 # Pillars should be named in conviend ROLE_XXX.sls or ROLE.sls
235 # Example:
236 # client.sls client_auth.sls server.sls server_auth.sls
237 if [ -d ${SCHEMARDIR} ]; then
238 # model validator require py modules
239 fetch_dependency "salt:https://github.com/salt-formulas/salt-formula-salt"
240 link_modules
241 salt_run saltutil.clear_cache; salt_run saltutil.refresh_pillar; salt_run saltutil.sync_all;
242 for role in $(find $SCHEMARDIR/* -maxdepth 0 -type f -iname *.yaml); do
243 role_name=$(basename "${role%*.yaml}")
244 for pillar in $(ls pillar/${role_name}*.sls | grep -v ${IGNORE_MODELVALIDATE_MASK} ); do
245 pillar_name=$(basename "${pillar%*.sls}")
246 local _message="FORMULA:${FORMULA_NAME} ROLE:${role_name} against PILLAR:${pillar_name}"
247 log_info "model_validate ${_message}"
248 # Rendered Example:
249 # python $(which salt-call) --local -c /test1/maas/tests/build/salt --id=maas_cluster modelschema.model_validate maas cluster
250 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 ; }
251 done
252 done
253 for schema in $(find $SCHEMARDIR -mindepth 2 -type f -iname *.yaml); do
254 role_name=$(basename "${schema%*.yaml}")
255 os_release=$(echo $schema | rev | cut -d'/' -f2 | rev)
256 local _message="FORMULA:${FORMULA_NAME} ROLE:${role_name} against PILLAR:${role_name}"
257 log_info "model_validate ${_message}"
258 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 ; }
259 done
260 else
261 log_info "${SCHEMARDIR} not found!";
262 fi
263}
264
265dependency_check() {
266 local DEPENDENCY_COMMANDS=$*
267
268 for DEPENDENCY_COMMAND in $DEPENDENCY_COMMANDS; do
269 which $DEPENDENCY_COMMAND > /dev/null || ( log_err "Command \"$DEPENDENCY_COMMAND\" can not be found in default path."; exit 1; )
270 done
271}
272
marco53bc2992016-01-25 21:24:20 +0100273_atexit() {
274 RETVAL=$?
275 trap true INT TERM EXIT
276
277 if [ $RETVAL -ne 0 ]; then
278 log_err "Execution failed"
279 else
280 log_info "Execution successful"
281 fi
282 return $RETVAL
283}
284
285## Main
Roman Lubianyia6caca92020-03-04 11:26:23 +0200286
287log_info "Running version: ${__ScriptVersion}"
288log_info "Command line: '${__ScriptFullName} ${__ScriptArgs}'"
289
marco53bc2992016-01-25 21:24:20 +0100290trap _atexit INT TERM EXIT
291
292case $1 in
293 clean)
294 clean
295 ;;
296 prepare)
297 prepare
298 ;;
Roman Lubianyia6caca92020-03-04 11:26:23 +0200299 lint)
300 lint
301 ;;
marco53bc2992016-01-25 21:24:20 +0100302 run)
303 run
304 ;;
Oleksandr Shyshko54786292018-08-01 13:43:02 +0300305 real-run)
306 real_run
307 ;;
Roman Lubianyia6caca92020-03-04 11:26:23 +0200308 model-validate)
309 prepare
310 run_model_validate
311 ;;
marco53bc2992016-01-25 21:24:20 +0100312 *)
313 prepare
Roman Lubianyia6caca92020-03-04 11:26:23 +0200314# lint
marco53bc2992016-01-25 21:24:20 +0100315 run
Roman Lubianyia6caca92020-03-04 11:26:23 +0200316 run_model_validate
marco53bc2992016-01-25 21:24:20 +0100317 ;;
318esac