github.com/argoproj/argo-cd/v3@v3.2.1/util/grpc/sanitizer_test.go (about)

     1  package grpc
     2  
     3  import (
     4  	"context"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  	"google.golang.org/grpc/codes"
    11  	"google.golang.org/grpc/status"
    12  )
    13  
    14  func TestSanitizer(t *testing.T) {
    15  	s := NewSanitizer()
    16  
    17  	ctx := ContextWithSanitizer(t.Context(), s)
    18  
    19  	sanitizer, ok := SanitizerFromContext(ctx)
    20  	require.True(t, ok)
    21  	sanitizer.AddReplacement("/my-random/path", ".")
    22  
    23  	res := s.Replace("error at /my-random/path/sub-dir: something went wrong")
    24  	assert.Equal(t, "error at ./sub-dir: something went wrong", res)
    25  }
    26  
    27  func TestSanitizer_RegexReplacement(t *testing.T) {
    28  	s := NewSanitizer()
    29  
    30  	ctx := ContextWithSanitizer(t.Context(), s)
    31  
    32  	sanitizer, ok := SanitizerFromContext(ctx)
    33  	require.True(t, ok)
    34  
    35  	sanitizer.AddRegexReplacement(regexp.MustCompile("(/my-random/path)"), ".")
    36  	res := s.Replace("error at /my-random/path/something: something went wrong")
    37  	assert.Equal(t, "error at ./something: something went wrong", res)
    38  }
    39  
    40  func TestErrorSanitizerUnaryServerInterceptor(t *testing.T) {
    41  	interceptor := ErrorSanitizerUnaryServerInterceptor()
    42  
    43  	_, err := interceptor(t.Context(), nil, nil, func(ctx context.Context, _ any) (any, error) {
    44  		sanitizer, ok := SanitizerFromContext(ctx)
    45  		require.True(t, ok)
    46  		sanitizer.AddReplacement("/my-random/path", ".")
    47  		return nil, status.Error(codes.Internal, "error at /my-random/path/sub-dir: something went wrong")
    48  	})
    49  
    50  	assert.EqualError(t, err, "rpc error: code = Internal desc = error at ./sub-dir: something went wrong")
    51  }