github.com/cycloidio/terraform@v1.1.10-0.20220513142504-76d5c768dc63/command/e2etest/provisioner_plugin_test.go (about)

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