mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
3f6ce3c588
* terraform: add helper functions for creating test state testSetResourceInstanceCurrent and testSetResourceInstanceTainted are wrapper functions around states.Module.SetResourceInstanceCurrent() used to set a resource in state. They work with current, non-deposed resources with no dependencies. testSetResourceInstanceDeposed can be used to set a desosed resource in state. * terraform: update all tests to use modern providers and state
73 lines
1.5 KiB
Go
73 lines
1.5 KiB
Go
package terraform
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
"github.com/hashicorp/terraform/plans"
|
|
)
|
|
|
|
func TestDiffTransformer_nilDiff(t *testing.T) {
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
|
tf := &DiffTransformer{}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
if len(g.Vertices()) > 0 {
|
|
t.Fatal("graph should be empty")
|
|
}
|
|
}
|
|
|
|
func TestDiffTransformer(t *testing.T) {
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
|
|
|
beforeVal, err := plans.NewDynamicValue(cty.StringVal(""), cty.String)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
afterVal, err := plans.NewDynamicValue(cty.StringVal(""), cty.String)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
tf := &DiffTransformer{
|
|
Changes: &plans.Changes{
|
|
Resources: []*plans.ResourceInstanceChangeSrc{
|
|
{
|
|
Addr: addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "aws_instance",
|
|
Name: "foo",
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
ProviderAddr: addrs.AbsProviderConfig{
|
|
Provider: addrs.NewDefaultProvider("aws"),
|
|
Module: addrs.RootModule,
|
|
},
|
|
ChangeSrc: plans.ChangeSrc{
|
|
Action: plans.Update,
|
|
Before: beforeVal,
|
|
After: afterVal,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
expected := strings.TrimSpace(testTransformDiffBasicStr)
|
|
if actual != expected {
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
}
|
|
}
|
|
|
|
const testTransformDiffBasicStr = `
|
|
aws_instance.foo
|
|
`
|