github.com/hugorut/terraform@v1.1.3/src/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/hugorut/terraform/src/e2e"
    10  	"github.com/hugorut/terraform/src/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  	t.Parallel()
    17  
    18  	tf := e2e.NewBinary(terraformBin, "testdata/provider-plugin")
    19  	defer tf.Close()
    20  
    21  	// In order to do a decent end-to-end test for this case we will need a real
    22  	// enough provider plugin to try to run and make sure we are able to
    23  	// actually run it. Here will build the simple and simple6 (built with
    24  	// protocol v6) providers.
    25  	simple6Provider := filepath.Join(tf.WorkDir(), "terraform-provider-simple6")
    26  	simple6ProviderExe := e2e.GoBuild("github.com/hugorut/terraform/src/provider-simple-v6/main", simple6Provider)
    27  
    28  	simpleProvider := filepath.Join(tf.WorkDir(), "terraform-provider-simple")
    29  	simpleProviderExe := e2e.GoBuild("github.com/hugorut/terraform/src/provider-simple/main", simpleProvider)
    30  
    31  	// Move the provider binaries into a directory that we will point terraform
    32  	// to using the -plugin-dir cli flag.
    33  	platform := getproviders.CurrentPlatform.String()
    34  	hashiDir := "cache/registry.terraform.io/hashicorp/"
    35  	if err := os.MkdirAll(tf.Path(hashiDir, "simple6/0.0.1/", platform), os.ModePerm); err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	if err := os.Rename(simple6ProviderExe, tf.Path(hashiDir, "simple6/0.0.1/", platform, "terraform-provider-simple6")); err != nil {
    39  		t.Fatal(err)
    40  	}
    41  
    42  	if err := os.MkdirAll(tf.Path(hashiDir, "simple/0.0.1/", platform), os.ModePerm); err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	if err := os.Rename(simpleProviderExe, tf.Path(hashiDir, "simple/0.0.1/", platform, "terraform-provider-simple")); err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	//// INIT
    50  	_, stderr, err := tf.Run("init", "-plugin-dir=cache")
    51  	if err != nil {
    52  		t.Fatalf("unexpected init error: %s\nstderr:\n%s", err, stderr)
    53  	}
    54  
    55  	//// PLAN
    56  	_, stderr, err = tf.Run("plan", "-out=tfplan")
    57  	if err != nil {
    58  		t.Fatalf("unexpected plan error: %s\nstderr:\n%s", err, stderr)
    59  	}
    60  
    61  	//// APPLY
    62  	stdout, stderr, err := tf.Run("apply", "tfplan")
    63  	if err != nil {
    64  		t.Fatalf("unexpected apply error: %s\nstderr:\n%s", err, stderr)
    65  	}
    66  
    67  	if !strings.Contains(stdout, "Apply complete! Resources: 2 added, 0 changed, 0 destroyed.") {
    68  		t.Fatalf("wrong output:\n%s", stdout)
    69  	}
    70  }