2014-10-18 02:36:22 +04:00
|
|
|
/*
|
|
|
|
Copyright 2014 CoreOS, Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
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-10-23 22:46:08 +04:00
|
|
|
system.Unit{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-10-23 22:46:08 +04:00
|
|
|
system.Unit{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-10-23 22:46:08 +04:00
|
|
|
system.Unit{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-10-23 22:46:08 +04:00
|
|
|
system.Unit{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-10-23 22:46:08 +04:00
|
|
|
system.Unit{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)
|
|
|
|
}
|
|
|
|
}
|