Skip to content
This repository was archived by the owner on May 14, 2026. It is now read-only.

Commit 6e9ef3f

Browse files
authored
chore: add unit tests for utilities.sh (#1948)
* chore: add unit tests for `generate_library.sh` * add tests * add test execution loop * add test for `search_additional_protos` * add tests for `search_additional_protos` * add tests for `get_gapic_opts` * add test for `remove_grpc_version` * add tests for download protobuf and grpc plugin * add tests for invalid download test * code reformat * fix typo * test exit code when download failed
1 parent 24ae2a3 commit 6e9ef3f

10 files changed

Lines changed: 1302 additions & 6 deletions

File tree

.github/workflows/verify_library_generation.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ on:
77
- library_generation/**
88

99
workflow_dispatch:
10-
name: verify_library_generation_against_googleapis-gen
10+
name: verify_library_generation
1111
jobs:
12-
verify_library_generation:
12+
integration_tests:
1313
strategy:
1414
matrix:
1515
java: [ 8 ]
@@ -30,3 +30,11 @@ jobs:
3030
-d google-cloud-bigtable-v2-java \
3131
--googleapis_gen_url https://cloud-java-bot:${{ secrets.CLOUD_JAVA_BOT_GITHUB_TOKEN }}@github.com/googleapis/googleapis-gen.git \
3232
--os_type ${{ matrix.os }}
33+
unit_tests:
34+
runs-on: ubuntu-22.04
35+
steps:
36+
- uses: actions/checkout@v3
37+
- name: Run unit tests
38+
run: |
39+
set -x
40+
library_generation/test/generate_library_unit_tests.sh
Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
#!/usr/bin/env bash
2+
3+
set -xeo pipefail
4+
5+
# Variables used to generate final result
6+
total_num=0
7+
succeed_num=0
8+
failed_num=0
9+
failed_tests=""
10+
# Unit tests against ./utilities.sh
11+
script_dir=$(dirname "$(readlink -f "$0")")
12+
source "${script_dir}"/../utilities.sh
13+
14+
# Helper functions, they shouldn't be called outside this file.
15+
__assertEquals() {
16+
expected=$1
17+
actual=$2
18+
if [[ "${expected}" == "${actual}" ]]; then
19+
return 0
20+
fi
21+
22+
echo "Error: expected ${expected}, got ${actual} instead."
23+
return 1
24+
}
25+
26+
__assertFileDoesNotExist() {
27+
expected_file=$1
28+
if [ ! -f "${expected_file}" ]; then
29+
return 0
30+
fi
31+
32+
echo "Error: ${expected_file} exists."
33+
return 1
34+
}
35+
36+
__test_executed() {
37+
total_num=$((1 + total_num))
38+
}
39+
40+
__test_succeed() {
41+
succeed_num=$((1 + succeed_num))
42+
}
43+
44+
__test_failed() {
45+
failed_test=$1
46+
failed_num=$((1 + failed_num))
47+
failed_tests="${failed_tests} ${failed_test}"
48+
}
49+
50+
# Unit tests
51+
extract_folder_name_test() {
52+
path="google/cloud/aiplatform/v1/google-cloud-aiplatform-v1-java"
53+
folder_name=$(extract_folder_name "${path}")
54+
__assertEquals "google-cloud-aiplatform-v1-java" "${folder_name}"
55+
}
56+
57+
get_grpc_version_succeed_with_valid_generator_version_test() {
58+
actual_version=$(get_grpc_version "2.24.0")
59+
__assertEquals "1.56.1" "${actual_version}"
60+
rm "gapic-generator-java-pom-parent-2.24.0.pom"
61+
}
62+
63+
get_grpc_version_failed_with_invalid_generator_version_test() {
64+
actual_version=$(get_grpc_version "1.99.0")
65+
__assertEquals "" "${actual_version}"
66+
}
67+
68+
get_protobuf_version_succeed_with_valid_generator_version_test() {
69+
actual_version=$(get_protobuf_version "2.24.0")
70+
__assertEquals "23.2" "${actual_version}"
71+
rm "gapic-generator-java-pom-parent-2.24.0.pom"
72+
}
73+
74+
get_protobuf_version_failed_with_invalid_generator_version_test() {
75+
actual_version=$(get_protobuf_version "1.99.0")
76+
__assertEquals "" "${actual_version}"
77+
}
78+
79+
search_additional_protos_common_resources_test() {
80+
proto_path="${script_dir}/resources/monitoring"
81+
addition_protos=$(search_additional_protos)
82+
__assertEquals "google/cloud/common_resources.proto" "${addition_protos}"
83+
}
84+
85+
search_additional_protos_iam_test() {
86+
proto_path="${script_dir}/resources/pubsub"
87+
addition_protos=$(search_additional_protos)
88+
__assertEquals \
89+
"google/cloud/common_resources.proto google/iam/v1/iam_policy.proto" \
90+
"${addition_protos}"
91+
}
92+
93+
search_additional_protos_location_test() {
94+
proto_path="${script_dir}/resources/firestore"
95+
addition_protos=$(search_additional_protos)
96+
__assertEquals \
97+
"google/cloud/common_resources.proto google/cloud/location/locations.proto" \
98+
"${addition_protos}"
99+
}
100+
101+
search_additional_protos_iam_location_test() {
102+
proto_path="${script_dir}/resources/alloydb"
103+
addition_protos=$(search_additional_protos)
104+
__assertEquals \
105+
"google/cloud/common_resources.proto google/iam/v1/iam_policy.proto google/cloud/location/locations.proto" \
106+
"${addition_protos}"
107+
}
108+
109+
get_gapic_opts_with_rest_test() {
110+
proto_path="${script_dir}/resources/monitoring"
111+
transport="grpc"
112+
rest_numeric_enums="true"
113+
gapic_opts="$(get_gapic_opts)"
114+
__assertEquals \
115+
"transport=grpc,rest-numeric-enums,grpc-service-config=${proto_path}/monitoring_grpc_service_config.json,gapic-config=${proto_path}/monitoring_gapic.yaml,api-service-config=${proto_path}/monitoring.yaml" \
116+
"${gapic_opts}"
117+
}
118+
119+
get_gapic_opts_without_rest_test() {
120+
proto_path="${script_dir}/resources/monitoring"
121+
transport="grpc"
122+
rest_numeric_enums="false"
123+
gapic_opts="$(get_gapic_opts)"
124+
__assertEquals \
125+
"transport=grpc,grpc-service-config=${proto_path}/monitoring_grpc_service_config.json,gapic-config=${proto_path}/monitoring_gapic.yaml,api-service-config=${proto_path}/monitoring.yaml" \
126+
"$gapic_opts"
127+
}
128+
129+
remove_grpc_version_test() {
130+
destination_path="${script_dir}/resources/monitoring"
131+
cp "${destination_path}/QueryServiceGrpc_copy.java" "${destination_path}/QueryServiceGrpc.java"
132+
remove_grpc_version
133+
return_code=0
134+
if grep -q 'value = "by gRPC proto compiler",' "${destination_path}/QueryServiceGrpc.java"; then
135+
echo "grpc version is removed."
136+
else
137+
echo "Error: grpc version is not removed."
138+
return_code=1
139+
fi
140+
141+
rm "${destination_path}/QueryServiceGrpc.java"
142+
return "${return_code}"
143+
}
144+
145+
download_generator_success_with_valid_version_test() {
146+
download_generator "2.24.0"
147+
__assertFileExists "gapic-generator-java-2.24.0.jar"
148+
rm "gapic-generator-java-2.24.0.jar"
149+
}
150+
151+
download_generator_failed_with_invalid_version_test() {
152+
# The download function will exit the shell
153+
# if download failed. Test the exit code instead of
154+
# downloaded file (there will be no downloaded file).
155+
# Use $() to execute the function in subshell so that
156+
# the other tests can continue executing in the current
157+
# shell.
158+
res=0
159+
$(download_generator "1.99.0") || res=$?
160+
__assertEquals 1 $((res))
161+
}
162+
163+
download_protobuf_succeed_with_valid_version_linux_test() {
164+
download_protobuf "23.2" "linux-x86_64"
165+
__assertFileExists "protobuf-23.2"
166+
rm -rf "protobuf-23.2"
167+
}
168+
169+
download_protobuf_succeed_with_valid_version_macos_test() {
170+
download_protobuf "23.2" "osx-x86_64"
171+
__assertFileExists "protobuf-23.2"
172+
rm -rf "protobuf-23.2" "google"
173+
}
174+
175+
download_protobuf_failed_with_invalid_version_linux_test() {
176+
res=0
177+
$(download_protobuf "22.99" "linux-x86_64") || res=$?
178+
__assertEquals 1 $((res))
179+
}
180+
181+
download_protobuf_failed_with_invalid_arch_test() {
182+
res=0
183+
$(download_protobuf "23.2" "customized-x86_64") || res=$?
184+
__assertEquals 1 $((res))
185+
}
186+
187+
download_grpc_plugin_succeed_with_valid_version_linux_test() {
188+
download_grpc_plugin "1.55.1" "linux-x86_64"
189+
__assertFileExists "protoc-gen-grpc-java-1.55.1-linux-x86_64.exe"
190+
rm "protoc-gen-grpc-java-1.55.1-linux-x86_64.exe"
191+
}
192+
193+
download_grpc_plugin_succeed_with_valid_version_macos_test() {
194+
download_grpc_plugin "1.55.1" "osx-x86_64"
195+
__assertFileExists "protoc-gen-grpc-java-1.55.1-osx-x86_64.exe"
196+
rm "protoc-gen-grpc-java-1.55.1-osx-x86_64.exe"
197+
}
198+
199+
download_grpc_plugin_failed_with_invalid_version_linux_test() {
200+
res=0
201+
$(download_grpc_plugin "0.99.0" "linux-x86_64") || res=$?
202+
__assertEquals 1 $((res))
203+
}
204+
205+
download_grpc_plugin_failed_with_invalid_arch_test() {
206+
res=0
207+
$(download_grpc_plugin "1.55.1" "customized-x86_64") || res=$?
208+
__assertEquals 1 $((res))
209+
}
210+
211+
# Execute tests.
212+
# One line per test.
213+
test_list=(
214+
extract_folder_name_test
215+
get_grpc_version_succeed_with_valid_generator_version_test
216+
get_grpc_version_failed_with_invalid_generator_version_test
217+
get_protobuf_version_succeed_with_valid_generator_version_test
218+
get_protobuf_version_failed_with_invalid_generator_version_test
219+
search_additional_protos_common_resources_test
220+
search_additional_protos_iam_test
221+
search_additional_protos_location_test
222+
search_additional_protos_iam_location_test
223+
get_gapic_opts_with_rest_test
224+
get_gapic_opts_without_rest_test
225+
remove_grpc_version_test
226+
download_generator_success_with_valid_version_test
227+
download_generator_failed_with_invalid_version_test
228+
download_protobuf_succeed_with_valid_version_linux_test
229+
download_protobuf_succeed_with_valid_version_macos_test
230+
download_protobuf_failed_with_invalid_version_linux_test
231+
download_protobuf_failed_with_invalid_arch_test
232+
download_grpc_plugin_succeed_with_valid_version_linux_test
233+
download_grpc_plugin_succeed_with_valid_version_macos_test
234+
download_grpc_plugin_failed_with_invalid_version_linux_test
235+
download_grpc_plugin_failed_with_invalid_arch_test
236+
)
237+
238+
for ut in "${test_list[@]}"; do
239+
pushd "${script_dir}"
240+
__test_executed
241+
result=0
242+
"${ut}" || result=$?
243+
if [[ "${result}" == 0 ]]; then
244+
__test_succeed
245+
else
246+
__test_failed "${ut}"
247+
fi
248+
popd
249+
done
250+
251+
echo "Test result: ${total_num} tests executed, ${succeed_num} succeed, ${failed_num} failed."
252+
if [[ "${total_num}" == "${succeed_num}" ]]; then
253+
echo "All tests passed."
254+
exit
255+
fi
256+
257+
echo "Test failed."
258+
echo "Failed test(s): ${failed_tests}."
259+
exit 1

0 commit comments

Comments
 (0)