system: clean up TestPlaceUnit()

This commit is contained in:
Alex Crawford 2014-11-25 17:42:37 -08:00
parent 624df676d0
commit 420f7cf202

View File

@ -17,6 +17,7 @@
package system package system
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
@ -25,101 +26,51 @@ import (
"github.com/coreos/coreos-cloudinit/config" "github.com/coreos/coreos-cloudinit/config"
) )
func TestPlaceNetworkUnit(t *testing.T) { func TestPlaceUnit(t *testing.T) {
u := Unit{config.Unit{ tests := []config.Unit{
{
Name: "50-eth0.network", Name: "50-eth0.network",
Runtime: true, Runtime: true,
Content: `[Match] Content: "[Match]\nName=eth47\n\n[Network]\nAddress=10.209.171.177/19\n",
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)
}
defer os.RemoveAll(dir)
sd := &systemd{dir}
dst := u.Destination(dir)
if err := sd.PlaceUnit(u); err != nil {
t.Fatalf("PlaceUnit failed: %v", err)
}
fi, err := os.Stat(dst)
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())
}
contents, err := ioutil.ReadFile(dst)
if err != nil {
t.Fatalf("Unable to read expected file: %v", err)
}
expectContents := `[Match]
Name=eth47
[Network]
Address=10.209.171.177/19
`
if string(contents) != expectContents {
t.Fatalf("File has incorrect contents '%s'.\nExpected '%s'", string(contents), expectContents)
}
}
func TestPlaceMountUnit(t *testing.T) {
u := Unit{config.Unit{
Name: "media-state.mount", Name: "media-state.mount",
Runtime: false, Content: "[Mount]\nWhat=/dev/sdb1\nWhere=/media/state\n",
Content: `[Mount] },
What=/dev/sdb1 }
Where=/media/state
`,
}}
for _, tt := range tests {
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-") dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
if err != nil { if err != nil {
t.Fatalf("Unable to create tempdir: %v", err) panic(fmt.Sprintf("Unable to create tempdir: %v", err))
} }
defer os.RemoveAll(dir)
u := Unit{tt}
sd := &systemd{dir} sd := &systemd{dir}
dst := u.Destination(dir)
if err := sd.PlaceUnit(u); err != nil { if err := sd.PlaceUnit(u); err != nil {
t.Fatalf("PlaceUnit failed: %v", err) t.Fatalf("PlaceUnit(): bad error (%+v): want nil, got %s", tt, err)
} }
fi, err := os.Stat(dst) fi, err := os.Stat(u.Destination(dir))
if err != nil { if err != nil {
t.Fatalf("Unable to stat file: %v", err) t.Fatalf("Stat(): bad error (%+v): want nil, got %s", tt, err)
} }
if fi.Mode() != os.FileMode(0644) { if mode := fi.Mode(); mode != os.FileMode(0644) {
t.Errorf("File has incorrect mode: %v", fi.Mode()) t.Errorf("bad filemode (%+v): want %v, got %v", tt, os.FileMode(0644), mode)
} }
contents, err := ioutil.ReadFile(dst) c, err := ioutil.ReadFile(u.Destination(dir))
if err != nil { if err != nil {
t.Fatalf("Unable to read expected file: %v", err) t.Fatalf("ReadFile(): bad error (%+v): want nil, got %s", tt, err)
} }
expectContents := `[Mount] if string(c) != tt.Content {
What=/dev/sdb1 t.Errorf("bad contents (%+v): want %q, got %q", tt, tt.Content, string(c))
Where=/media/state }
`
if string(contents) != expectContents { os.RemoveAll(dir)
t.Fatalf("File has incorrect contents '%s'.\nExpected '%s'", string(contents), expectContents)
} }
} }