Skip to content
Snippets Groups Projects
Commit 2e208569 authored by Tim Emiola's avatar Tim Emiola
Browse files

Adds an interop tests if OAuth behaviour is functioning

parent 179eabb2
Branches
Tags
No related merge requests found
...@@ -86,11 +86,11 @@ def prod_creds ...@@ -86,11 +86,11 @@ def prod_creds
end end
# creates a test stub that accesses host:port securely. # creates a test stub that accesses host:port securely.
def create_stub(host, port, is_secure, host_override, use_test_ca) def create_stub(opts)
address = "#{host}:#{port}" address = "#{opts.host}:#{opts.port}"
if is_secure if opts.secure
creds = nil creds = nil
if use_test_ca if opts.use_test_ca
creds = test_creds creds = test_creds
else else
creds = prod_creds creds = prod_creds
...@@ -98,8 +98,17 @@ def create_stub(host, port, is_secure, host_override, use_test_ca) ...@@ -98,8 +98,17 @@ def create_stub(host, port, is_secure, host_override, use_test_ca)
stub_opts = { stub_opts = {
:creds => creds, :creds => creds,
GRPC::Core::Channel::SSL_TARGET => host_override GRPC::Core::Channel::SSL_TARGET => opts.host_override
} }
# Allow service account updates if specified
unless opts.oauth_scope.nil?
cred_clz = Google::RPC::Auth::ServiceAccountCredentials
json_key_io = StringIO.new(File.read(opts.oauth_key_file))
auth_creds = cred_clz.new(opts.oauth_scope, json_key_io)
stub_opts[:update_metadata] = lambda(&auth_creds.method(:apply))
end
logger.info("... connecting securely to #{address}") logger.info("... connecting securely to #{address}")
Grpc::Testing::TestService::Stub.new(address, **stub_opts) Grpc::Testing::TestService::Stub.new(address, **stub_opts)
else else
...@@ -157,9 +166,10 @@ class NamedTests ...@@ -157,9 +166,10 @@ class NamedTests
include Grpc::Testing::PayloadType include Grpc::Testing::PayloadType
attr_accessor :assertions # required by Minitest::Assertions attr_accessor :assertions # required by Minitest::Assertions
def initialize(stub) def initialize(stub, opts)
@assertions = 0 # required by Minitest::Assertions @assertions = 0 # required by Minitest::Assertions
@stub = stub @stub = stub
@opts = opts
end end
def empty_unary def empty_unary
...@@ -169,21 +179,24 @@ class NamedTests ...@@ -169,21 +179,24 @@ class NamedTests
end end
def large_unary def large_unary
req_size, wanted_response_size = 271_828, 314_159 perform_large_unary
payload = Payload.new(type: :COMPRESSABLE, body: nulls(req_size))
req = SimpleRequest.new(response_type: :COMPRESSABLE,
response_size: wanted_response_size,
payload: payload)
resp = @stub.unary_call(req)
assert_equal(:COMPRESSABLE, resp.payload.type,
'large_unary: payload had the wrong type')
assert_equal(wanted_response_size, resp.payload.body.length,
'large_unary: payload had the wrong length')
assert_equal(nulls(wanted_response_size), resp.payload.body,
'large_unary: payload content is invalid')
p 'OK: large_unary' p 'OK: large_unary'
end end
def service_account_creds
# ignore this test if the oauth options are not set
if @opts.oauth_scope.nil? || @opts.oauth_key_file.nil?
p 'NOT RUN: service_account_creds; no service_account settings'
end
json_key = File.read(@opts.oauth_key_file)
wanted_email = MultiJson.load(json_key)['client_email']
resp = perform_large_unary
assert_equal(@opts.oauth_scope, resp.oauth_scope,
'service_account_creds: incorrect oauth_scope')
assert_equal(wanted_email, resp.username)
p 'OK: service_account_creds'
end
def client_streaming def client_streaming
msg_sizes = [27_182, 8, 1828, 45_904] msg_sizes = [27_182, 8, 1828, 45_904]
wanted_aggregate_size = 74_922 wanted_aggregate_size = 74_922
...@@ -229,28 +242,54 @@ class NamedTests ...@@ -229,28 +242,54 @@ class NamedTests
method(m).call method(m).call
end end
end end
private
def perform_large_unary(fill_username: false, fill_oauth_scope: false)
req_size, wanted_response_size = 271_828, 314_159
payload = Payload.new(type: :COMPRESSABLE, body: nulls(req_size))
req = SimpleRequest.new(response_type: :COMPRESSABLE,
response_size: wanted_response_size,
payload: payload)
req.fill_username = fill_username
req.fill_oauth_scope = fill_oauth_scope
resp = @stub.unary_call(req)
assert_equal(:COMPRESSABLE, resp.payload.type,
'large_unary: payload had the wrong type')
assert_equal(wanted_response_size, resp.payload.body.length,
'large_unary: payload had the wrong length')
assert_equal(nulls(wanted_response_size), resp.payload.body,
'large_unary: payload content is invalid')
resp
end
end end
Options = Struct.new(:oauth_scope, :oauth_key_file, :secure, :host,
:host_override, :port, :test_case, :use_test_ca)
# validates the the command line options, returning them as a Hash. # validates the the command line options, returning them as a Hash.
def parse_options def parse_options
options = { options = Options.new
'secure' => false, options.host_override = 'foo.test.google.com'
'server_host' => nil,
'server_host_override' => nil,
'server_port' => nil,
'test_case' => nil
}
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = 'Usage: --server_host <server_host> --server_port server_port' opts.banner = 'Usage: --server_host <server_host> --server_port server_port'
opts.on('--oauth_scope scope',
'Scope for OAuth tokens') do |v|
options['oauth_scope'] = v
end
opts.on('--server_host SERVER_HOST', 'server hostname') do |v| opts.on('--server_host SERVER_HOST', 'server hostname') do |v|
options['server_host'] = v options['host'] = v
end
opts.on('--service_account_key_file PATH',
'Path to the service account json key file') do |v|
options['oauth_key_file'] = v
end end
opts.on('--server_host_override HOST_OVERRIDE', opts.on('--server_host_override HOST_OVERRIDE',
'override host via a HTTP header') do |v| 'override host via a HTTP header') do |v|
options['server_host_override'] = v options['host_override'] = v
end end
opts.on('--server_port SERVER_PORT', 'server port') do |v| opts.on('--server_port SERVER_PORT', 'server port') do |v|
options['server_port'] = v options['port'] = v
end end
# instance_methods(false) gives only the methods defined in that class # instance_methods(false) gives only the methods defined in that class
test_cases = NamedTests.instance_methods(false).map(&:to_s) test_cases = NamedTests.instance_methods(false).map(&:to_s)
...@@ -271,22 +310,22 @@ def parse_options ...@@ -271,22 +310,22 @@ def parse_options
end end
def _check_options(opts) def _check_options(opts)
%w(server_host server_port test_case).each do |arg| %w(host port test_case).each do |arg|
if opts[arg].nil? if opts[arg].nil?
fail(OptionParser::MissingArgument, "please specify --#{arg}") fail(OptionParser::MissingArgument, "please specify --#{arg}")
end end
end end
if opts['server_host_override'].nil? if opts['oauth_key_file'].nil? ^ opts['oauth_scope'].nil?
opts['server_host_override'] = opts['server_host'] fail(OptionParser::MissingArgument,
'please specify both of --service_account_key_file and --oauth_scope')
end end
opts opts
end end
def main def main
opts = parse_options opts = parse_options
stub = create_stub(opts['server_host'], opts['server_port'], opts['secure'], stub = create_stub(opts)
opts['server_host_override'], opts['use_test_ca']) NamedTests.new(stub, opts).method(opts['test_case']).call
NamedTests.new(stub).method(opts['test_case']).call
end end
main main
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment