opentofu/terraform/transform_provisioner_test.go
Sander van Harmelen 0b1dbf31a3 core: close provider/provisioner connections
Currently Terraform is leaking goroutines and with that memory. I know
strictly speaking this maybe isn’t a real concern for Terraform as it’s
mostly used as a short running command line executable.

But there are a few of us out there that are using Terraform in some
long running processes and then this starts to become a problem.

Next to that it’s of course good programming practise to clean up
resources when they're not needed anymore. So even for the standard
command line use case, this seems an improvement in resource management.

Personally I see no downsides as the primary connection to the plugin
is kept alive (the plugin is not killed) and only unused connections
that will never be used again are closed to free up any related
goroutines and memory.
2015-06-19 21:52:50 +02:00

116 lines
2.5 KiB
Go

package terraform
import (
"strings"
"testing"
"github.com/hashicorp/terraform/dag"
)
func TestMissingProvisionerTransformer(t *testing.T) {
mod := testModule(t, "transform-provisioner-basic")
g := Graph{Path: RootModulePath}
{
tf := &ConfigTransformer{Module: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &MissingProvisionerTransformer{Provisioners: []string{"foo"}}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &CloseProvisionerTransformer{}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testTransformMissingProvisionerBasicStr)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
}
}
func TestPruneProvisionerTransformer(t *testing.T) {
mod := testModule(t, "transform-provisioner-prune")
g := Graph{Path: RootModulePath}
{
tf := &ConfigTransformer{Module: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &MissingProvisionerTransformer{
Provisioners: []string{"foo", "bar"}}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &ProvisionerTransformer{}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &CloseProvisionerTransformer{}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &PruneProvisionerTransformer{}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testTransformPruneProvisionerBasicStr)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
}
}
func TestGraphNodeMissingProvisioner_impl(t *testing.T) {
var _ dag.Vertex = new(graphNodeMissingProvisioner)
var _ dag.NamedVertex = new(graphNodeMissingProvisioner)
var _ GraphNodeProvisioner = new(graphNodeMissingProvisioner)
}
func TestGraphNodeMissingProvisioner_ProvisionerName(t *testing.T) {
n := &graphNodeMissingProvisioner{ProvisionerNameValue: "foo"}
if v := n.ProvisionerName(); v != "foo" {
t.Fatalf("bad: %#v", v)
}
}
const testTransformMissingProvisionerBasicStr = `
aws_instance.web
provisioner.foo
provisioner.shell (close)
aws_instance.web
`
const testTransformPruneProvisionerBasicStr = `
aws_instance.web
provisioner.foo
provisioner.foo
provisioner.foo (close)
aws_instance.web
`