2015-01-25 06:32:33 +03:00
|
|
|
// Copyright 2015 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-10-18 02:36:22 +04:00
|
|
|
|
2014-03-18 20:00:41 +04:00
|
|
|
package system
|
2014-03-13 02:43:51 +04:00
|
|
|
|
|
|
|
import (
|
2014-11-26 04:42:37 +03:00
|
|
|
"fmt"
|
2014-03-13 02:43:51 +04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
2014-09-22 06:22:38 +04:00
|
|
|
|
2015-11-09 11:24:52 +03:00
|
|
|
"github.com/coreos/coreos-cloudinit/config"
|
2014-03-13 02:43:51 +04:00
|
|
|
)
|
|
|
|
|
2014-11-26 04:42:37 +03:00
|
|
|
func TestPlaceUnit(t *testing.T) {
|
|
|
|
tests := []config.Unit{
|
|
|
|
{
|
|
|
|
Name: "50-eth0.network",
|
|
|
|
Runtime: true,
|
|
|
|
Content: "[Match]\nName=eth47\n\n[Network]\nAddress=10.209.171.177/19\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "media-state.mount",
|
|
|
|
Content: "[Mount]\nWhat=/dev/sdb1\nWhere=/media/state\n",
|
|
|
|
},
|
2014-03-13 02:43:51 +04:00
|
|
|
}
|
|
|
|
|
2014-11-26 04:42:37 +03:00
|
|
|
for _, tt := range tests {
|
|
|
|
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Unable to create tempdir: %v", err))
|
|
|
|
}
|
2014-03-13 02:43:51 +04:00
|
|
|
|
2014-11-26 04:42:37 +03:00
|
|
|
u := Unit{tt}
|
|
|
|
sd := &systemd{dir}
|
2014-03-13 02:43:51 +04:00
|
|
|
|
2014-11-26 04:42:37 +03:00
|
|
|
if err := sd.PlaceUnit(u); err != nil {
|
|
|
|
t.Fatalf("PlaceUnit(): bad error (%+v): want nil, got %s", tt, err)
|
|
|
|
}
|
2014-03-13 02:43:51 +04:00
|
|
|
|
2014-11-26 04:42:37 +03:00
|
|
|
fi, err := os.Stat(u.Destination(dir))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Stat(): bad error (%+v): want nil, got %s", tt, err)
|
|
|
|
}
|
2014-03-13 02:43:51 +04:00
|
|
|
|
2014-11-26 04:42:37 +03:00
|
|
|
if mode := fi.Mode(); mode != os.FileMode(0644) {
|
|
|
|
t.Errorf("bad filemode (%+v): want %v, got %v", tt, os.FileMode(0644), mode)
|
|
|
|
}
|
2014-06-06 04:40:53 +04:00
|
|
|
|
2014-11-26 04:42:37 +03:00
|
|
|
c, err := ioutil.ReadFile(u.Destination(dir))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ReadFile(): bad error (%+v): want nil, got %s", tt, err)
|
|
|
|
}
|
2014-04-14 21:10:10 +04:00
|
|
|
|
2014-11-26 04:42:37 +03:00
|
|
|
if string(c) != tt.Content {
|
|
|
|
t.Errorf("bad contents (%+v): want %q, got %q", tt, tt.Content, string(c))
|
|
|
|
}
|
2014-03-13 02:43:51 +04:00
|
|
|
|
2014-11-26 04:42:37 +03:00
|
|
|
os.RemoveAll(dir)
|
2014-03-13 02:43:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-26 03:57:15 +03:00
|
|
|
func TestPlaceUnitDropIn(t *testing.T) {
|
|
|
|
tests := []config.Unit{
|
|
|
|
{
|
|
|
|
Name: "false.service",
|
|
|
|
Runtime: true,
|
|
|
|
DropIns: []config.UnitDropIn{
|
|
|
|
{
|
|
|
|
Name: "00-true.conf",
|
|
|
|
Content: "[Service]\nExecStart=\nExecStart=/usr/bin/true\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "true.service",
|
|
|
|
DropIns: []config.UnitDropIn{
|
|
|
|
{
|
|
|
|
Name: "00-false.conf",
|
|
|
|
Content: "[Service]\nExecStart=\nExecStart=/usr/bin/false\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Unable to create tempdir: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
u := Unit{tt}
|
|
|
|
sd := &systemd{dir}
|
|
|
|
|
|
|
|
if err := sd.PlaceUnitDropIn(u, u.DropIns[0]); err != nil {
|
|
|
|
t.Fatalf("PlaceUnit(): bad error (%+v): want nil, got %s", tt, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := os.Stat(u.DropInDestination(dir, u.DropIns[0]))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Stat(): bad error (%+v): want nil, got %s", tt, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if mode := fi.Mode(); mode != os.FileMode(0644) {
|
|
|
|
t.Errorf("bad filemode (%+v): want %v, got %v", tt, os.FileMode(0644), mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := ioutil.ReadFile(u.DropInDestination(dir, u.DropIns[0]))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ReadFile(): bad error (%+v): want nil, got %s", tt, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(c) != u.DropIns[0].Content {
|
|
|
|
t.Errorf("bad contents (%+v): want %q, got %q", tt, u.DropIns[0].Content, string(c))
|
|
|
|
}
|
|
|
|
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2014-05-21 22:03:52 +04:00
|
|
|
|
2014-06-06 04:40:53 +04:00
|
|
|
sd := &systemd{dir}
|
|
|
|
|
2014-05-21 22:03:52 +04:00
|
|
|
// Ensure mask works with units that do not currently exist
|
2014-11-25 03:42:31 +03:00
|
|
|
uf := Unit{config.Unit{Name: "foo.service"}}
|
2014-06-06 04:40:53 +04:00
|
|
|
if err := sd.MaskUnit(uf); err != nil {
|
2014-05-21 22:03:52 +04:00
|
|
|
t.Fatalf("Unable to mask new unit: %v", err)
|
|
|
|
}
|
|
|
|
fooPath := path.Join(dir, "etc", "systemd", "system", "foo.service")
|
|
|
|
fooTgt, err := os.Readlink(fooPath)
|
|
|
|
if err != nil {
|
2014-10-23 22:46:08 +04:00
|
|
|
t.Fatal("Unable to read link", err)
|
2014-05-21 22:03:52 +04:00
|
|
|
}
|
|
|
|
if fooTgt != "/dev/null" {
|
2014-10-23 22:46:08 +04:00
|
|
|
t.Fatal("unit not masked, got unit target", fooTgt)
|
2014-05-07 03:18:36 +04:00
|
|
|
}
|
|
|
|
|
2014-05-21 22:03:52 +04:00
|
|
|
// Ensure mask works with unit files that already exist
|
2014-11-25 03:42:31 +03:00
|
|
|
ub := Unit{config.Unit{Name: "bar.service"}}
|
2014-05-21 22:03:52 +04:00
|
|
|
barPath := path.Join(dir, "etc", "systemd", "system", "bar.service")
|
|
|
|
if _, err := os.Create(barPath); err != nil {
|
|
|
|
t.Fatalf("Error creating new unit file: %v", err)
|
|
|
|
}
|
2014-06-06 04:40:53 +04:00
|
|
|
if err := sd.MaskUnit(ub); err != nil {
|
2014-05-21 22:03:52 +04:00
|
|
|
t.Fatalf("Unable to mask existing unit: %v", err)
|
|
|
|
}
|
|
|
|
barTgt, err := os.Readlink(barPath)
|
2014-05-07 03:18:36 +04:00
|
|
|
if err != nil {
|
2014-10-23 22:46:08 +04:00
|
|
|
t.Fatal("Unable to read link", err)
|
2014-05-07 03:18:36 +04:00
|
|
|
}
|
2014-05-21 22:03:52 +04:00
|
|
|
if barTgt != "/dev/null" {
|
2014-10-23 22:46:08 +04:00
|
|
|
t.Fatal("unit not masked, got unit target", barTgt)
|
2014-05-07 03:18:36 +04:00
|
|
|
}
|
|
|
|
}
|
2014-06-04 03:49:26 +04:00
|
|
|
|
|
|
|
func TestUnmaskUnit(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)
|
|
|
|
|
2014-06-06 04:40:53 +04:00
|
|
|
sd := &systemd{dir}
|
|
|
|
|
2014-11-25 03:42:31 +03:00
|
|
|
nilUnit := Unit{config.Unit{Name: "null.service"}}
|
2014-06-06 04:40:53 +04:00
|
|
|
if err := sd.UnmaskUnit(nilUnit); err != nil {
|
2014-06-04 03:49:26 +04:00
|
|
|
t.Errorf("unexpected error from unmasking nonexistent unit: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-11-25 03:42:31 +03:00
|
|
|
uf := Unit{config.Unit{Name: "foo.service", Content: "[Service]\nExecStart=/bin/true"}}
|
2014-06-04 03:49:26 +04:00
|
|
|
dst := uf.Destination(dir)
|
|
|
|
if err := os.MkdirAll(path.Dir(dst), os.FileMode(0755)); err != nil {
|
|
|
|
t.Fatalf("Unable to create unit directory: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := os.Create(dst); err != nil {
|
|
|
|
t.Fatalf("Unable to write unit file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(dst, []byte(uf.Content), 700); err != nil {
|
|
|
|
t.Fatalf("Unable to write unit file: %v", err)
|
|
|
|
}
|
2014-06-06 04:40:53 +04:00
|
|
|
if err := sd.UnmaskUnit(uf); err != nil {
|
2014-06-04 03:49:26 +04:00
|
|
|
t.Errorf("unmask of non-empty unit returned unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
got, _ := ioutil.ReadFile(dst)
|
|
|
|
if string(got) != uf.Content {
|
|
|
|
t.Errorf("unmask of non-empty unit mutated unit contents unexpectedly")
|
|
|
|
}
|
|
|
|
|
2014-11-25 03:42:31 +03:00
|
|
|
ub := Unit{config.Unit{Name: "bar.service"}}
|
2014-06-04 03:49:26 +04:00
|
|
|
dst = ub.Destination(dir)
|
|
|
|
if err := os.Symlink("/dev/null", dst); err != nil {
|
|
|
|
t.Fatalf("Unable to create masked unit: %v", err)
|
|
|
|
}
|
2014-06-06 04:40:53 +04:00
|
|
|
if err := sd.UnmaskUnit(ub); err != nil {
|
2014-06-04 03:49:26 +04:00
|
|
|
t.Errorf("unmask of unit returned unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(dst); !os.IsNotExist(err) {
|
2014-10-23 22:46:08 +04:00
|
|
|
t.Errorf("expected %s to not exist after unmask, but got err: %s", dst, err)
|
2014-06-04 03:49:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNullOrEmpty(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)
|
|
|
|
|
|
|
|
non := path.Join(dir, "does_not_exist")
|
|
|
|
ne, err := nullOrEmpty(non)
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
t.Errorf("nullOrEmpty on nonexistent file returned bad error: %v", err)
|
|
|
|
}
|
|
|
|
if ne {
|
|
|
|
t.Errorf("nullOrEmpty returned true unxpectedly")
|
|
|
|
}
|
|
|
|
|
|
|
|
regEmpty := path.Join(dir, "regular_empty_file")
|
|
|
|
_, err = os.Create(regEmpty)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create tempfile: %v", err)
|
|
|
|
}
|
|
|
|
gotNe, gotErr := nullOrEmpty(regEmpty)
|
|
|
|
if !gotNe || gotErr != nil {
|
|
|
|
t.Errorf("nullOrEmpty of regular empty file returned %t, %v - want true, nil", gotNe, gotErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
reg := path.Join(dir, "regular_file")
|
|
|
|
if err := ioutil.WriteFile(reg, []byte("asdf"), 700); err != nil {
|
|
|
|
t.Fatalf("Unable to create tempfile: %v", err)
|
|
|
|
}
|
|
|
|
gotNe, gotErr = nullOrEmpty(reg)
|
|
|
|
if gotNe || gotErr != nil {
|
|
|
|
t.Errorf("nullOrEmpty of regular file returned %t, %v - want false, nil", gotNe, gotErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
null := path.Join(dir, "null")
|
|
|
|
if err := os.Symlink(os.DevNull, null); err != nil {
|
|
|
|
t.Fatalf("Unable to create /dev/null link: %s", err)
|
|
|
|
}
|
|
|
|
gotNe, gotErr = nullOrEmpty(null)
|
|
|
|
if !gotNe || gotErr != nil {
|
|
|
|
t.Errorf("nullOrEmpty of null symlink returned %t, %v - want true, nil", gotNe, gotErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|