From e5a25f69fdeb4fae31d74b306e09d6e3b0110b2d Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Wed, 16 Aug 2017 16:36:28 +0200 Subject: [PATCH] tools/terraform-bundle: Add e2e tests --- tools/terraform-bundle/e2etest/main_test.go | 39 +++++++ .../terraform-bundle/e2etest/package_test.go | 103 ++++++++++++++++++ .../test-fixtures/empty/terraform-bundle.hcl | 3 + .../many-providers/terraform-bundle.hcl | 9 ++ 4 files changed, 154 insertions(+) create mode 100644 tools/terraform-bundle/e2etest/main_test.go create mode 100644 tools/terraform-bundle/e2etest/package_test.go create mode 100644 tools/terraform-bundle/e2etest/test-fixtures/empty/terraform-bundle.hcl create mode 100644 tools/terraform-bundle/e2etest/test-fixtures/many-providers/terraform-bundle.hcl diff --git a/tools/terraform-bundle/e2etest/main_test.go b/tools/terraform-bundle/e2etest/main_test.go new file mode 100644 index 000000000..30c2ac2f3 --- /dev/null +++ b/tools/terraform-bundle/e2etest/main_test.go @@ -0,0 +1,39 @@ +package e2etest + +import ( + "os" + "testing" + + "github.com/hashicorp/terraform/e2e" +) + +var bundleBin string + +func TestMain(m *testing.M) { + teardown := setup() + code := m.Run() + teardown() + os.Exit(code) +} + +func setup() func() { + tmpFilename := e2e.GoBuild("github.com/hashicorp/terraform/tools/terraform-bundle", "terraform-bundle") + bundleBin = tmpFilename + + return func() { + os.Remove(tmpFilename) + } +} + +func canAccessNetwork() bool { + // We re-use the flag normally used for acceptance tests since that's + // established as a way to opt-in to reaching out to real systems that + // may suffer transient errors. + return os.Getenv("TF_ACC") != "" +} + +func skipIfCannotAccessNetwork(t *testing.T) { + if !canAccessNetwork() { + t.Skip("network access not allowed; use TF_ACC=1 to enable") + } +} diff --git a/tools/terraform-bundle/e2etest/package_test.go b/tools/terraform-bundle/e2etest/package_test.go new file mode 100644 index 000000000..104553bc7 --- /dev/null +++ b/tools/terraform-bundle/e2etest/package_test.go @@ -0,0 +1,103 @@ +package e2etest + +import ( + "path/filepath" + "strings" + "testing" + + "github.com/hashicorp/terraform/e2e" +) + +func TestPackage_empty(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) + + fixturePath := filepath.Join("test-fixtures", "empty") + tfBundle := e2e.NewBinary(bundleBin, fixturePath) + defer tfBundle.Close() + + stdout, stderr, err := tfBundle.Run("package", "terraform-bundle.hcl") + if err != nil { + t.Errorf("unexpected error: %s", err) + } + + if stderr != "" { + t.Errorf("unexpected stderr output:\n%s", stderr) + } + + if !strings.Contains(stdout, "Fetching Terraform 0.10.1 core package...") { + t.Errorf("success message is missing from output:\n%s", stdout) + } + if !strings.Contains(stdout, "Creating terraform_0.10.1-bundle") { + t.Errorf("success message is missing from output:\n%s", stdout) + } + if !strings.Contains(stdout, "All done!") { + t.Errorf("success message is missing from output:\n%s", stdout) + } +} + +func TestPackage_manyProviders(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) + + fixturePath := filepath.Join("test-fixtures", "many-providers") + tfBundle := e2e.NewBinary(bundleBin, fixturePath) + defer tfBundle.Close() + + stdout, stderr, err := tfBundle.Run("package", "terraform-bundle.hcl") + if err != nil { + t.Errorf("unexpected error: %s", err) + } + + if stderr != "" { + t.Errorf("unexpected stderr output:\n%s", stderr) + } + + if !strings.Contains(stdout, "Checking for available provider plugins on ") { + t.Errorf("success message is missing from output:\n%s", stdout) + } + + // Here we have to check each provider separately + // because it's internally held in a map (i.e. not guaranteed order) + + if !strings.Contains(stdout, `- Resolving "aws" provider (~> 0.1)... +- Downloading plugin for provider "aws" (0.1.4)...`) { + t.Errorf("success message is missing from output:\n%s", stdout) + } + + if !strings.Contains(stdout, `- Resolving "kubernetes" provider (0.1.0)... +- Downloading plugin for provider "kubernetes" (0.1.0)... +- Resolving "kubernetes" provider (0.1.1)... +- Downloading plugin for provider "kubernetes" (0.1.1)... +- Resolving "kubernetes" provider (0.1.2)... +- Downloading plugin for provider "kubernetes" (0.1.2)...`) { + t.Errorf("success message is missing from output:\n%s", stdout) + } + + if !strings.Contains(stdout, `- Resolving "null" provider (0.1.0)... +- Downloading plugin for provider "null" (0.1.0)...`) { + t.Errorf("success message is missing from output:\n%s", stdout) + } + + if !strings.Contains(stdout, "Fetching Terraform 0.10.1 core package...") { + t.Errorf("success message is missing from output:\n%s", stdout) + } + if !strings.Contains(stdout, "Creating terraform_0.10.1-bundle") { + t.Errorf("success message is missing from output:\n%s", stdout) + } + if !strings.Contains(stdout, "All done!") { + t.Errorf("success message is missing from output:\n%s", stdout) + } +} diff --git a/tools/terraform-bundle/e2etest/test-fixtures/empty/terraform-bundle.hcl b/tools/terraform-bundle/e2etest/test-fixtures/empty/terraform-bundle.hcl new file mode 100644 index 000000000..5350cab4a --- /dev/null +++ b/tools/terraform-bundle/e2etest/test-fixtures/empty/terraform-bundle.hcl @@ -0,0 +1,3 @@ +terraform { + version = "0.10.1" +} diff --git a/tools/terraform-bundle/e2etest/test-fixtures/many-providers/terraform-bundle.hcl b/tools/terraform-bundle/e2etest/test-fixtures/many-providers/terraform-bundle.hcl new file mode 100644 index 000000000..05ddc8b5f --- /dev/null +++ b/tools/terraform-bundle/e2etest/test-fixtures/many-providers/terraform-bundle.hcl @@ -0,0 +1,9 @@ +terraform { + version = "0.10.1" +} + +providers { + aws = ["~> 0.1"] + kubernetes = ["0.1.0", "0.1.1", "0.1.2"] + null = ["0.1.0"] +} \ No newline at end of file