github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/e2etest/providers_mirror_test.go (about)

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