From 30e6f4e0662e5116495fc351a3e46692e013e798 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 1 Sep 2017 17:24:46 -0700 Subject: [PATCH] command/e2etest: test installation of plugins from cache This is a tough one to unit tests because the behavior is tangled up in the code that hits releases.hashicorp.com, so we'll add this e2etest as some extra insurance that this works end-to-end. --- command/e2etest/init_test.go | 56 +++++++++++++++++++ .../terraform-provider-template_v0.1.0_x4 | 1 + .../test-fixtures/plugin-cache/main.tf | 7 +++ 3 files changed, 64 insertions(+) create mode 100644 command/e2etest/test-fixtures/plugin-cache/cache/os_arch/terraform-provider-template_v0.1.0_x4 create mode 100644 command/e2etest/test-fixtures/plugin-cache/main.tf diff --git a/command/e2etest/init_test.go b/command/e2etest/init_test.go index bbfc3a6ae..f7eed5f2e 100644 --- a/command/e2etest/init_test.go +++ b/command/e2etest/init_test.go @@ -1,7 +1,11 @@ package e2etest import ( + "bytes" + "fmt" + "os" "path/filepath" + "runtime" "strings" "testing" @@ -45,3 +49,55 @@ func TestInitProviders(t *testing.T) { } } + +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 != "" { + t.Errorf("unexpected stderr output:\n%s", stderr) + } + + path := fmt.Sprintf(".terraform/plugins/%s_%s/terraform-provider-template_v0.1.0_x4", runtime.GOOS, runtime.GOARCH) + 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") + } + + if !tf.FileExists(fmt.Sprintf(".terraform/plugins/%s_%s/terraform-provider-null_v0.1.0_x4", runtime.GOOS, runtime.GOARCH)) { + t.Errorf("null plugin was not installed") + } + + if !tf.FileExists(fmt.Sprintf("cache/%s_%s/terraform-provider-null_v0.1.0_x4", runtime.GOOS, runtime.GOARCH)) { + t.Errorf("null plugin is not in cache after install") + } +} diff --git a/command/e2etest/test-fixtures/plugin-cache/cache/os_arch/terraform-provider-template_v0.1.0_x4 b/command/e2etest/test-fixtures/plugin-cache/cache/os_arch/terraform-provider-template_v0.1.0_x4 new file mode 100644 index 000000000..c92a59ab2 --- /dev/null +++ b/command/e2etest/test-fixtures/plugin-cache/cache/os_arch/terraform-provider-template_v0.1.0_x4 @@ -0,0 +1 @@ +this is not a real plugin diff --git a/command/e2etest/test-fixtures/plugin-cache/main.tf b/command/e2etest/test-fixtures/plugin-cache/main.tf new file mode 100644 index 000000000..94bae0fa2 --- /dev/null +++ b/command/e2etest/test-fixtures/plugin-cache/main.tf @@ -0,0 +1,7 @@ +provider "template" { + version = "0.1.0" +} + +provider "null" { + version = "0.1.0" +}