Skip to content

Commit 901a5db

Browse files
committed
Merge pull request kubernetes#5687 from nikhiljindal/client_test
Updating test-go.sh and test-integration.sh to be able to run the tests for multiple API versions
2 parents 6145b3b + 5e4ab8e commit 901a5db

File tree

3 files changed

+119
-87
lines changed

3 files changed

+119
-87
lines changed

.travis.yml

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ sudo: false
22

33
language: go
44

5-
go:
6-
- 1.4
7-
- 1.3
5+
matrix:
6+
include:
7+
- go: 1.4
8+
env: KUBE_TEST_API_VERSIONS="v1beta1"
9+
- go: 1.3
10+
env: KUBE_TEST_API_VERSIONS="v1beta3"
811

912
install:
1013
- if ! go get code.google.com/p/go.tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi
@@ -18,10 +21,10 @@ install:
1821
- GOPATH=$PWD/Godeps/_workspace:$GOPATH go install ./...
1922

2023
script:
21-
- KUBE_RACE="-race" KUBE_COVER="y" KUBE_GOVERALLS_BIN="$HOME/gopath/bin/goveralls" KUBE_TIMEOUT='-timeout 300s' KUBE_COVERPROCS=8 ./hack/test-go.sh -- -p=2
24+
- KUBE_RACE="-race" KUBE_COVER="y" KUBE_GOVERALLS_BIN="$HOME/gopath/bin/goveralls" KUBE_TIMEOUT='-timeout 300s' KUBE_COVERPROCS=8 KUBE_TEST_API_VERSIONS=$KUBE_TEST_API_VERSIONS ./hack/test-go.sh -- -p=2
2225
- PATH=$HOME/gopath/bin:./third_party/etcd:$PATH ./hack/test-cmd.sh
2326
- PATH=$HOME/gopath/bin:./third_party/etcd:$PATH ./hack/verify-gendocs.sh
24-
- PATH=$HOME/gopath/bin:./third_party/etcd:$PATH ./hack/test-integration.sh
27+
- PATH=$HOME/gopath/bin:./third_party/etcd:$PATH KUBE_TEST_API_VERSIONS=$KUBE_TEST_API_VERSIONS ./hack/test-integration.sh
2528

2629
notifications:
2730
irc: "chat.freenode.net#google-containers"

hack/test-go.sh

+102-80
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ KUBE_COVERPROCS=${KUBE_COVERPROCS:-4}
5050
KUBE_RACE=${KUBE_RACE:-} # use KUBE_RACE="-race" to enable race testing
5151
# Set to the goveralls binary path to report coverage results to Coveralls.io.
5252
KUBE_GOVERALLS_BIN=${KUBE_GOVERALLS_BIN:-}
53+
# Comma separated list of API Versions that should be tested.
54+
KUBE_TEST_API_VERSIONS=${KUBE_TEST_API_VERSIONS:-"v1beta1,v1beta3"}
5355

5456
kube::test::usage() {
5557
kube::log::usage_from_stdin <<EOF
@@ -109,87 +111,107 @@ if [[ ${#testcases[@]} -eq 0 ]]; then
109111
fi
110112
set -- "${testcases[@]+${testcases[@]}}"
111113

112-
# TODO: this should probably be refactored to avoid code duplication with the
113-
# coverage version.
114-
if [[ $iterations -gt 1 ]]; then
115-
if [[ $# -eq 0 ]]; then
116-
set -- $(kube::test::find_dirs)
114+
runTests() {
115+
# TODO: this should probably be refactored to avoid code duplication with the
116+
# coverage version.
117+
if [[ $iterations -gt 1 ]]; then
118+
if [[ $# -eq 0 ]]; then
119+
set -- $(kube::test::find_dirs)
120+
fi
121+
kube::log::status "Running ${iterations} times"
122+
fails=0
123+
for arg; do
124+
trap 'exit 1' SIGINT
125+
pkg=${KUBE_GO_PACKAGE}/${arg}
126+
kube::log::status "${pkg}"
127+
# keep going, even if there are failures
128+
pass=0
129+
count=0
130+
for i in $(seq 1 ${iterations}); do
131+
if go test "${goflags[@]:+${goflags[@]}}" \
132+
${KUBE_RACE} ${KUBE_TIMEOUT} "${pkg}"; then
133+
pass=$((pass + 1))
134+
else
135+
fails=$((fails + 1))
136+
fi
137+
count=$((count + 1))
138+
done 2>&1
139+
kube::log::status "${pass} / ${count} passed"
140+
done
141+
if [[ ${fails} -gt 0 ]]; then
142+
return 1
143+
else
144+
return 0
145+
fi
117146
fi
118-
kube::log::status "Running ${iterations} times"
119-
fails=0
120-
for arg; do
121-
trap 'exit 1' SIGINT
122-
pkg=${KUBE_GO_PACKAGE}/${arg}
123-
kube::log::status "${pkg}"
124-
# keep going, even if there are failures
125-
pass=0
126-
count=0
127-
for i in $(seq 1 ${iterations}); do
128-
if go test "${goflags[@]:+${goflags[@]}}" \
129-
${KUBE_RACE} ${KUBE_TIMEOUT} "${pkg}"; then
130-
pass=$((pass + 1))
131-
else
132-
fails=$((fails + 1))
133-
fi
134-
count=$((count + 1))
135-
done 2>&1
136-
kube::log::status "${pass} / ${count} passed"
137-
done
138-
if [[ ${fails} -gt 0 ]]; then
139-
exit 1
140-
else
141-
exit 0
147+
148+
# If we're not collecting coverage, run all requested tests with one 'go test'
149+
# command, which is much faster.
150+
if [[ ! ${KUBE_COVER} =~ ^[yY]$ ]]; then
151+
kube::log::status "Running unit tests without code coverage"
152+
go test "${goflags[@]:+${goflags[@]}}" \
153+
${KUBE_RACE} ${KUBE_TIMEOUT} "${@+${@/#/${KUBE_GO_PACKAGE}/}}"
154+
return 0
142155
fi
143-
fi
144156

145-
# If we're not collecting coverage, run all requested tests with one 'go test'
146-
# command, which is much faster.
147-
if [[ ! ${KUBE_COVER} =~ ^[yY]$ ]]; then
148-
kube::log::status "Running unit tests without code coverage"
149-
go test "${goflags[@]:+${goflags[@]}}" \
150-
${KUBE_RACE} ${KUBE_TIMEOUT} "${@+${@/#/${KUBE_GO_PACKAGE}/}}"
151-
exit 0
152-
fi
157+
# Create coverage report directories.
158+
cover_report_dir="/tmp/k8s_coverage/${KUBE_API_VERSION}/$(kube::util::sortable_date)"
159+
cover_profile="coverage.out" # Name for each individual coverage profile
160+
kube::log::status "Saving coverage output in '${cover_report_dir}'"
161+
mkdir -p "${@+${@/#/${cover_report_dir}/}}"
162+
163+
# Run all specified tests, collecting coverage results. Go currently doesn't
164+
# support collecting coverage across multiple packages at once, so we must issue
165+
# separate 'go test' commands for each package and then combine at the end.
166+
# To speed things up considerably, we can at least use xargs -P to run multiple
167+
# 'go test' commands at once.
168+
printf "%s\n" "${@}" | xargs -I{} -n1 -P${KUBE_COVERPROCS} \
169+
go test "${goflags[@]:+${goflags[@]}}" \
170+
${KUBE_RACE} \
171+
${KUBE_TIMEOUT} \
172+
-cover -covermode="${KUBE_COVERMODE}" \
173+
-coverprofile="${cover_report_dir}/{}/${cover_profile}" \
174+
"${cover_params[@]+${cover_params[@]}}" \
175+
"${KUBE_GO_PACKAGE}/{}"
176+
177+
COMBINED_COVER_PROFILE="${cover_report_dir}/combined-coverage.out"
178+
{
179+
# The combined coverage profile needs to start with a line indicating which
180+
# coverage mode was used (set, count, or atomic). This line is included in
181+
# each of the coverage profiles generated when running 'go test -cover', but
182+
# we strip these lines out when combining so that there's only one.
183+
echo "mode: ${KUBE_COVERMODE}"
184+
185+
# Include all coverage reach data in the combined profile, but exclude the
186+
# 'mode' lines, as there should be only one.
187+
for x in `find "${cover_report_dir}" -name "${cover_profile}"`; do
188+
cat $x | grep -h -v "^mode:" || true
189+
done
190+
} >"${COMBINED_COVER_PROFILE}"
191+
192+
coverage_html_file="${cover_report_dir}/combined-coverage.html"
193+
go tool cover -html="${COMBINED_COVER_PROFILE}" -o="${coverage_html_file}"
194+
kube::log::status "Combined coverage report: ${coverage_html_file}"
195+
}
153196

154-
# Create coverage report directories.
155-
cover_report_dir="/tmp/k8s_coverage/$(kube::util::sortable_date)"
156-
cover_profile="coverage.out" # Name for each individual coverage profile
157-
kube::log::status "Saving coverage output in '${cover_report_dir}'"
158-
mkdir -p "${@+${@/#/${cover_report_dir}/}}"
159-
160-
# Run all specified tests, collecting coverage results. Go currently doesn't
161-
# support collecting coverage across multiple packages at once, so we must issue
162-
# separate 'go test' commands for each package and then combine at the end.
163-
# To speed things up considerably, we can at least use xargs -P to run multiple
164-
# 'go test' commands at once.
165-
printf "%s\n" "${@}" | xargs -I{} -n1 -P${KUBE_COVERPROCS} \
166-
go test "${goflags[@]:+${goflags[@]}}" \
167-
${KUBE_RACE} \
168-
${KUBE_TIMEOUT} \
169-
-cover -covermode="${KUBE_COVERMODE}" \
170-
-coverprofile="${cover_report_dir}/{}/${cover_profile}" \
171-
"${cover_params[@]+${cover_params[@]}}" \
172-
"${KUBE_GO_PACKAGE}/{}"
173-
174-
combined_cover_profile="${cover_report_dir}/combined-coverage.out"
175-
{
176-
# The combined coverage profile needs to start with a line indicating which
177-
# coverage mode was used (set, count, or atomic). This line is included in
178-
# each of the coverage profiles generated when running 'go test -cover', but
179-
# we strip these lines out when combining so that there's only one.
180-
echo "mode: ${KUBE_COVERMODE}"
181-
182-
# Include all coverage reach data in the combined profile, but exclude the
183-
# 'mode' lines, as there should be only one.
184-
for x in `find "${cover_report_dir}" -name "${cover_profile}"`; do
185-
cat $x | grep -h -v "^mode:" || true
186-
done
187-
} >"${combined_cover_profile}"
188-
189-
coverage_html_file="${cover_report_dir}/combined-coverage.html"
190-
go tool cover -html="${combined_cover_profile}" -o="${coverage_html_file}"
191-
kube::log::status "Combined coverage report: ${coverage_html_file}"
192-
193-
if [[ -x "${KUBE_GOVERALLS_BIN}" ]]; then
194-
${KUBE_GOVERALLS_BIN} -coverprofile="${combined_cover_profile}" || true
195-
fi
197+
runTestsForVersion() {
198+
export KUBE_API_VERSION="$1"
199+
runTests "${@:2}"
200+
}
201+
202+
reportCoverageToCoveralls() {
203+
if [[ -x "${KUBE_GOVERALLS_BIN}" ]]; then
204+
${KUBE_GOVERALLS_BIN} -coverprofile="${COMBINED_COVER_PROFILE}" || true
205+
fi
206+
}
207+
208+
# Convert the CSV to an array of API versions to test
209+
IFS=',' read -a apiVersions <<< ${KUBE_TEST_API_VERSIONS}
210+
for apiVersion in "${apiVersions[@]}"; do
211+
echo "Running tests for APIVersion: $apiVersion"
212+
runTestsForVersion $apiVersion "${@}"
213+
done
214+
215+
# We might run the tests for multiple versions, but we want to report only
216+
# one of them to coveralls. Here we report coverage from the last run.
217+
reportCoverageToCoveralls

hack/test-integration.sh

+9-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ set -o pipefail
2424

2525
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
2626
source "${KUBE_ROOT}/hack/lib/init.sh"
27+
# Comma separated list of API Versions that should be tested.
28+
KUBE_TEST_API_VERSIONS=${KUBE_TEST_API_VERSIONS:-"v1beta1,v1beta3"}
29+
2730

2831
cleanup() {
2932
kube::etcd::cleanup
@@ -36,6 +39,7 @@ runTests() {
3639
kube::log::status "Running integration test cases"
3740
KUBE_GOFLAGS="-tags 'integration no-docker' " \
3841
KUBE_RACE="-race" \
42+
KUBE_TEST_API_VERSIONS="$1" \
3943
"${KUBE_ROOT}/hack/test-go.sh" test/integration
4044

4145
kube::log::status "Running integration test scenario"
@@ -50,5 +54,8 @@ runTests() {
5054
# Run cleanup to stop etcd on interrupt or other kill signal.
5155
trap cleanup EXIT
5256

53-
runTests "v1beta1"
54-
runTests "v1beta3"
57+
# Convert the CSV to an array of API versions to test
58+
IFS=',' read -a apiVersions <<< ${KUBE_TEST_API_VERSIONS}
59+
for apiVersion in "${apiVersions[@]}"; do
60+
runTests $apiVersion
61+
done

0 commit comments

Comments
 (0)