Skip to content

Commit 85d8bc0

Browse files
committed
fix: avoid race caused by klog.InitFlags in utils_test
Without this fix, running `go test -race -count 10` causes this failure: /var/folders/py/rwp4m4k977l9stj_f2kqrmxw000_5c/T/go-build3764188957/b001/csi-common.test flag redefined: log_dir --- FAIL: TestLogGRPC (0.00s) panic: /var/folders/py/rwp4m4k977l9stj_f2kqrmxw000_5c/T/go-build3764188957/b001/csi-common.test flag redefined: log_dir [recovered] panic: /var/folders/py/rwp4m4k977l9stj_f2kqrmxw000_5c/T/go-build3764188957/b001/csi-common.test flag redefined: log_dir goroutine 70 [running]: testing.tRunner.func1.2({0x17a37e0, 0xc0000b4180}) /usr/local/Cellar/go/1.18.5/libexec/src/testing/testing.go:1389 +0x366 testing.tRunner.func1() /usr/local/Cellar/go/1.18.5/libexec/src/testing/testing.go:1392 +0x5d2 panic({0x17a37e0, 0xc0000b4180}) /usr/local/Cellar/go/1.18.5/libexec/src/runtime/panic.go:844 +0x258 flag.(*FlagSet).Var(0xc00020c120, {0x19668c0, 0x1d00f90}, {0x186cfd4, 0x7}, {0x188d879, 0x52}) /usr/local/Cellar/go/1.18.5/libexec/src/flag/flag.go:879 +0x3a5 flag.(*FlagSet).StringVar(...) /usr/local/Cellar/go/1.18.5/libexec/src/flag/flag.go:762 k8s.io/klog/v2.InitFlags(0x0) /Users/avoltz/src/blob-csi-driver/vendor/k8s.io/klog/v2/klog.go:424 +0xee sigs.k8s.io/blob-csi-driver/pkg/csi-common.TestLogGRPC(0xc0000829c0) /Users/avoltz/src/blob-csi-driver/pkg/csi-common/utils_test.go:87 +0x45 testing.tRunner(0xc0000829c0, 0x18ae070) /usr/local/Cellar/go/1.18.5/libexec/src/testing/testing.go:1439 +0x214 created by testing.(*T).Run /usr/local/Cellar/go/1.18.5/libexec/src/testing/testing.go:1486 +0x725 FAIL sigs.k8s.io/blob-csi-driver/pkg/csi-common 4.298s FAIL
1 parent 9f5e5b5 commit 85d8bc0

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

pkg/csi-common/utils_test.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"bytes"
2121
"context"
2222
"flag"
23+
"io"
24+
"os"
2325
"testing"
2426

2527
"github.com/container-storage-interface/spec/lib/go/csi"
@@ -83,21 +85,9 @@ func TestParseEndpoint(t *testing.T) {
8385
}
8486

8587
func TestLogGRPC(t *testing.T) {
86-
// SET UP
87-
klog.InitFlags(nil)
88-
if e := flag.Set("logtostderr", "false"); e != nil {
89-
t.Error(e)
90-
}
91-
if e := flag.Set("alsologtostderr", "false"); e != nil {
92-
t.Error(e)
93-
}
94-
if e := flag.Set("v", "100"); e != nil {
95-
t.Error(e)
96-
}
97-
flag.Parse()
98-
9988
buf := new(bytes.Buffer)
10089
klog.SetOutput(buf)
90+
defer klog.SetOutput(io.Discard)
10191

10292
handler := func(ctx context.Context, req interface{}) (interface{}, error) { return nil, nil }
10393
info := grpc.UnaryServerInfo{
@@ -232,3 +222,12 @@ func TestGetLogLevel(t *testing.T) {
232222
}
233223
}
234224
}
225+
226+
func TestMain(m *testing.M) {
227+
klog.InitFlags(nil)
228+
_ = flag.Set("logtostderr", "false")
229+
_ = flag.Set("alsologtostderr", "false")
230+
_ = flag.Set("v", "100")
231+
klog.SetOutput(io.Discard)
232+
os.Exit(m.Run())
233+
}

0 commit comments

Comments
 (0)