2014-03-21 10:35:18 -07:00
|
|
|
package initialize
|
|
|
|
|
2014-06-24 18:49:49 -07:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
2014-03-21 10:35:18 -07:00
|
|
|
|
|
|
|
func TestEnvironmentApply(t *testing.T) {
|
2014-06-24 18:49:49 -07:00
|
|
|
os.Setenv("COREOS_PUBLIC_IPV4", "1.2.3.4")
|
|
|
|
os.Setenv("COREOS_PRIVATE_IPV4", "5.6.7.8")
|
|
|
|
for _, tt := range []struct {
|
|
|
|
subs map[string]string
|
|
|
|
input string
|
|
|
|
out string
|
|
|
|
}{
|
|
|
|
{
|
2014-06-24 18:52:08 -07:00
|
|
|
// Substituting both values directly should always take precedence
|
|
|
|
// over environment variables
|
2014-06-24 18:49:49 -07:00
|
|
|
map[string]string{
|
|
|
|
"$public_ipv4": "192.0.2.3",
|
|
|
|
"$private_ipv4": "192.0.2.203",
|
|
|
|
},
|
|
|
|
`[Service]
|
2014-03-21 10:35:18 -07:00
|
|
|
ExecStart=/usr/bin/echo "$public_ipv4"
|
|
|
|
ExecStop=/usr/bin/echo $private_ipv4
|
2014-06-24 18:49:49 -07:00
|
|
|
ExecStop=/usr/bin/echo $unknown`,
|
|
|
|
`[Service]
|
2014-03-21 10:35:18 -07:00
|
|
|
ExecStart=/usr/bin/echo "192.0.2.3"
|
|
|
|
ExecStop=/usr/bin/echo 192.0.2.203
|
2014-06-24 18:49:49 -07:00
|
|
|
ExecStop=/usr/bin/echo $unknown`,
|
|
|
|
},
|
|
|
|
{
|
2014-06-24 18:52:08 -07:00
|
|
|
// Substituting one value directly while falling back with the other
|
2014-06-24 18:49:49 -07: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-24 18:52:08 -07:00
|
|
|
// Falling back to environment variables for both values
|
2014-06-24 18:49:49 -07:00
|
|
|
map[string]string{"foo": "bar"},
|
|
|
|
"$private_ipv4\n$public_ipv4",
|
|
|
|
"5.6.7.8\n1.2.3.4",
|
|
|
|
},
|
2014-06-25 12:07:48 -07:00
|
|
|
{
|
|
|
|
// No substitutions
|
|
|
|
nil,
|
|
|
|
"$private_ipv4\nfoobar",
|
|
|
|
"5.6.7.8\nfoobar",
|
|
|
|
},
|
2014-06-24 18:49:49 -07:00
|
|
|
} {
|
2014-03-21 10:35:18 -07:00
|
|
|
|
2014-06-24 18:49:49 -07: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 10:35:18 -07:00
|
|
|
}
|
|
|
|
}
|