blob: cf8cc1f355c1e1ff0ac3a8f81aa25dc19377e4b4 [file] [log] [blame]
Filip Pytlound5d7cc42016-05-16 10:56:18 +02001#!/usr/bin/env bash
Jesse Pendergrass615d0d12021-03-08 15:51:57 -06002###
3# Script source: https://gerrit.mcp.mirantis.com/#/admin/projects/salt-formulas/cookiecutter-salt-formula
4# Script requirments:
5#apt-get install -y python-yaml virtualenv git
6__ScriptVersion="2019.01.07"
7__ScriptName="run_tests.sh"
8__ScriptFullName="$0"
9__ScriptArgs="$*"
Filip Pytlound5d7cc42016-05-16 10:56:18 +020010set -e
11[ -n "$DEBUG" ] && set -x
Filip Pytlound5d7cc42016-05-16 10:56:18 +020012CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
13METADATA=${CURDIR}/../metadata.yml
14FORMULA_NAME=$(cat $METADATA | python -c "import sys,yaml; print yaml.load(sys.stdin)['name']")
Dennis Dmitriev1b93de22017-07-06 21:07:27 +030015FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
Filip Pytlound5d7cc42016-05-16 10:56:18 +020016## Overrideable parameters
17PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
18BUILDDIR=${BUILDDIR:-${CURDIR}/build}
19VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
Dennis Dmitriev1b93de22017-07-06 21:07:27 +030020MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
Filip Pytlound5d7cc42016-05-16 10:56:18 +020021DEPSDIR=${BUILDDIR}/deps
Jesse Pendergrass615d0d12021-03-08 15:51:57 -060022SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"}
Filip Pytlound5d7cc42016-05-16 10:56:18 +020023SALT_FILE_DIR=${SALT_FILE_DIR:-${BUILDDIR}/file_root}
24SALT_PILLAR_DIR=${SALT_PILLAR_DIR:-${BUILDDIR}/pillar_root}
25SALT_CONFIG_DIR=${SALT_CONFIG_DIR:-${BUILDDIR}/salt}
26SALT_CACHE_DIR=${SALT_CACHE_DIR:-${SALT_CONFIG_DIR}/cache}
Jesse Pendergrass615d0d12021-03-08 15:51:57 -060027SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods}
Dennis Dmitriev1b93de22017-07-06 21:07:27 +030028SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
Jesse Pendergrass615d0d12021-03-08 15:51:57 -060029IGNORE_MODELVALIDATE_MASK=${IGNORE_MODELVALIDATE_MASK:-"novalidate"}
Filip Pytlound5d7cc42016-05-16 10:56:18 +020030if [ "x${SALT_VERSION}" != "x" ]; then
31 PIP_SALT_VERSION="==${SALT_VERSION}"
32fi
Filip Pytlound5d7cc42016-05-16 10:56:18 +020033## Functions
34log_info() {
Jesse Pendergrass615d0d12021-03-08 15:51:57 -060035 echo -e "[INFO] $*"
Filip Pytlound5d7cc42016-05-16 10:56:18 +020036}
Filip Pytlound5d7cc42016-05-16 10:56:18 +020037log_err() {
Jesse Pendergrass615d0d12021-03-08 15:51:57 -060038 echo -e "[ERROR] $*" >&2
Filip Pytlound5d7cc42016-05-16 10:56:18 +020039}
Filip Pytlound5d7cc42016-05-16 10:56:18 +020040setup_virtualenv() {
41 log_info "Setting up Python virtualenv"
Jesse Pendergrass615d0d12021-03-08 15:51:57 -060042 dependency_check virtualenv
Filip Pytlound5d7cc42016-05-16 10:56:18 +020043 virtualenv $VENV_DIR
44 source ${VENV_DIR}/bin/activate
Jakub Josef5e7c0a02018-02-08 13:22:17 +010045 python -m pip install salt${PIP_SALT_VERSION}
Jesse Pendergrass615d0d12021-03-08 15:51:57 -060046 if [[ -f ${CURDIR}/test-requirements.txt ]]; then
47 python -m pip install -r ${CURDIR}/test-requirements.txt
48 fi
Filip Pytlound5d7cc42016-05-16 10:56:18 +020049}
Dennis Dmitriev1b93de22017-07-06 21:07:27 +030050setup_mock_bin() {
51 # If some state requires a binary, a lightweight replacement for
52 # such binary can be put into MOCK_BIN_DIR for test purposes
53 if [ -d "${MOCK_BIN_DIR}" ]; then
54 PATH="${MOCK_BIN_DIR}:$PATH"
55 export PATH
56 fi
57}
Filip Pytlound5d7cc42016-05-16 10:56:18 +020058setup_pillar() {
59 [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR}
60 echo "base:" > ${SALT_PILLAR_DIR}/top.sls
61 for pillar in ${PILLARDIR}/*; do
Dennis Dmitriev1b93de22017-07-06 21:07:27 +030062 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Filip Pytlound5d7cc42016-05-16 10:56:18 +020063 state_name=$(basename ${pillar%.sls})
64 echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
65 done
Jesse Pendergrass615d0d12021-03-08 15:51:57 -060066 for pillar in $(find $PILLARDIR -mindepth 2 -type f -iname *.sls); do
67 state_name=$(basename "${pillar%*.sls}")
68 os_release=$(echo $pillar | rev | cut -d'/' -f2 | rev)
69 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
70 echo -e " ${os_release}_${state_name}:\n - ${os_release}.${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
71 done
Filip Pytlound5d7cc42016-05-16 10:56:18 +020072}
Filip Pytlound5d7cc42016-05-16 10:56:18 +020073setup_salt() {
74 [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR}
75 [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR}
76 [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR}
Jesse Pendergrass615d0d12021-03-08 15:51:57 -060077 [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR}
Filip Pytlound5d7cc42016-05-16 10:56:18 +020078 echo "base:" > ${SALT_FILE_DIR}/top.sls
79 for pillar in ${PILLARDIR}/*.sls; do
Dennis Dmitriev1b93de22017-07-06 21:07:27 +030080 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Filip Pytlound5d7cc42016-05-16 10:56:18 +020081 state_name=$(basename ${pillar%.sls})
82 echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
83 done
Jesse Pendergrass615d0d12021-03-08 15:51:57 -060084 for pillar in $(find $PILLARDIR -mindepth 2 -type f -iname *.sls); do
85 state_name=$(basename "${pillar%*.sls}")
86 os_release=$(echo $pillar | rev | cut -d'/' -f2 | rev)
87 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
88 echo -e " ${os_release}_${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
89 done
Filip Pytlound5d7cc42016-05-16 10:56:18 +020090 cat << EOF > ${SALT_CONFIG_DIR}/minion
91file_client: local
92cachedir: ${SALT_CACHE_DIR}
Jesse Pendergrass615d0d12021-03-08 15:51:57 -060093extension_modules: ${SALT_CACHE_EXTMODS_DIR}
Filip Pytlound5d7cc42016-05-16 10:56:18 +020094verify_env: False
Dennis Dmitriev1b93de22017-07-06 21:07:27 +030095minion_id_caching: False
Filip Pytlound5d7cc42016-05-16 10:56:18 +020096file_roots:
97 base:
98 - ${SALT_FILE_DIR}
99 - ${CURDIR}/..
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200100pillar_roots:
101 base:
102 - ${SALT_PILLAR_DIR}
103 - ${PILLARDIR}
104EOF
105}
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200106fetch_dependency() {
Jesse Pendergrass615d0d12021-03-08 15:51:57 -0600107 # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux"
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200108 dep_name="$(echo $1|cut -d : -f 1)"
109 dep_source="$(echo $1|cut -d : -f 2-)"
110 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
111 dep_metadata="${dep_root}/metadata.yml"
Jesse Pendergrass615d0d12021-03-08 15:51:57 -0600112 dependency_check git
113 [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; }
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200114 log_info "Fetching dependency $dep_name"
115 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
116 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
117 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200118 METADATA="${dep_metadata}" install_dependencies
119}
Jesse Pendergrass615d0d12021-03-08 15:51:57 -0600120link_modules(){
121 # Link modules *.py files to temporary salt-root
122 local SALT_ROOT=${1:-$SALT_FILE_DIR}
123 local SALT_ENV=${2:-$DEPSDIR}
124 mkdir -p "${SALT_ROOT}/_modules/"
125 # from git, development versions
126 find ${SALT_ENV} -maxdepth 3 -mindepth 3 -path '*_modules*' -iname "*.py" -type f -print0 | while read -d $'\0' file; do
127 ln -fs $(readlink -e ${file}) "$SALT_ROOT"/_modules/$(basename ${file}) ;
128 done
129 salt_run saltutil.sync_all
130}
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200131install_dependencies() {
132 grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
133 (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
134import sys,yaml
135for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
136 print '%s:%s' % (dep["name"], dep["source"])
137EOF
138}
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200139clean() {
140 log_info "Cleaning up ${BUILDDIR}"
141 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
142}
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200143salt_run() {
Jakub Josef8f600822017-12-15 16:58:30 +0100144 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
Jakub Josef5e7c0a02018-02-08 13:22:17 +0100145 python $(which salt-call) ${SALT_OPTS} $*
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200146}
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200147prepare() {
Jesse Pendergrass615d0d12021-03-08 15:51:57 -0600148 if [[ -f ${BUILDDIR}/.prepare_done ]]; then
149 log_info "${BUILDDIR}/.prepare_done exist, not rebuilding BUILDDIR"
150 return
151 fi
152 [[ -d ${BUILDDIR} ]] && mkdir -p ${BUILDDIR}
153 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
Dennis Dmitriev1b93de22017-07-06 21:07:27 +0300154 setup_mock_bin
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200155 setup_pillar
156 setup_salt
157 install_dependencies
Jesse Pendergrass615d0d12021-03-08 15:51:57 -0600158 link_modules
159 touch ${BUILDDIR}/.prepare_done
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200160}
Jesse Pendergrass615d0d12021-03-08 15:51:57 -0600161lint_releasenotes() {
162 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
163 source ${VENV_DIR}/bin/activate
164 reno lint ${CURDIR}/../
165}
166lint() {
167# lint_releasenotes
168 log_err "TODO: lint_releasenotes"
169}
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200170run() {
171 for pillar in ${PILLARDIR}/*.sls; do
Dennis Dmitriev1b93de22017-07-06 21:07:27 +0300172 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200173 state_name=$(basename ${pillar%.sls})
Dennis Dmitriev1b93de22017-07-06 21:07:27 +0300174 salt_run grains.set 'noservices' False force=True
Dennis Dmitriev1b93de22017-07-06 21:07:27 +0300175 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200176 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
Dennis Dmitriev1b93de22017-07-06 21:07:27 +0300177 # Check that all files in 'meta' folder can be rendered using any valid pillar
178 for meta in `find ${FORMULA_META_DIR} -type f`; do
179 meta_name=$(basename ${meta})
180 echo "Checking meta ${meta_name} ..."
181 salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
Jesse Pendergrass615d0d12021-03-08 15:51:57 -0600182 || { log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1; }
Dennis Dmitriev1b93de22017-07-06 21:07:27 +0300183 cat ${SALT_CACHE_DIR}/${meta_name}
184 done
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200185 done
186}
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200187real_run() {
188 for pillar in ${PILLARDIR}/*.sls; do
189 state_name=$(basename ${pillar%.sls})
Jesse Pendergrass615d0d12021-03-08 15:51:57 -0600190 salt_run --id=${state_name} state.sls ${FORMULA_NAME} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1; }
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200191 done
192}
Jesse Pendergrass615d0d12021-03-08 15:51:57 -0600193run_model_validate(){
194 # Run modelschema.model_validate validation.
195 # TEST iterateble, run for `each formula ROLE against each ROLE_PILLARNAME`
196 # Pillars should be named in conviend ROLE_XXX.sls or ROLE.sls
197 # Example:
198 # client.sls client_auth.sls server.sls server_auth.sls
199 if [ -d ${SCHEMARDIR} ]; then
200 # model validator require py modules
201 fetch_dependency "salt:https://github.com/salt-formulas/salt-formula-salt"
202 link_modules
203 salt_run saltutil.clear_cache; salt_run saltutil.refresh_pillar; salt_run saltutil.sync_all;
204 for role in $(find $SCHEMARDIR/* -maxdepth 0 -type f -iname *.yaml); do
205 role_name=$(basename "${role%*.yaml}")
206 for pillar in $(ls pillar/${role_name}*.sls | grep -v ${IGNORE_MODELVALIDATE_MASK} ); do
207 pillar_name=$(basename "${pillar%*.sls}")
208 local _message="FORMULA:${FORMULA_NAME} ROLE:${role_name} against PILLAR:${pillar_name}"
209 log_info "model_validate ${_message}"
210 # Rendered Example:
211 # python $(which salt-call) --local -c /test1/maas/tests/build/salt --id=maas_cluster modelschema.model_validate maas cluster
212 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 ; }
213 done
214 done
215 for schema in $(find $SCHEMARDIR -mindepth 2 -type f -iname *.yaml); do
216 role_name=$(basename "${schema%*.yaml}")
217 os_release=$(echo $schema | rev | cut -d'/' -f2 | rev)
218 local _message="FORMULA:${FORMULA_NAME} ROLE:${role_name} against PILLAR:${role_name}"
219 log_info "model_validate ${_message}"
220 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 ; }
221 done
222 else
223 log_info "${SCHEMARDIR} not found!";
224 fi
225}
226dependency_check() {
227 local DEPENDENCY_COMMANDS=$*
228 for DEPENDENCY_COMMAND in $DEPENDENCY_COMMANDS; do
229 which $DEPENDENCY_COMMAND > /dev/null || ( log_err "Command \"$DEPENDENCY_COMMAND\" can not be found in default path."; exit 1; )
230 done
231}
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200232_atexit() {
233 RETVAL=$?
234 trap true INT TERM EXIT
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200235 if [ $RETVAL -ne 0 ]; then
236 log_err "Execution failed"
237 else
238 log_info "Execution successful"
239 fi
240 return $RETVAL
241}
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200242## Main
Jesse Pendergrass615d0d12021-03-08 15:51:57 -0600243log_info "Running version: ${__ScriptVersion}"
244log_info "Command line: '${__ScriptFullName} ${__ScriptArgs}'"
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200245trap _atexit INT TERM EXIT
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200246case $1 in
247 clean)
248 clean
249 ;;
250 prepare)
251 prepare
252 ;;
Jesse Pendergrass615d0d12021-03-08 15:51:57 -0600253 lint)
254 lint
255 ;;
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200256 run)
257 run
258 ;;
259 real-run)
260 real_run
261 ;;
Jesse Pendergrass615d0d12021-03-08 15:51:57 -0600262 model-validate)
263 prepare
264 run_model_validate
265 ;;
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200266 *)
267 prepare
Jesse Pendergrass615d0d12021-03-08 15:51:57 -0600268# lint
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200269 run
Jesse Pendergrass615d0d12021-03-08 15:51:57 -0600270 run_model_validate
Filip Pytlound5d7cc42016-05-16 10:56:18 +0200271 ;;
272esac