Skip to content

Commit

Permalink
Fix flaky TestServiceLogs due to TestCurrentUserGroup reusing manager (
Browse files Browse the repository at this point in the history
…#242)

This was causing intermittent failures in TestServiceLogs (on the order
of 50% of the time), see test failure output below.

START: manager_test.go:456: S.TestServiceLogs
START: manager_test.go:147: S.SetUpTest
PASS: manager_test.go:147: S.SetUpTest	0.000s

2023-06-19T11:13:02.294Z [test] Service "test1" starting: /bin/sh -c "echo test1 | tee -a /tmp/check-270658404/27/log.txt; sleep 10"
2023-06-19T11:13:02.346Z [test] Service "test2" starting: /bin/sh -c "echo test2 | tee -a /tmp/check-270658404/27/log.txt; sleep 10"
2023-06-19T11:13:02.368Z [test] Service "usrtest" starting: /bin/sh -c "id -n -u | tee -a /tmp/check-270658404/8/log.txt; sleep 0.060000000000000005"
manager_test.go:461:
    s.testServiceLogs(c, outputs)
manager_test.go:204:
    c.Assert(s.logBuffer.String(), Matches, "(?s)"+expected)
... value string = "" +
...     "2023-06-19T11:13:02.300Z [test1] test1\n" +
...     "2023-06-19T11:13:02.348Z [test2] test2\n" +
...     "2023-06-19T11:13:02.372Z [usrtest] runner\n"
... regex string = "" +
...     "(?s).*test1\n" +
...     ".*test2\n"

START: manager_test.go:183: S.TearDownTest
PASS: manager_test.go:183: S.TearDownTest	0.000s

FAIL: manager_test.go:456: S.TestServiceLogs
  • Loading branch information
benhoyt authored Jun 21, 2023
1 parent b7a843b commit 8902cbe
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions internals/overlord/servstate/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,33 +506,40 @@ func (s *S) TestStartBadCommand(c *C) {
}

func (s *S) TestCurrentUserGroup(c *C) {
// Don't re-use s.manager, because we're adding a layer and service, and
// that would conflict with other tests like TestServiceLogs.
dir := c.MkDir()
err := os.Mkdir(filepath.Join(dir, "layers"), 0755)
c.Assert(err, IsNil)
manager, err := servstate.NewManager(s.st, s.runner, dir, nil, nil, fakeLogManager{})
c.Assert(err, IsNil)
defer manager.Stop()

current, err := user.Current()
c.Assert(err, IsNil)
group, err := user.LookupGroupId(current.Gid)
c.Assert(err, IsNil)

layer := parseLayer(c, 0, "layer99", fmt.Sprintf(`
outputPath := filepath.Join(dir, "output")
layer := parseLayer(c, 0, "layer", fmt.Sprintf(`
services:
usrtest:
override: replace
command: /bin/sh -c "id -n -u | tee -a %s; sleep %g"
command: /bin/sh -c "id -n -u >%s; sleep %g"
user: %s
group: %s
`, s.log, shortOkayDelay.Seconds()+0.01, current.Username, group.Name))
err = s.manager.AppendLayer(layer)
c.Assert(err, IsNil)
_, _, err = s.manager.Replan()
`, outputPath, shortOkayDelay.Seconds()+0.01, current.Username, group.Name))
err = manager.AppendLayer(layer)
c.Assert(err, IsNil)

chg := s.startServices(c, []string{"usrtest"}, 1)
s.st.Lock()
c.Assert(chg.Err(), IsNil)
s.st.Unlock()

s.waitUntilService(c, "usrtest", func(service *servstate.ServiceInfo) bool {
return service.Current == servstate.StatusBackoff
})
s.assertLog(c, ".*"+current.Username+"\n")
output, err := ioutil.ReadFile(outputPath)
c.Assert(err, IsNil)
c.Check(string(output), Equals, current.Username+"\n")
}

func (s *S) TestUserGroupFails(c *C) {
Expand Down

0 comments on commit 8902cbe

Please sign in to comment.