github.com/opentofu/opentofu@v1.7.1/internal/command/e2etest/provisioner_plugin_test.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package e2etest
     7  
     8  import (
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/opentofu/opentofu/internal/e2e"
    15  )
    16  
    17  // TestProvisionerPlugin is a test that tofu can execute a 3rd party
    18  // provisioner plugin.
    19  func TestProvisionerPlugin(t *testing.T) {
    20  	if !canRunGoBuild {
    21  		// We're running in a separate-build-then-run context, so we can't
    22  		// currently execute this test which depends on being able to build
    23  		// new executable at runtime.
    24  		//
    25  		// (See the comment on canRunGoBuild's declaration for more information.)
    26  		t.Skip("can't run without building a new provisioner executable")
    27  	}
    28  	t.Parallel()
    29  
    30  	// This test reaches out to registry.opentofu.org to download the
    31  	// template and null providers, so it can only run if network access is
    32  	// allowed.
    33  	skipIfCannotAccessNetwork(t)
    34  
    35  	tf := e2e.NewBinary(t, tofuBin, "testdata/provisioner-plugin")
    36  
    37  	// In order to do a decent end-to-end test for this case we will need a
    38  	// real enough provisioner plugin to try to run and make sure we are able
    39  	// to actually run it. Here will build the local-exec provisioner into a
    40  	// binary called test-provisioner
    41  	provisionerExePrefix := filepath.Join(tf.WorkDir(), "terraform-provisioner-test_")
    42  	provisionerExe := e2e.GoBuild("github.com/opentofu/opentofu/internal/provisioner-local-exec/main", provisionerExePrefix)
    43  
    44  	// provisioners must use the old binary name format, so rename this binary
    45  	newExe := filepath.Join(tf.WorkDir(), "terraform-provisioner-test")
    46  	if _, err := os.Stat(newExe); !os.IsNotExist(err) {
    47  		t.Fatalf("%q already exists", newExe)
    48  	}
    49  	if err := os.Rename(provisionerExe, newExe); err != nil {
    50  		t.Fatalf("error renaming provisioner binary: %v", err)
    51  	}
    52  	provisionerExe = newExe
    53  
    54  	t.Logf("temporary provisioner executable is %s", provisionerExe)
    55  
    56  	//// INIT
    57  	_, stderr, err := tf.Run("init")
    58  	if err != nil {
    59  		t.Fatalf("unexpected init error: %s\nstderr:\n%s", err, stderr)
    60  	}
    61  
    62  	//// PLAN
    63  	_, stderr, err = tf.Run("plan", "-out=tfplan")
    64  	if err != nil {
    65  		t.Fatalf("unexpected plan error: %s\nstderr:\n%s", err, stderr)
    66  	}
    67  
    68  	//// APPLY
    69  	stdout, stderr, err := tf.Run("apply", "tfplan")
    70  	if err != nil {
    71  		t.Fatalf("unexpected apply error: %s\nstderr:\n%s", err, stderr)
    72  	}
    73  
    74  	if !strings.Contains(stdout, "HelloProvisioner") {
    75  		t.Fatalf("missing provisioner output:\n%s", stdout)
    76  	}
    77  }