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