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