Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 3 | ### |
| 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 Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 13 | set -e |
| 14 | [ -n "$DEBUG" ] && set -x |
| 15 | |
| 16 | CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 17 | METADATA=${CURDIR}/../metadata.yml |
| 18 | FORMULA_NAME=$(cat $METADATA | python -c "import sys,yaml; print yaml.load(sys.stdin)['name']") |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 19 | FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 20 | |
| 21 | ## Overrideable parameters |
| 22 | PILLARDIR=${PILLARDIR:-${CURDIR}/pillar} |
| 23 | BUILDDIR=${BUILDDIR:-${CURDIR}/build} |
| 24 | VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv} |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 25 | MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin} |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 26 | DEPSDIR=${BUILDDIR}/deps |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 27 | SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"} |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 28 | |
| 29 | SALT_FILE_DIR=${SALT_FILE_DIR:-${BUILDDIR}/file_root} |
| 30 | SALT_PILLAR_DIR=${SALT_PILLAR_DIR:-${BUILDDIR}/pillar_root} |
| 31 | SALT_CONFIG_DIR=${SALT_CONFIG_DIR:-${BUILDDIR}/salt} |
| 32 | SALT_CACHE_DIR=${SALT_CACHE_DIR:-${SALT_CONFIG_DIR}/cache} |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 33 | SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods} |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 34 | |
| 35 | SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null" |
| 36 | |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 37 | IGNORE_MODELVALIDATE_MASK=${IGNORE_MODELVALIDATE_MASK:-"novalidate"} |
| 38 | |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 39 | if [ "x${SALT_VERSION}" != "x" ]; then |
| 40 | PIP_SALT_VERSION="==${SALT_VERSION}" |
| 41 | fi |
| 42 | |
| 43 | ## Functions |
| 44 | log_info() { |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 45 | echo -e "[INFO] $*" |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | log_err() { |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 49 | echo -e "[ERROR] $*" >&2 |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | setup_virtualenv() { |
| 53 | log_info "Setting up Python virtualenv" |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 54 | dependency_check virtualenv |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 55 | virtualenv $VENV_DIR |
| 56 | source ${VENV_DIR}/bin/activate |
Jakub Josef | 4084502 | 2018-02-08 13:22:13 +0100 | [diff] [blame] | 57 | python -m pip install salt${PIP_SALT_VERSION} |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 58 | if [[ -f ${CURDIR}/test-requirements.txt ]]; then |
| 59 | python -m pip install -r ${CURDIR}/test-requirements.txt |
| 60 | fi |
| 61 | } |
| 62 | |
| 63 | setup_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 Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | setup_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 | |
| 82 | setup_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 Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 86 | [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR} |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 87 | |
| 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 |
| 96 | file_client: local |
| 97 | cachedir: ${SALT_CACHE_DIR} |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 98 | extension_modules: ${SALT_CACHE_EXTMODS_DIR} |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 99 | verify_env: False |
| 100 | minion_id_caching: False |
| 101 | |
| 102 | file_roots: |
| 103 | base: |
| 104 | - ${SALT_FILE_DIR} |
| 105 | - ${CURDIR}/.. |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 106 | |
| 107 | pillar_roots: |
| 108 | base: |
| 109 | - ${SALT_PILLAR_DIR} |
| 110 | - ${PILLARDIR} |
| 111 | EOF |
| 112 | } |
| 113 | |
| 114 | fetch_dependency() { |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 115 | # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux" |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 116 | 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 Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 121 | dependency_check git |
| 122 | [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; } |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 123 | |
| 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 Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 132 | link_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 Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 145 | install_dependencies() { |
| 146 | grep -E "^dependencies:" ${METADATA} >/dev/null || return 0 |
| 147 | (python - | while read dep; do fetch_dependency "$dep"; done) << EOF |
| 148 | import sys,yaml |
| 149 | for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']: |
| 150 | print '%s:%s' % (dep["name"], dep["source"]) |
| 151 | EOF |
| 152 | } |
| 153 | |
| 154 | clean() { |
| 155 | log_info "Cleaning up ${BUILDDIR}" |
| 156 | [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0 |
| 157 | } |
| 158 | |
| 159 | salt_run() { |
Jakub Josef | dae63e7 | 2017-12-15 16:58:28 +0100 | [diff] [blame] | 160 | [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate |
Jakub Josef | 4084502 | 2018-02-08 13:22:13 +0100 | [diff] [blame] | 161 | python $(which salt-call) ${SALT_OPTS} $* |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | prepare() { |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 165 | 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 Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 170 | |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 171 | [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv |
| 172 | setup_mock_bin |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 173 | setup_pillar |
| 174 | setup_salt |
| 175 | install_dependencies |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 176 | link_modules |
| 177 | touch ${BUILDDIR}/.prepare_done |
| 178 | } |
| 179 | |
| 180 | lint_releasenotes() { |
| 181 | [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv |
| 182 | source ${VENV_DIR}/bin/activate |
| 183 | reno lint ${CURDIR}/../ |
| 184 | } |
| 185 | |
| 186 | lint() { |
| 187 | # lint_releasenotes |
| 188 | log_err "TODO: lint_releasenotes" |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | run() { |
| 192 | for pillar in ${PILLARDIR}/*.sls; do |
| 193 | grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue |
| 194 | state_name=$(basename ${pillar%.sls}) |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 195 | salt_run grains.set 'noservices' False force=True |
| 196 | |
| 197 | echo "Checking state ${FORMULA_NAME}.${state_name} ..." |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 198 | salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1) |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 199 | |
| 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 Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 208 | done |
| 209 | } |
| 210 | |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 211 | real_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 | |
| 218 | run_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 | |
| 245 | dependency_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 Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 253 | _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 Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 266 | |
| 267 | log_info "Running version: ${__ScriptVersion}" |
| 268 | log_info "Command line: '${__ScriptFullName} ${__ScriptArgs}'" |
| 269 | |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 270 | trap _atexit INT TERM EXIT |
| 271 | |
| 272 | case $1 in |
| 273 | clean) |
| 274 | clean |
| 275 | ;; |
| 276 | prepare) |
| 277 | prepare |
| 278 | ;; |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 279 | lint) |
| 280 | lint |
| 281 | ;; |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 282 | run) |
| 283 | run |
| 284 | ;; |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 285 | real-run) |
| 286 | real_run |
| 287 | ;; |
| 288 | model-validate) |
| 289 | prepare |
| 290 | run_model_validate |
| 291 | ;; |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 292 | *) |
| 293 | prepare |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 294 | # lint |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 295 | run |
Martin Polreich | 4f41de5 | 2019-02-06 13:01:20 +0100 | [diff] [blame] | 296 | run_model_validate |
Martin Polreich | 21f2314 | 2017-05-16 11:03:46 +0200 | [diff] [blame] | 297 | ;; |
| 298 | esac |