add test case for nfs/utils.go:logGRPC function

This commit is contained in:
Manohar Reddy 2021-02-04 19:30:39 +05:30
parent ab42e03317
commit 2358b5ac96
1 changed files with 64 additions and 0 deletions

View File

@ -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()
})
}
}