-
Notifications
You must be signed in to change notification settings - Fork 52
/
generate_library_unit_tests.sh
executable file
·306 lines (273 loc) · 10.9 KB
/
generate_library_unit_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env bash
set -xeo pipefail
# Unit tests against ../utilities.sh
script_dir=$(dirname "$(readlink -f "$0")")
source "${script_dir}"/test_utilities.sh
source "${script_dir}"/../utils/utilities.sh
# Unit tests
extract_folder_name_test() {
local path="google/cloud/aiplatform/v1/google-cloud-aiplatform-v1-java"
local folder_name
folder_name=$(extract_folder_name "${path}")
assertEquals "google-cloud-aiplatform-v1-java" "${folder_name}"
}
get_grpc_version_succeed_with_valid_generator_version_test() {
local actual_version
actual_version=$(get_grpc_version "2.24.0")
rm "gapic-generator-java-pom-parent-2.24.0.pom"
assertEquals "1.56.1" "${actual_version}"
}
get_grpc_version_failed_with_invalid_generator_version_test() {
local res=0
$(get_grpc_version "1.99.0") || res=$?
assertEquals 1 $((res))
}
get_protoc_version_succeed_with_valid_generator_version_test() {
local actual_version
actual_version=$(get_protoc_version "2.24.0")
assertEquals "23.2" "${actual_version}"
rm "gapic-generator-java-pom-parent-2.24.0.pom"
}
get_protoc_version_failed_with_invalid_generator_version_test() {
local res=0
$(get_protoc_version "1.99.0") || res=$?
assertEquals 1 $((res))
}
get_gapic_opts_with_rest_test() {
local proto_path="${script_dir}/resources/gapic_options"
local transport="grpc"
local rest_numeric_enums="true"
local gapic_opts
gapic_opts="$(get_gapic_opts "${transport}" "${rest_numeric_enums}" "" "" "")"
assertEquals \
"transport=grpc,rest-numeric-enums,grpc-service-config=${proto_path}/example_grpc_service_config.json,gapic-config=${proto_path}/example_gapic.yaml,api-service-config=${proto_path}/example.yaml" \
"${gapic_opts}"
}
get_gapic_opts_without_rest_test() {
local proto_path="${script_dir}/resources/gapic_options"
local transport="grpc"
local rest_numeric_enums="false"
local gapic_opts
gapic_opts="$(get_gapic_opts "${transport}" "${rest_numeric_enums}" "" "" "")"
assertEquals \
"transport=grpc,,grpc-service-config=${proto_path}/example_grpc_service_config.json,gapic-config=${proto_path}/example_gapic.yaml,api-service-config=${proto_path}/example.yaml" \
"$gapic_opts"
}
get_gapic_opts_with_non_default_test() {
local proto_path="${script_dir}/resources/gapic_options"
local transport="grpc"
local rest_numeric_enums="false"
local gapic_opts
gapic_opts="$(get_gapic_opts "${transport}" "${rest_numeric_enums}" "${proto_path}/example_gapic.yaml" "${proto_path}/example_grpc_service_config.json" "${proto_path}/example.yaml")"
assertEquals \
"transport=grpc,,grpc-service-config=${proto_path}/example_grpc_service_config.json,gapic-config=${proto_path}/example_gapic.yaml,api-service-config=${proto_path}/example.yaml" \
"$gapic_opts"
}
remove_grpc_version_test() {
local destination_path="${script_dir}/resources/gapic_options"
cp "${destination_path}/QueryServiceGrpc_copy.java" "${destination_path}/QueryServiceGrpc.java"
remove_grpc_version "${destination_path}"
local res=0
if ! grep -q 'value = "by gRPC proto compiler",' "${destination_path}/QueryServiceGrpc.java"; then
echo "Error: grpc version is not removed."
res=1
fi
assertEquals 0 $((res))
rm "${destination_path}/QueryServiceGrpc.java"
}
download_generator_success_with_valid_version_test() {
local version="2.24.0"
local artifact="gapic-generator-java-${version}.jar"
download_generator_artifact "${version}" "${artifact}"
assertFileOrDirectoryExists "${artifact}"
rm "${artifact}"
}
download_generator_failed_with_invalid_version_test() {
# The download function will exit the shell
# if download failed. Test the exit code instead of
# downloaded file (there will be no downloaded file).
# Use $() to execute the function in subshell so that
# the other tests can continue executing in the current
# shell.
local res=0
local version="1.99.0"
local artifact="gapic-generator-java-${version}.jar"
$(download_generator_artifact "${version}" "${artifact}") || res=$?
assertEquals 1 $((res))
}
download_protoc_succeed_with_valid_version_linux_test() {
download_protoc "23.2" "linux-x86_64"
assertFileOrDirectoryExists "protoc-23.2"
rm -rf "protoc-23.2"
}
download_protoc_succeed_with_valid_version_macos_test() {
download_protoc "23.2" "osx-x86_64"
assertFileOrDirectoryExists "protoc-23.2"
rm -rf "protoc-23.2" "google"
}
download_protoc_failed_with_invalid_version_linux_test() {
local res=0
$(download_protoc "22.99" "linux-x86_64") || res=$?
assertEquals 1 $((res))
}
download_protoc_failed_with_invalid_arch_test() {
local res=0
$(download_protoc "23.2" "customized-x86_64") || res=$?
assertEquals 1 $((res))
}
download_protoc_succeed_with_baked_protoc() {
# This mimics a docker container scenario.
# This test consists of creating an empty /tmp/.../protoc-99.99/bin folder and map
# it to the DOCKER_PROTOC_LOCATION env var (which is treated specially in the
# `download_protoc` function). If `DOCKER_PROTOC_VERSION` matches exactly as
# the version passed to `download_protoc`, then we will not download protoc
# but simply copy it from DOCKER_PROTOC_LOCATION into ${output_folder} (which
# we manually created in this test), so we should expect ${output_folder} to
# contain a folder called protoc-99.99.
export DOCKER_PROTOC_LOCATION=$(mktemp -d)
export DOCKER_PROTOC_VERSION="99.99"
export output_folder=$(get_output_folder)
mkdir -p "${DOCKER_PROTOC_LOCATION}/protoc-99.99/bin"
download_protoc "99.99" "linux-x86_64"
assertFileOrDirectoryExists "${output_folder}/protoc-99.99"
rm -rf "${output_folder}/protoc-99.99"
}
download_grpc_plugin_succeed_with_valid_version_linux_test() {
download_grpc_plugin "1.55.1" "linux-x86_64"
assertFileOrDirectoryExists "protoc-gen-grpc-java-1.55.1-linux-x86_64.exe"
rm "protoc-gen-grpc-java-1.55.1-linux-x86_64.exe"
}
download_grpc_plugin_succeed_with_valid_version_macos_test() {
download_grpc_plugin "1.55.1" "osx-x86_64"
assertFileOrDirectoryExists "protoc-gen-grpc-java-1.55.1-osx-x86_64.exe"
rm "protoc-gen-grpc-java-1.55.1-osx-x86_64.exe"
}
download_grpc_plugin_failed_with_invalid_version_linux_test() {
local res=0
$(download_grpc_plugin "0.99.0" "linux-x86_64") || res=$?
assertEquals 1 $((res))
}
download_grpc_plugin_failed_with_invalid_arch_test() {
local res=0
$(download_grpc_plugin "1.55.1" "customized-x86_64") || res=$?
assertEquals 1 $((res))
}
generate_library_failed_with_invalid_generator_version() {
local destination="google-cloud-alloydb-v1-java"
local res=0
cd "${script_dir}/resources"
bash "${script_dir}"/../generate_library.sh \
-p google/cloud/alloydb/v1 \
-d ../"${destination}" \
--gapic_generator_version 1.99.0 \
--protoc_version 23.2 \
--grpc_version 1.55.1 \
--transport grpc+rest \
--rest_numeric_enums true || res=$?
assertEquals 1 $((res))
# still need to clean up potential downloaded tooling.
cleanup "${destination}"
}
generate_library_failed_with_invalid_protoc_version() {
local destination="google-cloud-alloydb-v1-java"
local res=0
cd "${script_dir}/resources"
bash "${script_dir}"/../generate_library.sh \
-p google/cloud/alloydb/v1 \
-d ../"${destination}" \
--gapic_generator_version 2.24.0 \
--protoc_version 22.99 \
--grpc_version 1.55.1 \
--transport grpc+rest \
--rest_numeric_enums true || res=$?
assertEquals 1 $((res))
# still need to clean up potential downloaded tooling.
cleanup "${destination}"
}
generate_library_failed_with_invalid_grpc_version() {
local destination="google-cloud-alloydb-v1-java"
local res=0
cd "${script_dir}/resources"
bash "${script_dir}"/../generate_library.sh \
-p google/cloud/alloydb/v1 \
-d ../output/"${destination}" \
--gapic_generator_version 2.24.0 \
--grpc_version 0.99.0 \
--transport grpc+rest \
--rest_numeric_enums true || res=$?
assertEquals 1 $((res))
# still need to clean up potential downloaded tooling.
cleanup "${destination}"
}
copy_directory_if_exists_valid_folder_succeeds() {
local source_folder="${script_dir}/resources"
local destination="${script_dir}/test_destination_folder"
mkdir -p "${destination}"
copy_directory_if_exists "${source_folder}" "gapic" "${destination}/copied-folder"
n_matching_folders=$(ls "${destination}" | grep -e 'copied-folder' | wc -l)
rm -rdf "${destination}"
assertEquals 1 ${n_matching_folders}
}
copy_directory_if_exists_invalid_folder_does_not_copy() {
local source_folder="${script_dir}/non-existent"
local destination="${script_dir}/test_destination_folder"
mkdir -p "${destination}"
copy_directory_if_exists "${source_folder}" "gapic" "${destination}/copied-folder"
n_matching_folders=$(ls "${destination}" | grep -e 'copied-folder' | wc -l) || res=$?
rm -rdf "${destination}"
assertEquals 0 ${n_matching_folders}
}
get_proto_path_from_preprocessed_sources_valid_library_succeeds() {
local sources="${script_dir}/resources/proto_path_library"
local proto_path=$(get_proto_path_from_preprocessed_sources "${sources}")
assertEquals "google/cloud/test/v1" ${proto_path}
}
get_proto_path_from_preprocessed_sources_empty_library_fails() {
local sources=$(mktemp -d)
(
get_proto_path_from_preprocessed_sources "${sources}"
) || res=$?
assertEquals 1 ${res}
}
get_proto_path_from_preprocessed_sources_multiple_proto_dirs_fails() {
local sources="${script_dir}/resources/proto_path_library_multiple_protos"
(
get_proto_path_from_preprocessed_sources "${sources}"
) || res=$?
assertEquals 1 ${res}
}
# Execute tests.
# One line per test.
test_list=(
extract_folder_name_test
get_grpc_version_succeed_with_valid_generator_version_test
get_grpc_version_failed_with_invalid_generator_version_test
get_protoc_version_succeed_with_valid_generator_version_test
get_protoc_version_failed_with_invalid_generator_version_test
get_gapic_opts_with_rest_test
get_gapic_opts_without_rest_test
get_gapic_opts_with_non_default_test
remove_grpc_version_test
download_generator_success_with_valid_version_test
download_generator_failed_with_invalid_version_test
download_protoc_succeed_with_valid_version_linux_test
download_protoc_succeed_with_valid_version_macos_test
download_protoc_failed_with_invalid_version_linux_test
download_protoc_failed_with_invalid_arch_test
download_protoc_succeed_with_baked_protoc
download_grpc_plugin_succeed_with_valid_version_linux_test
download_grpc_plugin_succeed_with_valid_version_macos_test
download_grpc_plugin_failed_with_invalid_version_linux_test
download_grpc_plugin_failed_with_invalid_arch_test
generate_library_failed_with_invalid_generator_version
generate_library_failed_with_invalid_protoc_version
generate_library_failed_with_invalid_grpc_version
copy_directory_if_exists_valid_folder_succeeds
copy_directory_if_exists_invalid_folder_does_not_copy
get_proto_path_from_preprocessed_sources_valid_library_succeeds
get_proto_path_from_preprocessed_sources_empty_library_fails
get_proto_path_from_preprocessed_sources_multiple_proto_dirs_fails
)
pushd "${script_dir}"
execute_tests "${test_list[@]}"
popd