github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/Godeps/_workspace/src/google.golang.org/grpc/interop/client/client.go (about)

     1  /*
     2   *
     3   * Copyright 2014, Google Inc.
     4   * All rights reserved.
     5   *
     6   * Redistribution and use in source and binary forms, with or without
     7   * modification, are permitted provided that the following conditions are
     8   * met:
     9   *
    10   *     * Redistributions of source code must retain the above copyright
    11   * notice, this list of conditions and the following disclaimer.
    12   *     * Redistributions in binary form must reproduce the above
    13   * copyright notice, this list of conditions and the following disclaimer
    14   * in the documentation and/or other materials provided with the
    15   * distribution.
    16   *     * Neither the name of Google Inc. nor the names of its
    17   * contributors may be used to endorse or promote products derived from
    18   * this software without specific prior written permission.
    19   *
    20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    21   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    22   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    23   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    24   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    25   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    26   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    28   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    29   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    30   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    31   *
    32   */
    33  
    34  package main
    35  
    36  import (
    37  	"flag"
    38  	"net"
    39  	"strconv"
    40  
    41  	"google.golang.org/grpc"
    42  	"google.golang.org/grpc/credentials"
    43  	"google.golang.org/grpc/credentials/oauth"
    44  	"google.golang.org/grpc/grpclog"
    45  	"google.golang.org/grpc/interop"
    46  	testpb "google.golang.org/grpc/interop/grpc_testing"
    47  )
    48  
    49  var (
    50  	useTLS                = flag.Bool("use_tls", false, "Connection uses TLS if true, else plain TCP")
    51  	testCA                = flag.Bool("use_test_ca", false, "Whether to replace platform root CAs with test CA as the CA root")
    52  	serviceAccountKeyFile = flag.String("service_account_key_file", "", "Path to service account json key file")
    53  	oauthScope            = flag.String("oauth_scope", "", "The scope for OAuth2 tokens")
    54  	defaultServiceAccount = flag.String("default_service_account", "", "Email of GCE default service account")
    55  	serverHost            = flag.String("server_host", "127.0.0.1", "The server host name")
    56  	serverPort            = flag.Int("server_port", 10000, "The server port number")
    57  	tlsServerName         = flag.String("server_host_override", "x.test.youtube.com", "The server name use to verify the hostname returned by TLS handshake if it is not empty. Otherwise, --server_host is used.")
    58  	testCase              = flag.String("test_case", "large_unary",
    59  		`Configure different test cases. Valid options are:
    60          empty_unary : empty (zero bytes) request and response;
    61          large_unary : single request and (large) response;
    62          client_streaming : request streaming with single response;
    63          server_streaming : single request with response streaming;
    64          ping_pong : full-duplex streaming;
    65          empty_stream : full-duplex streaming with zero message;
    66          timeout_on_sleeping_server: fullduplex streaming on a sleeping server;
    67          compute_engine_creds: large_unary with compute engine auth;
    68          service_account_creds: large_unary with service account auth;
    69          jwt_token_creds: large_unary with jwt token auth;
    70          per_rpc_creds: large_unary with per rpc token;
    71          oauth2_auth_token: large_unary with oauth2 token auth;
    72          cancel_after_begin: cancellation after metadata has been sent but before payloads are sent;
    73          cancel_after_first_response: cancellation after receiving 1st message from the server.`)
    74  
    75  	// The test CA root cert file
    76  	testCAFile = "testdata/ca.pem"
    77  )
    78  
    79  func main() {
    80  	flag.Parse()
    81  	serverAddr := net.JoinHostPort(*serverHost, strconv.Itoa(*serverPort))
    82  	var opts []grpc.DialOption
    83  	if *useTLS {
    84  		var sn string
    85  		if *tlsServerName != "" {
    86  			sn = *tlsServerName
    87  		}
    88  		var creds credentials.TransportAuthenticator
    89  		if *testCA {
    90  			var err error
    91  			creds, err = credentials.NewClientTLSFromFile(testCAFile, sn)
    92  			if err != nil {
    93  				grpclog.Fatalf("Failed to create TLS credentials %v", err)
    94  			}
    95  		} else {
    96  			creds = credentials.NewClientTLSFromCert(nil, sn)
    97  		}
    98  		opts = append(opts, grpc.WithTransportCredentials(creds))
    99  		if *testCase == "compute_engine_creds" {
   100  			opts = append(opts, grpc.WithPerRPCCredentials(oauth.NewComputeEngine()))
   101  		} else if *testCase == "service_account_creds" {
   102  			jwtCreds, err := oauth.NewServiceAccountFromFile(*serviceAccountKeyFile, *oauthScope)
   103  			if err != nil {
   104  				grpclog.Fatalf("Failed to create JWT credentials: %v", err)
   105  			}
   106  			opts = append(opts, grpc.WithPerRPCCredentials(jwtCreds))
   107  		} else if *testCase == "jwt_token_creds" {
   108  			jwtCreds, err := oauth.NewJWTAccessFromFile(*serviceAccountKeyFile)
   109  			if err != nil {
   110  				grpclog.Fatalf("Failed to create JWT credentials: %v", err)
   111  			}
   112  			opts = append(opts, grpc.WithPerRPCCredentials(jwtCreds))
   113  		} else if *testCase == "oauth2_auth_token" {
   114  			opts = append(opts, grpc.WithPerRPCCredentials(oauth.NewOauthAccess(interop.GetToken(*serviceAccountKeyFile, *oauthScope))))
   115  		}
   116  	} else {
   117  		opts = append(opts, grpc.WithInsecure())
   118  	}
   119  	conn, err := grpc.Dial(serverAddr, opts...)
   120  	if err != nil {
   121  		grpclog.Fatalf("Fail to dial: %v", err)
   122  	}
   123  	defer conn.Close()
   124  	tc := testpb.NewTestServiceClient(conn)
   125  	switch *testCase {
   126  	case "empty_unary":
   127  		interop.DoEmptyUnaryCall(tc)
   128  	case "large_unary":
   129  		interop.DoLargeUnaryCall(tc)
   130  	case "client_streaming":
   131  		interop.DoClientStreaming(tc)
   132  	case "server_streaming":
   133  		interop.DoServerStreaming(tc)
   134  	case "ping_pong":
   135  		interop.DoPingPong(tc)
   136  	case "empty_stream":
   137  		interop.DoEmptyStream(tc)
   138  	case "timeout_on_sleeping_server":
   139  		interop.DoTimeoutOnSleepingServer(tc)
   140  	case "compute_engine_creds":
   141  		if !*useTLS {
   142  			grpclog.Fatalf("TLS is not enabled. TLS is required to execute compute_engine_creds test case.")
   143  		}
   144  		interop.DoComputeEngineCreds(tc, *defaultServiceAccount, *oauthScope)
   145  	case "service_account_creds":
   146  		if !*useTLS {
   147  			grpclog.Fatalf("TLS is not enabled. TLS is required to execute service_account_creds test case.")
   148  		}
   149  		interop.DoServiceAccountCreds(tc, *serviceAccountKeyFile, *oauthScope)
   150  	case "jwt_token_creds":
   151  		if !*useTLS {
   152  			grpclog.Fatalf("TLS is not enabled. TLS is required to execute jwt_token_creds test case.")
   153  		}
   154  		interop.DoJWTTokenCreds(tc, *serviceAccountKeyFile)
   155  	case "per_rpc_creds":
   156  		if !*useTLS {
   157  			grpclog.Fatalf("TLS is not enabled. TLS is required to execute per_rpc_creds test case.")
   158  		}
   159  		interop.DoPerRPCCreds(tc, *serviceAccountKeyFile, *oauthScope)
   160  	case "oauth2_auth_token":
   161  		if !*useTLS {
   162  			grpclog.Fatalf("TLS is not enabled. TLS is required to execute oauth2_auth_token test case.")
   163  		}
   164  		interop.DoOauth2TokenCreds(tc, *serviceAccountKeyFile, *oauthScope)
   165  	case "cancel_after_begin":
   166  		interop.DoCancelAfterBegin(tc)
   167  	case "cancel_after_first_response":
   168  		interop.DoCancelAfterFirstResponse(tc)
   169  	default:
   170  		grpclog.Fatal("Unsupported test case: ", *testCase)
   171  	}
   172  }