From 27ed015d0385b7629dedeed4eac2fb1a7962b8a2 Mon Sep 17 00:00:00 2001 From: Jordan Barrett Date: Mon, 3 Jul 2023 17:30:35 +1200 Subject: [PATCH] fix panic in manager tests --- internals/overlord/logstate/gatherer_test.go | 9 ++++++-- internals/overlord/logstate/manager_test.go | 24 ++++++++++++-------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/internals/overlord/logstate/gatherer_test.go b/internals/overlord/logstate/gatherer_test.go index ef452a13e..6ca43252e 100644 --- a/internals/overlord/logstate/gatherer_test.go +++ b/internals/overlord/logstate/gatherer_test.go @@ -18,6 +18,7 @@ import ( "io" "time" + "github.com/canonical/pebble/internals/plan" "gopkg.in/yaml.v3" "github.com/canonical/pebble/internals/servicelog" @@ -31,7 +32,7 @@ var _ = Suite(&gathererSuite{}) func (s *gathererSuite) TestGathererTimeout(c *C) { recv := make(chan []servicelog.Entry) - g := newLogGathererForTest(1*time.Microsecond, 2, recv) + g := newLogGathererForTest(nil, 1*time.Microsecond, 2, recv) go g.loop() entry := servicelog.Entry{ @@ -50,8 +51,12 @@ func (s *gathererSuite) TestGathererTimeout(c *C) { } } -func newLogGathererForTest(tickPeriod time.Duration, bufferCapacity int, recv chan []servicelog.Entry) *logGatherer { +func newLogGathererForTest( + target *plan.LogTarget, + tickPeriod time.Duration, bufferCapacity int, recv chan []servicelog.Entry, +) *logGatherer { return &logGatherer{ + target: target, tickPeriod: tickPeriod, buffer: &testBuffer{ capacity: bufferCapacity, diff --git a/internals/overlord/logstate/manager_test.go b/internals/overlord/logstate/manager_test.go index 884fecb72..4aa34bc3a 100644 --- a/internals/overlord/logstate/manager_test.go +++ b/internals/overlord/logstate/manager_test.go @@ -16,6 +16,7 @@ package logstate import ( "bytes" "sync" + "time" "github.com/canonical/pebble/internals/logger" "github.com/canonical/pebble/internals/plan" @@ -40,7 +41,7 @@ func (s *managerSuite) TearDownTest(c *C) { } func (s *managerSuite) TestLogManager(c *C) { - m := newLogManagerForTest() + m := newLogManagerForTest(1*time.Second, 10, make(chan []servicelog.Entry)) // Fake ringbuffer so that log manager can create forwarders rb := servicelog.RingBuffer{} @@ -207,19 +208,22 @@ func checkGatherers(c *C, gatherers map[string]*logGatherer, expected []string) //} func (s *managerSuite) TestFlushLogsOnInterrupt(c *C) { - m := newLogManagerForTest() + m := newLogManagerForTest(1*time.Second, 10, make(chan []servicelog.Entry)) m.Stop() // check buffered logs are sent through } -func newLogManagerForTest() *LogManager { - return NewLogManager() - //return &LogManager{ - // forwarders: map[string]*logForwarder{}, - // gatherers: map[string]*logGatherer{}, - // newForwarder: newLogForwarderForTest, - // newGatherer: newLogGathererForTest, - //} +func newLogManagerForTest( + tickPeriod time.Duration, bufferCapacity int, recv chan []servicelog.Entry, +) *LogManager { + return &LogManager{ + forwarders: map[string]*logForwarder{}, + gatherers: map[string]*logGatherer{}, + newForwarder: newLogForwarder, // ForTest ? + newGatherer: func(target *plan.LogTarget) *logGatherer { + return newLogGathererForTest(target, tickPeriod, bufferCapacity, recv) + }, + } }