2014-03-18 09:00:41 -07:00
|
|
|
package initialize
|
2014-03-04 16:36:05 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2014-06-05 17:40:53 -07:00
|
|
|
|
2014-09-21 19:22:38 -07:00
|
|
|
"github.com/coreos/coreos-cloudinit/config"
|
2014-06-05 17:40:53 -07:00
|
|
|
"github.com/coreos/coreos-cloudinit/system"
|
2014-03-04 16:36:05 -08:00
|
|
|
)
|
|
|
|
|
2014-06-05 17:40:53 -07: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-21 19:22:38 -07:00
|
|
|
system.Unit{config.Unit{
|
2014-06-05 17:40:53 -07:00
|
|
|
Name: "foo",
|
|
|
|
Mask: true,
|
2014-09-21 19:22:38 -07:00
|
|
|
}},
|
2014-06-05 17:40:53 -07: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-21 19:22:38 -07:00
|
|
|
system.Unit{config.Unit{
|
2014-06-05 17:40:53 -07:00
|
|
|
Name: "bar.network",
|
2014-09-21 19:22:38 -07:00
|
|
|
}},
|
2014-06-05 17:40:53 -07: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-21 19:22:38 -07:00
|
|
|
system.Unit{config.Unit{
|
2014-06-05 17:40:53 -07:00
|
|
|
Name: "baz.service",
|
|
|
|
Content: "[Service]\nExecStart=/bin/true",
|
2014-09-21 19:22:38 -07:00
|
|
|
}},
|
2014-06-05 17:40:53 -07: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-21 19:22:38 -07:00
|
|
|
system.Unit{config.Unit{
|
2014-06-05 17:40:53 -07:00
|
|
|
Name: "locksmithd.service",
|
|
|
|
Runtime: true,
|
2014-09-21 19:22:38 -07:00
|
|
|
}},
|
2014-06-05 17:40:53 -07: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-21 19:22:38 -07:00
|
|
|
system.Unit{config.Unit{
|
2014-06-05 17:40:53 -07:00
|
|
|
Name: "woof",
|
|
|
|
Enable: true,
|
2014-09-21 19:22:38 -07:00
|
|
|
}},
|
2014-06-05 17:40:53 -07: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)
|
|
|
|
}
|
|
|
|
}
|