Refactor oom watcher to allow greater test coverage

This diff contains a strict refactor; there are no behavioral changes.

Address a long standing TODO in `oom_watcher_linux_test.go` around test
coverage. We refactor our `oom.Watcher` so it takes in a struct
fulfulling the `streamer` interface (i.e. defines `StreamOoms` method).
In production, we will continue to use the `oomparser` from `cadvisor`.
However, for testing purposes, we can now create our own `fakeStreamer`,
and control how it streams `oomparser.OomInstance`. With this fake, we
can implement richer unit testing for the `oom.Watcher` itself.

Actually adding the additional unit tests will come in a later commit.
This commit is contained in:
mattjmcnaughton
2019-12-29 09:26:35 -05:00
parent 886cf062a4
commit 8897c435ad
5 changed files with 43 additions and 19 deletions

View File

@@ -19,19 +19,26 @@ package oom
import (
"testing"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
"github.com/google/cadvisor/utils/oomparser"
"github.com/stretchr/testify/assert"
)
// TestBasic verifies that the OOMWatch works without error.
func TestBasic(t *testing.T) {
fakeRecorder := &record.FakeRecorder{}
node := &v1.ObjectReference{}
oomWatcher := NewWatcher(fakeRecorder)
assert.NoError(t, oomWatcher.Start(node))
// TODO: Improve this test once cadvisor exports events.EventChannel as an interface
// and thereby allow using a mock version of cadvisor.
// TODO: Substitute this `oomStreamer` out for a fake, and then write
// more comprehensive unit tests of the actual behavior.
oomStreamer, err := oomparser.New()
assert.NoError(t, err)
oomWatcher := &realWatcher{
recorder: fakeRecorder,
oomStreamer: oomStreamer,
}
assert.NoError(t, oomWatcher.Start(node))
}