2014-04-02 02:02:12 +04:00
|
|
|
package initialize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
2014-05-10 07:33:34 +04:00
|
|
|
|
|
|
|
"github.com/coreos/coreos-cloudinit/system"
|
2014-04-02 02:02:12 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCloudConfigManageEtcHosts(t *testing.T) {
|
|
|
|
contents := `
|
|
|
|
manage_etc_hosts: localhost
|
|
|
|
`
|
|
|
|
cfg, err := NewCloudConfig(contents)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Encountered unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
manageEtcHosts := cfg.ManageEtcHosts
|
|
|
|
|
|
|
|
if manageEtcHosts != "localhost" {
|
|
|
|
t.Errorf("ManageEtcHosts value is %q, expected 'localhost'", manageEtcHosts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestManageEtcHostsInvalidValue(t *testing.T) {
|
2014-05-10 07:33:34 +04:00
|
|
|
eh := EtcHosts("invalid")
|
|
|
|
if f, err := eh.File(""); err == nil || f != nil {
|
|
|
|
t.Fatalf("EtcHosts File succeeded with invalid value!")
|
2014-04-02 02:02:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcHostsWrittenToDisk(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create tempdir: %v", err)
|
|
|
|
}
|
2014-05-10 07:33:34 +04:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
eh := EtcHosts("localhost")
|
|
|
|
|
|
|
|
f, err := eh.File(dir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error calling File on EtcHosts: %v", err)
|
|
|
|
}
|
|
|
|
if f == nil {
|
|
|
|
t.Fatalf("manageEtcHosts returned nil file unexpectedly")
|
|
|
|
}
|
|
|
|
|
2014-06-05 23:48:32 +04:00
|
|
|
if _, err := system.WriteFile(f, dir); err != nil {
|
2014-05-10 07:33:34 +04:00
|
|
|
t.Fatalf("Error writing EtcHosts: %v", err)
|
2014-04-02 02:02:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fullPath := path.Join(dir, "etc", "hosts")
|
|
|
|
|
|
|
|
fi, err := os.Stat(fullPath)
|
|
|
|
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(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to read expected file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to read OS hostname: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-07 22:01:17 +04:00
|
|
|
expect := fmt.Sprintf("%s %s\n", DefaultIpv4Address, hostname)
|
2014-04-02 02:02:12 +04:00
|
|
|
|
|
|
|
if string(contents) != expect {
|
|
|
|
t.Fatalf("File has incorrect contents")
|
|
|
|
}
|
|
|
|
}
|