2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-09-24 14:30:58 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-05-17 14:00:50 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 14:17:09 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/configs"
|
2021-05-17 12:34:40 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/instances"
|
2021-05-17 14:43:35 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/states"
|
2020-09-24 14:30:58 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNodeApplyableResourceExecute(t *testing.T) {
|
|
|
|
state := states.NewState()
|
|
|
|
ctx := &MockEvalContext{
|
|
|
|
StateState: state.SyncWrapper(),
|
|
|
|
InstanceExpanderExpander: instances.NewExpander(),
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("no config", func(t *testing.T) {
|
|
|
|
node := NodeApplyableResource{
|
|
|
|
NodeAbstractResource: &NodeAbstractResource{
|
|
|
|
Config: nil,
|
|
|
|
},
|
|
|
|
Addr: mustAbsResourceAddr("test_instance.foo"),
|
|
|
|
}
|
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.Fatalf("expected no state, got:\n %s", state.String())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("simple", func(t *testing.T) {
|
|
|
|
|
|
|
|
node := NodeApplyableResource{
|
|
|
|
NodeAbstractResource: &NodeAbstractResource{
|
|
|
|
Config: &configs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
ResolvedProvider: addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Addr: mustAbsResourceAddr("test_instance.foo"),
|
|
|
|
}
|
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")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|