2014-05-23 01:56:28 +02:00
|
|
|
package config
|
|
|
|
|
2014-05-24 20:41:19 +02:00
|
|
|
import (
|
2014-07-03 06:00:06 +02:00
|
|
|
"path/filepath"
|
2014-07-22 16:41:55 +02:00
|
|
|
"reflect"
|
2014-05-24 20:41:19 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-05-23 01:56:28 +02:00
|
|
|
// This is the directory where our test fixtures are.
|
|
|
|
const fixtureDir = "./test-fixtures"
|
2014-05-24 20:41:19 +02:00
|
|
|
|
2014-07-03 06:00:06 +02:00
|
|
|
func TestConfigValidate(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-good")
|
|
|
|
if err := c.Validate(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 22:46:56 +02:00
|
|
|
func TestConfigValidate_badMultiResource(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-bad-multi-resource")
|
|
|
|
if err := c.Validate(); err == nil {
|
|
|
|
t.Fatal("should not be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-19 01:31:01 +02:00
|
|
|
func TestConfigValidate_dupResource(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-dup-resource")
|
|
|
|
if err := c.Validate(); err == nil {
|
|
|
|
t.Fatal("should not be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-04 19:57:09 +02:00
|
|
|
func TestConfigValidate_outputBadField(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-output-bad-field")
|
|
|
|
if err := c.Validate(); err == nil {
|
|
|
|
t.Fatal("should not be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-19 01:00:21 +02:00
|
|
|
func TestConfigValidate_unknownThing(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-unknownthing")
|
|
|
|
if err := c.Validate(); err == nil {
|
|
|
|
t.Fatal("should not be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 06:06:26 +02:00
|
|
|
func TestConfigValidate_unknownResourceVar(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-unknown-resource-var")
|
|
|
|
if err := c.Validate(); err == nil {
|
|
|
|
t.Fatal("should not be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-04 19:53:36 +02:00
|
|
|
func TestConfigValidate_unknownResourceVar_output(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-unknown-resource-var-output")
|
|
|
|
if err := c.Validate(); err == nil {
|
|
|
|
t.Fatal("should not be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 06:00:06 +02:00
|
|
|
func TestConfigValidate_unknownVar(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-unknownvar")
|
|
|
|
if err := c.Validate(); err == nil {
|
|
|
|
t.Fatal("should not be valid")
|
2014-07-21 17:34:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigValidate_varDefault(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-var-default")
|
|
|
|
if err := c.Validate(); err != nil {
|
|
|
|
t.Fatalf("should be valid: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigValidate_varDefaultBadType(t *testing.T) {
|
|
|
|
t.Skip()
|
|
|
|
|
|
|
|
// TODO(mitchellh): FIX
|
|
|
|
|
|
|
|
c := testConfig(t, "validate-var-default-bad-type")
|
|
|
|
if err := c.Validate(); err == nil {
|
|
|
|
t.Fatal("should not be valid")
|
2014-07-03 06:00:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-24 22:29:07 +02:00
|
|
|
func TestProviderConfigName(t *testing.T) {
|
2014-07-20 01:05:48 +02:00
|
|
|
pcs := []*ProviderConfig{
|
|
|
|
&ProviderConfig{Name: "aw"},
|
|
|
|
&ProviderConfig{Name: "aws"},
|
|
|
|
&ProviderConfig{Name: "a"},
|
|
|
|
&ProviderConfig{Name: "gce_"},
|
2014-06-05 21:21:05 +02:00
|
|
|
}
|
|
|
|
|
2014-06-24 22:29:07 +02:00
|
|
|
n := ProviderConfigName("aws_instance", pcs)
|
2014-06-05 21:21:05 +02:00
|
|
|
if n != "aws" {
|
|
|
|
t.Fatalf("bad: %s", n)
|
|
|
|
}
|
|
|
|
}
|
2014-07-03 06:00:06 +02:00
|
|
|
|
2014-07-22 16:41:55 +02:00
|
|
|
func TestVariableDefaultsMap(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Default interface{}
|
|
|
|
Output map[string]string
|
|
|
|
}{
|
2014-07-22 17:10:06 +02:00
|
|
|
{
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
|
2014-07-22 16:41:55 +02:00
|
|
|
{
|
|
|
|
"foo",
|
2014-07-22 17:06:09 +02:00
|
|
|
map[string]string{"var.foo": "foo"},
|
2014-07-22 16:41:55 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
map[interface{}]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": "baz",
|
|
|
|
},
|
|
|
|
map[string]string{
|
2014-07-22 17:06:09 +02:00
|
|
|
"var.foo": "foo",
|
|
|
|
"var.foo.foo": "bar",
|
|
|
|
"var.foo.bar": "baz",
|
2014-07-22 16:41:55 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
v := &Variable{Name: "foo", Default: tc.Default}
|
|
|
|
actual := v.DefaultsMap()
|
|
|
|
if !reflect.DeepEqual(actual, tc.Output) {
|
|
|
|
t.Fatalf("%d: bad: %#v", i, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 06:00:06 +02:00
|
|
|
func testConfig(t *testing.T, name string) *Config {
|
|
|
|
c, err := Load(filepath.Join(fixtureDir, name, "main.tf"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|