terraform/internal/configs/module_call_test.go

193 lines
4.8 KiB
Go
Raw Normal View History

package configs
import (
"io/ioutil"
"testing"
"github.com/go-test/deep"
"github.com/hashicorp/hcl/v2"
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
"github.com/hashicorp/terraform/internal/addrs"
)
func TestLoadModuleCall(t *testing.T) {
src, err := ioutil.ReadFile("testdata/invalid-files/module-calls.tf")
if err != nil {
t.Fatal(err)
}
parser := testParser(map[string]string{
"module-calls.tf": string(src),
})
file, diags := parser.LoadConfigFile("module-calls.tf")
assertExactDiagnostics(t, diags, []string{
2020-04-07 20:18:08 +02:00
`module-calls.tf:20,3-11: Invalid combination of "count" and "for_each"; The "count" and "for_each" meta-arguments are mutually-exclusive, only one should be used to be explicit about the number of resources to be created.`,
})
gotModules := file.ModuleCalls
wantModules := []*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: "foo",
SourceAddr: addrs.ModuleSourceLocal("./foo"),
SourceAddrRaw: "./foo",
SourceSet: true,
SourceAddrRange: hcl.Range{
Filename: "module-calls.tf",
Start: hcl.Pos{Line: 3, Column: 12, Byte: 27},
End: hcl.Pos{Line: 3, Column: 19, Byte: 34},
},
DeclRange: hcl.Range{
Filename: "module-calls.tf",
Start: hcl.Pos{Line: 2, Column: 1, Byte: 1},
End: hcl.Pos{Line: 2, Column: 13, Byte: 13},
},
},
{
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: "bar",
SourceAddr: addrs.ModuleSourceRegistry{
addrs: ModuleRegistryPackage for representing module registry packages Previously we had a separation between ModuleSourceRemote and ModulePackage as a way to represent within the type system that there's an important difference between a module source address and a package address, because module packages often contain multiple modules and so a ModuleSourceRemote combines a ModulePackage with a subdirectory to represent one specific module. This commit applies that same strategy to ModuleSourceRegistry, creating a new type ModuleRegistryPackage to represent the different sort of package that we use for registry modules. Again, the main goal here is to try to reflect the conceptual modelling more directly in the type system so that we can more easily verify that uses of these different address types are correct. To make use of that, I've also lightly reworked initwd's module installer to use addrs.ModuleRegistryPackage directly, instead of a string representation thereof. This was in response to some earlier commits where I found myself accidentally mixing up package addresses and source addresses in the installRegistryModule method; with this new organization those bugs would've been caught at compile time, rather than only at unit and integration testing time. While in the area anyway, I also took this opportunity to fix some historical confusing names of fields in initwd.ModuleInstaller, to be clearer that they are only for registry packages and not for all module source address types.
2021-06-02 21:26:35 +02:00
PackageAddr: addrs.ModuleRegistryPackage{
Host: addrs.DefaultModuleRegistryHost,
Namespace: "hashicorp",
Name: "bar",
TargetSystem: "aws",
},
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
},
SourceAddrRaw: "hashicorp/bar/aws",
SourceSet: true,
SourceAddrRange: hcl.Range{
Filename: "module-calls.tf",
Start: hcl.Pos{Line: 8, Column: 12, Byte: 113},
End: hcl.Pos{Line: 8, Column: 31, Byte: 132},
},
DeclRange: hcl.Range{
Filename: "module-calls.tf",
Start: hcl.Pos{Line: 7, Column: 1, Byte: 87},
End: hcl.Pos{Line: 7, Column: 13, Byte: 99},
},
},
{
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: "baz",
SourceAddr: addrs.ModuleSourceRemote{
PackageAddr: addrs.ModulePackage("git::https://example.com/"),
},
SourceAddrRaw: "git::https://example.com/",
SourceSet: true,
SourceAddrRange: hcl.Range{
Filename: "module-calls.tf",
Start: hcl.Pos{Line: 15, Column: 12, Byte: 193},
End: hcl.Pos{Line: 15, Column: 39, Byte: 220},
},
DependsOn: []hcl.Traversal{
{
hcl.TraverseRoot{
Name: "module",
SrcRange: hcl.Range{
Filename: "module-calls.tf",
Start: hcl.Pos{Line: 23, Column: 5, Byte: 295},
End: hcl.Pos{Line: 23, Column: 11, Byte: 301},
},
},
hcl.TraverseAttr{
Name: "bar",
SrcRange: hcl.Range{
Filename: "module-calls.tf",
Start: hcl.Pos{Line: 23, Column: 11, Byte: 301},
End: hcl.Pos{Line: 23, Column: 15, Byte: 305},
},
},
},
},
Providers: []PassedProviderConfig{
{
InChild: &ProviderConfigRef{
Name: "aws",
NameRange: hcl.Range{
Filename: "module-calls.tf",
Start: hcl.Pos{Line: 27, Column: 5, Byte: 332},
End: hcl.Pos{Line: 27, Column: 8, Byte: 335},
},
},
InParent: &ProviderConfigRef{
Name: "aws",
NameRange: hcl.Range{
Filename: "module-calls.tf",
Start: hcl.Pos{Line: 27, Column: 11, Byte: 338},
End: hcl.Pos{Line: 27, Column: 14, Byte: 341},
},
Alias: "foo",
AliasRange: &hcl.Range{
Filename: "module-calls.tf",
Start: hcl.Pos{Line: 27, Column: 14, Byte: 341},
End: hcl.Pos{Line: 27, Column: 18, Byte: 345},
},
},
},
},
DeclRange: hcl.Range{
Filename: "module-calls.tf",
Start: hcl.Pos{Line: 14, Column: 1, Byte: 167},
End: hcl.Pos{Line: 14, Column: 13, Byte: 179},
},
},
}
// We'll hide all of the bodies/exprs since we're treating them as opaque
// here anyway... the point of this test is to ensure we handle everything
// else properly.
for _, m := range gotModules {
m.Config = nil
m.Count = nil
m.ForEach = nil
}
for _, problem := range deep.Equal(gotModules, wantModules) {
t.Error(problem)
}
}
func TestModuleSourceAddrEntersNewPackage(t *testing.T) {
tests := []struct {
Addr string
Want bool
}{
{
"./",
false,
},
{
"../bork",
false,
},
{
"/absolute/path",
true,
},
{
"github.com/example/foo",
true,
},
{
"hashicorp/subnets/cidr", // registry module
true,
},
{
"registry.terraform.io/hashicorp/subnets/cidr", // registry module
true,
},
}
for _, test := range tests {
t.Run(test.Addr, func(t *testing.T) {
addr, err := addrs.ParseModuleSource(test.Addr)
if err != nil {
t.Fatalf("parsing failed for %q: %s", test.Addr, err)
}
got := moduleSourceAddrEntersNewPackage(addr)
if got != test.Want {
t.Errorf("wrong result for %q\ngot: %#v\nwant: %#v", addr, got, test.Want)
}
})
}
}