2018-02-03 02:24:28 +01:00
|
|
|
package configs
|
|
|
|
|
|
|
|
import (
|
2019-11-08 23:48:52 +01:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2018-02-03 02:24:28 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2019-11-08 23:48:52 +01:00
|
|
|
"strings"
|
2018-02-03 02:24:28 +01:00
|
|
|
"testing"
|
2018-02-03 02:40:28 +01:00
|
|
|
|
2019-11-08 23:48:52 +01:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
2019-09-10 00:58:44 +02:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2018-02-03 02:24:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestParseLoadConfigFileSuccess is a simple test that just verifies that
|
2019-06-30 09:38:36 +02:00
|
|
|
// a number of test configuration files (in testdata/valid-files) can
|
2018-02-03 02:24:28 +01:00
|
|
|
// be parsed without raising any diagnostics.
|
|
|
|
//
|
|
|
|
// This test does not verify that reading these files produces the correct
|
|
|
|
// file element contents. More detailed assertions may be made on some subset
|
|
|
|
// of these configuration files in other tests.
|
|
|
|
func TestParserLoadConfigFileSuccess(t *testing.T) {
|
2019-06-30 09:38:36 +02:00
|
|
|
files, err := ioutil.ReadDir("testdata/valid-files")
|
2018-02-03 02:24:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range files {
|
|
|
|
name := info.Name()
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2019-06-30 09:38:36 +02:00
|
|
|
src, err := ioutil.ReadFile(filepath.Join("testdata/valid-files", name))
|
2018-02-03 02:24:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
parser := testParser(map[string]string{
|
|
|
|
name: string(src),
|
|
|
|
})
|
|
|
|
|
|
|
|
_, diags := parser.LoadConfigFile(name)
|
2019-11-08 23:48:52 +01:00
|
|
|
if len(diags) != 0 {
|
|
|
|
t.Errorf("unexpected diagnostics")
|
2018-02-03 02:24:28 +01:00
|
|
|
for _, diag := range diags {
|
|
|
|
t.Logf("- %s", diag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-02-03 02:40:28 +01:00
|
|
|
|
|
|
|
// TestParseLoadConfigFileFailure is a simple test that just verifies that
|
2019-06-30 09:38:36 +02:00
|
|
|
// a number of test configuration files (in testdata/invalid-files)
|
2018-02-03 02:40:28 +01:00
|
|
|
// produce errors as expected.
|
|
|
|
//
|
|
|
|
// This test does not verify specific error messages, so more detailed
|
|
|
|
// assertions should be made on some subset of these configuration files in
|
|
|
|
// other tests.
|
|
|
|
func TestParserLoadConfigFileFailure(t *testing.T) {
|
2019-06-30 09:38:36 +02:00
|
|
|
files, err := ioutil.ReadDir("testdata/invalid-files")
|
2018-02-03 02:40:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range files {
|
|
|
|
name := info.Name()
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2019-06-30 09:38:36 +02:00
|
|
|
src, err := ioutil.ReadFile(filepath.Join("testdata/invalid-files", name))
|
2018-02-03 02:40:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
parser := testParser(map[string]string{
|
|
|
|
name: string(src),
|
|
|
|
})
|
|
|
|
|
|
|
|
_, diags := parser.LoadConfigFile(name)
|
|
|
|
if !diags.HasErrors() {
|
|
|
|
t.Errorf("LoadConfigFile succeeded; want errors")
|
|
|
|
}
|
|
|
|
for _, diag := range diags {
|
|
|
|
t.Logf("- %s", diag)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test uses a subset of the same fixture files as
|
|
|
|
// TestParserLoadConfigFileFailure, but additionally verifies that each
|
|
|
|
// file produces the expected diagnostic summary.
|
|
|
|
func TestParserLoadConfigFileFailureMessages(t *testing.T) {
|
|
|
|
tests := []struct {
|
2018-02-15 19:17:36 +01:00
|
|
|
Filename string
|
|
|
|
WantSeverity hcl.DiagnosticSeverity
|
|
|
|
WantDiag string
|
2018-02-03 02:40:28 +01:00
|
|
|
}{
|
|
|
|
{
|
2018-02-15 19:17:36 +01:00
|
|
|
"invalid-files/data-resource-lifecycle.tf",
|
|
|
|
hcl.DiagError,
|
2018-02-03 02:40:28 +01:00
|
|
|
"Unsupported lifecycle block",
|
|
|
|
},
|
|
|
|
{
|
2018-02-15 19:17:36 +01:00
|
|
|
"invalid-files/variable-type-unknown.tf",
|
|
|
|
hcl.DiagError,
|
configs: allow full type constraints for variables
Previously we just ported over the simple "string", "list", and "map" type
hint keywords from the old loader, which exist primarily as hints to the
CLI for whether to treat -var=... arguments and environment variables as
literal strings or as HCL expressions.
However, we've been requested before to allow more specific constraints
here because it's generally better UX for a type error to be detected
within an expression in a calling "module" block rather than at some point
deep inside a third-party module.
To allow for more specific constraints, here we use the type constraint
expression syntax defined as an extension within HCL, which uses the
variable and function call syntaxes to represent types rather than values,
like this:
- string
- number
- bool
- list(string)
- list(any)
- list(map(string))
- object({id=string,name=string})
In native HCL syntax this looks like:
variable "foo" {
type = map(string)
}
In JSON, this looks like:
{
"variable": {
"foo": {
"type": "map(string)"
}
}
}
The selection of literal processing or HCL parsing of CLI-set values is
now explicit in the model and separate from the type, though it's still
derived from the type constraint and thus not directly controllable in
configuration.
Since this syntax is more complex than the keywords that replaced it, for
now the simpler keywords are still supported and "list" and "map" are
interpreted as list(any) and map(any) respectively, mimicking how they
were interpreted by Terraform 0.11 and earlier. For the time being our
documentation should continue to recommend these shorthand versions until
we gain more experience with the more-specific type constraints; most
users should just make use of the additional primitive type constraints
this enables: bool and number.
As a result of these more-complete type constraints, we can now type-check
the default value at config load time, which has the nice side-effect of
allowing us to produce a tailored error message if an override file
produces an invalid situation; previously the result was rather confusing
because the error message referred to the original definition of the
variable and not the overridden parts.
2018-03-07 02:37:51 +01:00
|
|
|
"Invalid type specification",
|
2018-02-03 02:40:28 +01:00
|
|
|
},
|
|
|
|
{
|
2018-02-15 19:17:36 +01:00
|
|
|
"invalid-files/unexpected-attr.tf",
|
|
|
|
hcl.DiagError,
|
2018-11-24 21:46:49 +01:00
|
|
|
"Unsupported argument",
|
2018-02-03 02:40:28 +01:00
|
|
|
},
|
|
|
|
{
|
2018-02-15 19:17:36 +01:00
|
|
|
"invalid-files/unexpected-block.tf",
|
|
|
|
hcl.DiagError,
|
2018-02-03 02:40:28 +01:00
|
|
|
"Unsupported block type",
|
|
|
|
},
|
2019-06-12 17:07:32 +02:00
|
|
|
{
|
|
|
|
"invalid-files/resource-count-and-for_each.tf",
|
|
|
|
hcl.DiagError,
|
|
|
|
`Invalid combination of "count" and "for_each"`,
|
|
|
|
},
|
2019-07-25 17:51:55 +02:00
|
|
|
{
|
|
|
|
"invalid-files/data-count-and-for_each.tf",
|
|
|
|
hcl.DiagError,
|
|
|
|
`Invalid combination of "count" and "for_each"`,
|
|
|
|
},
|
2018-02-03 02:40:28 +01:00
|
|
|
{
|
2018-02-15 19:17:36 +01:00
|
|
|
"invalid-files/resource-lifecycle-badbool.tf",
|
|
|
|
hcl.DiagError,
|
2018-02-03 02:40:28 +01:00
|
|
|
"Unsuitable value type",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.Filename, func(t *testing.T) {
|
2019-06-30 09:38:36 +02:00
|
|
|
src, err := ioutil.ReadFile(filepath.Join("testdata", test.Filename))
|
2018-02-03 02:40:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
parser := testParser(map[string]string{
|
|
|
|
test.Filename: string(src),
|
|
|
|
})
|
|
|
|
|
|
|
|
_, diags := parser.LoadConfigFile(test.Filename)
|
|
|
|
if len(diags) != 1 {
|
|
|
|
t.Errorf("Wrong number of diagnostics %d; want 1", len(diags))
|
|
|
|
for _, diag := range diags {
|
|
|
|
t.Logf("- %s", diag)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-02-15 19:17:36 +01:00
|
|
|
if diags[0].Severity != test.WantSeverity {
|
|
|
|
t.Errorf("Wrong diagnostic severity %#v; want %#v", diags[0].Severity, test.WantSeverity)
|
2018-02-03 02:40:28 +01:00
|
|
|
}
|
2018-02-15 19:17:36 +01:00
|
|
|
if diags[0].Summary != test.WantDiag {
|
|
|
|
t.Errorf("Wrong diagnostic summary\ngot: %s\nwant: %s", diags[0].Summary, test.WantDiag)
|
2018-02-03 02:40:28 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-11-08 23:48:52 +01:00
|
|
|
|
|
|
|
// TestParseLoadConfigFileWarning is a test that verifies files from
|
|
|
|
// testdata/warning-files produce particular warnings.
|
|
|
|
//
|
|
|
|
// This test does not verify that reading these files produces the correct
|
|
|
|
// file element contents in spite of those warnings. More detailed assertions
|
|
|
|
// may be made on some subset of these configuration files in other tests.
|
|
|
|
func TestParserLoadConfigFileWarning(t *testing.T) {
|
|
|
|
files, err := ioutil.ReadDir("testdata/warning-files")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range files {
|
|
|
|
name := info.Name()
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
src, err := ioutil.ReadFile(filepath.Join("testdata/warning-files", name))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// First we'll scan the file to see what warnings are expected.
|
|
|
|
// That's declared inside the files themselves by using the
|
|
|
|
// string "WARNING: " somewhere on each line that is expected
|
|
|
|
// to produce a warning, followed by the expected warning summary
|
|
|
|
// text. A single-line comment (with #) is the main way to do that.
|
|
|
|
const marker = "WARNING: "
|
|
|
|
sc := bufio.NewScanner(bytes.NewReader(src))
|
|
|
|
wantWarnings := make(map[int]string)
|
|
|
|
lineNum := 1
|
|
|
|
for sc.Scan() {
|
|
|
|
lineText := sc.Text()
|
|
|
|
if idx := strings.Index(lineText, marker); idx != -1 {
|
|
|
|
summaryText := lineText[idx+len(marker):]
|
|
|
|
wantWarnings[lineNum] = summaryText
|
|
|
|
}
|
|
|
|
lineNum++
|
|
|
|
}
|
|
|
|
|
|
|
|
parser := testParser(map[string]string{
|
|
|
|
name: string(src),
|
|
|
|
})
|
|
|
|
|
|
|
|
_, diags := parser.LoadConfigFile(name)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Errorf("unexpected error diagnostics")
|
|
|
|
for _, diag := range diags {
|
|
|
|
t.Logf("- %s", diag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gotWarnings := make(map[int]string)
|
|
|
|
for _, diag := range diags {
|
|
|
|
if diag.Severity != hcl.DiagWarning || diag.Subject == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
gotWarnings[diag.Subject.Start.Line] = diag.Summary
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := cmp.Diff(wantWarnings, gotWarnings); diff != "" {
|
|
|
|
t.Errorf("wrong warnings\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|