opentofu/command/e2etest/providers_mirror_test.go
Martin Atkins 49e2e00231 command: terraform providers mirror
This new command is intended to make it easy to create or update a mirror
directory containing suitable providers for the current configuration,
producing a layout that is appropriate both for a filesystem mirror or,
if copied into the document root of an HTTP server, a network mirror.

This initial version is not customizable aside from being able to select
multiple platforms to install packages for.

Future iterations of this could include commands to turn the JSON index
generation on and off, or to instruct it to produce the unpacked directory
layout instead of the packed directory layout as it currently does. Both
of those options would make the generated directory unsuitable to be
a network mirror, but it would still work as a filesystem mirror.

In the long run this will hopefully form part of a replacement workflow to
terraform-bundle as a way to put copies of providers somewhere so we don't
need to re-download them every time, but some other changes will be needed
outside of just this command before that'd be true, such as adding support
for network and/or filesystem mirrors in Terraform Enterprise.
2020-06-01 14:49:43 -07:00

77 lines
2.6 KiB
Go

package e2etest
import (
"io/ioutil"
"os"
"path/filepath"
"sort"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform/e2e"
)
// The tests in this file are for the "terraform providers mirror" command,
// which is tested in an e2etest mode rather than a unit test mode because it
// interacts directly with Terraform Registry and the full details of that are
// tricky to mock. Such a mock is _possible_, but we're using e2etest as a
// compromise for now to keep these tests relatively simple.
func TestTerraformProvidersMirror(t *testing.T) {
// This test reaches out to releases.hashicorp.com to download the
// template and null providers, so it can only run if network access is
// allowed.
skipIfCannotAccessNetwork(t)
outputDir, err := ioutil.TempDir("", "terraform-e2etest-providers-mirror")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(outputDir)
t.Logf("creating mirror directory in %s", outputDir)
fixturePath := filepath.Join("testdata", "terraform-providers-mirror")
tf := e2e.NewBinary(terraformBin, fixturePath)
defer tf.Close()
stdout, stderr, err := tf.Run("providers", "mirror", "-platform=linux_amd64", "-platform=windows_386", outputDir)
if err != nil {
t.Fatalf("unexpected error: %s\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
}
// The test fixture includes exact version constraints for the two
// providers it depends on so that the following should remain stable.
// In the (unlikely) event that these particular versions of these
// providers are removed from the registry, this test will start to fail.
want := []string{
"registry.terraform.io/hashicorp/null/2.1.0.json",
"registry.terraform.io/hashicorp/null/index.json",
"registry.terraform.io/hashicorp/null/terraform-provider-null_2.1.0_linux_amd64.zip",
"registry.terraform.io/hashicorp/null/terraform-provider-null_2.1.0_windows_386.zip",
"registry.terraform.io/hashicorp/template/2.1.1.json",
"registry.terraform.io/hashicorp/template/index.json",
"registry.terraform.io/hashicorp/template/terraform-provider-template_2.1.1_linux_amd64.zip",
"registry.terraform.io/hashicorp/template/terraform-provider-template_2.1.1_windows_386.zip",
}
var got []string
err = filepath.Walk(outputDir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil // we only care about leaf files for this test
}
relPath, err := filepath.Rel(outputDir, path)
if err != nil {
return err
}
got = append(got, filepath.ToSlash(relPath))
return nil
})
if err != nil {
t.Fatal(err)
}
sort.Strings(got)
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("unexpected files in result\n%s", diff)
}
}