From 2358b5ac9666828fe77ab96c991f7fd602c877f0 Mon Sep 17 00:00:00 2001 From: Manohar Reddy Date: Thu, 4 Feb 2021 19:30:39 +0530 Subject: [PATCH] add test case for nfs/utils.go:logGRPC function --- pkg/nfs/utils_test.go | 64 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/pkg/nfs/utils_test.go b/pkg/nfs/utils_test.go index 1177373d..1c4e96c9 100644 --- a/pkg/nfs/utils_test.go +++ b/pkg/nfs/utils_test.go @@ -17,8 +17,16 @@ limitations under the License. package nfs import ( + "bytes" + "flag" "fmt" "testing" + + "github.com/container-storage-interface/spec/lib/go/csi" + "github.com/stretchr/testify/assert" + "golang.org/x/net/context" + "google.golang.org/grpc" + "k8s.io/klog/v2" ) var ( @@ -118,3 +126,59 @@ func TestGetLogLevel(t *testing.T) { } } } + +func TestLogGRPC(t *testing.T) { + // SET UP + klog.InitFlags(nil) + if e := flag.Set("logtostderr", "false"); e != nil { + t.Error(e) + } + if e := flag.Set("alsologtostderr", "false"); e != nil { + t.Error(e) + } + if e := flag.Set("v", "100"); e != nil { + t.Error(e) + } + flag.Parse() + + buf := new(bytes.Buffer) + klog.SetOutput(buf) + + handler := func(ctx context.Context, req interface{}) (interface{}, error) { return nil, nil } + info := grpc.UnaryServerInfo{ + FullMethod: "fake", + } + + tests := []struct { + name string + req interface{} + expStr string + }{ + { + "with secrets", + &csi.NodeStageVolumeRequest{ + VolumeId: "vol_1", + Secrets: map[string]string{ + "account_name": "k8s", + "account_key": "testkey", + }, + XXX_sizecache: 100, + }, + `GRPC request: {"secrets":"***stripped***","volume_id":"vol_1"}`, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + // EXECUTE + _, _ = logGRPC(context.Background(), test.req, &info, handler) + klog.Flush() + // ASSERT + assert.Contains(t, buf.String(), "GRPC call: fake") + assert.Contains(t, buf.String(), test.expStr) + assert.Contains(t, buf.String(), "GRPC response: null") + // CLEANUP + buf.Reset() + }) + } +}