terraform/config/config_test.go

373 lines
8.4 KiB
Go
Raw Normal View History

2014-05-23 01:56:28 +02:00
package config
import (
"path/filepath"
2014-07-22 16:41:55 +02:00
"reflect"
"testing"
)
2014-05-23 01:56:28 +02:00
// This is the directory where our test fixtures are.
const fixtureDir = "./test-fixtures"
2014-10-02 20:34:08 +02:00
func TestConfigCount(t *testing.T) {
c := testConfig(t, "count-int")
actual, err := c.Resources[0].Count()
if err != nil {
t.Fatalf("err: %s", err)
}
if actual != 5 {
t.Fatalf("bad: %#v", actual)
}
}
func TestConfigCount_string(t *testing.T) {
c := testConfig(t, "count-string")
actual, err := c.Resources[0].Count()
if err != nil {
t.Fatalf("err: %s", err)
}
if actual != 5 {
t.Fatalf("bad: %#v", actual)
}
}
func TestConfigCount_var(t *testing.T) {
c := testConfig(t, "count-var")
_, err := c.Resources[0].Count()
if err == nil {
t.Fatalf("should error")
}
}
func TestConfigValidate(t *testing.T) {
c := testConfig(t, "validate-good")
if err := c.Validate(); err != nil {
t.Fatalf("err: %s", err)
}
}
2014-07-23 02:16:48 +02:00
func TestConfigValidate_badDependsOn(t *testing.T) {
c := testConfig(t, "validate-bad-depends-on")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
2014-10-03 01:51:20 +02:00
func TestConfigValidate_countInt(t *testing.T) {
c := testConfig(t, "validate-count-int")
if err := c.Validate(); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestConfigValidate_countCountVar(t *testing.T) {
c := testConfig(t, "validate-count-count-var")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_countModuleVar(t *testing.T) {
c := testConfig(t, "validate-count-module-var")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
2014-10-03 01:51:20 +02:00
func TestConfigValidate_countNotInt(t *testing.T) {
c := testConfig(t, "validate-count-not-int")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_countResourceVar(t *testing.T) {
c := testConfig(t, "validate-count-resource-var")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_countUserVar(t *testing.T) {
c := testConfig(t, "validate-count-user-var")
if err := c.Validate(); err != nil {
t.Fatalf("err: %s", err)
}
}
2014-10-03 03:25:18 +02:00
func TestConfigValidate_countVar(t *testing.T) {
c := testConfig(t, "validate-count-var")
if err := c.Validate(); err != nil {
t.Fatalf("err: %s", err)
2014-10-03 03:25:18 +02:00
}
}
func TestConfigValidate_countVarInvalid(t *testing.T) {
c := testConfig(t, "validate-count-var-invalid")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
2014-09-12 01:02:36 +02:00
func TestConfigValidate_dupModule(t *testing.T) {
c := testConfig(t, "validate-dup-module")
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")
}
}
func TestConfigValidate_moduleNameBad(t *testing.T) {
c := testConfig(t, "validate-module-name-bad")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_moduleSourceVar(t *testing.T) {
c := testConfig(t, "validate-module-source-var")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_moduleVarInt(t *testing.T) {
c := testConfig(t, "validate-module-var-int")
if err := c.Validate(); err != nil {
t.Fatalf("should be valid: %s", err)
}
}
func TestConfigValidate_moduleVarMap(t *testing.T) {
c := testConfig(t, "validate-module-var-map")
if err := c.Validate(); err == nil {
t.Fatal("should be invalid")
}
}
2014-09-25 00:48:46 +02:00
func TestConfigValidate_nil(t *testing.T) {
var c Config
if err := c.Validate(); err != nil {
t.Fatalf("err: %s", err)
}
}
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-10-08 03:03:11 +02:00
func TestConfigValidate_pathVar(t *testing.T) {
c := testConfig(t, "validate-path-var")
if err := c.Validate(); err != nil {
t.Fatalf("err: %s", err)
2014-10-08 03:03:11 +02:00
}
}
func TestConfigValidate_pathVarInvalid(t *testing.T) {
c := testConfig(t, "validate-path-var-invalid")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_provConnSplatOther(t *testing.T) {
c := testConfig(t, "validate-prov-conn-splat-other")
if err := c.Validate(); err != nil {
t.Fatalf("should be valid: %s", err)
}
}
func TestConfigValidate_provConnSplatSelf(t *testing.T) {
c := testConfig(t, "validate-prov-conn-splat-self")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
2014-10-08 03:03:11 +02:00
}
}
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")
}
}
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")
}
}
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")
}
}
func TestConfigValidate_unknownVar(t *testing.T) {
c := testConfig(t, "validate-unknownvar")
if err := c.Validate(); err == nil {
2014-10-02 20:18:57 +02:00
t.Fatal("should not be valid")
}
}
func TestConfigValidate_unknownVarCount(t *testing.T) {
c := testConfig(t, "validate-unknownvar-count")
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) {
c := testConfig(t, "validate-var-default-bad-type")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_varDefaultInterpolate(t *testing.T) {
c := testConfig(t, "validate-var-default-interpolate")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_varMultiExactNonSlice(t *testing.T) {
c := testConfig(t, "validate-var-multi-exact-non-slice")
if err := c.Validate(); err != nil {
t.Fatalf("should be valid: %s", err)
}
}
func TestConfigValidate_varMultiNonSlice(t *testing.T) {
c := testConfig(t, "validate-var-multi-non-slice")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_varMultiNonSliceProvisioner(t *testing.T) {
c := testConfig(t, "validate-var-multi-non-slice-provisioner")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_varMultiFunctionCall(t *testing.T) {
c := testConfig(t, "validate-var-multi-func")
if err := c.Validate(); err != nil {
t.Fatalf("should be valid: %s", err)
}
}
func TestConfigValidate_varModule(t *testing.T) {
c := testConfig(t, "validate-var-module")
if err := c.Validate(); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestConfigValidate_varModuleInvalid(t *testing.T) {
c := testConfig(t, "validate-var-module-invalid")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
2014-10-09 00:06:04 +02:00
func TestNameRegexp(t *testing.T) {
2014-10-10 23:50:35 +02:00
cases := []struct {
2014-10-09 00:06:04 +02:00
Input string
Match bool
}{
{"hello", true},
{"foo-bar", true},
{"foo_bar", true},
{"_hello", true},
{"foo bar", false},
{"foo.bar", false},
}
for _, tc := range cases {
if NameRegexp.Match([]byte(tc.Input)) != tc.Match {
t.Fatalf("Input: %s\n\nExpected: %#v", tc.Input, tc.Match)
}
}
}
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
}
n := ProviderConfigName("aws_instance", pcs)
2014-06-05 21:21:05 +02:00
if n != "aws" {
t.Fatalf("bad: %s", n)
}
}
2014-07-22 16:41:55 +02:00
func TestVariableDefaultsMap(t *testing.T) {
cases := []struct {
Default interface{}
Output map[string]string
}{
{
nil,
nil,
},
2014-07-22 16:41:55 +02:00
{
"foo",
map[string]string{"var.foo": "foo"},
2014-07-22 16:41:55 +02:00
},
{
map[interface{}]interface{}{
"foo": "bar",
"bar": "baz",
},
map[string]string{
"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)
}
}
}
func testConfig(t *testing.T, name string) *Config {
c, err := Load(filepath.Join(fixtureDir, name, "main.tf"))
if err != nil {
2014-08-05 07:04:48 +02:00
t.Fatalf("file: %s\n\nerr: %s", name, err)
}
return c
}