2015-01-24 19:32:33 -08:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-10-17 15:36:22 -07:00
|
|
|
|
2014-03-21 10:35:18 -07:00
|
|
|
package initialize
|
|
|
|
|
2014-06-24 18:49:49 -07:00
|
|
|
import (
|
2014-07-10 15:44:52 -07:00
|
|
|
"io/ioutil"
|
2015-01-26 15:45:15 -08:00
|
|
|
"net"
|
2014-06-24 18:49:49 -07:00
|
|
|
"os"
|
2014-07-10 15:44:52 -07:00
|
|
|
"path"
|
2014-06-24 18:49:49 -07:00
|
|
|
"testing"
|
2014-07-10 15:44:52 -07:00
|
|
|
|
2015-01-26 15:45:15 -08:00
|
|
|
"github.com/coreos/coreos-cloudinit/datasource"
|
2014-07-10 15:44:52 -07:00
|
|
|
"github.com/coreos/coreos-cloudinit/system"
|
2014-06-24 18:49:49 -07:00
|
|
|
)
|
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")
|
2014-09-11 08:35:09 -07:00
|
|
|
os.Setenv("COREOS_PUBLIC_IPV6", "1234::")
|
|
|
|
os.Setenv("COREOS_PRIVATE_IPV6", "5678::")
|
2014-06-24 18:49:49 -07:00
|
|
|
for _, tt := range []struct {
|
2015-01-26 15:45:15 -08:00
|
|
|
metadata datasource.Metadata
|
|
|
|
input string
|
|
|
|
out string
|
2014-06-24 18:49:49 -07:00
|
|
|
}{
|
|
|
|
{
|
2014-06-24 18:52:08 -07:00
|
|
|
// Substituting both values directly should always take precedence
|
|
|
|
// over environment variables
|
2015-01-26 15:45:15 -08:00
|
|
|
datasource.Metadata{
|
|
|
|
PublicIPv4: net.ParseIP("192.0.2.3"),
|
|
|
|
PrivateIPv4: net.ParseIP("192.0.2.203"),
|
|
|
|
PublicIPv6: net.ParseIP("fe00:1234::"),
|
|
|
|
PrivateIPv6: net.ParseIP("fe00:5678::"),
|
2014-06-24 18:49:49 -07:00
|
|
|
},
|
|
|
|
`[Service]
|
2014-09-11 08:35:09 -07:00
|
|
|
ExecStart=/usr/bin/echo "$public_ipv4 $public_ipv6"
|
|
|
|
ExecStop=/usr/bin/echo $private_ipv4 $private_ipv6
|
2014-06-24 18:49:49 -07:00
|
|
|
ExecStop=/usr/bin/echo $unknown`,
|
|
|
|
`[Service]
|
2014-09-11 08:35:09 -07:00
|
|
|
ExecStart=/usr/bin/echo "192.0.2.3 fe00:1234::"
|
|
|
|
ExecStop=/usr/bin/echo 192.0.2.203 fe00:5678::
|
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
|
2015-01-26 15:45:15 -08:00
|
|
|
datasource.Metadata{
|
|
|
|
PrivateIPv4: net.ParseIP("127.0.0.1"),
|
|
|
|
},
|
2014-06-24 18:49:49 -07:00
|
|
|
"$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
|
2015-01-26 15:45:15 -08:00
|
|
|
datasource.Metadata{},
|
2014-06-24 18:49:49 -07:00
|
|
|
"$private_ipv4\n$public_ipv4",
|
|
|
|
"5.6.7.8\n1.2.3.4",
|
|
|
|
},
|
2014-06-25 12:07:48 -07:00
|
|
|
{
|
|
|
|
// No substitutions
|
2015-01-26 15:45:15 -08:00
|
|
|
datasource.Metadata{},
|
2014-06-25 12:07:48 -07:00
|
|
|
"$private_ipv4\nfoobar",
|
|
|
|
"5.6.7.8\nfoobar",
|
|
|
|
},
|
2014-09-11 15:03:55 -07:00
|
|
|
{
|
|
|
|
// Escaping substitutions
|
2015-01-26 15:45:15 -08:00
|
|
|
datasource.Metadata{
|
|
|
|
PrivateIPv4: net.ParseIP("127.0.0.1"),
|
|
|
|
},
|
2014-09-11 15:03:55 -07:00
|
|
|
`\$private_ipv4
|
|
|
|
$private_ipv4
|
|
|
|
addr: \$private_ipv4
|
|
|
|
\\$private_ipv4`,
|
|
|
|
`$private_ipv4
|
|
|
|
127.0.0.1
|
|
|
|
addr: $private_ipv4
|
|
|
|
\$private_ipv4`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// No substitutions with escaping
|
2015-01-26 15:45:15 -08:00
|
|
|
datasource.Metadata{},
|
2014-09-11 15:03:55 -07:00
|
|
|
"\\$test\n$test",
|
|
|
|
"\\$test\n$test",
|
|
|
|
},
|
2014-06-24 18:49:49 -07:00
|
|
|
} {
|
2014-03-21 10:35:18 -07:00
|
|
|
|
2015-01-26 17:35:08 -08:00
|
|
|
env := NewEnvironment("./", "./", "./", "", tt.metadata)
|
2014-06-24 18:49:49 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2014-07-10 15:44:52 -07:00
|
|
|
|
|
|
|
func TestEnvironmentFile(t *testing.T) {
|
2015-01-26 15:45:15 -08:00
|
|
|
metadata := datasource.Metadata{
|
|
|
|
PublicIPv4: net.ParseIP("1.2.3.4"),
|
|
|
|
PrivateIPv4: net.ParseIP("5.6.7.8"),
|
|
|
|
PublicIPv6: net.ParseIP("1234::"),
|
|
|
|
PrivateIPv6: net.ParseIP("5678::"),
|
2014-07-10 15:44:52 -07:00
|
|
|
}
|
2014-09-11 08:35:09 -07: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-10 15:44:52 -07:00
|
|
|
|
|
|
|
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create tempdir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2015-01-26 17:35:08 -08:00
|
|
|
env := NewEnvironment("./", "./", "./", "", metadata)
|
2014-07-10 15:44:52 -07:00
|
|
|
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) {
|
2015-01-26 15:45:15 -08:00
|
|
|
os.Clearenv()
|
|
|
|
metadata := datasource.Metadata{}
|
2014-07-10 15:44:52 -07:00
|
|
|
|
2015-01-26 17:35:08 -08:00
|
|
|
env := NewEnvironment("./", "./", "./", "", metadata)
|
2014-07-10 15:44:52 -07:00
|
|
|
ef := env.DefaultEnvironmentFile()
|
|
|
|
if ef != nil {
|
|
|
|
t.Fatalf("Environment file not nil: %v", ef)
|
|
|
|
}
|
|
|
|
}
|