From aae8d315685db4e32b58473feaf0a8185d21dce4 Mon Sep 17 00:00:00 2001 From: alexz Date: Tue, 26 Jul 2016 19:04:55 +0300 Subject: [PATCH] Add simple {shell,yaml}-check script * shellcheck utility allow to determinate common mistakes of bash scripting[1] * yamllint utility allow to determinate common mistakes of yaml files[3] * Add List system dependencies for running common tests Add an other-requirements.txt file containing a cross-platform list of dependencies needed for running included tox-based tests. Also include a tox environment for convenience calling the bindep[2] utility to list any missing system requirements. For other-requirements.txt see also: http://docs.openstack.org/infra/manual/drivers.html#package-requirements [1] http://hackage.haskell.org/package/ShellCheck [2] http://docs.openstack.org/infra/bindep/ [3] https://pypi.python.org/pypi/yamllint/ Change-Id: Ia2498bdb0f7c310ec3d2c2f11f5d3fc08c8b352c --- other-requirements.txt | 2 ++ tools/jenkins/shellcheck.sh | 55 +++++++++++++++++++++++++++++++++++++ tools/jenkins/yamllint.sh | 55 +++++++++++++++++++++++++++++++++++++ tox.ini | 37 +++++++++++++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 other-requirements.txt create mode 100755 tools/jenkins/shellcheck.sh create mode 100755 tools/jenkins/yamllint.sh diff --git a/other-requirements.txt b/other-requirements.txt new file mode 100644 index 0000000..3d190bd --- /dev/null +++ b/other-requirements.txt @@ -0,0 +1,2 @@ +shellcheck [platform:ubuntu] +zip [platform:ubuntu] diff --git a/tools/jenkins/shellcheck.sh b/tools/jenkins/shellcheck.sh new file mode 100755 index 0000000..0d007d5 --- /dev/null +++ b/tools/jenkins/shellcheck.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +WORKSPACE="${WORKSPACE:-${1}}" + +function help_m() { + cat <<-EOF + *********************************************************************** + Shellcheck script help message: + Please use env variable: + - Set directory for scan: + export WORKSPACE='/dir/with/sh/files/to/scan' + - or directly: + ./shellcheck.sh "/dir/with/sh/files/to/scan" + *********************************************************************** + EOF +} + +function run_check() { + local e_count=0 + + cat <<-EOF + *********************************************************************** + * + * Starting shellcheck against dir:"${WORKSPACE}" + * + *********************************************************************** + EOF + while read -d '' -r script; do + unset RESULT + shellcheck "${script}" + RESULT=$? + if [ ${RESULT} != 0 ]; then + ((e_count++)) + fi + done < <(find "${WORKSPACE}" -name '*.sh' -print0) + cat <<-EOF + *********************************************************************** + * + * shellcheck finished with ${e_count} errors. + * + *********************************************************************** + EOF + if [ "${e_count}" -gt 0 ] ; then + exit 1 + fi +} + +### Body: + +if [[ -z "${WORKSPACE}" ]]; then + echo "ERROR: \${WORKSPACE} variable is not set!" + help_m + exit 1 +fi +run_check diff --git a/tools/jenkins/yamllint.sh b/tools/jenkins/yamllint.sh new file mode 100755 index 0000000..e0b9563 --- /dev/null +++ b/tools/jenkins/yamllint.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +WORKSPACE="${WORKSPACE:-${1}}" + +function help_m() { + cat <<-EOF + *********************************************************************** + Yamllint script help message: + Please use env variable: + - Set directory for scan: + export WORKSPACE='/dir/with/sh/files/to/scan' + - or directly: + ./yamllint.sh "/dir/with/sh/files/to/scan" + *********************************************************************** + EOF +} + +function run_check() { + local e_count=0 + + cat <<-EOF + *********************************************************************** + * + * Starting yamllint against dir:"${WORKSPACE}" + * + *********************************************************************** + EOF + while read -d '' -r y_file; do + unset RESULT + yamllint -d relaxed "${y_file}" + RESULT=$? + if [ ${RESULT} != 0 ]; then + ((e_count++)) + fi + done < <(find "${WORKSPACE}" -name '*.yaml' -print0) + cat <<-EOF + *********************************************************************** + * + * yamllint finished with ${e_count} errors. + * + *********************************************************************** + EOF + if [ "${e_count}" -gt 0 ] ; then + exit 1 + fi +} + +### Body: + +if [[ -z "${WORKSPACE}" ]]; then + echo "ERROR: \${WORKSPACE} variable is not set!" + help_m + exit 1 +fi +run_check diff --git a/tox.ini b/tox.ini index 5d75a15..984cbd7 100644 --- a/tox.ini +++ b/tox.ini @@ -35,3 +35,40 @@ commands = python -m unittest tests.test_cicd_apps.MuranoCiCdTest.test_deploy_ci [testenv:units] # FIXME! commands = python -m unittest unittests.test_namespaces.TestNamespaces.test_namespaces + +[testenv:bindep] +# Do not install any requirements. We want this to be fast and work even if +# system dependencies are missing, since it's used to tell you what system +# dependencies are missing! This also means that bindep must be installed +# separately, outside of the requirements files. +deps = bindep +commands = bindep test + +[testenv:shellcheck] +# 'shellcheck' is not an Python package, so it can be run w\o +# virtual env. But tox is a usable wrapper to run any kind of tests - +# let's use it for common test-run as well - for unification purposes. +whitelist_externals = shellcheck +commands = {toxinidir}/tools/jenkins/shellcheck.sh {toxinidir} + +[testenv:yaml-syntaxcheck] +deps = yamllint +commands = {toxinidir}/tools/jenkins/yamllint.sh {toxinidir} + +[testenv:linters] +# linters env - it's a combination of check's (usually syntax) +# for aggregate non-destructive run's. Used only in openstack-infra ci for +# decrease resource usage. +# Current duplicate list: +# shellcheck +# yaml-syntaxcheck +# +# We need to suppress exit code from 'command1', to be able run 'command2'; +# Otherwise, if some command failed - exit code from tox itself will be 1 +ignore_errors=True +deps = yamllint +whitelist_externals = shellcheck +commands = + {toxinidir}/tools/jenkins/shellcheck.sh {toxinidir} + {toxinidir}/tools/jenkins/yamllint.sh {toxinidir} +