2014-05-22 18:56:28 -05:00
|
|
|
package config
|
|
|
|
|
2014-05-24 13:41:19 -05:00
|
|
|
import (
|
2014-07-02 23:00:06 -05:00
|
|
|
"path/filepath"
|
2014-07-22 09:41:55 -05:00
|
|
|
"reflect"
|
2015-04-14 19:42:23 -05:00
|
|
|
"strings"
|
2014-05-24 13:41:19 -05:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-05-22 18:56:28 -05:00
|
|
|
// This is the directory where our test fixtures are.
|
|
|
|
const fixtureDir = "./test-fixtures"
|
2014-05-24 13:41:19 -05:00
|
|
|
|
2014-10-02 13:34:08 -05: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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 23:00:06 -05:00
|
|
|
func TestConfigValidate(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-good")
|
|
|
|
if err := c.Validate(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-22 19:16:48 -05: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-02 18:51:20 -05:00
|
|
|
func TestConfigValidate_countInt(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-count-int")
|
|
|
|
if err := c.Validate(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-14 19:42:23 -05:00
|
|
|
func TestConfigValidate_countBadContext(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-count-bad-context")
|
2015-04-15 10:41:56 -05:00
|
|
|
|
2015-04-14 19:42:23 -05:00
|
|
|
err := c.Validate()
|
2015-04-15 10:41:56 -05:00
|
|
|
|
|
|
|
expected := []string{
|
|
|
|
"no_count_in_output: count variables are only valid within resources",
|
|
|
|
"no_count_in_module: count variables are only valid within resources",
|
|
|
|
}
|
|
|
|
for _, exp := range expected {
|
|
|
|
if !strings.Contains(err.Error(), exp) {
|
|
|
|
t.Fatalf("expected: %q,\nto contain: %q", err, exp)
|
|
|
|
}
|
2015-04-14 19:42:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-02 20:22:32 -05:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-02 18:30:46 -05:00
|
|
|
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-02 18:51:20 -05: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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-02 18:30:46 -05:00
|
|
|
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-02 20:25:18 -05:00
|
|
|
func TestConfigValidate_countVar(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-count-var")
|
|
|
|
if err := c.Validate(); err != nil {
|
2014-11-02 06:56:44 -06:00
|
|
|
t.Fatalf("err: %s", err)
|
2014-10-02 20:25:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-02 20:24:37 -05: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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 11:07:41 -06:00
|
|
|
func TestConfigValidate_dependsOnVar(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-depends-on-var")
|
|
|
|
if err := c.Validate(); err == nil {
|
|
|
|
t.Fatal("should not be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-11 18:02:36 -05: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-18 18:31:01 -05: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-10-08 18:03:22 -05:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-07 22:19:32 -05:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 00:10:16 -06:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 03:39:07 -05:00
|
|
|
func TestConfigValidate_moduleVarSelf(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-module-var-self")
|
|
|
|
if err := c.Validate(); err == nil {
|
|
|
|
t.Fatal("should be invalid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-24 17:48:46 -05:00
|
|
|
func TestConfigValidate_nil(t *testing.T) {
|
|
|
|
var c Config
|
|
|
|
if err := c.Validate(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-04 12:57:09 -05: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-10-07 20:03:11 -05:00
|
|
|
func TestConfigValidate_pathVar(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-path-var")
|
|
|
|
if err := c.Validate(); err != nil {
|
2014-11-02 06:56:44 -06:00
|
|
|
t.Fatalf("err: %s", err)
|
2014-10-07 20:03:11 -05: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")
|
2015-02-20 11:18:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-20 16:25:33 -05:00
|
|
|
func TestConfigValidate_providerMulti(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-provider-multi")
|
|
|
|
if err := c.Validate(); err == nil {
|
|
|
|
t.Fatal("should not be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-20 16:27:44 -05:00
|
|
|
func TestConfigValidate_providerMultiGood(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-provider-multi-good")
|
|
|
|
if err := c.Validate(); err != nil {
|
|
|
|
t.Fatalf("should be valid: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-20 16:47:31 -05:00
|
|
|
func TestConfigValidate_providerMultiRefGood(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-provider-multi-ref-good")
|
|
|
|
if err := c.Validate(); err != nil {
|
|
|
|
t.Fatalf("should be valid: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigValidate_providerMultiRefBad(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-provider-multi-ref-bad")
|
|
|
|
if err := c.Validate(); err == nil {
|
|
|
|
t.Fatal("should not be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 11:19:13 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 11:18:08 -06:00
|
|
|
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")
|
2015-02-20 11:21:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigValidate_provSplatOther(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-prov-splat-other")
|
|
|
|
if err := c.Validate(); err != nil {
|
|
|
|
t.Fatalf("should be valid: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigValidate_provSplatSelf(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-prov-splat-self")
|
|
|
|
if err := c.Validate(); err == nil {
|
|
|
|
t.Fatal("should not be valid")
|
2015-02-23 16:43:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigValidate_resourceProvVarSelf(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-resource-prov-self")
|
|
|
|
if err := c.Validate(); err != nil {
|
|
|
|
t.Fatalf("should be valid: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigValidate_resourceVarSelf(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-resource-self")
|
|
|
|
if err := c.Validate(); err == nil {
|
|
|
|
t.Fatal("should not be valid")
|
2014-10-07 20:03:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 18:00:21 -05: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-02 23:06:26 -05: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 12:53:36 -05: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-02 23:00:06 -05:00
|
|
|
func TestConfigValidate_unknownVar(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-unknownvar")
|
|
|
|
if err := c.Validate(); err == nil {
|
2014-10-02 13:18:57 -05: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 {
|
2014-07-02 23:00:06 -05:00
|
|
|
t.Fatal("should not be valid")
|
2014-07-21 10:34:44 -05: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")
|
2014-07-02 23:00:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-11 11:46:56 -05:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-11 19:20:39 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 23:15:08 -05:00
|
|
|
func TestConfigValidate_varMultiNonSlice(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-var-multi-non-slice")
|
|
|
|
if err := c.Validate(); err == nil {
|
2015-02-17 15:32:45 -06:00
|
|
|
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 {
|
2014-10-09 23:15:08 -05:00
|
|
|
t.Fatal("should not be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 11:39:52 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 13:45:41 -05:00
|
|
|
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-08 17:06:04 -05:00
|
|
|
func TestNameRegexp(t *testing.T) {
|
2014-10-10 16:50:35 -05:00
|
|
|
cases := []struct {
|
2014-10-08 17:06:04 -05: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-24 15:29:07 -05:00
|
|
|
func TestProviderConfigName(t *testing.T) {
|
2014-07-19 18:05:48 -05:00
|
|
|
pcs := []*ProviderConfig{
|
|
|
|
&ProviderConfig{Name: "aw"},
|
|
|
|
&ProviderConfig{Name: "aws"},
|
|
|
|
&ProviderConfig{Name: "a"},
|
|
|
|
&ProviderConfig{Name: "gce_"},
|
2014-06-05 14:21:05 -05:00
|
|
|
}
|
|
|
|
|
2014-06-24 15:29:07 -05:00
|
|
|
n := ProviderConfigName("aws_instance", pcs)
|
2014-06-05 14:21:05 -05:00
|
|
|
if n != "aws" {
|
|
|
|
t.Fatalf("bad: %s", n)
|
|
|
|
}
|
|
|
|
}
|
2014-07-02 23:00:06 -05:00
|
|
|
|
2014-07-22 09:41:55 -05:00
|
|
|
func TestVariableDefaultsMap(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Default interface{}
|
|
|
|
Output map[string]string
|
|
|
|
}{
|
2014-07-22 10:10:06 -05:00
|
|
|
{
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
|
2014-07-22 09:41:55 -05:00
|
|
|
{
|
|
|
|
"foo",
|
2014-07-22 10:06:09 -05:00
|
|
|
map[string]string{"var.foo": "foo"},
|
2014-07-22 09:41:55 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
map[interface{}]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": "baz",
|
|
|
|
},
|
|
|
|
map[string]string{
|
2014-07-22 10:06:09 -05:00
|
|
|
"var.foo": "foo",
|
|
|
|
"var.foo.foo": "bar",
|
|
|
|
"var.foo.bar": "baz",
|
2014-07-22 09:41:55 -05: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-02 23:00:06 -05:00
|
|
|
func testConfig(t *testing.T, name string) *Config {
|
|
|
|
c, err := Load(filepath.Join(fixtureDir, name, "main.tf"))
|
|
|
|
if err != nil {
|
2014-08-05 00:04:48 -05:00
|
|
|
t.Fatalf("file: %s\n\nerr: %s", name, err)
|
2014-07-02 23:00:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|