blob: 62a61bfb36d638dc7615b7f672d134c4b5f0df3d [file] [log] [blame]
Alexander Noskovd0e69af2016-12-07 13:19:14 +04001#!/usr/bin/env bash
2
Stepan Rogov959bd5a2022-08-08 18:48:43 -07003###
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
Alexander Noskovd0e69af2016-12-07 13:19:14 +040013set -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']")
Stepan Rogov959bd5a2022-08-08 18:48:43 -070019FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
Alexander Noskovd0e69af2016-12-07 13:19:14 +040020
21## Overrideable parameters
22PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
23BUILDDIR=${BUILDDIR:-${CURDIR}/build}
24VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
Stepan Rogov959bd5a2022-08-08 18:48:43 -070025MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
Alexander Noskovd0e69af2016-12-07 13:19:14 +040026DEPSDIR=${BUILDDIR}/deps
Stepan Rogov959bd5a2022-08-08 18:48:43 -070027SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"}
Alexander Noskovd0e69af2016-12-07 13:19:14 +040028
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}
Stepan Rogov959bd5a2022-08-08 18:48:43 -070033SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods}
Alexander Noskovd0e69af2016-12-07 13:19:14 +040034
Stepan Rogov959bd5a2022-08-08 18:48:43 -070035SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
36
37IGNORE_MODELVALIDATE_MASK=${IGNORE_MODELVALIDATE_MASK:-"novalidate"}
Alexander Noskovd0e69af2016-12-07 13:19:14 +040038
39if [ "x${SALT_VERSION}" != "x" ]; then
40 PIP_SALT_VERSION="==${SALT_VERSION}"
41fi
42
43## Functions
44log_info() {
Stepan Rogov959bd5a2022-08-08 18:48:43 -070045 echo -e "[INFO] $*"
Alexander Noskovd0e69af2016-12-07 13:19:14 +040046}
47
48log_err() {
Stepan Rogov959bd5a2022-08-08 18:48:43 -070049 echo -e "[ERROR] $*" >&2
Alexander Noskovd0e69af2016-12-07 13:19:14 +040050}
51
52setup_virtualenv() {
53 log_info "Setting up Python virtualenv"
Stepan Rogov959bd5a2022-08-08 18:48:43 -070054 dependency_check virtualenv
Alexander Noskovd0e69af2016-12-07 13:19:14 +040055 virtualenv $VENV_DIR
56 source ${VENV_DIR}/bin/activate
Stepan Rogov959bd5a2022-08-08 18:48:43 -070057 python -m pip install salt${PIP_SALT_VERSION}
58 python -m pip install 'msgpack<1' 'setuptools<45' jsonschema
59 if [[ -f ${CURDIR}/test-requirements.txt ]]; then
60 python -m pip install -r ${CURDIR}/test-requirements.txt
61 fi
62}
63
64setup_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
Alexander Noskovd0e69af2016-12-07 13:19:14 +040071}
72
73setup_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
Stepan Rogov959bd5a2022-08-08 18:48:43 -070077 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Alexander Noskovd0e69af2016-12-07 13:19:14 +040078 state_name=$(basename ${pillar%.sls})
79 echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
80 done
Stepan Rogov959bd5a2022-08-08 18:48:43 -070081 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
Alexander Noskovd0e69af2016-12-07 13:19:14 +040087}
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}
Stepan Rogov959bd5a2022-08-08 18:48:43 -070093 [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR}
Alexander Noskovd0e69af2016-12-07 13:19:14 +040094
95 echo "base:" > ${SALT_FILE_DIR}/top.sls
96 for pillar in ${PILLARDIR}/*.sls; do
Stepan Rogov959bd5a2022-08-08 18:48:43 -070097 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Alexander Noskovd0e69af2016-12-07 13:19:14 +040098 state_name=$(basename ${pillar%.sls})
99 echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
100 done
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700101 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
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400107
108 cat << EOF > ${SALT_CONFIG_DIR}/minion
109file_client: local
110cachedir: ${SALT_CACHE_DIR}
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700111extension_modules: ${SALT_CACHE_EXTMODS_DIR}
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400112verify_env: False
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700113minion_id_caching: False
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400114
115file_roots:
116 base:
117 - ${SALT_FILE_DIR}
118 - ${CURDIR}/..
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400119
120pillar_roots:
121 base:
122 - ${SALT_PILLAR_DIR}
123 - ${PILLARDIR}
124EOF
125}
126
127fetch_dependency() {
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700128 # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux"
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400129 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)"
132 dep_metadata="${dep_root}/metadata.yml"
133
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700134 dependency_check git
135 [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; }
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400136
137 log_info "Fetching dependency $dep_name"
138 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
139 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
140 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
141
142 METADATA="${dep_metadata}" install_dependencies
143}
144
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700145link_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
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400158install_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']:
163 print '%s:%s' % (dep["name"], dep["source"])
164EOF
165}
166
167clean() {
168 log_info "Cleaning up ${BUILDDIR}"
169 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
170}
171
172salt_run() {
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700173 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
174 python $(which salt-call) ${SALT_OPTS} $*
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400175}
176
177prepare() {
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700178 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}
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400183
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700184 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
185 setup_mock_bin
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400186 setup_pillar
187 setup_salt
188 install_dependencies
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700189 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"
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400202}
203
204run() {
205 for pillar in ${PILLARDIR}/*.sls; do
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700206 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400207 state_name=$(basename ${pillar%.sls})
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700208 salt_run grains.set 'noservices' False force=True
209
210 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400211 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700212
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} \
218 || { log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1; }
219 cat ${SALT_CACHE_DIR}/${meta_name}
220 done
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400221 done
222}
223
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700224real_run() {
225 for pillar in ${PILLARDIR}/*.sls; do
226 state_name=$(basename ${pillar%.sls})
227 salt_run --id=${state_name} state.sls ${FORMULA_NAME} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1; }
228 done
229}
230
231run_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
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400273_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
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700286
287log_info "Running version: ${__ScriptVersion}"
288log_info "Command line: '${__ScriptFullName} ${__ScriptArgs}'"
289
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400290trap _atexit INT TERM EXIT
291
292case $1 in
293 clean)
294 clean
295 ;;
296 prepare)
297 prepare
298 ;;
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700299 lint)
300 lint
301 ;;
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400302 run)
303 run
304 ;;
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700305 real-run)
306 real_run
307 ;;
308 model-validate)
309 prepare
310 run_model_validate
311 ;;
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400312 *)
313 prepare
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700314# lint
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400315 run
Stepan Rogov959bd5a2022-08-08 18:48:43 -0700316 run_model_validate
Alexander Noskovd0e69af2016-12-07 13:19:14 +0400317 ;;
318esac