@@ -17,6 +17,12 @@ limitations under the License.
17
17
package csicommon
18
18
19
19
import (
20
+ "bytes"
21
+ "context"
22
+ "flag"
23
+ "github.com/container-storage-interface/spec/lib/go/csi"
24
+ "google.golang.org/grpc"
25
+ "k8s.io/klog"
20
26
"testing"
21
27
22
28
"github.com/stretchr/testify/assert"
@@ -74,3 +80,68 @@ func TestParseEndpoint(t *testing.T) {
74
80
_ , _ , err = ParseEndpoint ("" )
75
81
assert .NotNil (t , err )
76
82
}
83
+
84
+ func TestLogGRPC (t * testing.T ) {
85
+ // SET UP
86
+ klog .InitFlags (nil )
87
+ if e := flag .Set ("logtostderr" , "false" ); e != nil {
88
+ t .Error (e )
89
+ }
90
+ if e := flag .Set ("alsologtostderr" , "false" ); e != nil {
91
+ t .Error (e )
92
+ }
93
+ if e := flag .Set ("v" , "100" ); e != nil {
94
+ t .Error (e )
95
+ }
96
+ flag .Parse ()
97
+
98
+ buf := new (bytes.Buffer )
99
+ klog .SetOutput (buf )
100
+
101
+ handler := func (ctx context.Context , req interface {}) (interface {}, error ) { return nil , nil }
102
+ info := grpc.UnaryServerInfo {
103
+ FullMethod : "fake" ,
104
+ }
105
+
106
+ tests := []struct {
107
+ name string
108
+ req interface {}
109
+ expStr string
110
+ }{
111
+ {
112
+ "with secrets" ,
113
+ & csi.NodeStageVolumeRequest {
114
+ VolumeId : "vol_1" ,
115
+ Secrets : map [string ]string {
116
+ "account_name" : "k8s" ,
117
+ "account_key" : "testkey" ,
118
+ },
119
+ XXX_sizecache : 100 ,
120
+ },
121
+ `GRPC request: volume_id:"vol_1" secrets:<key:"account_key" value:"****" > secrets:<key:"account_name" value:"****" >` ,
122
+ },
123
+ {
124
+ "without secrets" ,
125
+ & csi.ListSnapshotsRequest {
126
+ StartingToken : "testtoken" ,
127
+ },
128
+ `GRPC request: starting_token:"testtoken"` ,
129
+ },
130
+ }
131
+
132
+ for _ , test := range tests {
133
+ t .Run (test .name , func (t * testing.T ) {
134
+ // EXECUTE
135
+ _ , _ = logGRPC (context .Background (), test .req , & info , handler )
136
+ klog .Flush ()
137
+
138
+ // ASSERT
139
+ assert .Contains (t , buf .String (), "GRPC call: fake" )
140
+ assert .Contains (t , buf .String (), test .expStr )
141
+ assert .Contains (t , buf .String (), "GRPC response: <nil>" )
142
+
143
+ // CLEANUP
144
+ buf .Reset ()
145
+ })
146
+ }
147
+ }
0 commit comments