forked from knative/serving
/
presubmit-tests.sh
executable file
·137 lines (120 loc) · 3.88 KB
/
presubmit-tests.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
# Copyright 2018 The Knative 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.
# This script runs the presubmit tests; it is started by prow for each PR.
# For convenience, it can also be executed manually.
# Running the script without parameters, or with the --all-tests
# flag, causes all tests to be executed, in the right order.
# Use the flags --build-tests, --unit-tests and --integration-tests
# to run a specific set of tests.
# Extensions or file patterns that don't require presubmit tests
readonly NO_PRESUBMIT_FILES=(\.md \.png ^OWNERS)
source "$(dirname $(readlink -f ${BASH_SOURCE}))/library.sh"
# Helper functions.
function build_tests() {
header "Running build tests"
go build -v ./cmd/... ./sample/... ./pkg/... || result=1
# "go build" on //test will fail with "no non-test Go files in ..."
# See https://github.com/golang/go/issues/8279 for details
# Workaround is to use "go test -run" with a non matching pattern
go test -run=^$ -tags=e2e ./test/... || result=1
# kubekins images don't have dep installed by default
subheader "Checking autogenerated code is up-to-date"
go get -u github.com/golang/dep/cmd/dep
./hack/verify-codegen.sh || result=1
return ${result}
}
function unit_tests() {
header "Running unit tests"
report_go_test ./...
}
function integration_tests() {
# Make sure environment variables are intact.
local options=""
(( EMIT_METRICS )) && options="--emit-metrics"
./test/e2e-tests.sh ${options}
}
# Script entry point.
# Parse script arguments:
# --all-tests or no arguments: run all tests
# --build-tests: run only the build tests
# --unit-tests: run only the unit tests
# --integration-tests: run only the integration tests
# --emit-metrics: emit metrics when running the E2E tests
RUN_BUILD_TESTS=0
RUN_UNIT_TESTS=0
RUN_INTEGRATION_TESTS=0
EMIT_METRICS=0
all_parameters=$@
[[ -z $1 ]] && all_parameters="--all-tests"
for parameter in ${all_parameters}; do
case $parameter in
--all-tests)
RUN_BUILD_TESTS=1
RUN_UNIT_TESTS=1
RUN_INTEGRATION_TESTS=1
shift
;;
--build-tests)
RUN_BUILD_TESTS=1
shift
;;
--unit-tests)
RUN_UNIT_TESTS=1
shift
;;
--integration-tests)
RUN_INTEGRATION_TESTS=1
shift
;;
--emit-metrics)
EMIT_METRICS=1
shift
;;
*)
echo "error: unknown option ${parameter}"
exit 1
;;
esac
done
readonly RUN_BUILD_TESTS
readonly RUN_UNIT_TESTS
readonly RUN_INTEGRATION_TESTS
readonly EMIT_METRICS
cd ${SERVING_ROOT_DIR}
# Skip presubmit tests if only markdown files were changed.
if [[ -n "${PULL_PULL_SHA}" ]]; then
# On a presubmit job
changes="$(git diff --name-only ${PULL_PULL_SHA} ${PULL_BASE_SHA})"
no_presubmit_pattern="${NO_PRESUBMIT_FILES[*]}"
no_presubmit_pattern="\(${no_presubmit_pattern// /\\|}\)$"
echo -e "Changed files in commit ${PULL_PULL_SHA}:\n${changes}"
if [[ -z "$(echo "${changes}" | grep -v ${no_presubmit_pattern})" ]]; then
# Nothing changed other than files that don't require presubmit tests
header "Commit only contains changes that don't affect tests, skipping"
exit 0
fi
fi
# Tests to be performed, in the right order if --all-tests is passed.
result=0
if (( RUN_BUILD_TESTS )); then
build_tests || result=1
fi
if (( RUN_UNIT_TESTS )); then
unit_tests || result=1
fi
if (( RUN_INTEGRATION_TESTS )); then
integration_tests || result=1
fi
exit ${result}