github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/e2etest/provider_plugin_test.go (about)

     1  package e2etest
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/internal/e2e"
    10  	"github.com/hashicorp/terraform/internal/getproviders"
    11  )
    12  
    13  // TestProviderProtocols verifies that Terraform can execute provider plugins
    14  // with both supported protocol versions.
    15  func TestProviderProtocols(t *testing.T) {
    16  	if !canRunGoBuild {
    17  		// We're running in a separate-build-then-run context, so we can't
    18  		// currently execute this test which depends on being able to build
    19  		// new executable at runtime.
    20  		//
    21  		// (See the comment on canRunGoBuild's declaration for more information.)
    22  		t.Skip("can't run without building a new provider executable")
    23  	}
    24  	t.Parallel()
    25  
    26  	tf := e2e.NewBinary(t, terraformBin, "testdata/provider-plugin")
    27  
    28  	// In order to do a decent end-to-end test for this case we will need a real
    29  	// enough provider plugin to try to run and make sure we are able to
    30  	// actually run it. Here will build the simple and simple6 (built with
    31  	// protocol v6) providers.
    32  	simple6Provider := filepath.Join(tf.WorkDir(), "terraform-provider-simple6")
    33  	simple6ProviderExe := e2e.GoBuild("github.com/hashicorp/terraform/internal/provider-simple-v6/main", simple6Provider)
    34  
    35  	simpleProvider := filepath.Join(tf.WorkDir(), "terraform-provider-simple")
    36  	simpleProviderExe := e2e.GoBuild("github.com/hashicorp/terraform/internal/provider-simple/main", simpleProvider)
    37  
    38  	// Move the provider binaries into a directory that we will point terraform
    39  	// to using the -plugin-dir cli flag.
    40  	platform := getproviders.CurrentPlatform.String()
    41  	hashiDir := "cache/registry.terraform.io/hashicorp/"
    42  	if err := os.MkdirAll(tf.Path(hashiDir, "simple6/0.0.1/", platform), os.ModePerm); err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	if err := os.Rename(simple6ProviderExe, tf.Path(hashiDir, "simple6/0.0.1/", platform, "terraform-provider-simple6")); err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	if err := os.MkdirAll(tf.Path(hashiDir, "simple/0.0.1/", platform), os.ModePerm); err != nil {
    50  		t.Fatal(err)
    51  	}
    52  	if err := os.Rename(simpleProviderExe, tf.Path(hashiDir, "simple/0.0.1/", platform, "terraform-provider-simple")); err != nil {
    53  		t.Fatal(err)
    54  	}
    55  
    56  	//// INIT
    57  	_, stderr, err := tf.Run("init", "-plugin-dir=cache")
    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, "Apply complete! Resources: 2 added, 0 changed, 0 destroyed.") {
    75  		t.Fatalf("wrong output:\nstdout:%s\nstderr%s", stdout, stderr)
    76  	}
    77  
    78  	/// DESTROY
    79  	stdout, stderr, err = tf.Run("destroy", "-auto-approve")
    80  	if err != nil {
    81  		t.Fatalf("unexpected apply error: %s\nstderr:\n%s", err, stderr)
    82  	}
    83  
    84  	if !strings.Contains(stdout, "Resources: 2 destroyed") {
    85  		t.Fatalf("wrong destroy output\nstdout:%s\nstderr:%s", stdout, stderr)
    86  	}
    87  }