Skip to content

Commit

Permalink
many: rename systemd.Kind to Backend for a bit more clarity (#10788)
Browse files Browse the repository at this point in the history
add some doc comments
adjust/simplify tests using MockNewSystemd
  • Loading branch information
pedronis authored Sep 24, 2021
1 parent f2cacda commit 4570677
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 30 deletions.
24 changes: 10 additions & 14 deletions overlord/snapstate/backend/mountunit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ type ResultForAddMountUnitFile struct {
type FakeSystemd struct {
systemd.Systemd

Kind systemd.Kind
Mode systemd.InstanceMode
Meter systemd.Reporter

AddMountUnitFileCalls []ParamsForAddMountUnitFile
AddMountUnitFileResult ResultForAddMountUnitFile

Expand Down Expand Up @@ -103,8 +99,8 @@ func (s *mountunitSuite) TestAddMountUnit(c *C) {
expectedErr := errors.New("creation error")

var sysd *FakeSystemd
restore := systemd.MockNewSystemd(func(kind systemd.Kind, roodDir string, mode systemd.InstanceMode, meter systemd.Reporter) systemd.Systemd {
sysd = &FakeSystemd{Kind: kind, Mode: mode, Meter: meter}
restore := systemd.MockNewSystemd(func(be systemd.Backend, roodDir string, mode systemd.InstanceMode, meter systemd.Reporter) systemd.Systemd {
sysd = &FakeSystemd{}
sysd.AddMountUnitFileResult = ResultForAddMountUnitFile{"", expectedErr}
return sysd
})
Expand Down Expand Up @@ -138,8 +134,8 @@ func (s *mountunitSuite) TestRemoveMountUnit(c *C) {
expectedErr := errors.New("removal error")

var sysd *FakeSystemd
restore := systemd.MockNewSystemd(func(kind systemd.Kind, roodDir string, mode systemd.InstanceMode, meter systemd.Reporter) systemd.Systemd {
sysd = &FakeSystemd{Kind: kind, Mode: mode, Meter: meter}
restore := systemd.MockNewSystemd(func(be systemd.Backend, roodDir string, mode systemd.InstanceMode, meter systemd.Reporter) systemd.Systemd {
sysd = &FakeSystemd{}
sysd.RemoveMountUnitFileResult = expectedErr
return sysd
})
Expand All @@ -164,8 +160,8 @@ func (s *mountunitSuite) TestRemoveSnapMountUnitsFailOnList(c *C) {
expectedErr := errors.New("listing error")

var sysd *FakeSystemd
restore := systemd.MockNewSystemd(func(kind systemd.Kind, roodDir string, mode systemd.InstanceMode, meter systemd.Reporter) systemd.Systemd {
sysd = &FakeSystemd{Kind: kind, Mode: mode, Meter: meter}
restore := systemd.MockNewSystemd(func(be systemd.Backend, roodDir string, mode systemd.InstanceMode, meter systemd.Reporter) systemd.Systemd {
sysd = &FakeSystemd{}
sysd.ListMountUnitsResult = ResultForListMountUnits{nil, expectedErr}
return sysd
})
Expand Down Expand Up @@ -195,8 +191,8 @@ func (s *mountunitSuite) TestRemoveSnapMountUnitsFailOnRemoval(c *C) {
returnedMountPoints := []string{"/here", "/and/there"}

var sysd *FakeSystemd
restore := systemd.MockNewSystemd(func(kind systemd.Kind, roodDir string, mode systemd.InstanceMode, meter systemd.Reporter) systemd.Systemd {
sysd = &FakeSystemd{Kind: kind, Mode: mode, Meter: meter}
restore := systemd.MockNewSystemd(func(be systemd.Backend, roodDir string, mode systemd.InstanceMode, meter systemd.Reporter) systemd.Systemd {
sysd = &FakeSystemd{}
sysd.ListMountUnitsResult = ResultForListMountUnits{returnedMountPoints, nil}
sysd.RemoveMountUnitFileResult = expectedErr
return sysd
Expand Down Expand Up @@ -228,8 +224,8 @@ func (s *mountunitSuite) TestRemoveSnapMountUnitsHappy(c *C) {
returnedMountPoints := []string{"/here", "/and/there", "/here/too"}

var sysd *FakeSystemd
restore := systemd.MockNewSystemd(func(kind systemd.Kind, roodDir string, mode systemd.InstanceMode, meter systemd.Reporter) systemd.Systemd {
sysd = &FakeSystemd{Kind: kind, Mode: mode, Meter: meter}
restore := systemd.MockNewSystemd(func(be systemd.Backend, roodDir string, mode systemd.InstanceMode, meter systemd.Reporter) systemd.Systemd {
sysd = &FakeSystemd{}
sysd.ListMountUnitsResult = ResultForListMountUnits{returnedMountPoints, nil}
sysd.RemoveMountUnitFileResult = nil
return sysd
Expand Down
4 changes: 4 additions & 0 deletions systemd/emulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ type emulation struct {

var errNotImplemented = errors.New("not implemented in emulation mode")

func (s *emulation) Backend() Backend {
return EmulationModeBackend
}

func (s *emulation) DaemonReload() error {
return errNotImplemented
}
Expand Down
43 changes: 27 additions & 16 deletions systemd/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (m *extMutex) Taken(errMsg string) {

// MockNewSystemd can be used to replace the constructor of the
// Systemd types with a function that returns a mock object.
func MockNewSystemd(f func(kind Kind, rootDir string, mode InstanceMode, rep Reporter) Systemd) func() {
func MockNewSystemd(f func(be Backend, rootDir string, mode InstanceMode, rep Reporter) Systemd) func() {
oldNewSystemd := newSystemd
newSystemd = f
return func() {
Expand Down Expand Up @@ -272,8 +272,22 @@ type MountUnitOptions struct {
Origin string
}

// Backend identifies the implementation backend in use by a Systemd instance.
type Backend int

const (
// RunningSystemdBackend identifies the implementation backend
// talking to the running system systemd daemon.
RunningSystemdBackend Backend = iota
// EmulationModeBackend identifies the implementation backend
// emulating a subset of systemd against a filesystem.
EmulationModeBackend
)

// Systemd exposes a minimal interface to manage systemd via the systemctl command.
type Systemd interface {
// Backend returns the underlying implementation backend.
Backend() Backend
// DaemonReload reloads systemd's configuration.
DaemonReload() error
// DaemonRexec reexecutes systemd's system manager, should be
Expand Down Expand Up @@ -385,26 +399,26 @@ type Reporter interface {
Notify(string)
}

func newSystemdReal(kind Kind, rootDir string, mode InstanceMode, rep Reporter) Systemd {
switch kind {
case FullImplementation:
func newSystemdReal(be Backend, rootDir string, mode InstanceMode, rep Reporter) Systemd {
switch be {
case RunningSystemdBackend:
return &systemd{rootDir: rootDir, mode: mode, reporter: rep}
case EmulationMode:
case EmulationModeBackend:
return &emulation{rootDir: rootDir}
default:
panic(fmt.Sprintf("unsupported systemd kind %v", kind))
panic(fmt.Sprintf("unsupported systemd backend %v", be))
}
}

// New returns a Systemd that uses the default root directory and omits
// --root argument when executing systemctl.
func New(mode InstanceMode, rep Reporter) Systemd {
return newSystemd(FullImplementation, "", mode, rep)
return newSystemd(RunningSystemdBackend, "", mode, rep)
}

// NewUnderRoot returns a Systemd that operates on the given rootdir.
func NewUnderRoot(rootDir string, mode InstanceMode, rep Reporter) Systemd {
return newSystemd(FullImplementation, rootDir, mode, rep)
return newSystemd(RunningSystemdBackend, rootDir, mode, rep)
}

// NewEmulationMode returns a Systemd that runs in emulation mode where
Expand All @@ -414,7 +428,7 @@ func NewEmulationMode(rootDir string) Systemd {
if rootDir == "" {
rootDir = dirs.GlobalRootDir
}
return newSystemd(EmulationMode, rootDir, SystemMode, nil)
return newSystemd(EmulationModeBackend, rootDir, SystemMode, nil)
}

// InstanceMode determines which instance of systemd to control.
Expand All @@ -435,13 +449,6 @@ const (
GlobalUserMode
)

type Kind int

const (
FullImplementation Kind = iota
EmulationMode
)

type systemd struct {
rootDir string
reporter Reporter
Expand All @@ -461,6 +468,10 @@ func (s *systemd) systemctl(args ...string) ([]byte, error) {
return systemctlCmd(args...)
}

func (s *systemd) Backend() Backend {
return RunningSystemdBackend
}

func (s *systemd) DaemonReload() error {
if s.mode == GlobalUserMode {
panic("cannot call daemon-reload with GlobalUserMode")
Expand Down
9 changes: 9 additions & 0 deletions systemd/systemd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ func (s *SystemdTestSuite) TestEnsureAtLeastFail(c *C) {
}
}

func (s *SystemdTestSuite) TestBackend(c *C) {
c.Check(New(SystemMode, s.rep).Backend(), Equals, RunningSystemdBackend)
}

func (s *SystemdTestSuite) TestDaemonReload(c *C) {
err := New(SystemMode, s.rep).DaemonReload()
c.Assert(err, IsNil)
Expand Down Expand Up @@ -1263,6 +1267,11 @@ func (s *SystemdTestSuite) TestStatusGlobalUserMode(c *C) {
c.Check(s.argses[2], DeepEquals, []string{"--user", "--global", "--root", rootDir, "is-enabled", "one"})
}

func (s *SystemdTestSuite) TestEmulationModeBackend(c *C) {
sysd := NewEmulationMode(dirs.GlobalRootDir)
c.Check(sysd.Backend(), Equals, EmulationModeBackend)
}

const unitTemplate = `
[Unit]
Description=Mount unit for foo, revision 42
Expand Down

0 comments on commit 4570677

Please sign in to comment.