github.com/kevinklinger/open_terraform@v1.3.6/noninternal/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/kevinklinger/open_terraform/noninternal/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  	// This test reaches out to releases.hashicorp.com to download the
    21  	// template and null providers, so it can only run if network access is
    22  	// allowed.
    23  	skipIfCannotAccessNetwork(t)
    24  
    25  	outputDir := t.TempDir()
    26  	t.Logf("creating mirror directory in %s", outputDir)
    27  
    28  	fixturePath := filepath.Join("testdata", "terraform-providers-mirror")
    29  	tf := e2e.NewBinary(t, terraformBin, fixturePath)
    30  
    31  	stdout, stderr, err := tf.Run("providers", "mirror", "-platform=linux_amd64", "-platform=windows_386", outputDir)
    32  	if err != nil {
    33  		t.Fatalf("unexpected error: %s\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
    34  	}
    35  
    36  	// The test fixture includes exact version constraints for the two
    37  	// providers it depends on so that the following should remain stable.
    38  	// In the (unlikely) event that these particular versions of these
    39  	// providers are removed from the registry, this test will start to fail.
    40  	want := []string{
    41  		"registry.terraform.io/hashicorp/null/2.1.0.json",
    42  		"registry.terraform.io/hashicorp/null/index.json",
    43  		"registry.terraform.io/hashicorp/null/terraform-provider-null_2.1.0_linux_amd64.zip",
    44  		"registry.terraform.io/hashicorp/null/terraform-provider-null_2.1.0_windows_386.zip",
    45  		"registry.terraform.io/hashicorp/template/2.1.1.json",
    46  		"registry.terraform.io/hashicorp/template/index.json",
    47  		"registry.terraform.io/hashicorp/template/terraform-provider-template_2.1.1_linux_amd64.zip",
    48  		"registry.terraform.io/hashicorp/template/terraform-provider-template_2.1.1_windows_386.zip",
    49  	}
    50  	var got []string
    51  	walkErr := filepath.Walk(outputDir, func(path string, info os.FileInfo, err error) error {
    52  		if err != nil {
    53  			return err
    54  		}
    55  		if info.IsDir() {
    56  			return nil // we only care about leaf files for this test
    57  		}
    58  		relPath, err := filepath.Rel(outputDir, path)
    59  		if err != nil {
    60  			return err
    61  		}
    62  		got = append(got, filepath.ToSlash(relPath))
    63  		return nil
    64  	})
    65  	if walkErr != nil {
    66  		t.Fatal(walkErr)
    67  	}
    68  	sort.Strings(got)
    69  
    70  	if diff := cmp.Diff(want, got); diff != "" {
    71  		t.Errorf("unexpected files in result\n%s", diff)
    72  	}
    73  }