mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 17:01:04 -06:00
1a1225ae29
* terraform: refactor EvalLocal, remove unused EvalDeleteLocal * terraform: refactor NodeCountBoundary * terraform: node_module_expand refactor
73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
package terraform
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/hashicorp/hcl/v2/hcltest"
|
|
"github.com/hashicorp/terraform/addrs"
|
|
"github.com/hashicorp/terraform/configs"
|
|
"github.com/hashicorp/terraform/states"
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
func TestNodeCountBoundaryExecute(t *testing.T) {
|
|
|
|
// Create a state with a single instance (addrs.NoKey) of test_instance.foo
|
|
state := states.NewState()
|
|
state.Module(addrs.RootModuleInstance).SetResourceInstanceCurrent(
|
|
addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_instance",
|
|
Name: "foo",
|
|
}.Instance(addrs.NoKey),
|
|
&states.ResourceInstanceObjectSrc{
|
|
Status: states.ObjectReady,
|
|
AttrsJSON: []byte(`{"type":"string","value":"hello"}`),
|
|
},
|
|
addrs.AbsProviderConfig{
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
Module: addrs.RootModule,
|
|
},
|
|
)
|
|
|
|
// Create a config that uses count to create 2 instances of test_instance.foo
|
|
rc := &configs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_instance",
|
|
Name: "foo",
|
|
Count: hcltest.MockExprLiteral(cty.NumberIntVal(2)),
|
|
Config: configs.SynthBody("", map[string]cty.Value{
|
|
"test_string": cty.StringVal("hello"),
|
|
}),
|
|
}
|
|
config := &configs.Config{
|
|
Module: &configs.Module{
|
|
ManagedResources: map[string]*configs.Resource{
|
|
"test_instance.foo": rc,
|
|
},
|
|
},
|
|
}
|
|
|
|
ctx := &MockEvalContext{
|
|
StateState: state.SyncWrapper(),
|
|
}
|
|
node := NodeCountBoundary{Config: config}
|
|
|
|
err := node.Execute(ctx, walkApply)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %s", err.Error())
|
|
}
|
|
if !state.HasResources() {
|
|
t.Fatal("resources missing from state")
|
|
}
|
|
|
|
// verify that the resource changed from test_instance.foo to
|
|
// test_instance.foo.0 in the state
|
|
actual := state.String()
|
|
expected := "test_instance.foo.0:\n ID = \n provider = provider[\"registry.terraform.io/hashicorp/test\"]\n type = string\n value = hello"
|
|
|
|
if actual != expected {
|
|
t.Fatalf("wrong result: %s", actual)
|
|
}
|
|
}
|