initialize/env: fall back to COREOS_*_IPV4 env variables

This commit is contained in:
Jonathan Boulle 2014-06-24 18:49:49 -07:00
parent ba1c1e97d0
commit 439b7e8b98
2 changed files with 47 additions and 15 deletions

View File

@ -1,6 +1,7 @@
package initialize package initialize
import ( import (
"os"
"path" "path"
"strings" "strings"
) )
@ -18,6 +19,15 @@ type Environment struct {
// TODO(jonboulle): this is getting unwieldy, should be able to simplify the interface somehow // TODO(jonboulle): this is getting unwieldy, should be able to simplify the interface somehow
func NewEnvironment(root, configRoot, workspace, netconfType, sshKeyName string, substitutions map[string]string) *Environment { func NewEnvironment(root, configRoot, workspace, netconfType, sshKeyName string, substitutions map[string]string) *Environment {
// If certain values are not in the supplied substitution, fall back to retrieving them from the environment
for k, v := range map[string]string{
"$public_ipv4": os.Getenv("COREOS_PUBLIC_IPV4"),
"$private_ipv4": os.Getenv("COREOS_PRIVATE_IPV4"),
} {
if _, ok := substitutions[k]; !ok {
substitutions[k] = v
}
}
return &Environment{root, configRoot, workspace, netconfType, sshKeyName, substitutions} return &Environment{root, configRoot, workspace, netconfType, sshKeyName, substitutions}
} }

View File

@ -1,26 +1,48 @@
package initialize package initialize
import "testing" import (
"os"
"testing"
)
func TestEnvironmentApply(t *testing.T) { func TestEnvironmentApply(t *testing.T) {
subs := map[string]string{ os.Setenv("COREOS_PUBLIC_IPV4", "1.2.3.4")
"$public_ipv4": "192.0.2.3", os.Setenv("COREOS_PRIVATE_IPV4", "5.6.7.8")
"$private_ipv4": "192.0.2.203", for _, tt := range []struct {
} subs map[string]string
env := NewEnvironment("./", "./", "./", "", "", subs) input string
input := `[Service] out string
}{
{
map[string]string{
"$public_ipv4": "192.0.2.3",
"$private_ipv4": "192.0.2.203",
},
`[Service]
ExecStart=/usr/bin/echo "$public_ipv4" ExecStart=/usr/bin/echo "$public_ipv4"
ExecStop=/usr/bin/echo $private_ipv4 ExecStop=/usr/bin/echo $private_ipv4
ExecStop=/usr/bin/echo $unknown ExecStop=/usr/bin/echo $unknown`,
` `[Service]
expected := `[Service]
ExecStart=/usr/bin/echo "192.0.2.3" ExecStart=/usr/bin/echo "192.0.2.3"
ExecStop=/usr/bin/echo 192.0.2.203 ExecStop=/usr/bin/echo 192.0.2.203
ExecStop=/usr/bin/echo $unknown ExecStop=/usr/bin/echo $unknown`,
` },
{
map[string]string{"$private_ipv4": "127.0.0.1"},
"$private_ipv4\n$public_ipv4",
"127.0.0.1\n1.2.3.4",
},
{
map[string]string{"foo": "bar"},
"$private_ipv4\n$public_ipv4",
"5.6.7.8\n1.2.3.4",
},
} {
output := env.Apply(input) env := NewEnvironment("./", "./", "./", "", "", tt.subs)
if output != expected { got := env.Apply(tt.input)
t.Fatalf("Environment incorrectly applied.\nOutput:\n%s\nExpected:\n%s", output, expected) if got != tt.out {
t.Fatalf("Environment incorrectly applied.\ngot:\n%s\nwant:\n%s", got, tt.out)
}
} }
} }