Skip to content

Commit

Permalink
add TestPlanChange
Browse files Browse the repository at this point in the history
- fix bug removing old buffers
  • Loading branch information
barrettj12 committed Jul 26, 2023
1 parent c015637 commit c7a061a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 18 deletions.
7 changes: 0 additions & 7 deletions internals/overlord/logstate/gatherer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ import (
. "gopkg.in/check.v1"
)

// need to test
// - move over existing puller
// - create new puller for existing service
// - before svc -/-> tgt, now svc ---> tgt
// - remove old pullers
// - before svc ---> tgt, now svc -/-> tgt

type gathererSuite struct{}

var _ = Suite(&gathererSuite{})
Expand Down
2 changes: 1 addition & 1 deletion internals/overlord/logstate/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (m *LogManager) PlanChanged(pl *plan.Plan) {

// Remove old buffers
for svc := range m.buffers {
if _, ok := pl.Services[svc]; ok {
if _, ok := pl.Services[svc]; !ok {
// Service has been removed
delete(m.buffers, svc)
}
Expand Down
89 changes: 79 additions & 10 deletions internals/overlord/logstate/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,85 @@ type managerSuite struct{}

var _ = Suite(&managerSuite{})

func (s *managerSuite) TestPlanChange(c *C) {
m := NewLogManager()

svc1 := newTestService("svc1")
svc2 := newTestService("svc2")
svc3 := newTestService("svc3")

m.PlanChanged(&plan.Plan{
Services: map[string]*plan.Service{
svc1.name: svc1.config,
svc2.name: svc2.config,
svc3.name: svc3.config,
},
LogTargets: map[string]*plan.LogTarget{
"tgt1": {Name: "tgt1", Services: []string{"all", "-svc3"}},
"tgt2": {Name: "tgt2", Services: []string{}},
"tgt3": {Name: "tgt3", Services: []string{"all"}},
},
})
m.ServiceStarted(svc1.config, svc1.ringBuffer)
m.ServiceStarted(svc2.config, svc2.ringBuffer)
m.ServiceStarted(svc3.config, svc3.ringBuffer)

checkGatherers(c, m.gatherers, map[string][]string{
"tgt1": {"svc1", "svc2"},
"tgt2": {},
"tgt3": {"svc1", "svc2", "svc3"},
})
checkBuffers(c, m.buffers, []string{"svc1", "svc2", "svc3"})

svc4 := newTestService("svc4")

m.PlanChanged(&plan.Plan{
Services: map[string]*plan.Service{
svc1.name: svc1.config,
svc2.name: svc2.config,
svc4.name: svc4.config,
},
LogTargets: map[string]*plan.LogTarget{
"tgt1": {Name: "tgt1", Services: []string{"svc1"}},
"tgt2": {Name: "tgt2", Services: []string{"svc1", "svc4"}},
"tgt4": {Name: "tgt4", Services: []string{"all", "-svc2"}},
},
})
m.ServiceStarted(svc4.config, svc4.ringBuffer)
// simulate service restart for svc2
m.ServiceStarted(svc2.config, svc2.ringBuffer)

checkGatherers(c, m.gatherers, map[string][]string{
"tgt1": {"svc1"},
"tgt2": {"svc1", "svc4"},
"tgt4": {"svc1", "svc4"},
})
// svc3 no longer exists so we should have dropped the reference to its buffer
checkBuffers(c, m.buffers, []string{"svc1", "svc2", "svc4"})
}

func checkGatherers(c *C, gatherers map[string]*logGatherer, expected map[string][]string) {
c.Assert(gatherers, HasLen, len(expected))
for tgtName, svcs := range expected {
g, ok := gatherers[tgtName]
c.Assert(ok, Equals, true)

c.Assert(g.pullers, HasLen, len(svcs))
for _, svc := range svcs {
_, ok := g.pullers[svc]
c.Check(ok, Equals, true)
}
}
}

func checkBuffers(c *C, buffers map[string]*servicelog.RingBuffer, expected []string) {
c.Assert(buffers, HasLen, len(expected))
for _, svcName := range expected {
_, ok := buffers[svcName]
c.Check(ok, Equals, true)
}
}

// TODO: why is this test intermittently failing?
func (s *managerSuite) TestTimelyShutdown(c *C) {
teardownTimeout = 2 * time.Millisecond
Expand Down Expand Up @@ -60,16 +139,6 @@ func (s *managerSuite) TestTimelyShutdown(c *C) {
}
}

// need to test
// - copy over existing gatherer
// - removing old gatherers/buffers
// - service restart
//

// services added/removed
// targets added/removed
// svc->tgt added/removed

type slowFlushingClient struct {
flushTime time.Duration
}
Expand Down

0 comments on commit c7a061a

Please sign in to comment.