b3a64c6edd41fbbaf90bd80e02d5ea56d4bec1d2
[infra/cicd.git] / jjb / engine / scripts / engine-verify-tox.sh
1 #!/bin/bash
2
3 # ============LICENSE_START=======================================================
4 #  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
20
21 set -o nounset
22 set -o errexit
23 set -o pipefail
24
25 #-------------------------------------------------------------------------------
26 # Various tests are run using tox such as docs, yamllint, ansible-lint, and
27 # shellcheck. Some repos do not have certain types of files and those repos
28 # are ignored using REPOS_TO_IGNORE variable which is controlled within the
29 # jjb per lint type.
30 #-------------------------------------------------------------------------------
31 run_tox() {
32
33   echo "Info  : Preparing to run tox for the repo $GERRIT_PROJECT"
34
35   # set defaults
36   VERBOSITY=${VERBOSITY:-false}
37   LINT_TYPE="${LINT_TYPE:-ansible-lint}"
38   export VERBOSITY LINT_TYPE
39
40   # check if the change is for a repo we should ignore
41   if [[ "$REPOS_TO_IGNORE" =~ "$GERRIT_PROJECT" ]]; then
42     echo "Info  : Ignoring change as $GERRIT_PROJECT is in ignore list!"
43     echo "Info  : Done!"
44     exit 0
45   fi
46
47   # ensure we are in job build WORKSPACE
48   cd "$WORKSPACE"
49
50   # set DEBIAN_FRONTEND to run apt non-interactively
51   DEBIAN_FRONTEND=noninteractive
52   export DEBIAN_FRONTEND
53
54   # install dependencies
55   echo "Info  : Install python3.6-minimal python3-distutils virtualenv using apt"
56   redirect_cmd sudo apt update
57   redirect_cmd sudo apt install -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confnew -y python3.6-minimal python3-distutils virtualenv
58
59   # create and activate virtualenv
60   echo "Info  : Create and activate python virtualenv"
61   redirect_cmd virtualenv -p python3.6 .venv
62   set +u
63   redirect_cmd source .venv/bin/activate
64   set -u
65
66   # install test-requirements
67   echo "Info  : Install python packages listed in test-requirements.txt using pip"
68   redirect_cmd pip install --force-reinstall -r test-requirements.txt
69
70   # run tox
71   echo "Info  : Run $LINT_TYPE using tox"
72   echo "----------------------------------------------------"
73   tox -e "$LINT_TYPE"
74   echo "----------------------------------------------------"
75   echo "Info  : Done!"
76
77 }
78 #-------------------------------------------------------------------------------
79 # In some cases, it is useful to see all the output generated by commands so
80 # this function makes it possible for users to achieve that by not redirecting
81 # output to /dev/null when verbosity is enabled
82 #-------------------------------------------------------------------------------
83 redirect_cmd() {
84
85   if [[ "$VERBOSITY" == "false" ]]; then
86     "$@" > /dev/null 2>&1
87   else
88     "$@"
89   fi
90
91 }
92 #-------------------------------------------------------------------------------
93 # run tox
94 #-------------------------------------------------------------------------------
95
96 run_tox "$@"
97
98 # vim: set ts=2 sw=2 expandtab: