-
Notifications
You must be signed in to change notification settings - Fork 516
Expand file tree
/
Copy pathpresubmit.sh
More file actions
130 lines (110 loc) · 4.32 KB
/
presubmit.sh
File metadata and controls
130 lines (110 loc) · 4.32 KB
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
#!/bin/bash
set -euxo pipefail
# Declare global variable for passing tests between functions
declare -a TESTS_TO_RUN
configure_gcloud() {
gcloud config set core/disable_prompts TRUE
gcloud config set compute/region us-central1
}
configure_gcloud_ssh_key() {
mkdir "${HOME}/.ssh"
gcloud kms decrypt --location=global --keyring=presubmit --key=presubmit \
--ciphertext-file=cloudbuild/ssh-key.enc \
--plaintext-file="${HOME}/.ssh/google_compute_engine"
gcloud kms decrypt --location=global --keyring=presubmit --key=presubmit \
--ciphertext-file=cloudbuild/ssh-key.pub.enc \
--plaintext-file="${HOME}/.ssh/google_compute_engine.pub"
chmod 600 "${HOME}/.ssh/google_compute_engine"
}
# Fetches master branch from GitHub and "resets" local changes to be relative to it,
# so we can diff what changed relatively to master branch.
initialize_git_repo() {
rm -fr .git
git config --global init.defaultBranch main
git init
git config user.email "ia-tests@presubmit.example.com"
git config user.name "ia-tests"
git remote add origin "https://github.com/GoogleCloudDataproc/initialization-actions.git"
git fetch origin master
# Fetch all PRs to get history for PRs created from forked repos
git fetch origin +refs/pull/*/merge:refs/remotes/origin/pr/* > /dev/null 2>&1
git reset --hard "${COMMIT_SHA}"
git rebase origin/master
}
# This function adds all changed files to git "index" and diffs them against master branch
# to determine all changed files and looks for tests in directories with changed files.
determine_tests_to_run() {
# Infer the files that changed
mapfile -t DELETED_BUILD_FILES < <(git diff origin/master --name-only --diff-filter=D | grep BUILD)
mapfile -t CHANGED_FILES < <(git diff origin/master --name-only | grep -v template)
for tt in $(git diff origin/master --name-only | grep 'templates/.*/.*\.sh\.in'); do
local genfile=`perl -e "print( q{${tt}} =~ m:templates/(.*?.sh).in: )"`
perl templates/generate-action.pl "${genfile}" > "${genfile}"
CHANGED_FILES+=("${genfile}")
done
echo "Deleted BUILD files: ${DELETED_BUILD_FILES[*]}"
echo "Changed files: ${CHANGED_FILES[*]}"
# Run all tests if common directories modified by deleting files
if [[ "${#DELETED_BUILD_FILES[@]}" -gt 0 ]]; then
echo "All tests will be run: the following BUILD files '${DELETED_BUILD_FILES[*]}' were removed"
TESTS_TO_RUN=(":DataprocInitActionsTestSuite")
return 0
fi
set +x
# Determines init actions directories that were changed
declare -a changed_dirs
for changed_file in "${CHANGED_FILES[@]}"; do
local changed_dir
changed_dir="$(dirname "${changed_file}")/"
# Convert `init/internal/` dir to `init/`
changed_dir="${changed_dir%%/*}/"
# Run all tests if common directories modified
if [[ ${changed_dir} =~ ^(integration_tests|util|cloudbuild)/$ ]]; then
continue
echo "All tests will be run: '${changed_dir}' was changed"
TESTS_TO_RUN=(":DataprocInitActionsTestSuite")
return 0
fi
# Hack to workaround empty array expansion on old versions of Bash.
# See: https://stackoverflow.com/a/7577209/3227693
if [[ $changed_dir != ./ ]] && [[ ${changed_dirs[*]+" ${changed_dirs[*]} "} != *" ${changed_dir} "* ]]; then
changed_dirs+=("$changed_dir")
fi
done
echo "Changed directories: ${changed_dirs[*]}"
# Determines test target in changed init action directories to run
for changed_dir in "${changed_dirs[@]}"; do
# NOTE: The ::-1 removes the trailing '/'
local test_name=${changed_dir::-1}
# Some of our py_tests (that has dashes in the name) are defined in the top-level directory
if [[ $test_name == *"-"* ]]; then
local test_target=":test_${test_name//-/_}"
else
local test_target="${test_name}:test_${test_name}"
fi
TESTS_TO_RUN+=("${test_target}")
done
echo "Tests: ${TESTS_TO_RUN[*]}"
set -x
}
run_tests() {
local -r max_parallel_tests=20
bazel test \
--jobs="${max_parallel_tests}" \
--local_test_jobs="${max_parallel_tests}" \
--action_env="INTERNAL_IP_SSH=true" \
--test_output="all" \
--noshow_progress \
--noshow_loading_progress \
--test_arg="--image_version=${IMAGE_VERSION}" \
"${TESTS_TO_RUN[@]}"
}
main() {
cd /init-actions
configure_gcloud
configure_gcloud_ssh_key
initialize_git_repo
determine_tests_to_run
run_tests
}
main