2017-07-07 20:49:41 -05:00
|
|
|
package e2etest
|
|
|
|
|
|
|
|
import (
|
2017-09-01 19:24:46 -05:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2017-08-16 08:48:51 -05:00
|
|
|
"path/filepath"
|
2017-09-01 19:24:46 -05:00
|
|
|
"runtime"
|
2017-07-07 20:49:41 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-08-16 08:48:51 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/e2e"
|
2017-07-07 20:49:41 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInitProviders(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// This test reaches out to releases.hashicorp.com to download the
|
|
|
|
// template provider, so it can only run if network access is allowed.
|
|
|
|
// We intentionally don't try to stub this here, because there's already
|
|
|
|
// a stubbed version of this in the "command" package and so the goal here
|
|
|
|
// is to test the interaction with the real repository.
|
|
|
|
skipIfCannotAccessNetwork(t)
|
|
|
|
|
2017-08-16 08:48:51 -05:00
|
|
|
fixturePath := filepath.Join("test-fixtures", "template-provider")
|
|
|
|
tf := e2e.NewBinary(terraformBin, fixturePath)
|
2017-07-07 20:49:41 -05:00
|
|
|
defer tf.Close()
|
|
|
|
|
|
|
|
stdout, stderr, err := tf.Run("init")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if stderr != "" {
|
|
|
|
t.Errorf("unexpected stderr output:\n%s", stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(stdout, "Terraform has been successfully initialized!") {
|
|
|
|
t.Errorf("success message is missing from output:\n%s", stdout)
|
|
|
|
}
|
|
|
|
|
2019-04-29 12:03:25 -05:00
|
|
|
if !strings.Contains(stdout, "- Downloading plugin for provider \"template\" (terraform-providers/template)") {
|
2017-07-07 20:49:41 -05:00
|
|
|
t.Errorf("provider download message is missing from output:\n%s", stdout)
|
|
|
|
t.Logf("(this can happen if you have a copy of the plugin in one of the global plugin search dirs)")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(stdout, "* provider.template: version = ") {
|
|
|
|
t.Errorf("provider pinning recommendation is missing from output:\n%s", stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-09-01 19:24:46 -05:00
|
|
|
|
2017-11-03 13:36:31 -05:00
|
|
|
func TestInitProvidersInternal(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// This test should _not_ reach out anywhere because the "terraform"
|
|
|
|
// provider is internal to the core terraform binary.
|
|
|
|
|
|
|
|
fixturePath := filepath.Join("test-fixtures", "terraform-provider")
|
|
|
|
tf := e2e.NewBinary(terraformBin, fixturePath)
|
|
|
|
defer tf.Close()
|
|
|
|
|
|
|
|
stdout, stderr, err := tf.Run("init")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if stderr != "" {
|
|
|
|
t.Errorf("unexpected stderr output:\n%s", stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(stdout, "Terraform has been successfully initialized!") {
|
|
|
|
t.Errorf("success message is missing from output:\n%s", stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(stdout, "Downloading plugin for provider") {
|
|
|
|
// Shouldn't have downloaded anything with this config, because the
|
|
|
|
// provider is built in.
|
|
|
|
t.Errorf("provider download message appeared in output:\n%s", stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-01 19:24:46 -05:00
|
|
|
func TestInitProviders_pluginCache(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// This test reaches out to releases.hashicorp.com to access plugin
|
|
|
|
// metadata, and download the null plugin, though the template plugin
|
|
|
|
// should come from local cache.
|
|
|
|
skipIfCannotAccessNetwork(t)
|
|
|
|
|
|
|
|
fixturePath := filepath.Join("test-fixtures", "plugin-cache")
|
|
|
|
tf := e2e.NewBinary(terraformBin, fixturePath)
|
|
|
|
defer tf.Close()
|
|
|
|
|
|
|
|
// Our fixture dir has a generic os_arch dir, which we need to customize
|
|
|
|
// to the actual OS/arch where this test is running in order to get the
|
|
|
|
// desired result.
|
|
|
|
fixtMachineDir := tf.Path("cache/os_arch")
|
|
|
|
wantMachineDir := tf.Path("cache", fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH))
|
|
|
|
os.Rename(fixtMachineDir, wantMachineDir)
|
|
|
|
|
|
|
|
cmd := tf.Cmd("init")
|
|
|
|
cmd.Env = append(cmd.Env, "TF_PLUGIN_CACHE_DIR=./cache")
|
|
|
|
cmd.Stdin = nil
|
|
|
|
cmd.Stderr = &bytes.Buffer{}
|
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stderr := cmd.Stderr.(*bytes.Buffer).String()
|
|
|
|
if stderr != "" {
|
2019-04-29 12:03:25 -05:00
|
|
|
t.Errorf("unexpected stderr output:\n%s\n", stderr)
|
2017-09-01 19:24:46 -05:00
|
|
|
}
|
|
|
|
|
2019-04-29 12:03:25 -05:00
|
|
|
path := fmt.Sprintf(".terraform/plugins/%s_%s/terraform-provider-template_v2.1.0_x4", runtime.GOOS, runtime.GOARCH)
|
2017-09-01 19:24:46 -05:00
|
|
|
content, err := tf.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read installed plugin from %s: %s", path, err)
|
|
|
|
}
|
|
|
|
if strings.TrimSpace(string(content)) != "this is not a real plugin" {
|
|
|
|
t.Errorf("template plugin was not installed from local cache")
|
|
|
|
}
|
|
|
|
|
2019-04-29 12:03:25 -05:00
|
|
|
if !tf.FileExists(fmt.Sprintf(".terraform/plugins/%s_%s/terraform-provider-null_v2.1.0_x4", runtime.GOOS, runtime.GOARCH)) {
|
2017-09-01 19:24:46 -05:00
|
|
|
t.Errorf("null plugin was not installed")
|
|
|
|
}
|
|
|
|
|
2019-04-29 12:03:25 -05:00
|
|
|
if !tf.FileExists(fmt.Sprintf("cache/%s_%s/terraform-provider-null_v2.1.0_x4", runtime.GOOS, runtime.GOARCH)) {
|
2017-09-01 19:24:46 -05:00
|
|
|
t.Errorf("null plugin is not in cache after install")
|
|
|
|
}
|
|
|
|
}
|
2017-10-30 10:27:27 -05:00
|
|
|
|
|
|
|
func TestInit_fromModule(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// This test reaches out to registry.terraform.io and github.com to lookup
|
|
|
|
// and fetch a module.
|
|
|
|
skipIfCannotAccessNetwork(t)
|
|
|
|
|
|
|
|
fixturePath := filepath.Join("test-fixtures", "empty")
|
|
|
|
tf := e2e.NewBinary(terraformBin, fixturePath)
|
|
|
|
defer tf.Close()
|
|
|
|
|
|
|
|
cmd := tf.Cmd("init", "-from-module=hashicorp/vault/aws")
|
|
|
|
cmd.Stdin = nil
|
|
|
|
cmd.Stderr = &bytes.Buffer{}
|
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stderr := cmd.Stderr.(*bytes.Buffer).String()
|
|
|
|
if stderr != "" {
|
|
|
|
t.Errorf("unexpected stderr output:\n%s", stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := tf.ReadFile("main.tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read main.tf: %s", err)
|
|
|
|
}
|
|
|
|
if !bytes.Contains(content, []byte("vault")) {
|
|
|
|
t.Fatalf("main.tf doesn't appear to be a vault configuration: \n%s", content)
|
|
|
|
}
|
|
|
|
}
|