From 8c936f10c569dcac35527e441794cf2d5c77f3d6 Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Tue, 2 Dec 2014 12:46:35 -0800 Subject: [PATCH] initialize: only restart networkd once per config Regression introduced by 9fcf338bf3e6c941068a2ce9e6ce64fbb3e4b65d. Networkd was erroneously being restarted once per network unit. It should only restart once for the entire config. --- initialize/config.go | 7 ++++++- initialize/config_test.go | 12 ++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/initialize/config.go b/initialize/config.go index d6e2f4c..684912e 100644 --- a/initialize/config.go +++ b/initialize/config.go @@ -313,6 +313,7 @@ func processUnits(units []system.Unit, root string, um system.UnitManager) error } actions := make([]action, 0, len(units)) reload := false + restartNetworkd := false for _, unit := range units { dst := unit.Destination(root) if unit.Content != "" { @@ -349,7 +350,7 @@ func processUnits(units []system.Unit, root string, um system.UnitManager) error } if unit.Group() == "network" { - actions = append(actions, action{"systemd-networkd.service", "restart"}) + restartNetworkd = true } else if unit.Command != "" { actions = append(actions, action{unit.Name, unit.Command}) } @@ -361,6 +362,10 @@ func processUnits(units []system.Unit, root string, um system.UnitManager) error } } + if restartNetworkd { + actions = append(actions, action{"systemd-networkd.service", "restart"}) + } + for _, action := range actions { log.Printf("Calling unit command '%s %s'", action.command, action.unit) res, err := um.RunUnitCommand(action.command, action.unit) diff --git a/initialize/config_test.go b/initialize/config_test.go index 59ebdc5..9d399ab 100644 --- a/initialize/config_test.go +++ b/initialize/config_test.go @@ -384,10 +384,15 @@ type TestUnitManager struct { enabled []string masked []string unmasked []string - commands map[string]string + commands []UnitAction reload bool } +type UnitAction struct { + unit string + command string +} + func (tum *TestUnitManager) PlaceUnit(unit *system.Unit, dst string) error { tum.placed = append(tum.placed, unit.Name) return nil @@ -397,8 +402,7 @@ func (tum *TestUnitManager) EnableUnitFile(unit string, runtime bool) error { return nil } func (tum *TestUnitManager) RunUnitCommand(command, unit string) (string, error) { - tum.commands = make(map[string]string) - tum.commands[unit] = command + tum.commands = append(tum.commands, UnitAction{unit, command}) return "", nil } func (tum *TestUnitManager) DaemonReload() error { @@ -438,7 +442,7 @@ func TestProcessUnits(t *testing.T) { if err := processUnits(units, "", tum); err != nil { t.Fatalf("unexpected error calling processUnits: %v", err) } - if _, ok := tum.commands["systemd-networkd.service"]; !ok { + if len(tum.commands) != 1 || (tum.commands[0] != UnitAction{"systemd-networkd.service", "restart"}) { t.Errorf("expected systemd-networkd.service to be reloaded!") }