2023-05-02 11:33:06 -04:00
// Copyright IBM Corp. 2014, 2026
2023-08-10 18:43:27 -04:00
// SPDX-License-Identifier: BUSL-1.1
2023-05-02 11:33:06 -04:00
2018-04-24 13:06:51 -04:00
package configs
import (
2026-02-11 06:15:23 -05:00
"os"
2018-04-24 13:06:51 -04:00
"testing"
"github.com/go-test/deep"
2019-09-09 18:58:44 -04:00
"github.com/hashicorp/hcl/v2"
moduleaddrs: Split module source address parsing from package addrs
Our package addrs ends up getting imported from just about ever other
package in Terraform, because it contains the types we use to talk about
various different kinds of objects. Therefore we typically try to keep its
transitive dependency graph relatively small, because anything it depends
on becomes an indirect dependency of nearly everything else.
A while back we moved the module source address models into package addrs,
which also brought with them the code for parsing strings to produce those
addresses. Unfortunately, remote module source addresses are defined using
the external dependency go-getter, which is pretty heavy itself and also
brings with it numerous other external dependencies such as the AWS SDK,
the Google Cloud Platform SDK, etc.
Since only relatively few packages actually need to _parse_ source
addresses -- with most either not caring about them at all or only
consuming addresses that were already parsed by someone else -- we'll
move the parser functions into their own package, while keeping the
resulting address types in package addrs.
This does still retain the package addrs dependency on external module
github.com/hashicorp/terraform-registry-address, which is not ideal but
that one at least has a relatively shallow dependency subgraph, so there's
not so much urgency to tidy that one.
2024-03-13 18:44:36 -04:00
2026-02-26 10:31:32 -05:00
"github.com/hashicorp/hcl/v2/hclsyntax"
2018-04-24 13:06:51 -04:00
)
func TestLoadModuleCall ( t * testing . T ) {
2026-02-11 06:15:23 -05:00
src , err := os . ReadFile ( "testdata/invalid-files/module-calls.tf" )
2018-04-24 13:06:51 -04:00
if err != nil {
t . Fatal ( err )
}
parser := testParser ( map [ string ] string {
"module-calls.tf" : string ( src ) ,
} )
file , diags := parser . LoadConfigFile ( "module-calls.tf" )
2018-11-20 14:53:45 -05:00
assertExactDiagnostics ( t , diags , [ ] string {
2020-04-07 14:18:08 -04: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. ` ,
2018-11-20 14:53:45 -05:00
} )
2018-04-24 13:06:51 -04:00
gotModules := file . ModuleCalls
wantModules := [ ] * ModuleCall {
{
2026-02-26 10:31:32 -05:00
Name : "foo" ,
SourceExpr : mustExpr ( hclsyntax . ParseExpression (
[ ] byte ( "\"./foo\"" ) , "module-calls.tf" ,
hcl . Pos { Line : 3 , Column : 12 , Byte : 27 } ,
) ) ,
2018-04-24 13:06:51 -04:00
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-27 22:24:59 -04:00
Name : "bar" ,
2026-02-26 10:31:32 -05:00
SourceExpr : mustExpr ( hclsyntax . ParseExpression (
[ ] byte ( "\"hashicorp/bar/aws\"" ) , "module-calls.tf" ,
hcl . Pos { Line : 8 , Column : 12 , Byte : 113 } ,
) ) ,
2018-04-24 13:06:51 -04:00
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-27 22:24:59 -04:00
Name : "baz" ,
2026-02-26 10:31:32 -05:00
SourceExpr : mustExpr ( hclsyntax . ParseExpression (
[ ] byte ( "\"git::https://example.com/\"" ) , "module-calls.tf" ,
hcl . Pos { Line : 15 , Column : 12 , Byte : 193 } ,
) ) ,
2018-04-24 13:06:51 -04:00
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 )
}
}