github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/not-internal/command/e2etest/providers_mirror_test.go (about)

     1  package e2etest
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"sort"
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  	"github.com/muratcelep/terraform/not-internal/e2e"
    12  )
    13  
    14  // The tests in this file are for the "terraform providers mirror" command,
    15  // which is tested in an e2etest mode rather than a unit test mode because it
    16  // interacts directly with Terraform Registry and the full details of that are
    17  // tricky to mock. Such a mock is _possible_, but we're using e2etest as a
    18  // compromise for now to keep these tests relatively simple.
    19  
    20  func TestTerraformProvidersMirror(t *testing.T) {
    21  	// This test reaches out to releases.hashicorp.com to download the
    22  	// template and null providers, so it can only run if network access is
    23  	// allowed.
    24  	skipIfCannotAccessNetwork(t)
    25  
    26  	outputDir, err := ioutil.TempDir("", "terraform-e2etest-providers-mirror")
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	defer os.RemoveAll(outputDir)
    31  	t.Logf("creating mirror directory in %s", outputDir)
    32  
    33  	fixturePath := filepath.Join("testdata", "terraform-providers-mirror")
    34  	tf := e2e.NewBinary(terraformBin, fixturePath)
    35  	defer tf.Close()
    36  
    37  	stdout, stderr, err := tf.Run("providers", "mirror", "-platform=linux_amd64", "-platform=windows_386", outputDir)
    38  	if err != nil {
    39  		t.Fatalf("unexpected error: %s\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
    40  	}
    41  
    42  	// The test fixture includes exact version constraints for the two
    43  	// providers it depends on so that the following should remain stable.
    44  	// In the (unlikely) event that these particular versions of these
    45  	// providers are removed from the registry, this test will start to fail.
    46  	want := []string{
    47  		"registry.terraform.io/hashicorp/null/2.1.0.json",
    48  		"registry.terraform.io/hashicorp/null/index.json",
    49  		"registry.terraform.io/hashicorp/null/terraform-provider-null_2.1.0_linux_amd64.zip",
    50  		"registry.terraform.io/hashicorp/null/terraform-provider-null_2.1.0_windows_386.zip",
    51  		"registry.terraform.io/hashicorp/template/2.1.1.json",
    52  		"registry.terraform.io/hashicorp/template/index.json",
    53  		"registry.terraform.io/hashicorp/template/terraform-provider-template_2.1.1_linux_amd64.zip",
    54  		"registry.terraform.io/hashicorp/template/terraform-provider-template_2.1.1_windows_386.zip",
    55  	}
    56  	var got []string
    57  	walkErr := filepath.Walk(outputDir, func(path string, info os.FileInfo, err error) error {
    58  		if err != nil {
    59  			return err
    60  		}
    61  		if info.IsDir() {
    62  			return nil // we only care about leaf files for this test
    63  		}
    64  		relPath, err := filepath.Rel(outputDir, path)
    65  		if err != nil {
    66  			return err
    67  		}
    68  		got = append(got, filepath.ToSlash(relPath))
    69  		return nil
    70  	})
    71  	if walkErr != nil {
    72  		t.Fatal(walkErr)
    73  	}
    74  	sort.Strings(got)
    75  
    76  	if diff := cmp.Diff(want, got); diff != "" {
    77  		t.Errorf("unexpected files in result\n%s", diff)
    78  	}
    79  }