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