MCPcopy
hub / github.com/grpc/grpc-go / TestFileWatcherEnd2End

Method TestFileWatcherEnd2End

authz/grpc_authz_end2end_test.go:481–546  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

479}
480
481func (s) TestFileWatcherEnd2End(t *testing.T) {
482 for name, test := range authzTests {
483 t.Run(name, func(t *testing.T) {
484 file := createTmpPolicyFile(t, name, []byte(test.authzPolicy))
485 i, _ := authz.NewFileWatcher(file, 1*time.Second)
486 defer i.Close()
487
488 stub := &stubserver.StubServer{
489 UnaryCallF: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
490 return &testpb.SimpleResponse{}, nil
491 },
492 StreamingInputCallF: func(stream testgrpc.TestService_StreamingInputCallServer) error {
493 for {
494 _, err := stream.Recv()
495 if err == io.EOF {
496 return stream.SendAndClose(&testpb.StreamingInputCallResponse{})
497 }
498 if err != nil {
499 return err
500 }
501 }
502 },
503 // Start a gRPC server with gRPC authz unary and stream server interceptors.
504 S: grpc.NewServer(grpc.ChainUnaryInterceptor(i.UnaryInterceptor), grpc.ChainStreamInterceptor(i.StreamInterceptor)),
505 }
506 stubserver.StartTestService(t, stub)
507 defer stub.Stop()
508
509 // Establish a connection to the server.
510 cc, err := grpc.NewClient(stub.Address, grpc.WithTransportCredentials(insecure.NewCredentials()))
511 if err != nil {
512 t.Fatalf("grpc.NewClient(%v) failed: %v", stub.Address, err)
513 }
514 defer cc.Close()
515 client := testgrpc.NewTestServiceClient(cc)
516
517 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
518 defer cancel()
519 ctx = metadata.NewOutgoingContext(ctx, test.md)
520
521 // Verifying authorization decision for Unary RPC.
522 _, err = client.UnaryCall(ctx, &testpb.SimpleRequest{})
523 if got := status.Convert(err); got.Code() != test.wantStatus.Code() || got.Message() != test.wantStatus.Message() {
524 t.Fatalf("[UnaryCall] error want:{%v} got:{%v}", test.wantStatus.Err(), got.Err())
525 }
526
527 // Verifying authorization decision for Streaming RPC.
528 stream, err := client.StreamingInputCall(ctx)
529 if err != nil {
530 t.Fatalf("failed StreamingInputCall : %v", err)
531 }
532 req := &testpb.StreamingInputCallRequest{
533 Payload: &testpb.Payload{
534 Body: []byte("hi"),
535 },
536 }
537 if err := stream.Send(req); err != nil && err != io.EOF {
538 t.Fatalf("failed stream.Send : %v", err)

Callers

nothing calls this directly

Calls 15

StopMethod · 0.95
UnaryCallMethod · 0.95
StreamingInputCallMethod · 0.95
NewFileWatcherFunction · 0.92
NewServerFunction · 0.92
ChainUnaryInterceptorFunction · 0.92
ChainStreamInterceptorFunction · 0.92
StartTestServiceFunction · 0.92
NewClientFunction · 0.92
WithTransportCredentialsFunction · 0.92
NewCredentialsFunction · 0.92
NewOutgoingContextFunction · 0.92

Tested by

no test coverage detected