-
Notifications
You must be signed in to change notification settings - Fork 5.5k
thrift: filter integration tests #3679
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
Merged
zuercher
merged 8 commits into
envoyproxy:master
from
turbinelabs:stephan/thrift-filter-integration-tests
Jun 26, 2018
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f49e23f
thrift: integration test scripts
zuercher 01cadb9
better test names
zuercher 013db30
thrift: filter integration test (and bug fixes)
zuercher f17602c
driver readme
zuercher 5719aaf
fix python imports
zuercher 9ae1c18
fix most python lint warnings
zuercher 539dfa2
better port handling
zuercher 57cf185
ws change to force rebuild
zuercher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # The apache-thrift distribution does not keep the thrift files in a directory with the | ||
| # expected package name (it uses src/Thrift.py vs src/thrift/Thrift.py), so we provide a | ||
| # genrule to copy src/**/*.py to thrift/**/*.py. | ||
| src_files = glob(["src/**/*.py"]) | ||
|
|
||
| genrule( | ||
| name = "thrift_files", | ||
| srcs = src_files, | ||
| outs = [f.replace("src/", "thrift/") for f in src_files], | ||
| cmd = '\n'.join( | ||
| ['mkdir -p $$(dirname $(location %s)) && cp $(location %s) $(location :%s)' % (f, f, f.replace('src/', 'thrift/')) for f in src_files] | ||
| ), | ||
| visibility = ["//visibility:private"], | ||
| ) | ||
|
|
||
| py_library( | ||
| name = "apache_thrift", | ||
| srcs = [":thrift_files"], | ||
| visibility = ["//visibility:public"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| py_library( | ||
| name = "twitter_common_finagle_thrift", | ||
| srcs = glob([ | ||
| "gen/**/*.py", | ||
| ]), | ||
| visibility = ["//visibility:public"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| py_library( | ||
| name = "twitter_common_lang", | ||
| srcs = glob([ | ||
| "twitter/**/*.py", | ||
| ]), | ||
| visibility = ["//visibility:public"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| py_library( | ||
| name = "twitter_common_rpc", | ||
| srcs = glob([ | ||
| "twitter/**/*.py", | ||
| ]), | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "@com_github_twitter_common_lang//:twitter_common_lang", | ||
| "@com_github_twitter_common_finagle_thrift//:twitter_common_finagle_thrift" | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| licenses(["notice"]) # Apache 2 | ||
|
|
||
| load("//bazel:envoy_build_system.bzl", "envoy_package") | ||
|
|
||
| envoy_package() | ||
|
|
||
| filegroup( | ||
| name = "generate_fixture", | ||
| srcs = ["generate_fixture.sh"], | ||
| data = [ | ||
| ":client", | ||
| ":server", | ||
| ], | ||
| ) | ||
|
|
||
| py_binary( | ||
| name = "client", | ||
| srcs = ["client.py"], | ||
| deps = [ | ||
| "//test/extensions/filters/network/thrift_proxy/driver/fbthrift:fbthrift_lib", | ||
| "//test/extensions/filters/network/thrift_proxy/driver/finagle:finagle_lib", | ||
| "//test/extensions/filters/network/thrift_proxy/driver/generated/example:example_lib", | ||
| "@com_github_twitter_common_rpc//:twitter_common_rpc", | ||
| ], | ||
| ) | ||
|
|
||
| py_binary( | ||
| name = "server", | ||
| srcs = ["server.py"], | ||
| deps = [ | ||
| "//test/extensions/filters/network/thrift_proxy/driver/fbthrift:fbthrift_lib", | ||
| "//test/extensions/filters/network/thrift_proxy/driver/finagle:finagle_lib", | ||
| "//test/extensions/filters/network/thrift_proxy/driver/generated/example:example_lib", | ||
| "@com_github_twitter_common_rpc//:twitter_common_rpc", | ||
| ], | ||
| ) |
236 changes: 236 additions & 0 deletions
236
test/extensions/filters/network/thrift_proxy/driver/client.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| import os | ||
| import argparse | ||
| import io | ||
| import sys | ||
|
|
||
| from generated.example import Example | ||
| from generated.example.ttypes import ( | ||
| Param, Result, TheWorks, AppException | ||
| ) | ||
|
|
||
| from thrift import Thrift | ||
| from thrift.protocol import TBinaryProtocol, TCompactProtocol, TJSONProtocol, TMultiplexedProtocol | ||
| from thrift.transport import TSocket | ||
| from thrift.transport import TTransport | ||
| from fbthrift import THeaderTransport | ||
| from twitter.common.rpc.finagle.protocol import TFinagleProtocol | ||
|
|
||
| class TRecordingTransport(TTransport.TTransportBase): | ||
| def __init__(self, underlying, writehandle, readhandle): | ||
| self._underlying = underlying | ||
| self._whandle = writehandle | ||
| self._rhandle = readhandle | ||
|
|
||
| def isOpen(self): | ||
| return self._underlying.isOpen() | ||
|
|
||
| def open(self): | ||
| if not self._underlying.isOpen(): | ||
| self._underlying.open() | ||
|
|
||
| def close(self): | ||
| self._underlying.close() | ||
| self._whandle.close() | ||
| self._rhandle.close() | ||
|
|
||
| def read(self, sz): | ||
| buf = self._underlying.read(sz) | ||
| if len(buf) != 0: | ||
| self._rhandle.write(buf) | ||
| return buf | ||
|
|
||
| def write(self, buf): | ||
| if len(buf) != 0: | ||
| self._whandle.write(buf) | ||
| self._underlying.write(buf) | ||
|
|
||
| def flush(self): | ||
| self._underlying.flush() | ||
| self._whandle.flush() | ||
| self._rhandle.flush() | ||
|
|
||
|
|
||
| def main(cfg, reqhandle, resphandle): | ||
| if cfg.unix: | ||
| if cfg.addr == "": | ||
| sys.exit("invalid unix domain socket: {}".format(cfg.addr)) | ||
| socket = TSocket.TSocket(unix_socket=cfg.addr) | ||
| else: | ||
| (host, port) = cfg.addr.split(":") | ||
|
Member
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. this will raise |
||
| if host == "": | ||
| host = "localhost" | ||
| if port == "": | ||
| sys.exit("invalid address: {}".format(cfg.addr)) | ||
| socket = TSocket.TSocket(host=host, port=int(port)) | ||
|
|
||
| transport = TRecordingTransport(socket, reqhandle, resphandle) | ||
|
|
||
| if cfg.transport == "framed": | ||
| transport = TTransport.TFramedTransport(transport) | ||
| elif cfg.transport == "unframed": | ||
| transport = TTransport.TBufferedTransport(transport) | ||
| elif cfg.transport == "header": | ||
| transport = THeaderTransport.THeaderTransport( | ||
| transport, | ||
| client_type=THeaderTransport.CLIENT_TYPE.HEADER, | ||
| ) | ||
| else: | ||
| sys.exit("unknown transport {0}".format(cfg.transport)) | ||
|
|
||
| transport.open() | ||
|
|
||
| if cfg.protocol == "binary": | ||
| protocol = TBinaryProtocol.TBinaryProtocol(transport) | ||
| elif cfg.protocol == "compact": | ||
| protocol = TCompactProtocol.TCompactProtocol(transport) | ||
| elif cfg.protocol == "json": | ||
| protocol = TJSONProtocol.TJSONProtocol(transport) | ||
| elif cfg.protocol == "finagle": | ||
| protocol = TFinagleProtocol(transport, client_id="thrift-playground") | ||
| else: | ||
| sys.exit("unknown protocol {0}".format(cfg.protocol)) | ||
|
|
||
| if cfg.service is not None: | ||
| protocol = TMultiplexedProtocol.TMultiplexedProtocol(protocol, cfg.service) | ||
|
|
||
| client = Example.Client(protocol) | ||
|
|
||
| try: | ||
| if cfg.method == "ping": | ||
| client.ping() | ||
| print("client: pinged") | ||
| elif cfg.method == "poke": | ||
| client.poke() | ||
| print("client: poked") | ||
| elif cfg.method == "add": | ||
| if len(cfg.params) != 2: | ||
| sys.exit("add takes 2 arguments, got: {0}".format(cfg.params)) | ||
|
|
||
| a = int(cfg.params[0]) | ||
| b = int(cfg.params[1]) | ||
| v = client.add(a, b) | ||
| print("client: added {0} + {1} = {2}".format(a, b, v)) | ||
| elif cfg.method == "execute": | ||
| param = Param( | ||
| return_fields = cfg.params, | ||
| the_works = TheWorks( | ||
| field_1 = True, | ||
| field_2 = 0x7f, | ||
| field_3 = 0x7fff, | ||
| field_4 = 0x7fffffff, | ||
| field_5 = 0x7fffffffffffffff, | ||
| field_6 = -1.5, | ||
| field_7 = u"string is UTF-8: \U0001f60e", | ||
| field_8 = b"binary is bytes: \x80\x7f\x00\x01", | ||
| field_9 = { 1: "one", 2: "two", 3: "three" }, | ||
| field_10 = [ 1, 2, 4, 8 ], | ||
| field_11 = set(["a", "b", "c"]), | ||
| field_12 = False, | ||
| ) | ||
| ) | ||
|
|
||
| try: | ||
| result = client.execute(param) | ||
| print("client: executed {0}: {1}".format(param, result)) | ||
| except AppException as e: | ||
| print("client: execute failed with IDL Exception: {0}".format(e.why)) | ||
| else: | ||
| sys.exit("unknown method {0}".format(cfg.method)) | ||
| except Thrift.TApplicationException as e: | ||
| print("client exception: {0}: {1}".format(e.type, e.message)) | ||
|
|
||
| if cfg.request is None: | ||
| req = "".join( [ "%02X " % ord( x ) for x in reqhandle.getvalue() ] ).strip() | ||
| print("request: {}".format(req)) | ||
| if cfg.response is None: | ||
| resp = "".join( [ "%02X " % ord( x ) for x in resphandle.getvalue() ] ).strip() | ||
| print("response: {}".format(resp)) | ||
|
|
||
| transport.close() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser( | ||
| description="Thrift client tool.", | ||
| ) | ||
| parser.add_argument( | ||
| "method", | ||
| metavar="METHOD", | ||
| help="Name of the service method to invoke.", | ||
| ) | ||
| parser.add_argument( | ||
| "params", | ||
| metavar="PARAMS", | ||
| nargs="*", | ||
| help="Method parameters", | ||
| ) | ||
| parser.add_argument( | ||
| "-a", | ||
| "--addr", | ||
| metavar="ADDR", | ||
| dest="addr", | ||
| required=True, | ||
| help="Target address for requests in the form host:port. The host is optional. If --unix" + | ||
| " is set, the address is the socket name.", | ||
| ) | ||
| parser.add_argument( | ||
| "-m", | ||
| "--multiplex", | ||
| metavar="SERVICE", | ||
| dest="service", | ||
| help="Enable service multiplexing and set the service name.", | ||
| ) | ||
| parser.add_argument( | ||
| "-p", | ||
| "--protocol", | ||
| dest="protocol", | ||
| default="binary", | ||
| choices=["binary", "compact", "json", "finagle"], | ||
| help="selects a protocol.", | ||
| ) | ||
| parser.add_argument( | ||
| "--request", | ||
| metavar="FILE", | ||
| dest="request", | ||
| help="Writes the Thrift request to a file.", | ||
| ) | ||
| parser.add_argument( | ||
| "--response", | ||
| metavar="FILE", | ||
| dest="response", | ||
| help="Writes the Thrift response to a file.", | ||
| ) | ||
| parser.add_argument( | ||
| "-t", | ||
| "--transport", | ||
| dest="transport", | ||
| default="framed", | ||
| choices=["framed", "unframed", "header"], | ||
| help="selects a transport.", | ||
| ) | ||
| parser.add_argument( | ||
| "-u", | ||
| "--unix", | ||
| dest="unix", | ||
| action="store_true", | ||
| ) | ||
| cfg = parser.parse_args() | ||
|
|
||
| reqhandle = io.BytesIO() | ||
| resphandle = io.BytesIO() | ||
| if cfg.request is not None: | ||
| try: | ||
| reqhandle = io.open(cfg.request, "wb") | ||
| except IOError as e: | ||
| sys.exit("I/O error({0}): {1}".format(e.errno, e.strerror)) | ||
| if cfg.response is not None: | ||
| try: | ||
| resphandle = io.open(cfg.response, "wb") | ||
| except IOError as e: | ||
| sys.exit("I/O error({0}): {1}".format(e.errno, e.strerror)) | ||
| try: | ||
| main(cfg, reqhandle, resphandle) | ||
| except Thrift.TException as tx: | ||
| sys.exit("Unhandled Thrift Exception: {0}".format(tx.message)) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
hyper nit: sort imports