...

Source file src/golang.conradwood.net/tests/authtest/client.go

Documentation: golang.conradwood.net/tests/authtest

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	ge "golang.conradwood.net/apis/getestservice"
     7  	"golang.conradwood.net/go-easyops/auth"
     8  	"golang.conradwood.net/go-easyops/authremote"
     9  	"golang.conradwood.net/go-easyops/client"
    10  	"golang.conradwood.net/go-easyops/utils"
    11  	"google.golang.org/grpc"
    12  	"os"
    13  	"time"
    14  )
    15  
    16  var (
    17  	direct_rpc = flag.Bool("direct_rpc", false, "do not go through interceptors and go-easyops, open connection to service directly (with LB)")
    18  	cl         ge.EasyOpsTestClient
    19  )
    20  
    21  func initClient() {
    22  	if !*direct_rpc {
    23  		cl = ge.GetEasyOpsTestClient()
    24  		return
    25  	}
    26  	serviceName := "goeasyops.EasyOpsTest"
    27  	conn, err := grpc.Dial(
    28  		"go-easyops://"+serviceName+"/"+serviceName,
    29  		grpc.WithBlock(),
    30  		//		grpc.WithBalancerName("fancybalancer"),
    31  		grpc.WithTransportCredentials(client.GetClientCreds()),
    32  	)
    33  	utils.Bail("Failed to dial", err)
    34  	cl = ge.NewEasyOpsTestClient(conn)
    35  }
    36  func StartClient() {
    37  	initClient()
    38  xloop:
    39  	ctx := authremote.Context()
    40  	me := authremote.WhoAmI()
    41  	if me == nil {
    42  		fmt.Printf("authtest-client: pinging as 'nobody'. (new context did not provide a user)\n")
    43  	} else {
    44  		fmt.Printf("authtest-client: pinging as %s\n", me.Email)
    45  	}
    46  	r, err := cl.Ping(ctx, &ge.Chain{})
    47  	if err != nil {
    48  		fmt.Printf("failed to ping with standard token: %s\n", err)
    49  		if *loop {
    50  			time.Sleep(1 * time.Second)
    51  			goto xloop
    52  		}
    53  		os.Exit(10)
    54  
    55  	}
    56  	fmt.Printf("%d reports\n", len(r.Calls))
    57  	ft := "%5s | %10s | %10s | %s\n"
    58  	fmt.Printf(ft, "#", "reqid", "userid", "serviceid")
    59  	for _, c := range r.Calls {
    60  		user, err := authremote.GetUserByID(ctx, c.UserID)
    61  		utils.Bail("Failed to get user", err)
    62  		service, err := authremote.GetUserByID(ctx, c.ServiceID)
    63  		sn := c.ServiceID
    64  		if err == nil {
    65  			sn = auth.Description(service)
    66  		}
    67  		fmt.Printf(ft,
    68  			fmt.Sprintf("%d", c.Position),
    69  			c.RequestID,
    70  			auth.Description(user),
    71  			sn,
    72  		)
    73  	}
    74  	if *loop {
    75  		time.Sleep(time.Duration(300) * time.Millisecond)
    76  		goto xloop
    77  	}
    78  	fmt.Printf("OK\n")
    79  }
    80  

View as plain text