Skip to content

Commit 8992786

Browse files
[ggj][infra][2/5] feat: bazel rules support for integration test (#393)
* add goldens files for redis client lib * add bazel file * format * add todo comment for Redis client lib goldens * add README to integration test goldens folder * update redis goldens using micro * add bazel rules * remove package-info * add bazel rules for integration test * take api name as arg * feedback * work * emit diff to test.log * fix diff command * rename * add api name to outputs
1 parent 99798fd commit 8992786

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
def _diff_integration_goldens_impl(ctx):
2+
# Extract the Java source files from the generated 3 srcjars from API bazel target,
3+
# and put them in the temporary folder `codegen_tmp`.
4+
# Compare the `codegen_tmp` with the goldens folder e.g `test/integration/goldens/redis`
5+
# and save the differences in output file `diff_output.txt`.
6+
7+
diff_output = ctx.outputs.diff_output
8+
check_diff_script = ctx.outputs.check_diff_script
9+
gapic_library = ctx.attr.gapic_library
10+
resource_name_library = ctx.attr.resource_name_library
11+
test_library = ctx.attr.test_library
12+
srcs = ctx.files.srcs
13+
api_name = ctx.attr.name
14+
15+
script = """
16+
mkdir codegen_tmp
17+
unzip -j {input} -d codegen_tmp
18+
unzip -j {input_resource_name} -d codegen_tmp
19+
unzip -j {input_test} -d codegen_tmp
20+
cd codegen_tmp
21+
# Remove unneeded non-Java files, like MANIFEST
22+
rm -rf $(find . -type f ! -name "*.java")
23+
cd ..
24+
diff codegen_tmp test/integration/goldens/{api_name}/ > {diff_output}
25+
# Bash `diff` command will return exit code 1 when there are differences between the two
26+
# folders. So we explicitly `exit 0` after the diff command to avoid build failure.
27+
exit 0
28+
""".format(
29+
diff_output = diff_output.path,
30+
input = gapic_library[JavaInfo].source_jars[0].path,
31+
input_resource_name = resource_name_library[JavaInfo].source_jars[0].path,
32+
input_test = test_library[JavaInfo].source_jars[0].path,
33+
api_name = api_name
34+
)
35+
ctx.actions.run_shell(
36+
inputs = srcs + [
37+
gapic_library[JavaInfo].source_jars[0],
38+
resource_name_library[JavaInfo].source_jars[0],
39+
test_library[JavaInfo].source_jars[0],
40+
],
41+
outputs = [diff_output],
42+
command = script,
43+
)
44+
45+
# Check the generated diff_output file, if it is empty, that means there is no difference
46+
# between generated source code and goldens files, test should pass. If it is not empty, then
47+
# test will fail by exiting 1.
48+
49+
check_diff_script_content = """
50+
# This will not print diff_output to the console unless `--test_output=all` option
51+
# is enabled, it only emits the comparison results to the test.log.
52+
# We could not copy the diff_output.txt to the test.log ($XML_OUTPUT_FILE) because that
53+
# file is not existing at the moment. It is generated once test is finished.
54+
cat $PWD/test/integration/{api_name}_diff_output.txt
55+
if [ -s $PWD/test/integration/{api_name}_diff_output.txt ]
56+
then
57+
exit 1
58+
fi
59+
""".format(
60+
api_name = api_name,
61+
)
62+
63+
ctx.actions.write(
64+
output = check_diff_script,
65+
content = check_diff_script_content,
66+
)
67+
runfiles = ctx.runfiles(files = [ctx.outputs.diff_output])
68+
return [DefaultInfo(executable = check_diff_script, runfiles = runfiles)]
69+
70+
71+
diff_integration_goldens_test = rule(
72+
attrs = {
73+
"gapic_library": attr.label(),
74+
"resource_name_library": attr.label(),
75+
"test_library": attr.label(),
76+
"srcs": attr.label_list(
77+
allow_files = True,
78+
mandatory = True,
79+
),
80+
},
81+
outputs = {
82+
"diff_output": "%{name}_diff_output.txt",
83+
"check_diff_script": "%{name}_check_diff_script.sh",
84+
},
85+
implementation = _diff_integration_goldens_impl,
86+
test = True,
87+
)
88+
89+
90+
def integration_test(name, target, data):
91+
# Bazel target `java_gapic_library` will generate 3 source jars including the
92+
# the source Java code of the gapic_library, resource_name_library and test_library.
93+
diff_integration_goldens_test(
94+
name = name,
95+
gapic_library = target,
96+
resource_name_library = "%s_resource_name" % target,
97+
test_library = "%s_test" % target,
98+
srcs = data,
99+
)

test/integration/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ load(
44
java_gapic_library = "java_gapic_library2",
55
)
66

7+
load(
8+
"//:rules_bazel/java/integration_test.bzl",
9+
"integration_test",
10+
)
11+
712
package(default_visibility = ["//visibility:public"])
813

914
####################################################
@@ -50,3 +55,9 @@ java_gapic_library(
5055
"@com_google_googleapis//google/cloud/redis/v1:redis_java_proto",
5156
],
5257
)
58+
59+
integration_test(
60+
name = "redis",
61+
target = ":redis_java_gapic",
62+
data = ["//test/integration/goldens/redis:goldens_files"],
63+
)

0 commit comments

Comments
 (0)