Skip to content

Commit

Permalink
daemon: pass a function when testing reboot calls
Browse files Browse the repository at this point in the history
Instead of capturing the arguments. This allows more flexibility on
how to perform the tests.
  • Loading branch information
alfonsosanchezbeato committed Feb 18, 2022
1 parent edcc27d commit abf677a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 42 deletions.
22 changes: 14 additions & 8 deletions daemon/api_systems_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,16 @@ func (s *systemsSuite) TestSystemActionRequestWithSeeded(c *check.C) {
bootloader.Force(bt)
defer func() { bootloader.Force(nil) }()

var rebootAction boot.RebootAction
var durationUntilReboot time.Duration
var rebootInfo *boot.RebootInfo
r := daemon.MockRebootAndCaptureArgs(&rebootAction, &durationUntilReboot, rebootInfo)
nRebootCall := 0
rebootCheck := func(ra boot.RebootAction, d time.Duration, ri *boot.RebootInfo) error {
nRebootCall++
// slow reboot schedule
c.Check(ra, check.Equals, boot.RebootReboot)
c.Check(d, check.Equals, 10*time.Minute)
c.Check(ri, check.IsNil)
return nil
}
r := daemon.MockReboot(rebootCheck)
defer r()

restore := s.mockSystemSeeds(c)
Expand Down Expand Up @@ -356,6 +362,7 @@ func (s *systemsSuite) TestSystemActionRequestWithSeeded(c *check.C) {
"seed-time": "2009-11-10T23:00:00Z",
}}

numExpRestart := 0
tt := []struct {
currentMode string
actionMode string
Expand Down Expand Up @@ -508,11 +515,8 @@ func (s *systemsSuite) TestSystemActionRequestWithSeeded(c *check.C) {
// daemon is not started, only check whether reboot was scheduled as expected

// reboot flag
numExpRestart++
c.Check(d.RequestedRestart(), check.Equals, restart.RestartSystemNow, check.Commentf(tc.comment))
// slow reboot schedule
c.Check(rebootAction, check.Equals, boot.RebootReboot)
c.Check(durationUntilReboot, check.Equals, 10*time.Minute)
c.Check(rebootInfo, check.IsNil)
}
}

Expand All @@ -521,6 +525,8 @@ func (s *systemsSuite) TestSystemActionRequestWithSeeded(c *check.C) {
s.resetDaemon()
}

// we must have called reboot numExpRestart times
c.Check(nRebootCall, check.Equals, numExpRestart)
}

func (s *systemsSuite) TestSystemActionBrokenSeed(c *check.C) {
Expand Down
72 changes: 48 additions & 24 deletions daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1127,10 +1127,22 @@ func (s *daemonSuite) TestRestartShutdownWithSigtermInBetween(c *check.C) {
}()
rebootNoticeWait = 150 * time.Millisecond

var rebootAction boot.RebootAction
var durationUntilReboot time.Duration
var rebootInfo *boot.RebootInfo
r := MockRebootAndCaptureArgs(&rebootAction, &durationUntilReboot, rebootInfo)
nRebootCall := 0
rebootCheck := func(ra boot.RebootAction, d time.Duration, ri *boot.RebootInfo) error {
// Check arguments passed to reboot call
nRebootCall++
c.Check(ra, check.Equals, boot.RebootReboot)
switch nRebootCall {
case 1:
c.Check(d, check.Equals, 10*time.Minute)
c.Check(ri, check.IsNil)
default:
c.Check(d, check.Equals, 1*time.Minute)
c.Check(ri, check.DeepEquals, &boot.RebootInfo{})
}
return nil
}
r := MockReboot(rebootCheck)
defer r()

d := newTestDaemon(c)
Expand All @@ -1150,10 +1162,8 @@ func (s *daemonSuite) TestRestartShutdownWithSigtermInBetween(c *check.C) {
err := d.Stop(ch)
c.Assert(err, check.IsNil)

// Check arguments passed to reboot call
c.Check(rebootAction, check.Equals, boot.RebootReboot)
c.Check(durationUntilReboot, check.Equals, 1*time.Minute)
c.Check(rebootInfo, check.IsNil)
// we must have called reboot twice
c.Check(nRebootCall, check.Equals, 2)
}

// This test tests that when there is a shutdown we close the sigterm
Expand All @@ -1168,10 +1178,22 @@ func (s *daemonSuite) TestRestartShutdown(c *check.C) {
rebootWaitTimeout = 100 * time.Millisecond
rebootNoticeWait = 150 * time.Millisecond

var rebootAction boot.RebootAction
var durationUntilReboot time.Duration
var rebootInfo *boot.RebootInfo
r := MockRebootAndCaptureArgs(&rebootAction, &durationUntilReboot, rebootInfo)
nRebootCall := 0
rebootCheck := func(ra boot.RebootAction, d time.Duration, ri *boot.RebootInfo) error {
// Check arguments passed to reboot call
nRebootCall++
c.Check(ra, check.Equals, boot.RebootReboot)
switch nRebootCall {
case 1:
c.Check(d, check.Equals, 100*time.Millisecond)
c.Check(ri, check.IsNil)
default:
c.Check(d, check.Equals, 1*time.Minute)
c.Check(ri, check.DeepEquals, &boot.RebootInfo{})
}
return nil
}
r := MockReboot(rebootCheck)
defer r()

d := newTestDaemon(c)
Expand All @@ -1193,10 +1215,8 @@ func (s *daemonSuite) TestRestartShutdown(c *check.C) {
_, chOpen := <-sigCh
c.Assert(chOpen, check.Equals, false)

// Check arguments passed to reboot call
c.Check(rebootAction, check.Equals, boot.RebootReboot)
c.Check(durationUntilReboot, check.Equals, 1*time.Minute)
c.Check(rebootInfo, check.IsNil)
// we must have called reboot twice
c.Check(nRebootCall, check.Equals, 2)
}

func (s *daemonSuite) TestRestartExpectedRebootDidNotHappen(c *check.C) {
Expand All @@ -1216,10 +1236,16 @@ func (s *daemonSuite) TestRestartExpectedRebootDidNotHappen(c *check.C) {
rebootRetryWaitTimeout = 100 * time.Millisecond
rebootNoticeWait = 150 * time.Millisecond

var rebootAction boot.RebootAction
var durationUntilReboot time.Duration
var rebootInfo *boot.RebootInfo
r := MockRebootAndCaptureArgs(&rebootAction, &durationUntilReboot, rebootInfo)
nRebootCall := 0
rebootCheck := func(ra boot.RebootAction, d time.Duration, ri *boot.RebootInfo) error {
nRebootCall++
// an immediate shutdown was scheduled again
c.Check(ra, check.Equals, boot.RebootReboot)
c.Check(d <= 0, check.Equals, true)
c.Check(ri, check.IsNil)
return nil
}
r := MockReboot(rebootCheck)
defer r()

d := newTestDaemon(c)
Expand Down Expand Up @@ -1247,10 +1273,8 @@ func (s *daemonSuite) TestRestartExpectedRebootDidNotHappen(c *check.C) {
// stop (this will timeout but thats not relevant for this test)
d.Stop(sigCh)

// an immediate shutdown was scheduled again
c.Check(rebootAction, check.Equals, boot.RebootReboot)
c.Check(durationUntilReboot <= 0, check.Equals, true)
c.Check(rebootInfo, check.IsNil)
// we must have called reboot once
c.Check(nRebootCall, check.Equals, 1)
}

func (s *daemonSuite) TestRestartExpectedRebootOK(c *check.C) {
Expand Down
12 changes: 2 additions & 10 deletions daemon/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,8 @@ func MockSnapstateInstallPathMany(f func(context.Context, *state.State, []*snap.
}
}

func MockRebootAndCaptureArgs(rebootAction *boot.RebootAction,
durationUntilReboot *time.Duration, rebootInfo *boot.RebootInfo) func() {

mockReboot := func(ra boot.RebootAction, d time.Duration, ri *boot.RebootInfo) error {
*rebootAction = ra
*durationUntilReboot = d
rebootInfo = ri
return nil
}
reboot = mockReboot
func MockReboot(f func(boot.RebootAction, time.Duration, *boot.RebootInfo) error) func() {
reboot = f
return func() { reboot = boot.Reboot }
}

Expand Down

0 comments on commit abf677a

Please sign in to comment.