2014-06-13 01:40:53 +02:00
|
|
|
package config
|
2014-06-13 02:24:55 +02:00
|
|
|
|
|
|
|
import (
|
2014-06-20 19:33:26 +02:00
|
|
|
"encoding/gob"
|
2014-06-13 02:24:55 +02:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewRawConfig(t *testing.T) {
|
|
|
|
raw := map[string]interface{}{
|
|
|
|
"foo": "${var.bar}",
|
2014-08-22 00:05:56 +02:00
|
|
|
"bar": `${file("boom.txt")}`,
|
2014-06-13 02:24:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rc, err := NewRawConfig(raw)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-08-22 00:05:56 +02:00
|
|
|
if len(rc.Interpolations) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", rc.Interpolations)
|
|
|
|
}
|
2014-06-13 02:24:55 +02:00
|
|
|
if len(rc.Variables) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", rc.Variables)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRawConfig(t *testing.T) {
|
|
|
|
raw := map[string]interface{}{
|
|
|
|
"foo": "${var.bar}",
|
|
|
|
}
|
|
|
|
|
|
|
|
rc, err := NewRawConfig(raw)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:47:05 +02:00
|
|
|
// Before interpolate, Config() should be the raw
|
|
|
|
if !reflect.DeepEqual(rc.Config(), raw) {
|
|
|
|
t.Fatalf("bad: %#v", rc.Config())
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:24:55 +02:00
|
|
|
vars := map[string]string{"var.bar": "baz"}
|
|
|
|
if err := rc.Interpolate(vars); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := rc.Config()
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"foo": "baz",
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
if len(rc.UnknownKeys()) != 0 {
|
|
|
|
t.Fatalf("bad: %#v", rc.UnknownKeys())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:26:05 +02:00
|
|
|
func TestRawConfig_double(t *testing.T) {
|
|
|
|
raw := map[string]interface{}{
|
|
|
|
"foo": "${var.bar}",
|
|
|
|
}
|
|
|
|
|
|
|
|
rc, err := NewRawConfig(raw)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := map[string]string{"var.bar": "baz"}
|
|
|
|
if err := rc.Interpolate(vars); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := rc.Config()
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"foo": "baz",
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
vars = map[string]string{"var.bar": "what"}
|
|
|
|
if err := rc.Interpolate(vars); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = rc.Config()
|
|
|
|
expected = map[string]interface{}{
|
|
|
|
"foo": "what",
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:24:55 +02:00
|
|
|
func TestRawConfig_unknown(t *testing.T) {
|
|
|
|
raw := map[string]interface{}{
|
|
|
|
"foo": "${var.bar}",
|
|
|
|
}
|
|
|
|
|
|
|
|
rc, err := NewRawConfig(raw)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := map[string]string{"var.bar": UnknownVariableValue}
|
|
|
|
if err := rc.Interpolate(vars); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := rc.Config()
|
|
|
|
expected := map[string]interface{}{}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedKeys := []string{"foo"}
|
|
|
|
if !reflect.DeepEqual(rc.UnknownKeys(), expectedKeys) {
|
|
|
|
t.Fatalf("bad: %#v", rc.UnknownKeys())
|
|
|
|
}
|
|
|
|
}
|
2014-06-20 19:33:26 +02:00
|
|
|
|
2014-10-02 20:14:50 +02:00
|
|
|
func TestRawConfigValue(t *testing.T) {
|
|
|
|
raw := map[string]interface{}{
|
|
|
|
"foo": "${var.bar}",
|
|
|
|
}
|
|
|
|
|
|
|
|
rc, err := NewRawConfig(raw)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rc.Key = ""
|
|
|
|
if rc.Value() != nil {
|
|
|
|
t.Fatalf("bad: %#v", rc.Value())
|
|
|
|
}
|
|
|
|
|
|
|
|
rc.Key = "foo"
|
|
|
|
if rc.Value() != "${var.bar}" {
|
|
|
|
t.Fatalf("err: %#v", rc.Value())
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := map[string]string{"var.bar": "baz"}
|
|
|
|
if err := rc.Interpolate(vars); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rc.Value() != "baz" {
|
|
|
|
t.Fatalf("bad: %#v", rc.Value())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 19:33:26 +02:00
|
|
|
func TestRawConfig_implGob(t *testing.T) {
|
|
|
|
var _ gob.GobDecoder = new(RawConfig)
|
|
|
|
var _ gob.GobEncoder = new(RawConfig)
|
|
|
|
}
|