2018-02-07 03:05:14 +01:00
|
|
|
package configs
|
|
|
|
|
|
|
|
import (
|
2020-09-30 23:23:54 +02:00
|
|
|
"fmt"
|
2018-02-07 03:05:14 +01:00
|
|
|
"testing"
|
|
|
|
|
2019-09-10 00:58:44 +02:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/hcl/v2/gohcl"
|
2021-05-17 21:00:50 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2018-02-07 03:05:14 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestModuleOverrideVariable(t *testing.T) {
|
2019-06-30 09:38:36 +02:00
|
|
|
mod, diags := testModuleFromDir("testdata/valid-modules/override-variable")
|
2018-02-07 03:05:14 +01:00
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
if mod == nil {
|
|
|
|
t.Fatalf("module is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
got := mod.Variables
|
|
|
|
want := map[string]*Variable{
|
|
|
|
"fully_overridden": {
|
|
|
|
Name: "fully_overridden",
|
|
|
|
Description: "b_override description",
|
|
|
|
DescriptionSet: true,
|
|
|
|
Default: cty.StringVal("b_override"),
|
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
|
|
|
Type: cty.String,
|
|
|
|
ParsingMode: VariableParseLiteral,
|
2018-02-07 03:05:14 +01:00
|
|
|
DeclRange: hcl.Range{
|
2019-06-30 09:38:36 +02:00
|
|
|
Filename: "testdata/valid-modules/override-variable/primary.tf",
|
2018-02-07 03:05:14 +01:00
|
|
|
Start: hcl.Pos{
|
|
|
|
Line: 1,
|
|
|
|
Column: 1,
|
|
|
|
Byte: 0,
|
|
|
|
},
|
|
|
|
End: hcl.Pos{
|
|
|
|
Line: 1,
|
|
|
|
Column: 28,
|
|
|
|
Byte: 27,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"partially_overridden": {
|
|
|
|
Name: "partially_overridden",
|
|
|
|
Description: "base description",
|
|
|
|
DescriptionSet: true,
|
|
|
|
Default: cty.StringVal("b_override partial"),
|
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
|
|
|
Type: cty.String,
|
|
|
|
ParsingMode: VariableParseLiteral,
|
2018-02-07 03:05:14 +01:00
|
|
|
DeclRange: hcl.Range{
|
2019-06-30 09:38:36 +02:00
|
|
|
Filename: "testdata/valid-modules/override-variable/primary.tf",
|
2018-02-07 03:05:14 +01:00
|
|
|
Start: hcl.Pos{
|
|
|
|
Line: 7,
|
|
|
|
Column: 1,
|
|
|
|
Byte: 103,
|
|
|
|
},
|
|
|
|
End: hcl.Pos{
|
|
|
|
Line: 7,
|
|
|
|
Column: 32,
|
|
|
|
Byte: 134,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assertResultDeepEqual(t, got, want)
|
|
|
|
}
|
2018-02-07 03:30:12 +01:00
|
|
|
|
|
|
|
func TestModuleOverrideModule(t *testing.T) {
|
2019-06-30 09:38:36 +02:00
|
|
|
mod, diags := testModuleFromDir("testdata/valid-modules/override-module")
|
2018-02-07 03:30:12 +01:00
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
if mod == nil {
|
|
|
|
t.Fatalf("module is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists := mod.ModuleCalls["example"]; !exists {
|
|
|
|
t.Fatalf("no module 'example'")
|
|
|
|
}
|
|
|
|
if len(mod.ModuleCalls) != 1 {
|
|
|
|
t.Fatalf("wrong number of module calls in result %d; want 1", len(mod.ModuleCalls))
|
|
|
|
}
|
|
|
|
|
|
|
|
got := mod.ModuleCalls["example"]
|
|
|
|
want := &ModuleCall{
|
Refactoring of module source addresses and module installation
It's been a long while since we gave close attention to the codepaths for
module source address parsing and external module package installation.
Due to their age, these codepaths often diverged from our modern practices
such as representing address types in the addrs package, and encapsulating
package installation details only in a particular location.
In particular, this refactor makes source address parsing a separate step
from module installation, which therefore makes the result of that parsing
available to other Terraform subsystems which work with the configuration
representation objects.
This also presented the opportunity to better encapsulate our use of
go-getter into a new package "getmodules" (echoing "getproviders"), which
is intended to be the only part of Terraform that directly interacts with
go-getter.
This is largely just a refactor of the existing functionality into a new
code organization, but there is one notable change in behavior here: the
source address parsing now happens during configuration loading rather
than module installation, which may cause errors about invalid addresses
to be returned in different situations than before. That counts as
backward compatible because we only promise to remain compatible with
configurations that are _valid_, which means that they can be initialized,
planned, and applied without any errors. This doesn't introduce any new
error cases, and instead just makes a pre-existing error case be detected
earlier.
Our module registry client is still using its own special module address
type from registry/regsrc for now, with a small shim from the new
addrs.ModuleSourceRegistry type. Hopefully in a later commit we'll also
rework the registry client to work with the new address type, but this
commit is already big enough as it is.
2021-05-28 04:24:59 +02:00
|
|
|
Name: "example",
|
|
|
|
SourceAddr: addrs.ModuleSourceLocal("./example2-a_override"),
|
|
|
|
SourceAddrRaw: "./example2-a_override",
|
2018-02-07 03:30:12 +01:00
|
|
|
SourceAddrRange: hcl.Range{
|
2019-06-30 09:38:36 +02:00
|
|
|
Filename: "testdata/valid-modules/override-module/a_override.tf",
|
2018-02-07 03:30:12 +01:00
|
|
|
Start: hcl.Pos{
|
|
|
|
Line: 3,
|
|
|
|
Column: 12,
|
|
|
|
Byte: 31,
|
|
|
|
},
|
|
|
|
End: hcl.Pos{
|
|
|
|
Line: 3,
|
|
|
|
Column: 35,
|
|
|
|
Byte: 54,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
SourceSet: true,
|
|
|
|
DeclRange: hcl.Range{
|
2019-06-30 09:38:36 +02:00
|
|
|
Filename: "testdata/valid-modules/override-module/primary.tf",
|
2018-02-07 03:30:12 +01:00
|
|
|
Start: hcl.Pos{
|
|
|
|
Line: 2,
|
|
|
|
Column: 1,
|
|
|
|
Byte: 1,
|
|
|
|
},
|
|
|
|
End: hcl.Pos{
|
|
|
|
Line: 2,
|
|
|
|
Column: 17,
|
|
|
|
Byte: 17,
|
|
|
|
},
|
|
|
|
},
|
2020-08-11 17:46:40 +02:00
|
|
|
Providers: []PassedProviderConfig{
|
|
|
|
{
|
|
|
|
InChild: &ProviderConfigRef{
|
|
|
|
Name: "test",
|
|
|
|
NameRange: hcl.Range{
|
|
|
|
Filename: "testdata/valid-modules/override-module/b_override.tf",
|
|
|
|
Start: hcl.Pos{Line: 7, Column: 5, Byte: 97},
|
|
|
|
End: hcl.Pos{Line: 7, Column: 9, Byte: 101},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
InParent: &ProviderConfigRef{
|
|
|
|
Name: "test",
|
|
|
|
NameRange: hcl.Range{
|
|
|
|
Filename: "testdata/valid-modules/override-module/b_override.tf",
|
|
|
|
Start: hcl.Pos{Line: 7, Column: 12, Byte: 104},
|
|
|
|
End: hcl.Pos{Line: 7, Column: 16, Byte: 108},
|
|
|
|
},
|
|
|
|
Alias: "b_override",
|
|
|
|
AliasRange: &hcl.Range{
|
|
|
|
Filename: "testdata/valid-modules/override-module/b_override.tf",
|
|
|
|
Start: hcl.Pos{Line: 7, Column: 16, Byte: 108},
|
|
|
|
End: hcl.Pos{Line: 7, Column: 27, Byte: 119},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-02-07 03:30:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// We're going to extract and nil out our hcl.Body here because DeepEqual
|
|
|
|
// is not a useful way to assert on that.
|
|
|
|
gotConfig := got.Config
|
|
|
|
got.Config = nil
|
|
|
|
|
|
|
|
assertResultDeepEqual(t, got, want)
|
|
|
|
|
|
|
|
type content struct {
|
|
|
|
Kept *string `hcl:"kept"`
|
|
|
|
Foo *string `hcl:"foo"`
|
|
|
|
New *string `hcl:"new"`
|
|
|
|
Newer *string `hcl:"newer"`
|
|
|
|
}
|
|
|
|
var gotArgs content
|
|
|
|
diags = gohcl.DecodeBody(gotConfig, nil, &gotArgs)
|
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
|
|
|
|
wantArgs := content{
|
|
|
|
Kept: stringPtr("primary kept"),
|
|
|
|
Foo: stringPtr("a_override foo"),
|
|
|
|
New: stringPtr("b_override new"),
|
|
|
|
Newer: stringPtr("b_override newer"),
|
|
|
|
}
|
|
|
|
|
|
|
|
assertResultDeepEqual(t, gotArgs, wantArgs)
|
|
|
|
}
|
2019-04-16 00:25:00 +02:00
|
|
|
|
|
|
|
func TestModuleOverrideDynamic(t *testing.T) {
|
|
|
|
schema := &hcl.BodySchema{
|
|
|
|
Blocks: []hcl.BlockHeaderSchema{
|
|
|
|
{Type: "foo"},
|
|
|
|
{Type: "dynamic", LabelNames: []string{"type"}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("base is dynamic", func(t *testing.T) {
|
2019-06-30 09:38:36 +02:00
|
|
|
mod, diags := testModuleFromDir("testdata/valid-modules/override-dynamic-block-base")
|
2019-04-16 00:25:00 +02:00
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
if mod == nil {
|
|
|
|
t.Fatalf("module is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists := mod.ManagedResources["test.foo"]; !exists {
|
|
|
|
t.Fatalf("no module 'example'")
|
|
|
|
}
|
|
|
|
if len(mod.ManagedResources) != 1 {
|
|
|
|
t.Fatalf("wrong number of managed resources in result %d; want 1", len(mod.ManagedResources))
|
|
|
|
}
|
|
|
|
|
|
|
|
body := mod.ManagedResources["test.foo"].Config
|
|
|
|
content, diags := body.Content(schema)
|
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
|
|
|
|
if len(content.Blocks) != 1 {
|
|
|
|
t.Fatalf("wrong number of blocks in result %d; want 1", len(content.Blocks))
|
|
|
|
}
|
|
|
|
if got, want := content.Blocks[0].Type, "foo"; got != want {
|
|
|
|
t.Fatalf("wrong block type %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("override is dynamic", func(t *testing.T) {
|
2019-06-30 09:38:36 +02:00
|
|
|
mod, diags := testModuleFromDir("testdata/valid-modules/override-dynamic-block-override")
|
2019-04-16 00:25:00 +02:00
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
if mod == nil {
|
|
|
|
t.Fatalf("module is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists := mod.ManagedResources["test.foo"]; !exists {
|
|
|
|
t.Fatalf("no module 'example'")
|
|
|
|
}
|
|
|
|
if len(mod.ManagedResources) != 1 {
|
|
|
|
t.Fatalf("wrong number of managed resources in result %d; want 1", len(mod.ManagedResources))
|
|
|
|
}
|
|
|
|
|
|
|
|
body := mod.ManagedResources["test.foo"].Config
|
|
|
|
content, diags := body.Content(schema)
|
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
|
|
|
|
if len(content.Blocks) != 1 {
|
|
|
|
t.Fatalf("wrong number of blocks in result %d; want 1", len(content.Blocks))
|
|
|
|
}
|
|
|
|
if got, want := content.Blocks[0].Type, "dynamic"; got != want {
|
|
|
|
t.Fatalf("wrong block type %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
if got, want := content.Blocks[0].Labels[0], "foo"; got != want {
|
|
|
|
t.Fatalf("wrong dynamic block label %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-01-13 17:31:47 +01:00
|
|
|
|
2020-09-30 23:23:54 +02:00
|
|
|
func TestModuleOverrideSensitiveVariable(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
sensitive bool
|
|
|
|
sensitiveSet bool
|
|
|
|
}
|
|
|
|
cases := map[string]testCase{
|
|
|
|
"false_true": {
|
|
|
|
sensitive: true,
|
|
|
|
sensitiveSet: true,
|
|
|
|
},
|
|
|
|
"true_false": {
|
|
|
|
sensitive: false,
|
|
|
|
sensitiveSet: true,
|
|
|
|
},
|
|
|
|
"false_false_true": {
|
|
|
|
sensitive: true,
|
|
|
|
sensitiveSet: true,
|
|
|
|
},
|
|
|
|
"true_true_false": {
|
|
|
|
sensitive: false,
|
|
|
|
sensitiveSet: true,
|
|
|
|
},
|
|
|
|
"false_true_false": {
|
|
|
|
sensitive: false,
|
|
|
|
sensitiveSet: true,
|
|
|
|
},
|
|
|
|
"true_false_true": {
|
|
|
|
sensitive: true,
|
|
|
|
sensitiveSet: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-10-08 17:14:50 +02:00
|
|
|
mod, diags := testModuleFromDir("testdata/valid-modules/override-variable-sensitive")
|
2020-09-30 23:23:54 +02:00
|
|
|
|
2020-10-08 17:14:50 +02:00
|
|
|
assertNoDiagnostics(t, diags)
|
2020-09-30 23:23:54 +02:00
|
|
|
|
|
|
|
if mod == nil {
|
|
|
|
t.Fatalf("module is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
got := mod.Variables
|
|
|
|
|
|
|
|
for v, want := range cases {
|
|
|
|
t.Run(fmt.Sprintf("variable %s", v), func(t *testing.T) {
|
|
|
|
if got[v].Sensitive != want.sensitive {
|
|
|
|
t.Errorf("wrong result for sensitive\ngot: %t want: %t", got[v].Sensitive, want.sensitive)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got[v].SensitiveSet != want.sensitiveSet {
|
|
|
|
t.Errorf("wrong result for sensitive set\ngot: %t want: %t", got[v].Sensitive, want.sensitive)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 19:36:16 +01:00
|
|
|
func TestModuleOverrideResourceFQNs(t *testing.T) {
|
|
|
|
mod, diags := testModuleFromDir("testdata/valid-modules/override-resource-provider")
|
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
|
|
|
|
got := mod.ManagedResources["test_instance.explicit"]
|
2021-05-27 20:09:28 +02:00
|
|
|
wantProvider := addrs.NewProvider(addrs.DefaultProviderRegistryHost, "bar", "test")
|
2020-03-16 19:36:16 +01:00
|
|
|
wantProviderCfg := &ProviderConfigRef{
|
|
|
|
Name: "bar-test",
|
|
|
|
NameRange: hcl.Range{
|
|
|
|
Filename: "testdata/valid-modules/override-resource-provider/a_override.tf",
|
|
|
|
Start: hcl.Pos{Line: 2, Column: 14, Byte: 51},
|
|
|
|
End: hcl.Pos{Line: 2, Column: 22, Byte: 59},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !got.Provider.Equals(wantProvider) {
|
|
|
|
t.Fatalf("wrong provider %s, want %s", got.Provider, wantProvider)
|
|
|
|
}
|
|
|
|
assertResultDeepEqual(t, got.ProviderConfigRef, wantProviderCfg)
|
|
|
|
|
|
|
|
// now verify that a resource with no provider config falls back to default
|
|
|
|
got = mod.ManagedResources["test_instance.default"]
|
2020-03-31 20:03:33 +02:00
|
|
|
wantProvider = addrs.NewDefaultProvider("test")
|
2020-03-16 19:36:16 +01:00
|
|
|
if !got.Provider.Equals(wantProvider) {
|
|
|
|
t.Fatalf("wrong provider %s, want %s", got.Provider, wantProvider)
|
|
|
|
}
|
|
|
|
if got.ProviderConfigRef != nil {
|
|
|
|
t.Fatalf("wrong result: found provider config ref %s, expected nil", got.ProviderConfigRef)
|
|
|
|
}
|
|
|
|
}
|