2014-03-18 20:00:41 +04:00
|
|
|
package system
|
2014-03-13 02:43:51 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPlaceNetworkUnit(t *testing.T) {
|
|
|
|
u := Unit{
|
2014-05-07 03:11:26 +04:00
|
|
|
Name: "50-eth0.network",
|
|
|
|
Runtime: true,
|
|
|
|
Content: `[Match]
|
2014-03-13 02:43:51 +04:00
|
|
|
Name=eth47
|
|
|
|
|
|
|
|
[Network]
|
|
|
|
Address=10.209.171.177/19
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create tempdir: %v", err)
|
|
|
|
}
|
2014-05-07 03:11:26 +04:00
|
|
|
defer os.RemoveAll(dir)
|
2014-03-13 02:43:51 +04:00
|
|
|
|
2014-04-14 21:10:10 +04:00
|
|
|
dst := UnitDestination(&u, dir)
|
|
|
|
expectDst := path.Join(dir, "run", "systemd", "network", "50-eth0.network")
|
|
|
|
if dst != expectDst {
|
|
|
|
t.Fatalf("UnitDestination returned %s, expected %s", dst, expectDst)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := PlaceUnit(&u, dst); err != nil {
|
2014-03-13 02:43:51 +04:00
|
|
|
t.Fatalf("PlaceUnit failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-14 21:10:10 +04:00
|
|
|
fi, err := os.Stat(dst)
|
2014-03-13 02:43:51 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to stat file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.Mode() != os.FileMode(0644) {
|
|
|
|
t.Errorf("File has incorrect mode: %v", fi.Mode())
|
|
|
|
}
|
|
|
|
|
2014-04-14 21:10:10 +04:00
|
|
|
contents, err := ioutil.ReadFile(dst)
|
2014-03-13 02:43:51 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to read expected file: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-14 21:10:10 +04:00
|
|
|
expectContents := `[Match]
|
2014-03-13 02:43:51 +04:00
|
|
|
Name=eth47
|
|
|
|
|
|
|
|
[Network]
|
|
|
|
Address=10.209.171.177/19
|
|
|
|
`
|
2014-04-14 21:10:10 +04:00
|
|
|
if string(contents) != expectContents {
|
|
|
|
t.Fatalf("File has incorrect contents '%s'.\nExpected '%s'", string(contents), expectContents)
|
2014-03-13 02:43:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
func TestUnitDestination(t *testing.T) {
|
|
|
|
dir := "/some/dir"
|
|
|
|
name := "foobar.service"
|
|
|
|
|
|
|
|
u := Unit{
|
|
|
|
Name: name,
|
|
|
|
DropIn: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
dst := UnitDestination(&u, dir)
|
|
|
|
expectDst := path.Join(dir, "etc", "systemd", "system", "foobar.service")
|
|
|
|
if dst != expectDst {
|
|
|
|
t.Errorf("UnitDestination returned %s, expected %s", dst, expectDst)
|
|
|
|
}
|
|
|
|
|
|
|
|
u.DropIn = true
|
|
|
|
|
|
|
|
dst = UnitDestination(&u, dir)
|
|
|
|
expectDst = path.Join(dir, "etc", "systemd", "system", "foobar.service.d", cloudConfigDropIn)
|
|
|
|
if dst != expectDst {
|
|
|
|
t.Errorf("UnitDestination returned %s, expected %s", dst, expectDst)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-13 02:43:51 +04:00
|
|
|
func TestPlaceMountUnit(t *testing.T) {
|
|
|
|
u := Unit{
|
2014-05-07 03:11:26 +04:00
|
|
|
Name: "media-state.mount",
|
|
|
|
Runtime: false,
|
|
|
|
Content: `[Mount]
|
2014-03-13 02:43:51 +04:00
|
|
|
What=/dev/sdb1
|
|
|
|
Where=/media/state
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create tempdir: %v", err)
|
|
|
|
}
|
2014-05-07 03:11:26 +04:00
|
|
|
defer os.RemoveAll(dir)
|
2014-03-13 02:43:51 +04:00
|
|
|
|
2014-04-14 21:10:10 +04:00
|
|
|
dst := UnitDestination(&u, dir)
|
|
|
|
expectDst := path.Join(dir, "etc", "systemd", "system", "media-state.mount")
|
|
|
|
if dst != expectDst {
|
|
|
|
t.Fatalf("UnitDestination returned %s, expected %s", dst, expectDst)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := PlaceUnit(&u, dst); err != nil {
|
2014-03-13 02:43:51 +04:00
|
|
|
t.Fatalf("PlaceUnit failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-14 21:10:10 +04:00
|
|
|
fi, err := os.Stat(dst)
|
2014-03-13 02:43:51 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to stat file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.Mode() != os.FileMode(0644) {
|
|
|
|
t.Errorf("File has incorrect mode: %v", fi.Mode())
|
|
|
|
}
|
|
|
|
|
2014-04-14 21:10:10 +04:00
|
|
|
contents, err := ioutil.ReadFile(dst)
|
2014-03-13 02:43:51 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to read expected file: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-14 21:10:10 +04:00
|
|
|
expectContents := `[Mount]
|
2014-03-13 02:43:51 +04:00
|
|
|
What=/dev/sdb1
|
|
|
|
Where=/media/state
|
|
|
|
`
|
2014-04-14 21:10:10 +04:00
|
|
|
if string(contents) != expectContents {
|
|
|
|
t.Fatalf("File has incorrect contents '%s'.\nExpected '%s'", string(contents), expectContents)
|
2014-03-13 02:43:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-18 20:36:31 +04:00
|
|
|
func TestMachineID(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create tempdir: %v", err)
|
|
|
|
}
|
2014-05-07 03:11:26 +04:00
|
|
|
defer os.RemoveAll(dir)
|
2014-03-18 20:36:31 +04:00
|
|
|
|
|
|
|
os.Mkdir(path.Join(dir, "etc"), os.FileMode(0755))
|
|
|
|
ioutil.WriteFile(path.Join(dir, "etc", "machine-id"), []byte("node007\n"), os.FileMode(0444))
|
|
|
|
|
|
|
|
if MachineID(dir) != "node007" {
|
|
|
|
t.Fatalf("File has incorrect contents")
|
|
|
|
}
|
|
|
|
}
|
2014-05-10 07:33:34 +04:00
|
|
|
|
2014-05-07 03:18:36 +04:00
|
|
|
func TestMaskUnit(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create tempdir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
if err := MaskUnit("foo.service", dir); err != nil {
|
|
|
|
t.Fatalf("Unable to mask unit: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fullPath := path.Join(dir, "etc", "systemd", "system", "foo.service")
|
|
|
|
target, err := os.Readlink(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to read link", err)
|
|
|
|
}
|
|
|
|
if target != "/dev/null" {
|
|
|
|
t.Fatalf("unit not masked, got unit target", target)
|
|
|
|
}
|
|
|
|
}
|