mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
a3403f2766
Due to how often the state and plan types are referenced throughout Terraform, there isn't a great way to switch them out gradually. As a consequence, this huge commit gets us from the old world to a _compilable_ new world, but still has a large number of known test failures due to key functionality being stubbed out. The stubs here are for anything that interacts with providers, since we now need to do the follow-up work to similarly replace the old terraform.ResourceProvider interface with its replacement in the new "providers" package. That work, along with work to fix the remaining failing tests, will follow in subsequent commits. The aim here was to replace all references to terraform.State and its downstream types with states.State, terraform.Plan with plans.Plan, state.State with statemgr.State, and switch to the new implementations of the state and plan file formats. However, due to the number of times those types are used, this also ended up affecting numerous other parts of core such as terraform.Hook, the backend.Backend interface, and most of the CLI commands. Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize in advance to the person who inevitably just found this huge commit while spelunking through the commit history.
260 lines
6.5 KiB
Go
260 lines
6.5 KiB
Go
package terraform
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/go-test/deep"
|
|
|
|
"github.com/hashicorp/terraform/configs"
|
|
"github.com/hashicorp/terraform/moduledeps"
|
|
"github.com/hashicorp/terraform/plugin/discovery"
|
|
)
|
|
|
|
func TestModuleTreeDependencies(t *testing.T) {
|
|
tests := map[string]struct {
|
|
ConfigDir string // directory name from test-fixtures dir
|
|
State *State
|
|
Want *moduledeps.Module
|
|
}{
|
|
"no config or state": {
|
|
"",
|
|
nil,
|
|
&moduledeps.Module{
|
|
Name: "root",
|
|
Providers: moduledeps.Providers{},
|
|
Children: nil,
|
|
},
|
|
},
|
|
"empty config no state": {
|
|
"empty",
|
|
nil,
|
|
&moduledeps.Module{
|
|
Name: "root",
|
|
Providers: moduledeps.Providers{},
|
|
Children: nil,
|
|
},
|
|
},
|
|
"explicit provider": {
|
|
"module-deps-explicit-provider",
|
|
nil,
|
|
&moduledeps.Module{
|
|
Name: "root",
|
|
Providers: moduledeps.Providers{
|
|
"foo": moduledeps.ProviderDependency{
|
|
Constraints: discovery.ConstraintStr(">=1.0.0").MustParse(),
|
|
Reason: moduledeps.ProviderDependencyExplicit,
|
|
},
|
|
"foo.bar": moduledeps.ProviderDependency{
|
|
Constraints: discovery.ConstraintStr(">=2.0.0").MustParse(),
|
|
Reason: moduledeps.ProviderDependencyExplicit,
|
|
},
|
|
},
|
|
Children: nil,
|
|
},
|
|
},
|
|
"explicit provider unconstrained": {
|
|
"module-deps-explicit-provider-unconstrained",
|
|
nil,
|
|
&moduledeps.Module{
|
|
Name: "root",
|
|
Providers: moduledeps.Providers{
|
|
"foo": moduledeps.ProviderDependency{
|
|
Constraints: discovery.AllVersions,
|
|
Reason: moduledeps.ProviderDependencyExplicit,
|
|
},
|
|
},
|
|
Children: nil,
|
|
},
|
|
},
|
|
"implicit provider": {
|
|
"module-deps-implicit-provider",
|
|
nil,
|
|
&moduledeps.Module{
|
|
Name: "root",
|
|
Providers: moduledeps.Providers{
|
|
"foo": moduledeps.ProviderDependency{
|
|
Constraints: discovery.AllVersions,
|
|
Reason: moduledeps.ProviderDependencyImplicit,
|
|
},
|
|
"foo.baz": moduledeps.ProviderDependency{
|
|
Constraints: discovery.AllVersions,
|
|
Reason: moduledeps.ProviderDependencyImplicit,
|
|
},
|
|
},
|
|
Children: nil,
|
|
},
|
|
},
|
|
"explicit provider with resource": {
|
|
"module-deps-explicit-provider-resource",
|
|
nil,
|
|
&moduledeps.Module{
|
|
Name: "root",
|
|
Providers: moduledeps.Providers{
|
|
"foo": moduledeps.ProviderDependency{
|
|
Constraints: discovery.ConstraintStr(">=1.0.0").MustParse(),
|
|
Reason: moduledeps.ProviderDependencyExplicit,
|
|
},
|
|
},
|
|
Children: nil,
|
|
},
|
|
},
|
|
"inheritance of providers": {
|
|
"module-deps-inherit-provider",
|
|
nil,
|
|
&moduledeps.Module{
|
|
Name: "root",
|
|
Providers: moduledeps.Providers{
|
|
"foo": moduledeps.ProviderDependency{
|
|
Constraints: discovery.AllVersions,
|
|
Reason: moduledeps.ProviderDependencyExplicit,
|
|
},
|
|
"bar": moduledeps.ProviderDependency{
|
|
Constraints: discovery.AllVersions,
|
|
Reason: moduledeps.ProviderDependencyExplicit,
|
|
},
|
|
},
|
|
Children: []*moduledeps.Module{
|
|
{
|
|
Name: "child",
|
|
Providers: moduledeps.Providers{
|
|
"foo": moduledeps.ProviderDependency{
|
|
Constraints: discovery.AllVersions,
|
|
Reason: moduledeps.ProviderDependencyInherited,
|
|
},
|
|
"baz": moduledeps.ProviderDependency{
|
|
Constraints: discovery.AllVersions,
|
|
Reason: moduledeps.ProviderDependencyImplicit,
|
|
},
|
|
},
|
|
Children: []*moduledeps.Module{
|
|
{
|
|
Name: "grandchild",
|
|
Providers: moduledeps.Providers{
|
|
"bar": moduledeps.ProviderDependency{
|
|
Constraints: discovery.AllVersions,
|
|
Reason: moduledeps.ProviderDependencyInherited,
|
|
},
|
|
"foo": moduledeps.ProviderDependency{
|
|
Constraints: discovery.AllVersions,
|
|
Reason: moduledeps.ProviderDependencyExplicit,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
"provider from state": {
|
|
"empty",
|
|
&State{
|
|
Modules: []*ModuleState{
|
|
{
|
|
Path: []string{"root"},
|
|
Resources: map[string]*ResourceState{
|
|
"foo_bar.baz": {
|
|
Type: "foo_bar",
|
|
Provider: "",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
&moduledeps.Module{
|
|
Name: "root",
|
|
Providers: moduledeps.Providers{
|
|
"foo": moduledeps.ProviderDependency{
|
|
Constraints: discovery.AllVersions,
|
|
Reason: moduledeps.ProviderDependencyFromState,
|
|
},
|
|
},
|
|
Children: nil,
|
|
},
|
|
},
|
|
"providers in both config and state": {
|
|
"module-deps-explicit-provider",
|
|
&State{
|
|
Modules: []*ModuleState{
|
|
{
|
|
Path: []string{"root"},
|
|
Resources: map[string]*ResourceState{
|
|
"foo_bar.test1": {
|
|
Type: "foo_bar",
|
|
Provider: "",
|
|
},
|
|
"foo_bar.test2": {
|
|
Type: "foo_bar",
|
|
Provider: "foo.bar",
|
|
},
|
|
"baz_bar.test": {
|
|
Type: "baz_bar",
|
|
Provider: "",
|
|
},
|
|
},
|
|
},
|
|
// note that we've skipped root.child intentionally here,
|
|
// to verify that we'll infer it based on the following
|
|
// module rather than crashing.
|
|
{
|
|
Path: []string{"root", "child", "grandchild"},
|
|
Resources: map[string]*ResourceState{
|
|
"banana_skin.test": {
|
|
Type: "banana_skin",
|
|
Provider: "",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
&moduledeps.Module{
|
|
Name: "root",
|
|
Providers: moduledeps.Providers{
|
|
"foo": moduledeps.ProviderDependency{
|
|
Constraints: discovery.ConstraintStr(">=1.0.0").MustParse(),
|
|
Reason: moduledeps.ProviderDependencyExplicit,
|
|
},
|
|
"foo.bar": moduledeps.ProviderDependency{
|
|
Constraints: discovery.ConstraintStr(">=2.0.0").MustParse(),
|
|
Reason: moduledeps.ProviderDependencyExplicit,
|
|
},
|
|
"baz": moduledeps.ProviderDependency{
|
|
Constraints: discovery.AllVersions,
|
|
Reason: moduledeps.ProviderDependencyFromState,
|
|
},
|
|
},
|
|
Children: []*moduledeps.Module{
|
|
{
|
|
Name: "child",
|
|
Providers: make(moduledeps.Providers),
|
|
Children: []*moduledeps.Module{
|
|
{
|
|
Name: "grandchild",
|
|
Providers: moduledeps.Providers{
|
|
"banana": moduledeps.ProviderDependency{
|
|
Constraints: discovery.AllVersions,
|
|
Reason: moduledeps.ProviderDependencyFromState,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for name, test := range tests {
|
|
t.Run(name, func(t *testing.T) {
|
|
var root *configs.Config
|
|
if test.ConfigDir != "" {
|
|
root = testModule(t, test.ConfigDir)
|
|
}
|
|
|
|
got := ConfigTreeDependencies(root, mustShimLegacyState(test.State))
|
|
for _, problem := range deep.Equal(got, test.Want) {
|
|
t.Error(problem)
|
|
}
|
|
})
|
|
}
|
|
}
|