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