2014-03-18 20:00:41 +04:00
|
|
|
package initialize
|
2014-03-05 04:36:05 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2014-06-06 04:40:53 +04:00
|
|
|
|
2014-09-22 06:22:38 +04:00
|
|
|
"github.com/coreos/coreos-cloudinit/config"
|
2014-06-06 04:40:53 +04:00
|
|
|
"github.com/coreos/coreos-cloudinit/system"
|
2014-03-05 04:36:05 +04:00
|
|
|
)
|
|
|
|
|
2014-06-06 04:40:53 +04:00
|
|
|
type TestUnitManager struct {
|
|
|
|
placed []string
|
|
|
|
enabled []string
|
|
|
|
masked []string
|
|
|
|
unmasked []string
|
|
|
|
commands map[string]string
|
|
|
|
reload bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tum *TestUnitManager) PlaceUnit(unit *system.Unit, dst string) error {
|
|
|
|
tum.placed = append(tum.placed, unit.Name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (tum *TestUnitManager) EnableUnitFile(unit string, runtime bool) error {
|
|
|
|
tum.enabled = append(tum.enabled, unit)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (tum *TestUnitManager) RunUnitCommand(command, unit string) (string, error) {
|
|
|
|
tum.commands = make(map[string]string)
|
|
|
|
tum.commands[unit] = command
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
func (tum *TestUnitManager) DaemonReload() error {
|
|
|
|
tum.reload = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (tum *TestUnitManager) MaskUnit(unit *system.Unit) error {
|
|
|
|
tum.masked = append(tum.masked, unit.Name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (tum *TestUnitManager) UnmaskUnit(unit *system.Unit) error {
|
|
|
|
tum.unmasked = append(tum.unmasked, unit.Name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessUnits(t *testing.T) {
|
|
|
|
tum := &TestUnitManager{}
|
|
|
|
units := []system.Unit{
|
2014-09-22 06:22:38 +04:00
|
|
|
system.Unit{config.Unit{
|
2014-06-06 04:40:53 +04:00
|
|
|
Name: "foo",
|
|
|
|
Mask: true,
|
2014-09-22 06:22:38 +04:00
|
|
|
}},
|
2014-06-06 04:40:53 +04:00
|
|
|
}
|
|
|
|
if err := processUnits(units, "", tum); err != nil {
|
|
|
|
t.Fatalf("unexpected error calling processUnits: %v", err)
|
|
|
|
}
|
|
|
|
if len(tum.masked) != 1 || tum.masked[0] != "foo" {
|
|
|
|
t.Errorf("expected foo to be masked, but found %v", tum.masked)
|
|
|
|
}
|
|
|
|
|
|
|
|
tum = &TestUnitManager{}
|
|
|
|
units = []system.Unit{
|
2014-09-22 06:22:38 +04:00
|
|
|
system.Unit{config.Unit{
|
2014-06-06 04:40:53 +04:00
|
|
|
Name: "bar.network",
|
2014-09-22 06:22:38 +04:00
|
|
|
}},
|
2014-06-06 04:40:53 +04:00
|
|
|
}
|
|
|
|
if err := processUnits(units, "", tum); err != nil {
|
|
|
|
t.Fatalf("unexpected error calling processUnits: %v", err)
|
|
|
|
}
|
|
|
|
if _, ok := tum.commands["systemd-networkd.service"]; !ok {
|
|
|
|
t.Errorf("expected systemd-networkd.service to be reloaded!")
|
|
|
|
}
|
|
|
|
|
|
|
|
tum = &TestUnitManager{}
|
|
|
|
units = []system.Unit{
|
2014-09-22 06:22:38 +04:00
|
|
|
system.Unit{config.Unit{
|
2014-06-06 04:40:53 +04:00
|
|
|
Name: "baz.service",
|
|
|
|
Content: "[Service]\nExecStart=/bin/true",
|
2014-09-22 06:22:38 +04:00
|
|
|
}},
|
2014-06-06 04:40:53 +04:00
|
|
|
}
|
|
|
|
if err := processUnits(units, "", tum); err != nil {
|
|
|
|
t.Fatalf("unexpected error calling processUnits: %v", err)
|
|
|
|
}
|
|
|
|
if len(tum.placed) != 1 || tum.placed[0] != "baz.service" {
|
|
|
|
t.Fatalf("expected baz.service to be written, but got %v", tum.placed)
|
|
|
|
}
|
|
|
|
|
|
|
|
tum = &TestUnitManager{}
|
|
|
|
units = []system.Unit{
|
2014-09-22 06:22:38 +04:00
|
|
|
system.Unit{config.Unit{
|
2014-06-06 04:40:53 +04:00
|
|
|
Name: "locksmithd.service",
|
|
|
|
Runtime: true,
|
2014-09-22 06:22:38 +04:00
|
|
|
}},
|
2014-06-06 04:40:53 +04:00
|
|
|
}
|
|
|
|
if err := processUnits(units, "", tum); err != nil {
|
|
|
|
t.Fatalf("unexpected error calling processUnits: %v", err)
|
|
|
|
}
|
|
|
|
if len(tum.unmasked) != 1 || tum.unmasked[0] != "locksmithd.service" {
|
|
|
|
t.Fatalf("expected locksmithd.service to be unmasked, but got %v", tum.unmasked)
|
|
|
|
}
|
|
|
|
|
|
|
|
tum = &TestUnitManager{}
|
|
|
|
units = []system.Unit{
|
2014-09-22 06:22:38 +04:00
|
|
|
system.Unit{config.Unit{
|
2014-06-06 04:40:53 +04:00
|
|
|
Name: "woof",
|
|
|
|
Enable: true,
|
2014-09-22 06:22:38 +04:00
|
|
|
}},
|
2014-06-06 04:40:53 +04:00
|
|
|
}
|
|
|
|
if err := processUnits(units, "", tum); err != nil {
|
|
|
|
t.Fatalf("unexpected error calling processUnits: %v", err)
|
|
|
|
}
|
|
|
|
if len(tum.enabled) != 1 || tum.enabled[0] != "woof" {
|
|
|
|
t.Fatalf("expected woof to be enabled, but got %v", tum.enabled)
|
|
|
|
}
|
|
|
|
}
|