2024-02-08 03:48:59 -06:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 10:33:06 -05:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-09-20 07:16:53 -05:00
|
|
|
package tofu
|
2020-09-09 14:59:29 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/hcl/v2/hcltest"
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/addrs"
|
|
|
|
"github.com/opentofu/opentofu/internal/configs"
|
|
|
|
"github.com/opentofu/opentofu/internal/instances"
|
|
|
|
"github.com/opentofu/opentofu/internal/states"
|
2020-09-09 14:59:29 -05:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNodeExpandModuleExecute(t *testing.T) {
|
|
|
|
ctx := &MockEvalContext{
|
|
|
|
InstanceExpanderExpander: instances.NewExpander(),
|
|
|
|
}
|
|
|
|
ctx.installSimpleEval()
|
|
|
|
|
|
|
|
node := nodeExpandModule{
|
|
|
|
Addr: addrs.Module{"child"},
|
|
|
|
ModuleCall: &configs.ModuleCall{
|
|
|
|
Count: hcltest.MockExprLiteral(cty.NumberIntVal(2)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := node.Execute(ctx, walkApply)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ctx.InstanceExpanderCalled {
|
|
|
|
t.Fatal("did not expand")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeCloseModuleExecute(t *testing.T) {
|
|
|
|
t.Run("walkApply", func(t *testing.T) {
|
|
|
|
state := states.NewState()
|
|
|
|
state.EnsureModule(addrs.RootModuleInstance.Child("child", addrs.NoKey))
|
|
|
|
ctx := &MockEvalContext{
|
|
|
|
StateState: state.SyncWrapper(),
|
|
|
|
}
|
2024-06-06 05:20:41 -05:00
|
|
|
node := nodeCloseModule{Addr: addrs.Module{"child"}}
|
2020-10-28 12:47:04 -05:00
|
|
|
diags := node.Execute(ctx, walkApply)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2020-09-09 14:59:29 -05:00
|
|
|
}
|
|
|
|
|
2022-11-22 08:11:10 -06:00
|
|
|
// Since module.child has no resources, it should be removed
|
|
|
|
if _, ok := state.Modules["module.child"]; !ok {
|
|
|
|
t.Fatal("module.child should not be removed from state yet")
|
|
|
|
}
|
|
|
|
|
|
|
|
// the root module should do all the module cleanup
|
2024-06-06 05:20:41 -05:00
|
|
|
node = nodeCloseModule{Addr: addrs.RootModule}
|
2022-11-22 08:11:10 -06:00
|
|
|
diags = node.Execute(ctx, walkApply)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
|
|
|
}
|
|
|
|
|
2020-09-09 14:59:29 -05:00
|
|
|
// Since module.child has no resources, it should be removed
|
|
|
|
if _, ok := state.Modules["module.child"]; ok {
|
|
|
|
t.Fatal("module.child was not removed from state")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// walkImport is a no-op
|
|
|
|
t.Run("walkImport", func(t *testing.T) {
|
|
|
|
state := states.NewState()
|
|
|
|
state.EnsureModule(addrs.RootModuleInstance.Child("child", addrs.NoKey))
|
|
|
|
ctx := &MockEvalContext{
|
|
|
|
StateState: state.SyncWrapper(),
|
|
|
|
}
|
2024-06-06 05:20:41 -05:00
|
|
|
node := nodeCloseModule{Addr: addrs.Module{"child"}}
|
2020-09-09 14:59:29 -05:00
|
|
|
|
2020-10-28 12:47:04 -05:00
|
|
|
diags := node.Execute(ctx, walkImport)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2020-09-09 14:59:29 -05:00
|
|
|
}
|
|
|
|
if _, ok := state.Modules["module.child"]; !ok {
|
|
|
|
t.Fatal("module.child was removed from state, expected no-op")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeValidateModuleExecute(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
ctx := &MockEvalContext{
|
|
|
|
InstanceExpanderExpander: instances.NewExpander(),
|
|
|
|
}
|
|
|
|
ctx.installSimpleEval()
|
|
|
|
node := nodeValidateModule{
|
|
|
|
nodeExpandModule{
|
|
|
|
Addr: addrs.Module{"child"},
|
|
|
|
ModuleCall: &configs.ModuleCall{
|
|
|
|
Count: hcltest.MockExprLiteral(cty.NumberIntVal(2)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-10-28 12:47:04 -05:00
|
|
|
diags := node.Execute(ctx, walkApply)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Fatalf("unexpected error: %v", diags.Err())
|
2020-09-09 14:59:29 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid count", func(t *testing.T) {
|
|
|
|
ctx := &MockEvalContext{
|
|
|
|
InstanceExpanderExpander: instances.NewExpander(),
|
|
|
|
}
|
|
|
|
ctx.installSimpleEval()
|
|
|
|
node := nodeValidateModule{
|
|
|
|
nodeExpandModule{
|
|
|
|
Addr: addrs.Module{"child"},
|
|
|
|
ModuleCall: &configs.ModuleCall{
|
|
|
|
Count: hcltest.MockExprLiteral(cty.StringVal("invalid")),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := node.Execute(ctx, walkApply)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error, got success")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|