2018-04-16 12:31:44 -04:00
#!/usr/bin/env bash
2016-05-24 11:40:44 -04:00
# Copyright 2014 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o nounset
set -o pipefail
2019-04-11 01:53:57 -04:00
KUBE_ROOT = $( dirname " ${ BASH_SOURCE [0] } " ) /../..
2017-03-11 01:18:38 -05:00
source " ${ KUBE_ROOT } /hack/lib/util.sh "
2016-05-24 11:40:44 -04:00
2018-12-19 18:17:13 -05:00
# If KUBE_JUNIT_REPORT_DIR is unset, and ARTIFACTS is set, then have them match.
if [ [ -z " ${ KUBE_JUNIT_REPORT_DIR :- } " && -n " ${ ARTIFACTS :- } " ] ] ; then
export KUBE_JUNIT_REPORT_DIR = " ${ ARTIFACTS } "
fi
2018-01-25 16:30:25 -05:00
# include shell2junit library
source " ${ KUBE_ROOT } /third_party/forked/shell2junit/sh2ju.sh "
2017-05-31 19:52:51 -04:00
# Excluded check patterns are always skipped.
EXCLUDED_PATTERNS = (
"verify-all.sh" # this script calls the make rule and would cause a loop
"verify-*-dockerized.sh" # Don't run any scripts that intended to be run dockerized
2022-05-03 07:18:23 -04:00
"verify-golangci-lint-pr.sh" # Don't run this as part of the block pull-kubernetes-verify yet. TODO(pohly): try this in a non-blocking job and then reconsider this.
2022-05-06 09:33:03 -04:00
"verify-licenses.sh" # runs in a separate job to monitor availability of the dependencies periodically
2023-03-10 20:36:03 -05:00
"verify-openapi-docs-urls.sh" # Spams docs URLs, don't run in CI.
2016-05-24 11:40:44 -04:00
)
2018-12-11 19:42:45 -05:00
# Exclude typecheck in certain cases, if they're running in a separate job.
if [ [ ${ EXCLUDE_TYPECHECK :- } = ~ ^[ yY] $ ] ] ; then
EXCLUDED_PATTERNS += (
2019-11-19 12:43:18 -05:00
"verify-typecheck.sh" # runs in separate typecheck job
"verify-typecheck-providerless.sh" # runs in separate typecheck job
2018-12-11 19:42:45 -05:00
)
fi
2020-05-12 04:26:16 -04:00
# Exclude dependency checks in certain cases, if they're running in a separate job.
# From @cblecker: We can't change the variable name here, unless we update it throughout
# test-infra (and we would need to pick it backwards).
2018-12-11 19:42:45 -05:00
if [ [ ${ EXCLUDE_GODEP :- } = ~ ^[ yY] $ ] ] ; then
EXCLUDED_PATTERNS += (
2020-05-12 04:26:16 -04:00
"verify-external-dependencies-version.sh" # runs in separate dependencies job
"verify-vendor.sh" # runs in separate dependencies job
"verify-vendor-licenses.sh" # runs in separate dependencies job
2018-12-11 19:42:45 -05:00
)
fi
2019-01-11 19:37:46 -05:00
# Exclude readonly package check in certain cases, aka, in periodic jobs we don't care and a readonly package won't be touched
if [ [ ${ EXCLUDE_READONLY_PACKAGE :- } = ~ ^[ yY] $ ] ] ; then
EXCLUDED_PATTERNS += (
"verify-readonly-packages.sh" # skip in CI, if env is set
)
fi
2021-03-07 16:03:17 -05:00
# Only run known fast checks in quick mode.
# These ideally run in less than 10s.
2017-07-12 18:48:12 -04:00
QUICK_PATTERNS += (
"verify-api-groups.sh"
"verify-boilerplate.sh"
2020-05-12 04:26:16 -04:00
"verify-external-dependencies-version.sh"
2019-03-04 00:27:03 -05:00
"verify-vendor-licenses.sh"
2017-07-12 18:48:12 -04:00
"verify-gofmt.sh"
2017-10-25 07:26:42 -04:00
"verify-imports.sh"
2021-10-08 11:19:18 -04:00
"verify-non-mutating-validation.sh"
2017-07-12 18:48:12 -04:00
"verify-pkg-names.sh"
"verify-readonly-packages.sh"
2018-02-06 05:12:36 -05:00
"verify-spelling.sh"
2017-07-12 18:48:12 -04:00
"verify-staging-client-go.sh"
2018-08-23 22:51:30 -04:00
"verify-staging-meta-files.sh"
2018-11-21 10:11:14 -05:00
"verify-test-featuregates.sh"
2017-07-12 18:48:12 -04:00
"verify-test-images.sh"
)
2020-10-16 14:39:25 -04:00
while IFS = '' read -r line; do EXCLUDED_CHECKS += ( " $line " ) ; done < <( ls " ${ EXCLUDED_PATTERNS [@]/#/ ${ KUBE_ROOT } /hack/ } " 2>/dev/null || true )
while IFS = '' read -r line; do QUICK_CHECKS += ( " $line " ) ; done < <( ls " ${ QUICK_PATTERNS [@]/#/ ${ KUBE_ROOT } /hack/ } " 2>/dev/null || true )
2019-04-08 03:38:18 -04:00
TARGET_LIST = ( )
IFS = " " read -r -a TARGET_LIST <<< " ${ WHAT :- } "
2017-05-31 19:52:51 -04:00
2016-05-24 11:40:44 -04:00
function is-excluded {
2019-04-11 01:53:57 -04:00
for e in " ${ EXCLUDED_CHECKS [@] } " ; do
2019-01-21 17:11:58 -05:00
if [ [ $1 -ef " ${ e } " ] ] ; then
2016-05-24 11:40:44 -04:00
return
fi
done
return 1
}
2017-07-12 18:48:12 -04:00
function is-quick {
2019-04-11 01:53:57 -04:00
for e in " ${ QUICK_CHECKS [@] } " ; do
2019-01-21 17:11:58 -05:00
if [ [ $1 -ef " ${ e } " ] ] ; then
2017-07-12 18:48:12 -04:00
return
fi
done
return 1
}
2018-02-28 23:44:37 -05:00
function is-explicitly-chosen {
local name = " ${ 1 #verify- } "
name = " ${ name %.* } "
2019-04-08 03:38:18 -04:00
index = 0
for e in " ${ TARGET_LIST [@] } " ; do
2019-01-21 17:11:58 -05:00
if [ [ " ${ e } " = = " ${ name } " ] ] ; then
2019-04-08 03:38:18 -04:00
TARGET_LIST[ ${ index } ] = ""
2018-02-28 23:44:37 -05:00
return
fi
2019-04-08 03:38:18 -04:00
index = $(( index + 1 ))
2018-02-28 23:44:37 -05:00
done
return 1
}
2016-05-24 11:40:44 -04:00
function run-cmd {
2018-01-25 16:30:25 -05:00
local filename = " ${ 2 ##*/verify- } "
local testname = " ${ filename %%.* } "
local output = " ${ KUBE_JUNIT_REPORT_DIR :- /tmp/junit-results } "
local tr
2016-05-24 11:40:44 -04:00
if ${ SILENT } ; then
2018-01-25 16:30:25 -05:00
juLog -output= " ${ output } " -class= "verify" -name= " ${ testname } " " $@ " & > /dev/null
tr = $?
2016-05-24 11:40:44 -04:00
else
2018-01-25 16:30:25 -05:00
juLog -output= " ${ output } " -class= "verify" -name= " ${ testname } " " $@ "
tr = $?
2016-05-24 11:40:44 -04:00
fi
2018-01-25 16:30:25 -05:00
return ${ tr }
2016-05-24 11:40:44 -04:00
}
2018-02-09 01:53:53 -05:00
# Collect Failed tests in this Array , initialize it to nil
2017-07-27 09:40:17 -04:00
FAILED_TESTS = ( )
function print-failed-tests {
echo -e "========================"
2019-04-11 01:53:57 -04:00
echo -e " ${ color_red : ? } FAILED TESTS ${ color_norm : ? } "
2017-07-27 09:40:17 -04:00
echo -e "========================"
2019-04-11 01:53:57 -04:00
for t in " ${ FAILED_TESTS [@] } " ; do
2017-07-27 09:40:17 -04:00
echo -e " ${ color_red } ${ t } ${ color_norm } "
done
}
2016-05-24 11:40:44 -04:00
function run-checks {
local -r pattern = $1
local -r runner = $2
2017-11-21 13:12:26 -05:00
local t
2019-04-11 01:53:57 -04:00
for t in ${ pattern }
2016-05-24 11:40:44 -04:00
do
2019-04-11 01:53:57 -04:00
local check_name
check_name = " $( basename " ${ t } " ) "
if [ [ -n ${ WHAT :- } ] ] ; then
2018-02-28 23:44:37 -05:00
if ! is-explicitly-chosen " ${ check_name } " ; then
continue
fi
else
if is-excluded " ${ t } " ; then
echo " Skipping ${ check_name } "
continue
fi
if ${ QUICK } && ! is-quick " ${ t } " ; then
echo " Skipping ${ check_name } in quick mode "
continue
fi
2017-07-12 18:48:12 -04:00
fi
2017-11-21 13:12:26 -05:00
echo -e " Verifying ${ check_name } "
2019-04-11 01:53:57 -04:00
local start
start = $( date +%s)
2016-05-24 11:40:44 -04:00
run-cmd " ${ runner } " " ${ t } " && tr = $? || tr = $?
2019-04-11 01:53:57 -04:00
local elapsed = $(( $( date +%s) - start))
2016-05-24 11:40:44 -04:00
if [ [ ${ tr } -eq 0 ] ] ; then
2019-04-11 01:53:57 -04:00
echo -e " ${ color_green : ? } SUCCESS ${ color_norm } ${ check_name } \t ${ elapsed } s "
2016-05-24 11:40:44 -04:00
else
2017-11-21 13:12:26 -05:00
echo -e " ${ color_red } FAILED ${ color_norm } ${ check_name } \t ${ elapsed } s "
2016-05-24 11:40:44 -04:00
ret = 1
2019-04-11 01:53:57 -04:00
FAILED_TESTS += ( " ${ t } " )
2016-05-24 11:40:44 -04:00
fi
done
}
2019-04-08 03:38:18 -04:00
# Check invalid targets specified in "WHAT" and mark them as failure cases
function missing-target-checks {
# In case WHAT is not specified
[ [ ${# TARGET_LIST [@] } -eq 0 ] ] && return
for v in " ${ TARGET_LIST [@] } "
do
[ [ -z " ${ v } " ] ] && continue
2019-10-17 20:01:21 -04:00
2019-04-11 01:53:57 -04:00
FAILED_TESTS += ( " ${ v } " )
2019-04-08 03:38:18 -04:00
ret = 1
done
}
2018-01-25 16:30:30 -05:00
SILENT = ${ SILENT :- false }
QUICK = ${ QUICK :- false }
2016-05-24 11:40:44 -04:00
if ${ SILENT } ; then
2018-01-25 16:30:30 -05:00
echo "Running in silent mode, run with SILENT=false if you want to see script logs."
2016-05-24 11:40:44 -04:00
fi
2017-07-12 18:48:12 -04:00
if ${ QUICK } ; then
2018-01-25 16:30:30 -05:00
echo "Running in quick mode (QUICK=true). Only fast checks will run."
2017-07-12 18:48:12 -04:00
fi
2016-05-24 11:40:44 -04:00
ret = 0
run-checks " ${ KUBE_ROOT } /hack/verify-*.sh " bash
2020-08-01 08:27:16 -04:00
run-checks " ${ KUBE_ROOT } /hack/verify-*.py " python3
2019-04-08 03:38:18 -04:00
missing-target-checks
2017-07-27 09:40:17 -04:00
if [ [ ${ ret } -eq 1 ] ] ; then
2018-01-25 16:30:25 -05:00
print-failed-tests
2017-07-27 09:40:17 -04:00
fi
2016-05-24 11:40:44 -04:00
exit ${ ret }
# ex: ts=2 sw=2 et filetype=sh