forked from GoogleCloudPlatform/cloudml-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_tests.sh
executable file
·129 lines (114 loc) · 4.16 KB
/
run_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
#!/bin/bash
# Copyright 2019 Google LLC
#
# 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.
# `-e` enables the script to automatically fail when a command fails
# `-o pipefail` sets the exit code to the rightmost comment to exit with a non-zero
set -eo pipefail
# Enables `**` to include files nested inside sub-folders
shopt -s globstar
# `--only-changed` will only run tests on projects container changes from the master branch.
if [[ $* == *--only-diff* ]]; then
ONLY_DIFF="true"
else
ONLY_DIFF="false"
fi
project_setup(){
# Update to latest SDK for gcloud ai-platform command.
local KEYFILE="${KOKORO_GFILE_DIR}/keyfile.json"
gcloud components update --quiet
export GOOGLE_APPLICATION_CREDENTIALS="${KEYFILE}"
gcloud auth activate-service-account --key-file "${KEYFILE}"
gcloud config list
}
create_virtualenv(){
virtualenv -p $(which "${CMLE_PYTHON_VERSION}") "${KOKORO_ARTIFACTS_DIR}"/envs/"${CMLE_PYTHON_VERSION}"/venv
source "${KOKORO_ARTIFACTS_DIR}"/envs/"${CMLE_PYTHON_VERSION}"/venv/bin/activate
}
download_files() {
# Download files for testing.
GCS_FOLDER="gs://cloud-samples-data/ml-engine/census/data"
data_dir="${KOKORO_ARTIFACTS_DIR}"/data
echo "------------------------------------------------------------"
echo "- Downloading files to $data_dir"
echo "------------------------------------------------------------"
gsutil cp "${GCS_FOLDER}"/adult.data.csv "${data_dir}"/adult.data.csv
gsutil cp "${GCS_FOLDER}"/adult.test.csv "${data_dir}"/adult.test.csv
gsutil cp "${GCS_FOLDER}"/test.json "${data_dir}"/test.json
gsutil cp "${GCS_FOLDER}"/test.json "${data_dir}"/test.csv
# Define ENV for `train-local.sh` script
export CENSUS_TRAIN="${data_dir}"/adult.data.csv
export CENSUS_EVAL="${data_dir}"/adult.test.csv
export CENSUS_PREDICTION_JSON="${data_dir}"/test.json
export CENSUS_PREDICTION_CSV="${data_dir}"/test.csv
}
run_flake8() {
pip install -q flake8
flake8 --max-line-length=80 . --statistics
result=$?
if [ ${result} -ne 0 ];then
echo -e "\n Testing failed: flake8 returned a non-zero exit code. \n"
exit 1
else
echo -e "\n flake8 run successfully in directory $(pwd).\n"
fi
}
run_tests() {
set +e
# Use RTN to return a non-zero value if the test fails.
RTN=0
ROOT=$(pwd)
# Download training and test data
download_files
for file in **/train-local.sh; do
cd "$ROOT"
# Navigate to the project folder.
file=$(dirname "$file")
cd "${KOKORO_ARTIFACTS_DIR}"/"${file%/*}"
# If $ONLY_DIFF is true, skip projects without changes.
if [[ "$ONLY_DIFF" = "true" ]]; then
CHANGED=$(git diff master "${KOKORO_GITHUB_PULL_REQUEST_COMMIT}" $(pwd))
if [[ -z "$CHANGED" ]]; then
echo -e "\n Skipping $file: no changes in folder.\n"
continue
fi
fi
echo "------------------------------------------------------------"
echo "- Testing $file"
echo "------------------------------------------------------------"
if [[ "$file" == *"pytorch/"* ]]; then
continue
fi
run_flake8
echo "------------------------------------------------------------"
echo "- Installing dependencies for $file"
echo "------------------------------------------------------------"
pip install -q -r requirements.txt
source scripts/train-local.sh
EXIT=$?
if [[ $EXIT -ne 0 ]]; then
RTN=1
echo -e "\n Testing failed: Script returned a non-zero exit code. \n"
else
echo -e "\n Testing completed.\n"
fi
done
cd "$ROOT"
exit "$RTN"
}
main(){
project_setup
create_virtualenv
run_tests
}
main