2014-05-22 18:56:28 -05:00
|
|
|
package config
|
|
|
|
|
2014-05-24 13:41:19 -05:00
|
|
|
import (
|
2016-08-01 16:16:22 -05:00
|
|
|
"flag"
|
2016-11-12 10:21:27 -06:00
|
|
|
"fmt"
|
2016-08-01 16:16:22 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2014-07-02 23:00:06 -05:00
|
|
|
"path/filepath"
|
2016-06-12 04:19:03 -05:00
|
|
|
"reflect"
|
2015-04-14 19:42:23 -05:00
|
|
|
"strings"
|
2014-05-24 13:41:19 -05:00
|
|
|
"testing"
|
2016-08-01 16:16:22 -05:00
|
|
|
|
2017-02-10 12:41:41 -06:00
|
|
|
"github.com/hashicorp/hil/ast"
|
2016-08-01 16:16:22 -05:00
|
|
|
"github.com/hashicorp/terraform/helper/logging"
|
2014-05-24 13:41:19 -05:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2016-08-01 16:16:22 -05:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
flag.Parse()
|
|
|
|
if testing.Verbose() {
|
|
|
|
// if we're verbose, use the logging requested by TF_LOG
|
|
|
|
logging.SetOutput()
|
|
|
|
} else {
|
|
|
|
// otherwise silence all logs
|
|
|
|
log.SetOutput(ioutil.Discard)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2016-02-05 17:40:35 -06:00
|
|
|
func TestConfigCopy(t *testing.T) {
|
|
|
|
c := testConfig(t, "copy-basic")
|
|
|
|
rOrig := c.Resources[0]
|
|
|
|
rCopy := rOrig.Copy()
|
|
|
|
|
|
|
|
if rCopy.Name != rOrig.Name {
|
|
|
|
t.Fatalf("Expected names to equal: %q <=> %q", rCopy.Name, rOrig.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rCopy.Type != rOrig.Type {
|
|
|
|
t.Fatalf("Expected types to equal: %q <=> %q", rCopy.Type, rOrig.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
origCount := rOrig.RawCount.Config()["count"]
|
|
|
|
rCopy.RawCount.Config()["count"] = "5"
|
|
|
|
if rOrig.RawCount.Config()["count"] != origCount {
|
|
|
|
t.Fatalf("Expected RawCount to be copied, but it behaves like a ref!")
|
|
|
|
}
|
|
|
|
|
|
|
|
rCopy.RawConfig.Config()["newfield"] = "hello"
|
|
|
|
if rOrig.RawConfig.Config()["newfield"] == "hello" {
|
|
|
|
t.Fatalf("Expected RawConfig to be copied, but it behaves like a ref!")
|
|
|
|
}
|
|
|
|
|
|
|
|
rCopy.Provisioners = append(rCopy.Provisioners, &Provisioner{})
|
|
|
|
if len(rOrig.Provisioners) == len(rCopy.Provisioners) {
|
|
|
|
t.Fatalf("Expected Provisioners to be copied, but it behaves like a ref!")
|
|
|
|
}
|
|
|
|
|
|
|
|
if rCopy.Provider != rOrig.Provider {
|
|
|
|
t.Fatalf("Expected providers to equal: %q <=> %q",
|
|
|
|
rCopy.Provider, rOrig.Provider)
|
|
|
|
}
|
|
|
|
|
|
|
|
rCopy.DependsOn[0] = "gotchya"
|
|
|
|
if rOrig.DependsOn[0] == rCopy.DependsOn[0] {
|
|
|
|
t.Fatalf("Expected DependsOn to be copied, but it behaves like a ref!")
|
|
|
|
}
|
|
|
|
|
|
|
|
rCopy.Lifecycle.IgnoreChanges[0] = "gotchya"
|
|
|
|
if rOrig.Lifecycle.IgnoreChanges[0] == rCopy.Lifecycle.IgnoreChanges[0] {
|
|
|
|
t.Fatalf("Expected Lifecycle to be copied, but it behaves like a ref!")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 12:41:41 -06:00
|
|
|
// Terraform GH-11800
|
|
|
|
func TestConfigCount_list(t *testing.T) {
|
|
|
|
c := testConfig(t, "count-list")
|
|
|
|
|
|
|
|
// The key is to interpolate so it doesn't fail parsing
|
|
|
|
c.Resources[0].RawCount.Interpolate(map[string]ast.Variable{
|
|
|
|
"var.list": ast.Variable{
|
|
|
|
Value: []ast.Variable{},
|
|
|
|
Type: ast.TypeList,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err := c.Resources[0].Count()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-02 13:34:08 -05:00
|
|
|
func TestConfigCount_var(t *testing.T) {
|
|
|
|
c := testConfig(t, "count-var")
|
|
|
|
_, err := c.Resources[0].Count()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 04:19:03 -05:00
|
|
|
func TestConfig_emptyCollections(t *testing.T) {
|
|
|
|
c := testConfig(t, "empty-collections")
|
|
|
|
if len(c.Variables) != 3 {
|
|
|
|
t.Fatalf("bad: expected 3 variables, got %d", len(c.Variables))
|
|
|
|
}
|
|
|
|
for _, variable := range c.Variables {
|
|
|
|
switch variable.Name {
|
|
|
|
case "empty_string":
|
|
|
|
if variable.Default != "" {
|
|
|
|
t.Fatalf("bad: wrong default %q for variable empty_string", variable.Default)
|
|
|
|
}
|
|
|
|
case "empty_map":
|
|
|
|
if !reflect.DeepEqual(variable.Default, map[string]interface{}{}) {
|
|
|
|
t.Fatalf("bad: wrong default %#v for variable empty_map", variable.Default)
|
|
|
|
}
|
|
|
|
case "empty_list":
|
|
|
|
if !reflect.DeepEqual(variable.Default, []interface{}{}) {
|
|
|
|
t.Fatalf("bad: wrong default %#v for variable empty_list", variable.Default)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatalf("Unexpected variable: %s", variable.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-12 10:21:27 -06:00
|
|
|
// This table test is the preferred way to test validation of configuration.
|
|
|
|
// There are dozens of functions below which do not follow this that are
|
|
|
|
// there mostly historically. They should be converted at some point.
|
|
|
|
func TestConfigValidate_table(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Fixture string
|
|
|
|
Err bool
|
|
|
|
ErrString string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"basic good",
|
|
|
|
"validate-good",
|
|
|
|
false,
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"depends on module",
|
|
|
|
"validate-depends-on-module",
|
|
|
|
false,
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"depends on non-existent module",
|
|
|
|
"validate-depends-on-bad-module",
|
|
|
|
true,
|
|
|
|
"non-existent module 'foo'",
|
|
|
|
},
|
2016-11-23 10:46:13 -06:00
|
|
|
|
|
|
|
{
|
|
|
|
"data source with provisioners",
|
|
|
|
"validate-data-provisioner",
|
|
|
|
true,
|
|
|
|
"data sources cannot have",
|
|
|
|
},
|
2017-01-19 20:10:21 -06:00
|
|
|
|
|
|
|
{
|
|
|
|
"basic provisioners",
|
|
|
|
"validate-basic-provisioners",
|
|
|
|
false,
|
|
|
|
"",
|
|
|
|
},
|
2017-02-17 18:25:03 -06:00
|
|
|
|
|
|
|
{
|
|
|
|
"backend config with interpolations",
|
|
|
|
"validate-backend-interpolate",
|
|
|
|
true,
|
|
|
|
"cannot contain interp",
|
|
|
|
},
|
2017-03-07 14:01:29 -06:00
|
|
|
{
|
|
|
|
"nested types in variable default",
|
|
|
|
"validate-var-nested",
|
2017-03-07 14:33:39 -06:00
|
|
|
false,
|
|
|
|
"",
|
2017-03-07 14:01:29 -06:00
|
|
|
},
|
2017-04-11 17:38:44 -05:00
|
|
|
{
|
2017-04-14 19:33:18 -05:00
|
|
|
"provider with valid version constraint",
|
2017-04-11 17:38:44 -05:00
|
|
|
"provider-version",
|
2017-04-14 19:33:18 -05:00
|
|
|
false,
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"provider with invalid version constraint",
|
|
|
|
"provider-version-invalid",
|
2017-04-11 17:38:44 -05:00
|
|
|
true,
|
2017-04-14 19:33:18 -05:00
|
|
|
"invalid version constraint",
|
2017-04-11 17:38:44 -05:00
|
|
|
},
|
2016-11-12 10:21:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
|
|
|
|
c := testConfig(t, tc.Fixture)
|
|
|
|
err := c.Validate()
|
|
|
|
if (err != nil) != tc.Err {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if tc.ErrString != "" && !strings.Contains(err.Error(), tc.ErrString) {
|
|
|
|
t.Fatalf("expected err to contain: %s\n\ngot: %s", tc.ErrString, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
2014-07-02 23:00:06 -05:00
|
|
|
}
|
2016-11-12 10:21:27 -06:00
|
|
|
|
2014-07-02 23:00:06 -05:00
|
|
|
}
|
|
|
|
|
2016-11-12 18:20:33 -06:00
|
|
|
func TestConfigValidate_tfVersion(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-tf-version")
|
|
|
|
if err := c.Validate(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigValidate_tfVersionBad(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-bad-tf-version")
|
|
|
|
if err := c.Validate(); err == nil {
|
|
|
|
t.Fatal("should not be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigValidate_tfVersionInterpolations(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-tf-version-interp")
|
|
|
|
if err := c.Validate(); err == nil {
|
|
|
|
t.Fatal("should not be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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: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_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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 15:48:12 -05:00
|
|
|
func TestConfigValidate_countVarUnknown(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-count-var-unknown")
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 08:44:35 -05:00
|
|
|
func TestConfigValidate_ignoreChanges(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-ignore-changes")
|
|
|
|
if err := c.Validate(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigValidate_ignoreChangesBad(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-ignore-changes-bad")
|
|
|
|
if err := c.Validate(); err == nil {
|
|
|
|
t.Fatal("should not be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 01:06:33 -05:00
|
|
|
func TestConfigValidate_ignoreChangesInterpolate(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-ignore-changes-interpolate")
|
|
|
|
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")
|
core: support native list variables in config
This commit adds support for native list variables and outputs, building
up on the previous change to state. Interpolation functions now return
native lists in preference to StringList.
List variables are defined like this:
variable "test" {
# This can also be inferred
type = "list"
default = ["Hello", "World"]
}
output "test_out" {
value = "${var.a_list}"
}
This results in the following state:
```
...
"outputs": {
"test_out": [
"hello",
"world"
]
},
...
```
And the result of terraform output is as follows:
```
$ terraform output
test_out = [
hello
world
]
```
Using the output name, an xargs-friendly representation is output:
```
$ terraform output test_out
hello
world
```
The output command also supports indexing into the list (with
appropriate range checking and no wrapping):
```
$ terraform output test_out 1
world
```
Along with maps, list outputs from one module may be passed as variables
into another, removing the need for the `join(",", var.list_as_string)`
and `split(",", var.list_as_string)` which was previously necessary in
Terraform configuration.
This commit also updates the tests and implementations of built-in
interpolation functions to take and return native lists where
appropriate.
A backwards compatibility note: previously the concat interpolation
function was capable of concatenating either strings or lists. The
strings use case was deprectated a long time ago but still remained.
Because we cannot return `ast.TypeAny` from an interpolation function,
this use case is no longer supported for strings - `concat` is only
capable of concatenating lists. This should not be a huge issue - the
type checker picks up incorrect parameters, and the native HIL string
concatenation - or the `join` function - can be used to replicate the
missing behaviour.
2016-04-21 19:03:24 -05:00
|
|
|
if err := c.Validate(); err != nil {
|
|
|
|
t.Fatalf("should be valid: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigValidate_moduleVarList(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-module-var-list")
|
|
|
|
if err := c.Validate(); err != nil {
|
|
|
|
t.Fatalf("should be valid: %s", err)
|
2014-12-16 00:10:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 11:09:43 -06:00
|
|
|
func TestConfigValidate_outputDescription(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-output-description")
|
|
|
|
if err := c.Validate(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if len(c.Outputs) != 1 {
|
|
|
|
t.Fatalf("got %d outputs; want 1", len(c.Outputs))
|
|
|
|
}
|
|
|
|
if got, want := "Number 5", c.Outputs[0].Description; got != want {
|
|
|
|
t.Fatalf("got description %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 16:43:57 -05:00
|
|
|
func TestConfigValidate_outputDuplicate(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-output-dup")
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
core: support native list variables in config
This commit adds support for native list variables and outputs, building
up on the previous change to state. Interpolation functions now return
native lists in preference to StringList.
List variables are defined like this:
variable "test" {
# This can also be inferred
type = "list"
default = ["Hello", "World"]
}
output "test_out" {
value = "${var.a_list}"
}
This results in the following state:
```
...
"outputs": {
"test_out": [
"hello",
"world"
]
},
...
```
And the result of terraform output is as follows:
```
$ terraform output
test_out = [
hello
world
]
```
Using the output name, an xargs-friendly representation is output:
```
$ terraform output test_out
hello
world
```
The output command also supports indexing into the list (with
appropriate range checking and no wrapping):
```
$ terraform output test_out 1
world
```
Along with maps, list outputs from one module may be passed as variables
into another, removing the need for the `join(",", var.list_as_string)`
and `split(",", var.list_as_string)` which was previously necessary in
Terraform configuration.
This commit also updates the tests and implementations of built-in
interpolation functions to take and return native lists where
appropriate.
A backwards compatibility note: previously the concat interpolation
function was capable of concatenating either strings or lists. The
strings use case was deprectated a long time ago but still remained.
Because we cannot return `ast.TypeAny` from an interpolation function,
this use case is no longer supported for strings - `concat` is only
capable of concatenating lists. This should not be a huge issue - the
type checker picks up incorrect parameters, and the native HIL string
concatenation - or the `join` function - can be used to replicate the
missing behaviour.
2016-04-21 19:03:24 -05:00
|
|
|
func TestConfigValidate_varDefaultListType(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-var-default-list-type")
|
|
|
|
if err := c.Validate(); err != nil {
|
2016-05-10 15:00:28 -05:00
|
|
|
t.Fatalf("should be valid: %s", err)
|
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 {
|
2016-08-25 16:51:49 -05:00
|
|
|
t.Fatal("should not be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 10:34:45 -05:00
|
|
|
func TestConfigValidate_varDefaultInterpolateEscaped(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-var-default-interpolate-escaped")
|
|
|
|
if err := c.Validate(); err != nil {
|
|
|
|
t.Fatalf("should be valid, but got err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 16:51:49 -05:00
|
|
|
func TestConfigValidate_varDup(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-var-dup")
|
|
|
|
if err := c.Validate(); err == nil {
|
2014-08-11 11:46:56 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-06-01 19:51:07 -05:00
|
|
|
t.Fatal("should not be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigValidate_varProviderVersionInvalid(t *testing.T) {
|
|
|
|
c := testConfig(t, "validate-provider-version-invalid")
|
|
|
|
if err := c.Validate(); err == nil {
|
2014-09-15 13:45:41 -05:00
|
|
|
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
|
|
|
|
|
|
|
func testConfig(t *testing.T, name string) *Config {
|
2015-06-23 09:15:26 -05:00
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, name, "main.tf"))
|
2014-07-02 23:00:06 -05:00
|
|
|
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
|
|
|
|
}
|
2016-09-02 13:45:21 -05:00
|
|
|
|
|
|
|
func TestConfigDataCount(t *testing.T) {
|
|
|
|
c := testConfig(t, "data-count")
|
|
|
|
actual, err := c.Resources[0].Count()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if actual != 5 {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// we need to make sure "count" has been removed from the RawConfig, since
|
|
|
|
// it's not a real key and won't validate.
|
|
|
|
if _, ok := c.Resources[0].RawConfig.Raw["count"]; ok {
|
|
|
|
t.Fatal("count key still exists in RawConfig")
|
|
|
|
}
|
|
|
|
}
|
2017-04-11 17:38:44 -05:00
|
|
|
|
|
|
|
func TestConfigProviderVersion(t *testing.T) {
|
|
|
|
c := testConfig(t, "provider-version")
|
|
|
|
|
|
|
|
if len(c.ProviderConfigs) != 1 {
|
|
|
|
t.Fatal("expected 1 provider")
|
|
|
|
}
|
|
|
|
|
|
|
|
p := c.ProviderConfigs[0]
|
|
|
|
if p.Name != "aws" {
|
|
|
|
t.Fatalf("expected provider name 'aws', got %q", p.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Version != "0.0.1" {
|
|
|
|
t.Fatalf("expected providers version '0.0.1', got %q", p.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := p.RawConfig.Raw["version"]; ok {
|
|
|
|
t.Fatal("'version' should not exist in raw config")
|
|
|
|
}
|
|
|
|
}
|
2017-04-14 21:07:33 -05:00
|
|
|
|
|
|
|
func TestResourceProviderFullName(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
ResourceName string
|
|
|
|
Alias string
|
|
|
|
Expected string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []testCase{
|
|
|
|
{
|
|
|
|
// If no alias is provided, the first underscore-separated segment
|
|
|
|
// is assumed to be the provider name.
|
|
|
|
ResourceName: "aws_thing",
|
|
|
|
Alias: "",
|
|
|
|
Expected: "aws",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// If we have more than one underscore then it's the first one that we'll use.
|
|
|
|
ResourceName: "aws_thingy_thing",
|
|
|
|
Alias: "",
|
|
|
|
Expected: "aws",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// A provider can export a resource whose name is just the bare provider name,
|
|
|
|
// e.g. because the provider only has one resource and so any additional
|
|
|
|
// parts would be redundant.
|
|
|
|
ResourceName: "external",
|
|
|
|
Alias: "",
|
|
|
|
Expected: "external",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Alias always overrides the default extraction of the name
|
|
|
|
ResourceName: "aws_thing",
|
|
|
|
Alias: "tls.baz",
|
|
|
|
Expected: "tls.baz",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
got := ResourceProviderFullName(test.ResourceName, test.Alias)
|
|
|
|
if got != test.Expected {
|
|
|
|
t.Errorf(
|
|
|
|
"(%q, %q) produced %q; want %q",
|
|
|
|
test.ResourceName, test.Alias,
|
|
|
|
got,
|
|
|
|
test.Expected,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|