2014-03-21 21:35:18 +04:00
|
|
|
package initialize
|
|
|
|
|
2014-06-25 05:49:49 +04:00
|
|
|
import (
|
2014-07-11 02:44:52 +04:00
|
|
|
"io/ioutil"
|
2014-06-25 05:49:49 +04:00
|
|
|
"os"
|
2014-07-11 02:44:52 +04:00
|
|
|
"path"
|
2014-06-25 05:49:49 +04:00
|
|
|
"testing"
|
2014-07-11 02:44:52 +04:00
|
|
|
|
|
|
|
"github.com/coreos/coreos-cloudinit/system"
|
2014-06-25 05:49:49 +04:00
|
|
|
)
|
2014-03-21 21:35:18 +04:00
|
|
|
|
|
|
|
func TestEnvironmentApply(t *testing.T) {
|
2014-06-25 05:49:49 +04:00
|
|
|
os.Setenv("COREOS_PUBLIC_IPV4", "1.2.3.4")
|
|
|
|
os.Setenv("COREOS_PRIVATE_IPV4", "5.6.7.8")
|
2014-09-11 19:35:09 +04:00
|
|
|
os.Setenv("COREOS_PUBLIC_IPV6", "1234::")
|
|
|
|
os.Setenv("COREOS_PRIVATE_IPV6", "5678::")
|
2014-06-25 05:49:49 +04:00
|
|
|
for _, tt := range []struct {
|
|
|
|
subs map[string]string
|
|
|
|
input string
|
|
|
|
out string
|
|
|
|
}{
|
|
|
|
{
|
2014-06-25 05:52:08 +04:00
|
|
|
// Substituting both values directly should always take precedence
|
|
|
|
// over environment variables
|
2014-06-25 05:49:49 +04:00
|
|
|
map[string]string{
|
|
|
|
"$public_ipv4": "192.0.2.3",
|
|
|
|
"$private_ipv4": "192.0.2.203",
|
2014-09-11 19:35:09 +04:00
|
|
|
"$public_ipv6": "fe00:1234::",
|
|
|
|
"$private_ipv6": "fe00:5678::",
|
2014-06-25 05:49:49 +04:00
|
|
|
},
|
|
|
|
`[Service]
|
2014-09-11 19:35:09 +04:00
|
|
|
ExecStart=/usr/bin/echo "$public_ipv4 $public_ipv6"
|
|
|
|
ExecStop=/usr/bin/echo $private_ipv4 $private_ipv6
|
2014-06-25 05:49:49 +04:00
|
|
|
ExecStop=/usr/bin/echo $unknown`,
|
|
|
|
`[Service]
|
2014-09-11 19:35:09 +04:00
|
|
|
ExecStart=/usr/bin/echo "192.0.2.3 fe00:1234::"
|
|
|
|
ExecStop=/usr/bin/echo 192.0.2.203 fe00:5678::
|
2014-06-25 05:49:49 +04:00
|
|
|
ExecStop=/usr/bin/echo $unknown`,
|
|
|
|
},
|
|
|
|
{
|
2014-06-25 05:52:08 +04:00
|
|
|
// Substituting one value directly while falling back with the other
|
2014-06-25 05:49:49 +04:00
|
|
|
map[string]string{"$private_ipv4": "127.0.0.1"},
|
|
|
|
"$private_ipv4\n$public_ipv4",
|
|
|
|
"127.0.0.1\n1.2.3.4",
|
|
|
|
},
|
|
|
|
{
|
2014-06-25 05:52:08 +04:00
|
|
|
// Falling back to environment variables for both values
|
2014-06-25 05:49:49 +04:00
|
|
|
map[string]string{"foo": "bar"},
|
|
|
|
"$private_ipv4\n$public_ipv4",
|
|
|
|
"5.6.7.8\n1.2.3.4",
|
|
|
|
},
|
2014-06-25 23:07:48 +04:00
|
|
|
{
|
|
|
|
// No substitutions
|
|
|
|
nil,
|
|
|
|
"$private_ipv4\nfoobar",
|
|
|
|
"5.6.7.8\nfoobar",
|
|
|
|
},
|
2014-06-25 05:49:49 +04:00
|
|
|
} {
|
2014-03-21 21:35:18 +04:00
|
|
|
|
2014-06-25 05:49:49 +04:00
|
|
|
env := NewEnvironment("./", "./", "./", "", "", tt.subs)
|
|
|
|
got := env.Apply(tt.input)
|
|
|
|
if got != tt.out {
|
|
|
|
t.Fatalf("Environment incorrectly applied.\ngot:\n%s\nwant:\n%s", got, tt.out)
|
|
|
|
}
|
2014-03-21 21:35:18 +04:00
|
|
|
}
|
|
|
|
}
|
2014-07-11 02:44:52 +04:00
|
|
|
|
|
|
|
func TestEnvironmentFile(t *testing.T) {
|
|
|
|
subs := map[string]string{
|
|
|
|
"$public_ipv4": "1.2.3.4",
|
|
|
|
"$private_ipv4": "5.6.7.8",
|
2014-09-11 19:35:09 +04:00
|
|
|
"$public_ipv6": "1234::",
|
|
|
|
"$private_ipv6": "5678::",
|
2014-07-11 02:44:52 +04:00
|
|
|
}
|
2014-09-11 19:35:09 +04:00
|
|
|
expect := "COREOS_PRIVATE_IPV4=5.6.7.8\nCOREOS_PRIVATE_IPV6=5678::\nCOREOS_PUBLIC_IPV4=1.2.3.4\nCOREOS_PUBLIC_IPV6=1234::\n"
|
2014-07-11 02:44:52 +04:00
|
|
|
|
|
|
|
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create tempdir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
env := NewEnvironment("./", "./", "./", "", "", subs)
|
|
|
|
ef := env.DefaultEnvironmentFile()
|
|
|
|
err = system.WriteEnvFile(ef, dir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("WriteEnvFile failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fullPath := path.Join(dir, "etc", "environment")
|
|
|
|
contents, err := ioutil.ReadFile(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to read expected file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(contents) != expect {
|
|
|
|
t.Fatalf("File has incorrect contents: %q", contents)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironmentFileNil(t *testing.T) {
|
|
|
|
subs := map[string]string{
|
|
|
|
"$public_ipv4": "",
|
|
|
|
"$private_ipv4": "",
|
2014-09-11 19:35:09 +04:00
|
|
|
"$public_ipv6": "",
|
|
|
|
"$private_ipv6": "",
|
2014-07-11 02:44:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
env := NewEnvironment("./", "./", "./", "", "", subs)
|
|
|
|
ef := env.DefaultEnvironmentFile()
|
|
|
|
if ef != nil {
|
|
|
|
t.Fatalf("Environment file not nil: %v", ef)
|
|
|
|
}
|
|
|
|
}
|