Skip to content

Commit

Permalink
tests: mock systemctl output for listing mount units
Browse files Browse the repository at this point in the history
  • Loading branch information
mardy committed Sep 10, 2021
1 parent ff307c1 commit 31cae3c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions overlord/managers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ func (s *baseMgrsSuite) SetUpTest(c *C) {
if out := systemdtest.HandleMockAllUnitsActiveOutput(cmd, nil); out != nil {
return out, nil
}
if out, ok := systemdtest.HandleMockListMountUnitsOutput(cmd, nil); ok {
return out, nil
}
return []byte("ActiveState=inactive\n"), nil
})
s.AddCleanup(r)
Expand Down
31 changes: 31 additions & 0 deletions systemd/systemdtest/systemdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,34 @@ Type=simple
}
return output
}

type MountUnitInfo struct {
Description string
Where string
FragmentPath string
}

// HandleMockListMountUnitsOutput returns the output for systemctl in the case
// where units have the state as described by states.
// If `cmd` is the command issued by systemd.Status(), this function returns
// the output to be produced by the command so that the queried services will
// appear having the ActiveState and UnitFileState according to the data
// passed in the `states` map.
func HandleMockListMountUnitsOutput(cmd []string, mounts []MountUnitInfo) ([]byte, bool) {
osutil.MustBeTestBinary("mocking systemctl output can only be done from tests")
if cmd[0] != "show" ||
cmd[1] != "--property=Description,Where,FragmentPath" {
return nil, false
}
var output []byte
for _, mountInfo := range mounts {
if len(output) > 0 {
output = append(output, byte('\n'))
}
output = append(output, []byte(fmt.Sprintf(`Description=%s
Where=%s
FragmentPath=%s
`, mountInfo.Description, mountInfo.Where, mountInfo.FragmentPath))...)
}
return output, true
}

0 comments on commit 31cae3c

Please sign in to comment.