-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tools: Add protoxform tests #9241
Changes from all commits
7fa5ee1
46290c0
caca8d5
c4a7eef
db92556
660f1fe
dbd6f4b
6851f4e
266886f
bd44286
7366a4a
2667652
e2c07d6
9b42b16
ad60337
4df9c39
959e815
d8591eb
ff147d8
89ce863
f33d48d
fef4641
407ac8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -535,4 +535,4 @@ def Main(): | |
|
||
|
||
if __name__ == '__main__': | ||
Main() | ||
Main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/bin/bash | ||
|
||
rm -rf bazel-bin/tools | ||
|
||
declare -r PROTO_TARGETS=$(bazel query "labels(srcs, labels(deps, //tools/testdata/protoxform:protos))") | ||
|
||
BAZEL_BUILD_OPTIONS+=" --remote_download_outputs=all" | ||
|
||
bazel build ${BAZEL_BUILD_OPTIONS} --//tools/api_proto_plugin:default_type_db_target=//tools/testdata/protoxform:protos \ | ||
//tools/testdata/protoxform:protos --aspects //tools/protoxform:protoxform.bzl%protoxform_aspect --output_groups=proto \ | ||
--action_env=CPROFILE_ENABLED=1 --host_force_python=PY3 | ||
|
||
./tools/protoxform_test_helper.py ${PROTO_TARGETS} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from run_command import runCommand | ||
|
||
import logging | ||
import os | ||
import re | ||
import sys | ||
|
||
|
||
def PathAndFilename(label): | ||
Shikugawa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Retrieve actual path and filename from bazel label | ||
|
||
Args: | ||
label: bazel label to specify target proto. | ||
|
||
Returns: | ||
actual path and filename | ||
""" | ||
if label.startswith('/'): | ||
label = label.replace('//', '/', 1) | ||
elif label.startswith('@'): | ||
label = re.sub(r'@.*/', '/', label) | ||
else: | ||
return label | ||
label = label.replace(":", "/") | ||
splitted_label = label.split('/') | ||
return ['/'.join(splitted_label[:len(splitted_label) - 1]), splitted_label[-1]] | ||
|
||
|
||
def GoldenProtoFile(path, filename, version): | ||
"""Retrieve golden proto file path. In general, those are placed in tools/testdata/protoxform. | ||
|
||
Args: | ||
path: target proto path | ||
filename: target proto filename | ||
version: api version to specify target golden proto filename | ||
|
||
Returns: | ||
actual golden proto absolute path | ||
""" | ||
base = "./" | ||
base += path + "/" + filename + "." + version + ".gold" | ||
return os.path.abspath(base) | ||
|
||
|
||
def ResultProtoFile(path, filename, version): | ||
"""Retrieve result proto file path. In general, those are placed in bazel artifacts. | ||
|
||
Args: | ||
path: target proto path | ||
filename: target proto filename | ||
version: api version to specify target result proto filename | ||
|
||
Returns: | ||
actual result proto absolute path | ||
""" | ||
base = "./bazel-bin" | ||
base += os.path.join(path, "protos") | ||
base += os.path.join(base, path) | ||
base += "/{0}.{1}.proto".format(filename, version) | ||
return os.path.abspath(base) | ||
|
||
|
||
def Diff(result_file, golden_file): | ||
"""Execute diff command with unified form | ||
|
||
Args: | ||
result_file: result proto file | ||
golden_file: golden proto file | ||
|
||
Returns: | ||
output and status code | ||
""" | ||
command = 'diff -u ' | ||
command += result_file + ' ' | ||
command += golden_file | ||
status, stdout, stderr = runCommand(command) | ||
return [status, stdout, stderr] | ||
|
||
|
||
def Run(path, filename, version): | ||
"""Run main execution for protoxform test | ||
|
||
Args: | ||
path: target proto path | ||
filename: target proto filename | ||
version: api version to specify target result proto filename | ||
|
||
Returns: | ||
result message extracted from diff command | ||
""" | ||
message = "" | ||
golden_path = GoldenProtoFile(path, filename, version) | ||
test_path = ResultProtoFile(path, filename, version) | ||
|
||
status, stdout, stderr = Diff(test_path, golden_path) | ||
|
||
if status != 0: | ||
message = '\n'.join([str(line) for line in stdout + stderr]) | ||
|
||
return message | ||
|
||
|
||
if __name__ == "__main__": | ||
messages = "" | ||
logging.basicConfig(format='%(message)s') | ||
path, filename = PathAndFilename(sys.argv[1]) | ||
messages += Run(path, filename, 'v2') | ||
messages += Run(path, filename, 'v3alpha') | ||
|
||
if len(messages) == 0: | ||
logging.warning("PASS") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you are going to want to |
||
sys.exit(0) | ||
else: | ||
logging.error("FAILED:\n{}".format(messages)) | ||
sys.exit(1) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import subprocess | ||
|
||
|
||
# Echoes and runs an OS command, returning exit status and the captured | ||
# stdout and stderr as a string array. | ||
def runCommand(command): | ||
Shikugawa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
proc = subprocess.run([command], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can just do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @htuch we can't use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was able to use it in https://github.com/envoyproxy/envoy/pull/9258/files#diff-bba1df52a76edae166f0c52c4fed21dbR38, I'm wondering what version issues we're hitting here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @htuch it seems that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. anyway, add issue #9339 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Shikugawa is this blocking? I think I will have a similar issue with a test I'm working on, so I can take a look. Is there anything else here that we need to sort out? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @htuch hmm...can't pass formatting check because of confusing exception... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so is @Shikugawa the exception seems from yapf so it's failing on Python formatter somewhere, were you able to run |
||
|
||
return proc.returncode, proc.stdout.decode('utf-8').split('\n'), proc.stderr.decode( | ||
'utf-8').split('\n') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
licenses(["notice"]) # Apache 2 | ||
|
||
proto_library( | ||
name = "protos", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//tools/testdata/protoxform/envoy/v2:protos", | ||
], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
licenses(["notice"]) # Apache 2 | ||
|
||
proto_library( | ||
name = "protos", | ||
srcs = [ | ||
"sample.proto", | ||
], | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"@com_github_cncf_udpa//udpa/annotations:pkg", | ||
], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
syntax = "proto3"; | ||
|
||
package envoy.v2; | ||
|
||
import "udpa/annotations/migrate.proto"; | ||
|
||
message Sample { | ||
message Entry { | ||
string key = 1; | ||
string value = 2; | ||
} | ||
repeated Entry entries = 1; | ||
string will_deprecated = 2 [deprecated = true]; | ||
string will_rename_compoent = 3 [(udpa.annotations.field_migrate).rename = "renamed_component"]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
syntax = "proto3"; | ||
|
||
package envoy.v2; | ||
|
||
option java_package = "io.envoyproxy.envoy.v2"; | ||
option java_outer_classname = "SampleProto"; | ||
option java_multiple_files = true; | ||
Shikugawa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import "udpa/annotations/migrate.proto"; | ||
|
||
message Sample { | ||
message Entry { | ||
string key = 1; | ||
|
||
string value = 2; | ||
} | ||
|
||
repeated Entry entries = 1; | ||
|
||
string will_deprecated = 2 [deprecated = true]; | ||
|
||
string will_rename_compoent = 3 [(udpa.annotations.field_migrate).rename = "renamed_component"]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if @lizan has any Bazel Fu to make the tests work as Bazel aspects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you can write a rule that depends on aspect (like what
type_database
rule does depend on type_whisperer aspect), then define a test target on that. but we're good to go with this for now.