ceph-csi/vendor/google.golang.org/grpc/interop/client/client.go

249 lines
9.7 KiB
Go
Raw Normal View History

2018-01-09 18:57:14 +00:00
/*
*
* Copyright 2014 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package main
import (
"flag"
"net"
"strconv"
"google.golang.org/grpc"
2018-07-18 14:47:22 +00:00
_ "google.golang.org/grpc/balancer/grpclb"
2018-01-09 18:57:14 +00:00
"google.golang.org/grpc/credentials"
2018-07-18 14:47:22 +00:00
"google.golang.org/grpc/credentials/alts"
2018-11-26 18:23:56 +00:00
"google.golang.org/grpc/credentials/google"
2018-01-09 18:57:14 +00:00
"google.golang.org/grpc/credentials/oauth"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/interop"
testpb "google.golang.org/grpc/interop/grpc_testing"
2018-11-26 18:23:56 +00:00
"google.golang.org/grpc/resolver"
2018-01-09 18:57:14 +00:00
"google.golang.org/grpc/testdata"
)
2018-11-26 18:23:56 +00:00
const (
googleDefaultCredsName = "google_default_credentials"
)
2018-01-09 18:57:14 +00:00
var (
caFile = flag.String("ca_file", "", "The file containning the CA root cert file")
2018-07-18 14:47:22 +00:00
useTLS = flag.Bool("use_tls", false, "Connection uses TLS if true")
useALTS = flag.Bool("use_alts", false, "Connection uses ALTS if true (this option can only be used on GCP)")
2018-11-26 18:23:56 +00:00
customCredentialsType = flag.String("custom_credentials_type", "", "Custom creds to use, excluding TLS or ALTS")
2018-07-18 14:47:22 +00:00
altsHSAddr = flag.String("alts_handshaker_service_address", "", "ALTS handshaker gRPC service address")
2018-01-09 18:57:14 +00:00
testCA = flag.Bool("use_test_ca", false, "Whether to replace platform root CAs with test CA as the CA root")
serviceAccountKeyFile = flag.String("service_account_key_file", "", "Path to service account json key file")
oauthScope = flag.String("oauth_scope", "", "The scope for OAuth2 tokens")
defaultServiceAccount = flag.String("default_service_account", "", "Email of GCE default service account")
serverHost = flag.String("server_host", "localhost", "The server host name")
serverPort = flag.Int("server_port", 10000, "The server port number")
tlsServerName = flag.String("server_host_override", "", "The server name use to verify the hostname returned by TLS handshake if it is not empty. Otherwise, --server_host is used.")
testCase = flag.String("test_case", "large_unary",
`Configure different test cases. Valid options are:
empty_unary : empty (zero bytes) request and response;
large_unary : single request and (large) response;
client_streaming : request streaming with single response;
server_streaming : single request with response streaming;
ping_pong : full-duplex streaming;
empty_stream : full-duplex streaming with zero message;
timeout_on_sleeping_server: fullduplex streaming on a sleeping server;
compute_engine_creds: large_unary with compute engine auth;
service_account_creds: large_unary with service account auth;
jwt_token_creds: large_unary with jwt token auth;
per_rpc_creds: large_unary with per rpc token;
oauth2_auth_token: large_unary with oauth2 token auth;
cancel_after_begin: cancellation after metadata has been sent but before payloads are sent;
cancel_after_first_response: cancellation after receiving 1st message from the server;
status_code_and_message: status code propagated back to client;
2018-11-26 18:23:56 +00:00
special_status_message: Unicode and whitespace is correctly processed in status message;
2018-01-09 18:57:14 +00:00
custom_metadata: server will echo custom metadata;
unimplemented_method: client attempts to call unimplemented method;
unimplemented_service: client attempts to call unimplemented service.`)
)
2018-11-26 18:23:56 +00:00
type credsMode uint8
const (
credsNone credsMode = iota
credsTLS
credsALTS
credsGoogleDefaultCreds
)
2018-01-09 18:57:14 +00:00
func main() {
flag.Parse()
2018-11-26 18:23:56 +00:00
var useGDC bool // use google default creds
if *customCredentialsType != "" {
if *customCredentialsType != googleDefaultCredsName {
grpclog.Fatalf("custom_credentials_type can only be set to %v or not set", googleDefaultCredsName)
}
useGDC = true
}
if (*useTLS && *useALTS) || (*useTLS && useGDC) || (*useALTS && useGDC) {
grpclog.Fatalf("only one of TLS, ALTS and google default creds can be used")
2018-07-18 14:47:22 +00:00
}
2018-11-26 18:23:56 +00:00
var credsChosen credsMode
switch {
case *useTLS:
credsChosen = credsTLS
case *useALTS:
credsChosen = credsALTS
case useGDC:
credsChosen = credsGoogleDefaultCreds
}
resolver.SetDefaultScheme("dns")
2018-01-09 18:57:14 +00:00
serverAddr := net.JoinHostPort(*serverHost, strconv.Itoa(*serverPort))
var opts []grpc.DialOption
2018-11-26 18:23:56 +00:00
switch credsChosen {
case credsTLS:
2018-01-09 18:57:14 +00:00
var sn string
if *tlsServerName != "" {
sn = *tlsServerName
}
var creds credentials.TransportCredentials
if *testCA {
var err error
if *caFile == "" {
*caFile = testdata.Path("ca.pem")
}
creds, err = credentials.NewClientTLSFromFile(*caFile, sn)
if err != nil {
grpclog.Fatalf("Failed to create TLS credentials %v", err)
}
} else {
creds = credentials.NewClientTLSFromCert(nil, sn)
}
opts = append(opts, grpc.WithTransportCredentials(creds))
2018-11-26 18:23:56 +00:00
case credsALTS:
altsOpts := alts.DefaultClientOptions()
if *altsHSAddr != "" {
altsOpts.HandshakerServiceAddress = *altsHSAddr
}
altsTC := alts.NewClientCreds(altsOpts)
opts = append(opts, grpc.WithTransportCredentials(altsTC))
case credsGoogleDefaultCreds:
opts = append(opts, grpc.WithCredentialsBundle(google.NewDefaultCredentials()))
default:
opts = append(opts, grpc.WithInsecure())
}
if credsChosen == credsTLS || credsChosen == credsALTS {
2018-01-09 18:57:14 +00:00
if *testCase == "compute_engine_creds" {
opts = append(opts, grpc.WithPerRPCCredentials(oauth.NewComputeEngine()))
} else if *testCase == "service_account_creds" {
jwtCreds, err := oauth.NewServiceAccountFromFile(*serviceAccountKeyFile, *oauthScope)
if err != nil {
grpclog.Fatalf("Failed to create JWT credentials: %v", err)
}
opts = append(opts, grpc.WithPerRPCCredentials(jwtCreds))
} else if *testCase == "jwt_token_creds" {
jwtCreds, err := oauth.NewJWTAccessFromFile(*serviceAccountKeyFile)
if err != nil {
grpclog.Fatalf("Failed to create JWT credentials: %v", err)
}
opts = append(opts, grpc.WithPerRPCCredentials(jwtCreds))
} else if *testCase == "oauth2_auth_token" {
opts = append(opts, grpc.WithPerRPCCredentials(oauth.NewOauthAccess(interop.GetToken(*serviceAccountKeyFile, *oauthScope))))
}
}
opts = append(opts, grpc.WithBlock())
conn, err := grpc.Dial(serverAddr, opts...)
if err != nil {
grpclog.Fatalf("Fail to dial: %v", err)
}
defer conn.Close()
tc := testpb.NewTestServiceClient(conn)
switch *testCase {
case "empty_unary":
interop.DoEmptyUnaryCall(tc)
2018-07-18 14:47:22 +00:00
grpclog.Infoln("EmptyUnaryCall done")
2018-01-09 18:57:14 +00:00
case "large_unary":
interop.DoLargeUnaryCall(tc)
2018-07-18 14:47:22 +00:00
grpclog.Infoln("LargeUnaryCall done")
2018-01-09 18:57:14 +00:00
case "client_streaming":
interop.DoClientStreaming(tc)
2018-07-18 14:47:22 +00:00
grpclog.Infoln("ClientStreaming done")
2018-01-09 18:57:14 +00:00
case "server_streaming":
interop.DoServerStreaming(tc)
2018-07-18 14:47:22 +00:00
grpclog.Infoln("ServerStreaming done")
2018-01-09 18:57:14 +00:00
case "ping_pong":
interop.DoPingPong(tc)
2018-07-18 14:47:22 +00:00
grpclog.Infoln("Pingpong done")
2018-01-09 18:57:14 +00:00
case "empty_stream":
interop.DoEmptyStream(tc)
2018-07-18 14:47:22 +00:00
grpclog.Infoln("Emptystream done")
2018-01-09 18:57:14 +00:00
case "timeout_on_sleeping_server":
interop.DoTimeoutOnSleepingServer(tc)
2018-07-18 14:47:22 +00:00
grpclog.Infoln("TimeoutOnSleepingServer done")
2018-01-09 18:57:14 +00:00
case "compute_engine_creds":
2018-11-26 18:23:56 +00:00
if credsChosen == credsNone {
grpclog.Fatalf("Credentials (TLS, ALTS or google default creds) need to be set for compute_engine_creds test case.")
2018-01-09 18:57:14 +00:00
}
interop.DoComputeEngineCreds(tc, *defaultServiceAccount, *oauthScope)
2018-07-18 14:47:22 +00:00
grpclog.Infoln("ComputeEngineCreds done")
2018-01-09 18:57:14 +00:00
case "service_account_creds":
2018-11-26 18:23:56 +00:00
if credsChosen == credsNone {
grpclog.Fatalf("Credentials (TLS, ALTS or google default creds) need to be set for service_account_creds test case.")
2018-01-09 18:57:14 +00:00
}
interop.DoServiceAccountCreds(tc, *serviceAccountKeyFile, *oauthScope)
2018-07-18 14:47:22 +00:00
grpclog.Infoln("ServiceAccountCreds done")
2018-01-09 18:57:14 +00:00
case "jwt_token_creds":
2018-11-26 18:23:56 +00:00
if credsChosen == credsNone {
grpclog.Fatalf("Credentials (TLS, ALTS or google default creds) need to be set for jwt_token_creds test case.")
2018-01-09 18:57:14 +00:00
}
interop.DoJWTTokenCreds(tc, *serviceAccountKeyFile)
2018-07-18 14:47:22 +00:00
grpclog.Infoln("JWTtokenCreds done")
2018-01-09 18:57:14 +00:00
case "per_rpc_creds":
2018-11-26 18:23:56 +00:00
if credsChosen == credsNone {
grpclog.Fatalf("Credentials (TLS, ALTS or google default creds) need to be set for per_rpc_creds test case.")
2018-01-09 18:57:14 +00:00
}
interop.DoPerRPCCreds(tc, *serviceAccountKeyFile, *oauthScope)
2018-07-18 14:47:22 +00:00
grpclog.Infoln("PerRPCCreds done")
2018-01-09 18:57:14 +00:00
case "oauth2_auth_token":
2018-11-26 18:23:56 +00:00
if credsChosen == credsNone {
grpclog.Fatalf("Credentials (TLS, ALTS or google default creds) need to be set for oauth2_auth_token test case.")
2018-01-09 18:57:14 +00:00
}
interop.DoOauth2TokenCreds(tc, *serviceAccountKeyFile, *oauthScope)
2018-07-18 14:47:22 +00:00
grpclog.Infoln("Oauth2TokenCreds done")
2018-01-09 18:57:14 +00:00
case "cancel_after_begin":
interop.DoCancelAfterBegin(tc)
2018-07-18 14:47:22 +00:00
grpclog.Infoln("CancelAfterBegin done")
2018-01-09 18:57:14 +00:00
case "cancel_after_first_response":
interop.DoCancelAfterFirstResponse(tc)
2018-07-18 14:47:22 +00:00
grpclog.Infoln("CancelAfterFirstResponse done")
2018-01-09 18:57:14 +00:00
case "status_code_and_message":
interop.DoStatusCodeAndMessage(tc)
2018-07-18 14:47:22 +00:00
grpclog.Infoln("StatusCodeAndMessage done")
2018-11-26 18:23:56 +00:00
case "special_status_message":
interop.DoSpecialStatusMessage(tc)
grpclog.Infoln("SpecialStatusMessage done")
2018-01-09 18:57:14 +00:00
case "custom_metadata":
interop.DoCustomMetadata(tc)
2018-07-18 14:47:22 +00:00
grpclog.Infoln("CustomMetadata done")
2018-01-09 18:57:14 +00:00
case "unimplemented_method":
interop.DoUnimplementedMethod(conn)
2018-07-18 14:47:22 +00:00
grpclog.Infoln("UnimplementedMethod done")
2018-01-09 18:57:14 +00:00
case "unimplemented_service":
interop.DoUnimplementedService(testpb.NewUnimplementedServiceClient(conn))
2018-07-18 14:47:22 +00:00
grpclog.Infoln("UnimplementedService done")
2018-01-09 18:57:14 +00:00
default:
grpclog.Fatal("Unsupported test case: ", *testCase)
}
}