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 (
|
2014-11-25 04:05:36 +03:00
|
|
|
"reflect"
|
2014-03-05 04:36:05 +04:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2014-11-25 03:42:31 +03:00
|
|
|
func (tum *TestUnitManager) PlaceUnit(u system.Unit) error {
|
|
|
|
tum.placed = append(tum.placed, u.Name)
|
2014-06-06 04:40:53 +04:00
|
|
|
return nil
|
|
|
|
}
|
2014-11-25 03:42:31 +03:00
|
|
|
func (tum *TestUnitManager) EnableUnitFile(u system.Unit) error {
|
|
|
|
tum.enabled = append(tum.enabled, u.Name)
|
2014-06-06 04:40:53 +04:00
|
|
|
return nil
|
|
|
|
}
|
2014-11-25 03:42:31 +03:00
|
|
|
func (tum *TestUnitManager) RunUnitCommand(u system.Unit, c string) (string, error) {
|
2014-06-06 04:40:53 +04:00
|
|
|
tum.commands = make(map[string]string)
|
2014-11-25 03:42:31 +03:00
|
|
|
tum.commands[u.Name] = c
|
2014-06-06 04:40:53 +04:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
func (tum *TestUnitManager) DaemonReload() error {
|
|
|
|
tum.reload = true
|
|
|
|
return nil
|
|
|
|
}
|
2014-11-25 03:42:31 +03:00
|
|
|
func (tum *TestUnitManager) MaskUnit(u system.Unit) error {
|
|
|
|
tum.masked = append(tum.masked, u.Name)
|
2014-06-06 04:40:53 +04:00
|
|
|
return nil
|
|
|
|
}
|
2014-11-25 03:42:31 +03:00
|
|
|
func (tum *TestUnitManager) UnmaskUnit(u system.Unit) error {
|
|
|
|
tum.unmasked = append(tum.unmasked, u.Name)
|
2014-06-06 04:40:53 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessUnits(t *testing.T) {
|
2014-11-25 04:05:36 +03:00
|
|
|
tests := []struct {
|
|
|
|
units []system.Unit
|
2014-06-06 04:40:53 +04:00
|
|
|
|
2014-11-25 04:05:36 +03:00
|
|
|
result TestUnitManager
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
units: []system.Unit{
|
|
|
|
system.Unit{Unit: config.Unit{
|
|
|
|
Name: "foo",
|
|
|
|
Mask: true,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
result: TestUnitManager{
|
|
|
|
masked: []string{"foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
units: []system.Unit{
|
|
|
|
system.Unit{Unit: config.Unit{
|
|
|
|
Name: "bar.network",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
result: TestUnitManager{
|
|
|
|
commands: map[string]string{
|
|
|
|
"systemd-networkd.service": "restart",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
units: []system.Unit{
|
|
|
|
system.Unit{Unit: config.Unit{
|
|
|
|
Name: "baz.service",
|
|
|
|
Content: "[Service]\nExecStart=/bin/true",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
result: TestUnitManager{
|
|
|
|
placed: []string{"baz.service"},
|
|
|
|
reload: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
units: []system.Unit{
|
|
|
|
system.Unit{Unit: config.Unit{
|
|
|
|
Name: "locksmithd.service",
|
|
|
|
Runtime: true,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
result: TestUnitManager{
|
|
|
|
unmasked: []string{"locksmithd.service"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
units: []system.Unit{
|
|
|
|
system.Unit{Unit: config.Unit{
|
|
|
|
Name: "woof",
|
|
|
|
Enable: true,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
result: TestUnitManager{
|
|
|
|
enabled: []string{"woof"},
|
|
|
|
},
|
|
|
|
},
|
2014-06-06 04:40:53 +04:00
|
|
|
}
|
|
|
|
|
2014-11-25 04:05:36 +03:00
|
|
|
for _, tt := range tests {
|
|
|
|
tum := &TestUnitManager{}
|
|
|
|
if err := processUnits(tt.units, "", tum); err != nil {
|
|
|
|
t.Errorf("bad error (%+v): want nil, got %s", tt.units, err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tt.result, *tum) {
|
|
|
|
t.Errorf("bad result (%+v): want %+v, got %+v", tt.units, tt.result, tum)
|
|
|
|
}
|
2014-06-06 04:40:53 +04:00
|
|
|
}
|
|
|
|
}
|