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-24 14:30:58 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
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-24 14:30:58 -05:00
|
|
|
)
|
|
|
|
|
2023-06-20 15:21:21 -05:00
|
|
|
func TestNodeExpandApplyableResourceExecute(t *testing.T) {
|
2020-09-24 14:30:58 -05:00
|
|
|
state := states.NewState()
|
|
|
|
t.Run("no config", func(t *testing.T) {
|
2023-06-20 15:21:21 -05:00
|
|
|
ctx := &MockEvalContext{
|
|
|
|
StateState: state.SyncWrapper(),
|
|
|
|
InstanceExpanderExpander: instances.NewExpander(),
|
|
|
|
}
|
|
|
|
|
|
|
|
node := &nodeExpandApplyableResource{
|
2020-09-24 14:30:58 -05:00
|
|
|
NodeAbstractResource: &NodeAbstractResource{
|
2023-06-20 15:21:21 -05:00
|
|
|
Addr: mustConfigResourceAddr("test_instance.foo"),
|
2020-09-24 14:30:58 -05:00
|
|
|
Config: nil,
|
|
|
|
},
|
|
|
|
}
|
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-24 14:30:58 -05:00
|
|
|
}
|
2023-06-20 15:21:21 -05:00
|
|
|
|
|
|
|
state.PruneResourceHusks()
|
2020-09-24 14:30:58 -05:00
|
|
|
if !state.Empty() {
|
|
|
|
t.Fatalf("expected no state, got:\n %s", state.String())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("simple", func(t *testing.T) {
|
2023-06-20 15:21:21 -05:00
|
|
|
ctx := &MockEvalContext{
|
|
|
|
StateState: state.SyncWrapper(),
|
|
|
|
InstanceExpanderExpander: instances.NewExpander(),
|
|
|
|
}
|
2020-09-24 14:30:58 -05:00
|
|
|
|
2023-06-20 15:21:21 -05:00
|
|
|
node := &nodeExpandApplyableResource{
|
2020-09-24 14:30:58 -05:00
|
|
|
NodeAbstractResource: &NodeAbstractResource{
|
2023-06-20 15:21:21 -05:00
|
|
|
Addr: mustConfigResourceAddr("test_instance.foo"),
|
2020-09-24 14:30:58 -05:00
|
|
|
Config: &configs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
ResolvedProvider: addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
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-24 14:30:58 -05:00
|
|
|
}
|
|
|
|
if state.Empty() {
|
|
|
|
t.Fatal("expected resources in state, got empty state")
|
|
|
|
}
|
|
|
|
r := state.Resource(mustAbsResourceAddr("test_instance.foo"))
|
|
|
|
if r == nil {
|
|
|
|
t.Fatal("test_instance.foo not found in state")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|