/
generate_library_unit_tests.sh
executable file
·349 lines (307 loc) · 12.6 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/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}"/../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_protobuf_version_succeed_with_valid_generator_version_test() {
local actual_version
actual_version=$(get_protobuf_version "2.24.0")
assertEquals "23.2" "${actual_version}"
rm "gapic-generator-java-pom-parent-2.24.0.pom"
}
get_protobuf_version_failed_with_invalid_generator_version_test() {
local res=0
$(get_protobuf_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)"
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)"
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
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() {
download_generator "2.24.0"
assertFileOrDirectoryExists "gapic-generator-java-2.24.0.jar"
rm "gapic-generator-java-2.24.0.jar"
}
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
$(download_generator "1.99.0") || res=$?
assertEquals 1 $((res))
}
download_protobuf_succeed_with_valid_version_linux_test() {
download_protobuf "23.2" "linux-x86_64"
assertFileOrDirectoryExists "protobuf-23.2"
rm -rf "protobuf-23.2"
}
download_protobuf_succeed_with_valid_version_macos_test() {
download_protobuf "23.2" "osx-x86_64"
assertFileOrDirectoryExists "protobuf-23.2"
rm -rf "protobuf-23.2" "google"
}
download_protobuf_failed_with_invalid_version_linux_test() {
local res=0
$(download_protobuf "22.99" "linux-x86_64") || res=$?
assertEquals 1 $((res))
}
download_protobuf_failed_with_invalid_arch_test() {
local res=0
$(download_protobuf "23.2" "customized-x86_64") || res=$?
assertEquals 1 $((res))
}
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 \
--protobuf_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_protobuf_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 \
--protobuf_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}"
}
get_gapic_additional_protos_from_BUILD_common_resources_test() {
local proto_path="${script_dir}/resources/search_additional_protos/BUILD_common_resources.bazel"
local addition_protos
addition_protos=$(get_gapic_additional_protos_from_BUILD "${proto_path}")
assertEquals "google/cloud/common_resources.proto" "${addition_protos}"
}
get_gapic_additional_protos_from_BUILD_iam_policy_test() {
local proto_path="${script_dir}/resources/search_additional_protos/BUILD_iam_policy.bazel"
local addition_protos
addition_protos=$(get_gapic_additional_protos_from_BUILD "${proto_path}")
assertEquals "google/cloud/common_resources.proto google/iam/v1/iam_policy.proto" "${addition_protos}"
}
get_gapic_additional_protos_from_BUILD_locations_test() {
local proto_path="${script_dir}/resources/search_additional_protos/BUILD_locations.bazel"
local addition_protos
addition_protos=$(get_gapic_additional_protos_from_BUILD "${proto_path}")
assertEquals "google/cloud/common_resources.proto google/cloud/location/locations.proto" "${addition_protos}"
}
get_gapic_additional_protos_from_BUILD_iam_locations_test() {
local proto_path="${script_dir}/resources/search_additional_protos/BUILD_iam_locations.bazel"
local addition_protos
addition_protos=$(get_gapic_additional_protos_from_BUILD "${proto_path}")
assertEquals "google/cloud/common_resources.proto google/iam/v1/iam_policy.proto google/cloud/location/locations.proto" "${addition_protos}"
}
get_transport_from_BUILD_grpc_rest_test() {
local build_file="${script_dir}/resources/misc/BUILD_grpc_rest.bazel"
local transport
transport=$(get_transport_from_BUILD "${build_file}")
assertEquals "grpc+rest" "${transport}"
}
get_transport_from_BUILD_grpc_test() {
local build_file="${script_dir}/resources/misc/BUILD_grpc.bazel"
local transport
transport=$(get_transport_from_BUILD "${build_file}")
assertEquals "grpc" "${transport}"
}
get_transport_from_BUILD_rest_test() {
local build_file="${script_dir}/resources/misc/BUILD_rest.bazel"
local transport
transport=$(get_transport_from_BUILD "${build_file}")
assertEquals "rest" "${transport}"
}
get_rest_numeric_enums_from_BUILD_true_test() {
local build_file="${script_dir}/resources/misc/BUILD_rest_numeric_enums_true.bazel"
local rest_numeric_enums
rest_numeric_enums=$(get_rest_numeric_enums_from_BUILD "${build_file}")
assertEquals "true" "${rest_numeric_enums}"
}
get_rest_numeric_enums_from_BUILD_false_test() {
local build_file="${script_dir}/resources/misc/BUILD_rest_numeric_enums_false.bazel"
local rest_numeric_enums
rest_numeric_enums=$(get_rest_numeric_enums_from_BUILD "${build_file}")
assertEquals "false" "${rest_numeric_enums}"
}
get_rest_numeric_enums_from_BUILD_empty_test() {
local build_file="${script_dir}/resources/misc/BUILD_rest_numeric_enums_empty.bazel"
local rest_numeric_enums
rest_numeric_enums=$(get_rest_numeric_enums_from_BUILD "${build_file}")
assertEquals "false" "${rest_numeric_enums}"
}
get_include_samples_from_BUILD_true_test() {
local build_file="${script_dir}/resources/misc/BUILD_include_samples_true.bazel"
local include_samples
include_samples=$(get_include_samples_from_BUILD "${build_file}")
assertEquals "true" "${include_samples}"
}
get_include_samples_from_BUILD_false_test() {
local build_file="${script_dir}/resources/misc/BUILD_include_samples_false.bazel"
local include_samples
include_samples=$(get_include_samples_from_BUILD "${build_file}")
assertEquals "false" "${include_samples}"
}
get_include_samples_from_BUILD_empty_test() {
local build_file="${script_dir}/resources/misc/BUILD_include_samples_empty.bazel"
local include_samples
include_samples=$(get_include_samples_from_BUILD "${build_file}")
assertEquals "false" "${include_samples}"
}
get_version_from_valid_WORKSPACE_test() {
workspace_file="${script_dir}/resources/misc/TESTWORKSPACE"
obtained_ggj_version=$(get_version_from_WORKSPACE "_gapic_generator_java_version" "${workspace_file}")
assertEquals '2.25.1-SNAPSHOT' "${obtained_ggj_version}"
}
get_generator_version_from_valid_versions_txt_test() {
versions_file="${script_dir}/resources/misc/testversions.txt"
obtained_ggj_version=$(get_version_from_versions_txt "${versions_file}" "gapic-generator-java")
assertEquals '2.25.1-SNAPSHOT' "${obtained_ggj_version}"
}
get_gax_version_from_valid_versions_txt_test() {
versions_file="${script_dir}/resources/misc/testversions.txt"
obtained_gax_version=$(get_version_from_versions_txt "${versions_file}" "gax")
assertEquals '2.33.1-SNAPSHOT' "${obtained_gax_version}"
}
# 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_protobuf_version_succeed_with_valid_generator_version_test
get_protobuf_version_failed_with_invalid_generator_version_test
get_gapic_opts_with_rest_test
get_gapic_opts_without_rest_test
remove_grpc_version_test
download_generator_success_with_valid_version_test
download_generator_failed_with_invalid_version_test
download_protobuf_succeed_with_valid_version_linux_test
download_protobuf_succeed_with_valid_version_macos_test
download_protobuf_failed_with_invalid_version_linux_test
download_protobuf_failed_with_invalid_arch_test
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_protobuf_version
generate_library_failed_with_invalid_grpc_version
get_gapic_additional_protos_from_BUILD_common_resources_test
get_gapic_additional_protos_from_BUILD_iam_policy_test
get_gapic_additional_protos_from_BUILD_locations_test
get_gapic_additional_protos_from_BUILD_iam_locations_test
get_transport_from_BUILD_grpc_rest_test
get_transport_from_BUILD_grpc_test
get_transport_from_BUILD_rest_test
get_rest_numeric_enums_from_BUILD_true_test
get_rest_numeric_enums_from_BUILD_false_test
get_rest_numeric_enums_from_BUILD_empty_test
get_include_samples_from_BUILD_true_test
get_include_samples_from_BUILD_false_test
get_include_samples_from_BUILD_empty_test
get_version_from_valid_WORKSPACE_test
get_generator_version_from_valid_versions_txt_test
get_gax_version_from_valid_versions_txt_test
)
pushd "${script_dir}"
execute_tests "${test_list[@]}"
popd