2018-02-02 04:07:02 +01:00
|
|
|
package configs
|
|
|
|
|
|
|
|
import (
|
2020-03-12 17:00:00 +01:00
|
|
|
"fmt"
|
2018-02-02 04:07:02 +01:00
|
|
|
"os"
|
|
|
|
"path"
|
2020-03-12 17:00:00 +01:00
|
|
|
"path/filepath"
|
2018-02-07 03:05:14 +01:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2018-02-02 04:07:02 +01:00
|
|
|
|
2018-02-07 03:05:14 +01:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
|
2020-03-12 17:00:00 +01:00
|
|
|
version "github.com/hashicorp/go-version"
|
2019-09-10 00:58:44 +02:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2018-02-02 04:07:02 +01:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
// testParser returns a parser that reads files from the given map, which
|
|
|
|
// is from paths to file contents.
|
|
|
|
//
|
|
|
|
// Since this function uses only in-memory objects, it should never fail.
|
|
|
|
// If any errors are encountered in practice, this function will panic.
|
|
|
|
func testParser(files map[string]string) *Parser {
|
|
|
|
fs := afero.Afero{Fs: afero.NewMemMapFs()}
|
|
|
|
|
|
|
|
for filePath, contents := range files {
|
|
|
|
dirPath := path.Dir(filePath)
|
|
|
|
err := fs.MkdirAll(dirPath, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
err = fs.WriteFile(filePath, []byte(contents), os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewParser(fs)
|
|
|
|
}
|
2018-02-07 03:05:14 +01:00
|
|
|
|
2020-03-12 17:00:00 +01:00
|
|
|
// testModuleConfigFrom File reads a single file from the given path as a
|
|
|
|
// module and returns its configuration. This is a helper for use in unit tests.
|
|
|
|
func testModuleConfigFromFile(filename string) (*Config, hcl.Diagnostics) {
|
|
|
|
parser := NewParser(nil)
|
|
|
|
f, diags := parser.LoadConfigFile(filename)
|
|
|
|
mod, modDiags := NewModule([]*File{f}, nil)
|
|
|
|
diags = append(diags, modDiags...)
|
|
|
|
cfg, moreDiags := BuildConfig(mod, nil)
|
|
|
|
return cfg, append(diags, moreDiags...)
|
|
|
|
}
|
|
|
|
|
2018-02-07 03:05:14 +01:00
|
|
|
// testModuleFromDir reads configuration from the given directory path as
|
|
|
|
// a module and returns it. This is a helper for use in unit tests.
|
|
|
|
func testModuleFromDir(path string) (*Module, hcl.Diagnostics) {
|
|
|
|
parser := NewParser(nil)
|
|
|
|
return parser.LoadConfigDir(path)
|
|
|
|
}
|
|
|
|
|
2020-03-12 17:00:00 +01:00
|
|
|
// testModuleFromDir reads configuration from the given directory path as a
|
|
|
|
// module and returns its configuration. This is a helper for use in unit tests.
|
|
|
|
func testModuleConfigFromDir(path string) (*Config, hcl.Diagnostics) {
|
|
|
|
parser := NewParser(nil)
|
|
|
|
mod, diags := parser.LoadConfigDir(path)
|
|
|
|
cfg, moreDiags := BuildConfig(mod, nil)
|
|
|
|
return cfg, append(diags, moreDiags...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testNestedModuleConfigFromDir reads configuration from the given directory path as
|
|
|
|
// a module with (optional) submodules and returns its configuration. This is a
|
|
|
|
// helper for use in unit tests.
|
|
|
|
func testNestedModuleConfigFromDir(t *testing.T, path string) (*Config, hcl.Diagnostics) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
parser := NewParser(nil)
|
|
|
|
mod, diags := parser.LoadConfigDir(path)
|
|
|
|
if mod == nil {
|
|
|
|
t.Fatal("got nil root module; want non-nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
versionI := 0
|
2020-09-08 14:19:00 +02:00
|
|
|
cfg, nestedDiags := BuildConfig(mod, ModuleWalkerFunc(
|
2020-03-12 17:00:00 +01:00
|
|
|
func(req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics) {
|
|
|
|
// For the sake of this test we're going to just treat our
|
2020-06-22 18:13:35 +02:00
|
|
|
// SourceAddr as a path relative to the calling module.
|
2020-03-12 17:00:00 +01:00
|
|
|
// A "real" implementation of ModuleWalker should accept the
|
|
|
|
// various different source address syntaxes Terraform supports.
|
2020-06-22 18:13:35 +02:00
|
|
|
|
|
|
|
// Build a full path by walking up the module tree, prepending each
|
|
|
|
// source address path until we hit the root
|
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
|
|
|
paths := []string{req.SourceAddr.String()}
|
2020-06-22 18:13:35 +02:00
|
|
|
for config := req.Parent; config != nil && config.Parent != nil; config = config.Parent {
|
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
|
|
|
paths = append([]string{config.SourceAddr.String()}, paths...)
|
2020-06-22 18:13:35 +02:00
|
|
|
}
|
|
|
|
paths = append([]string{path}, paths...)
|
|
|
|
sourcePath := filepath.Join(paths...)
|
2020-03-12 17:00:00 +01:00
|
|
|
|
|
|
|
mod, diags := parser.LoadConfigDir(sourcePath)
|
|
|
|
version, _ := version.NewVersion(fmt.Sprintf("1.0.%d", versionI))
|
|
|
|
versionI++
|
|
|
|
return mod, version, diags
|
|
|
|
},
|
|
|
|
))
|
2020-09-08 14:19:00 +02:00
|
|
|
|
|
|
|
diags = append(diags, nestedDiags...)
|
2020-03-12 17:00:00 +01:00
|
|
|
return cfg, diags
|
|
|
|
}
|
|
|
|
|
2018-02-07 03:05:14 +01:00
|
|
|
func assertNoDiagnostics(t *testing.T, diags hcl.Diagnostics) bool {
|
|
|
|
t.Helper()
|
|
|
|
return assertDiagnosticCount(t, diags, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertDiagnosticCount(t *testing.T, diags hcl.Diagnostics, want int) bool {
|
|
|
|
t.Helper()
|
2020-03-12 17:00:00 +01:00
|
|
|
if len(diags) != want {
|
2018-02-07 03:05:14 +01:00
|
|
|
t.Errorf("wrong number of diagnostics %d; want %d", len(diags), want)
|
|
|
|
for _, diag := range diags {
|
|
|
|
t.Logf("- %s", diag)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertDiagnosticSummary(t *testing.T, diags hcl.Diagnostics, want string) bool {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
for _, diag := range diags {
|
|
|
|
if diag.Summary == want {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Errorf("missing diagnostic summary %q", want)
|
|
|
|
for _, diag := range diags {
|
|
|
|
t.Logf("- %s", diag)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-11-20 20:53:45 +01:00
|
|
|
func assertExactDiagnostics(t *testing.T, diags hcl.Diagnostics, want []string) bool {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
gotDiags := map[string]bool{}
|
|
|
|
wantDiags := map[string]bool{}
|
|
|
|
|
|
|
|
for _, diag := range diags {
|
|
|
|
gotDiags[diag.Error()] = true
|
|
|
|
}
|
|
|
|
for _, msg := range want {
|
|
|
|
wantDiags[msg] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
bad := false
|
|
|
|
for got := range gotDiags {
|
|
|
|
if _, exists := wantDiags[got]; !exists {
|
|
|
|
t.Errorf("unexpected diagnostic: %s", got)
|
|
|
|
bad = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for want := range wantDiags {
|
|
|
|
if _, exists := gotDiags[want]; !exists {
|
|
|
|
t.Errorf("missing expected diagnostic: %s", want)
|
|
|
|
bad = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bad
|
|
|
|
}
|
|
|
|
|
2018-02-07 03:05:14 +01:00
|
|
|
func assertResultDeepEqual(t *testing.T, got, want interface{}) bool {
|
|
|
|
t.Helper()
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(want))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2018-02-07 03:30:12 +01:00
|
|
|
|
|
|
|
func stringPtr(s string) *string {
|
|
|
|
return &s
|
|
|
|
}
|