2014-03-18 20:00:41 +04:00
|
|
|
package initialize
|
2014-03-18 02:09:59 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
2014-05-10 07:33:34 +04:00
|
|
|
|
|
|
|
"github.com/coreos/coreos-cloudinit/system"
|
2014-03-18 02:09:59 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEtcdEnvironment(t *testing.T) {
|
2014-03-19 00:01:10 +04:00
|
|
|
cfg := make(EtcdEnvironment, 0)
|
|
|
|
cfg["discovery"] = "http://disco.example.com/foobar"
|
|
|
|
cfg["peer-bind-addr"] = "127.0.0.1:7002"
|
|
|
|
|
|
|
|
env := cfg.String()
|
|
|
|
expect := `[Service]
|
|
|
|
Environment="ETCD_DISCOVERY=http://disco.example.com/foobar"
|
|
|
|
Environment="ETCD_PEER_BIND_ADDR=127.0.0.1:7002"
|
|
|
|
`
|
|
|
|
|
|
|
|
if env != expect {
|
|
|
|
t.Errorf("Generated environment:\n%s\nExpected environment:\n%s", env, expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdEnvironmentDiscoveryURLTranslated(t *testing.T) {
|
2014-03-18 02:09:59 +04:00
|
|
|
cfg := make(EtcdEnvironment, 0)
|
|
|
|
cfg["discovery_url"] = "http://disco.example.com/foobar"
|
|
|
|
cfg["peer-bind-addr"] = "127.0.0.1:7002"
|
|
|
|
|
|
|
|
env := cfg.String()
|
|
|
|
expect := `[Service]
|
2014-03-19 00:01:10 +04:00
|
|
|
Environment="ETCD_DISCOVERY=http://disco.example.com/foobar"
|
|
|
|
Environment="ETCD_PEER_BIND_ADDR=127.0.0.1:7002"
|
|
|
|
`
|
|
|
|
|
|
|
|
if env != expect {
|
|
|
|
t.Errorf("Generated environment:\n%s\nExpected environment:\n%s", env, expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdEnvironmentDiscoveryOverridesDiscoveryURL(t *testing.T) {
|
|
|
|
cfg := make(EtcdEnvironment, 0)
|
|
|
|
cfg["discovery_url"] = "ping"
|
|
|
|
cfg["discovery"] = "pong"
|
|
|
|
cfg["peer-bind-addr"] = "127.0.0.1:7002"
|
|
|
|
|
|
|
|
env := cfg.String()
|
|
|
|
expect := `[Service]
|
|
|
|
Environment="ETCD_DISCOVERY=pong"
|
2014-03-18 02:09:59 +04:00
|
|
|
Environment="ETCD_PEER_BIND_ADDR=127.0.0.1:7002"
|
|
|
|
`
|
|
|
|
|
|
|
|
if env != expect {
|
|
|
|
t.Errorf("Generated environment:\n%s\nExpected environment:\n%s", env, expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdEnvironmentWrittenToDisk(t *testing.T) {
|
2014-06-06 03:12:40 +04:00
|
|
|
ee := EtcdEnvironment{
|
2014-05-07 03:11:26 +04:00
|
|
|
"name": "node001",
|
|
|
|
"discovery": "http://disco.example.com/foobar",
|
2014-03-18 02:09:59 +04:00
|
|
|
"peer-bind-addr": "127.0.0.1:7002",
|
|
|
|
}
|
|
|
|
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 02:09:59 +04:00
|
|
|
|
2014-06-06 03:12:40 +04:00
|
|
|
uu, err := ee.Units(dir)
|
2014-05-10 07:33:34 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Generating etcd unit failed: %v", err)
|
|
|
|
}
|
2014-06-06 03:12:40 +04:00
|
|
|
if len(uu) != 1 {
|
|
|
|
t.Fatalf("Expected 1 unit to be returned, got %d", len(uu))
|
2014-05-10 07:33:34 +04:00
|
|
|
}
|
2014-06-06 03:12:40 +04:00
|
|
|
u := uu[0]
|
2014-05-10 07:33:34 +04:00
|
|
|
|
2014-06-04 03:49:26 +04:00
|
|
|
dst := u.Destination(dir)
|
2014-05-10 07:33:34 +04:00
|
|
|
os.Stderr.WriteString("writing to " + dir + "\n")
|
2014-06-06 03:12:40 +04:00
|
|
|
if err := system.PlaceUnit(&u, dst); err != nil {
|
2014-05-10 07:33:34 +04:00
|
|
|
t.Fatalf("Writing of EtcdEnvironment failed: %v", err)
|
2014-03-18 02:09:59 +04:00
|
|
|
}
|
|
|
|
|
2014-03-18 21:36:34 +04:00
|
|
|
fullPath := path.Join(dir, "run", "systemd", "system", "etcd.service.d", "20-cloudinit.conf")
|
2014-03-18 02:09:59 +04:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
expect := `[Service]
|
2014-03-19 00:01:10 +04:00
|
|
|
Environment="ETCD_DISCOVERY=http://disco.example.com/foobar"
|
2014-06-24 02:13:11 +04:00
|
|
|
Environment="ETCD_NAME=node001"
|
2014-03-18 02:09:59 +04:00
|
|
|
Environment="ETCD_PEER_BIND_ADDR=127.0.0.1:7002"
|
|
|
|
`
|
|
|
|
if string(contents) != expect {
|
|
|
|
t.Fatalf("File has incorrect contents")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-18 21:57:10 +04:00
|
|
|
func TestEtcdEnvironmentWrittenToDiskDefaultToMachineID(t *testing.T) {
|
2014-05-10 07:33:34 +04:00
|
|
|
ee := EtcdEnvironment{}
|
2014-03-18 20:36:31 +04:00
|
|
|
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))
|
|
|
|
err = ioutil.WriteFile(path.Join(dir, "etc", "machine-id"), []byte("node007"), os.FileMode(0444))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed writing out /etc/machine-id: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-06 03:12:40 +04:00
|
|
|
uu, err := ee.Units(dir)
|
2014-05-10 07:33:34 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Generating etcd unit failed: %v", err)
|
|
|
|
}
|
2014-06-06 03:12:40 +04:00
|
|
|
if len(uu) == 0 {
|
|
|
|
t.Fatalf("Returned empty etcd units unexpectedly")
|
2014-05-10 07:33:34 +04:00
|
|
|
}
|
2014-06-06 03:12:40 +04:00
|
|
|
u := uu[0]
|
2014-05-10 07:33:34 +04:00
|
|
|
|
2014-06-04 03:49:26 +04:00
|
|
|
dst := u.Destination(dir)
|
2014-05-10 07:33:34 +04:00
|
|
|
os.Stderr.WriteString("writing to " + dir + "\n")
|
2014-06-06 03:12:40 +04:00
|
|
|
if err := system.PlaceUnit(&u, dst); err != nil {
|
2014-05-10 07:33:34 +04:00
|
|
|
t.Fatalf("Writing of EtcdEnvironment failed: %v", err)
|
2014-03-18 20:36:31 +04:00
|
|
|
}
|
|
|
|
|
2014-03-18 21:57:10 +04:00
|
|
|
fullPath := path.Join(dir, "run", "systemd", "system", "etcd.service.d", "20-cloudinit.conf")
|
2014-03-18 20:36:31 +04:00
|
|
|
|
|
|
|
contents, err := ioutil.ReadFile(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to read expected file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expect := `[Service]
|
|
|
|
Environment="ETCD_NAME=node007"
|
|
|
|
`
|
|
|
|
if string(contents) != expect {
|
|
|
|
t.Fatalf("File has incorrect contents")
|
|
|
|
}
|
|
|
|
}
|
2014-05-17 07:27:50 +04:00
|
|
|
|
|
|
|
func TestEtcdEnvironmentWhenNil(t *testing.T) {
|
|
|
|
// EtcdEnvironment will be a nil map if it wasn't in the yaml
|
|
|
|
var ee EtcdEnvironment
|
|
|
|
if ee != nil {
|
|
|
|
t.Fatalf("EtcdEnvironment is not nil")
|
|
|
|
}
|
2014-06-06 03:12:40 +04:00
|
|
|
uu, err := ee.Units("")
|
|
|
|
if len(uu) != 0 || err != nil {
|
|
|
|
t.Fatalf("Units returned value for nil input")
|
2014-05-17 07:27:50 +04:00
|
|
|
}
|
|
|
|
}
|