mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-15 19:22:46 -06:00
72ad927c4d
Previously the behavior for loading and installing modules was included in the same package as the representation of the module tree (in the config/module package). In our new world, the model of a module tree (now called a "Config") is included in "configs" along with the Module and File structs. This new package replaces the loading and installation functionality previously in config/module with new equivalents that work with the model objects in "configs". As of this commit, only the loading functionality is implemented. The installation functionality will follow in subsequent commits.
61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
package configload
|
|
|
|
import (
|
|
"path/filepath"
|
|
"reflect"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
"github.com/hashicorp/terraform/configs"
|
|
)
|
|
|
|
func TestLoaderLoadConfig_okay(t *testing.T) {
|
|
fixtureDir := filepath.Clean("test-fixtures/already-installed")
|
|
loader, err := NewLoader(&Config{
|
|
ModulesDir: filepath.Join(fixtureDir, ".terraform/modules"),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error from NewLoader: %s", err)
|
|
}
|
|
|
|
cfg, diags := loader.LoadConfig(fixtureDir)
|
|
assertNoDiagnostics(t, diags)
|
|
if cfg == nil {
|
|
t.Fatalf("config is nil; want non-nil")
|
|
}
|
|
|
|
var gotPaths []string
|
|
cfg.DeepEach(func(c *configs.Config) {
|
|
gotPaths = append(gotPaths, strings.Join(c.Path, "."))
|
|
})
|
|
sort.Strings(gotPaths)
|
|
wantPaths := []string{
|
|
"", // root module
|
|
"child_a",
|
|
"child_a.child_c",
|
|
"child_b",
|
|
"child_b.child_d",
|
|
}
|
|
|
|
if !reflect.DeepEqual(gotPaths, wantPaths) {
|
|
t.Fatalf("wrong module paths\ngot: %swant %s", spew.Sdump(gotPaths), spew.Sdump(wantPaths))
|
|
}
|
|
|
|
t.Run("child_a.child_c output", func(t *testing.T) {
|
|
output := cfg.Children["child_a"].Children["child_c"].Module.Outputs["hello"]
|
|
got, diags := output.Expr.Value(nil)
|
|
assertNoDiagnostics(t, diags)
|
|
assertResultCtyEqual(t, got, cty.StringVal("Hello from child_c"))
|
|
})
|
|
t.Run("child_b.child_d output", func(t *testing.T) {
|
|
output := cfg.Children["child_b"].Children["child_d"].Module.Outputs["hello"]
|
|
got, diags := output.Expr.Value(nil)
|
|
assertNoDiagnostics(t, diags)
|
|
assertResultCtyEqual(t, got, cty.StringVal("Hello from child_d"))
|
|
})
|
|
}
|