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