Skip to content

Commit 889d9ef

Browse files
committed
Add unit test for goroutine leak.
1 parent 6170b0c commit 889d9ef

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

pkg/systemlogmonitor/log_monitor_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ limitations under the License.
1717
package systemlogmonitor
1818

1919
import (
20+
"fmt"
2021
"reflect"
22+
"runtime"
2123
"testing"
2224
"time"
2325

26+
"github.com/stretchr/testify/assert"
27+
28+
watchertest "k8s.io/node-problem-detector/pkg/systemlogmonitor/logwatchers/testing"
2429
logtypes "k8s.io/node-problem-detector/pkg/systemlogmonitor/types"
2530
"k8s.io/node-problem-detector/pkg/types"
2631
)
@@ -131,3 +136,13 @@ func TestGenerateStatus(t *testing.T) {
131136
}
132137
}
133138
}
139+
140+
func TestGoroutineLeak(t *testing.T) {
141+
orignal := runtime.NumGoroutine()
142+
f := watchertest.NewFakeLogWatcher(10)
143+
f.InjectError(fmt.Errorf("unexpected error"))
144+
l := &logMonitor{watcher: f}
145+
_, err := l.Start()
146+
assert.Error(t, err)
147+
assert.Equal(t, orignal, runtime.NumGoroutine())
148+
}

pkg/systemlogmonitor/logwatchers/filelog/log_watcher_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package filelog
1919
import (
2020
"io/ioutil"
2121
"os"
22+
"runtime"
2223
"testing"
2324
"time"
2425

@@ -170,3 +171,16 @@ Jan 2 03:04:05 kernel: [2.000000] 3
170171
}
171172
}
172173
}
174+
175+
func TestGoroutineLeak(t *testing.T) {
176+
orignal := runtime.NumGoroutine()
177+
w := NewSyslogWatcherOrDie(types.WatcherConfig{
178+
Plugin: "filelog",
179+
PluginConfig: getTestPluginConfig(),
180+
LogPath: "/not/exist/path",
181+
Lookback: "10m",
182+
})
183+
_, err := w.Watch()
184+
assert.Error(t, err)
185+
assert.Equal(t, orignal, runtime.NumGoroutine())
186+
}

pkg/systemlogmonitor/logwatchers/journald/log_watcher_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ limitations under the License.
1919
package journald
2020

2121
import (
22+
"runtime"
2223
"testing"
2324
"time"
2425

2526
"github.com/coreos/go-systemd/sdjournal"
2627
"github.com/stretchr/testify/assert"
2728

29+
"k8s.io/node-problem-detector/pkg/systemlogmonitor/logwatchers/types"
2830
logtypes "k8s.io/node-problem-detector/pkg/systemlogmonitor/types"
2931
)
3032

@@ -62,3 +64,16 @@ func TestTranslate(t *testing.T) {
6264
assert.Equal(t, test.log, translate(test.entry))
6365
}
6466
}
67+
68+
func TestGoroutineLeak(t *testing.T) {
69+
orignal := runtime.NumGoroutine()
70+
w := NewJournaldWatcher(types.WatcherConfig{
71+
Plugin: "journald",
72+
PluginConfig: map[string]string{"source": "not-exist-service"},
73+
LogPath: "/not/exist/path",
74+
Lookback: "10m",
75+
})
76+
_, err := w.Watch()
77+
assert.Error(t, err)
78+
assert.Equal(t, orignal, runtime.NumGoroutine())
79+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package testing
18+
19+
import (
20+
"sync"
21+
22+
"k8s.io/node-problem-detector/pkg/systemlogmonitor/logwatchers/types"
23+
logtypes "k8s.io/node-problem-detector/pkg/systemlogmonitor/types"
24+
)
25+
26+
// FakeLogWatcher is a fake mock of log watcher.
27+
type FakeLogWatcher struct {
28+
sync.Mutex
29+
buf chan *logtypes.Log
30+
err error
31+
}
32+
33+
var _ types.LogWatcher = &FakeLogWatcher{}
34+
35+
func NewFakeLogWatcher(bufferSize int) *FakeLogWatcher {
36+
return &FakeLogWatcher{buf: make(chan *logtypes.Log, bufferSize)}
37+
}
38+
39+
// InjectLog injects a fake log into the watch channel
40+
func (f *FakeLogWatcher) InjectLog(log *logtypes.Log) {
41+
f.buf <- log
42+
}
43+
44+
// InjectError injects an error of Watch function.
45+
func (f *FakeLogWatcher) InjectError(err error) {
46+
f.Lock()
47+
defer f.Unlock()
48+
f.err = err
49+
}
50+
51+
// Watch is the fake watch function.
52+
func (f *FakeLogWatcher) Watch() (<-chan *logtypes.Log, error) {
53+
return f.buf, f.err
54+
}
55+
56+
// Stop is the fake stop function.
57+
func (f *FakeLogWatcher) Stop() {
58+
close(f.buf)
59+
}

0 commit comments

Comments
 (0)