github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/providercache/dir_modify_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package providercache 5 6 import ( 7 "context" 8 "path/filepath" 9 "testing" 10 11 "github.com/apparentlymart/go-versions/versions" 12 "github.com/google/go-cmp/cmp" 13 14 "github.com/terramate-io/tf/addrs" 15 "github.com/terramate-io/tf/getproviders" 16 ) 17 18 func TestInstallPackage(t *testing.T) { 19 tmpDirPath, err := filepath.EvalSymlinks(t.TempDir()) 20 if err != nil { 21 t.Fatal(err) 22 } 23 24 linuxPlatform := getproviders.Platform{ 25 OS: "linux", 26 Arch: "amd64", 27 } 28 nullProvider := addrs.NewProvider( 29 addrs.DefaultProviderRegistryHost, "hashicorp", "null", 30 ) 31 32 tmpDir := NewDirWithPlatform(tmpDirPath, linuxPlatform) 33 34 meta := getproviders.PackageMeta{ 35 Provider: nullProvider, 36 Version: versions.MustParseVersion("2.1.0"), 37 38 ProtocolVersions: getproviders.VersionList{versions.MustParseVersion("5.0.0")}, 39 TargetPlatform: linuxPlatform, 40 41 Filename: "terraform-provider-null_2.1.0_linux_amd64.zip", 42 Location: getproviders.PackageLocalArchive("testdata/terraform-provider-null_2.1.0_linux_amd64.zip"), 43 } 44 45 result, err := tmpDir.InstallPackage(context.TODO(), meta, nil) 46 if err != nil { 47 t.Fatalf("InstallPackage failed: %s", err) 48 } 49 if result != nil { 50 t.Errorf("unexpected result %#v, wanted nil", result) 51 } 52 53 // Now we should see the same version reflected in the temporary directory. 54 got := tmpDir.AllAvailablePackages() 55 want := map[addrs.Provider][]CachedProvider{ 56 nullProvider: { 57 CachedProvider{ 58 Provider: nullProvider, 59 60 Version: versions.MustParseVersion("2.1.0"), 61 62 PackageDir: tmpDirPath + "/registry.terraform.io/hashicorp/null/2.1.0/linux_amd64", 63 }, 64 }, 65 } 66 if diff := cmp.Diff(want, got); diff != "" { 67 t.Errorf("wrong cache contents after install\n%s", diff) 68 } 69 } 70 71 func TestLinkFromOtherCache(t *testing.T) { 72 srcDirPath := "testdata/cachedir" 73 tmpDirPath, err := filepath.EvalSymlinks(t.TempDir()) 74 if err != nil { 75 t.Fatal(err) 76 } 77 78 windowsPlatform := getproviders.Platform{ 79 OS: "windows", 80 Arch: "amd64", 81 } 82 nullProvider := addrs.NewProvider( 83 addrs.DefaultProviderRegistryHost, "hashicorp", "null", 84 ) 85 86 srcDir := NewDirWithPlatform(srcDirPath, windowsPlatform) 87 tmpDir := NewDirWithPlatform(tmpDirPath, windowsPlatform) 88 89 // First we'll check our preconditions: srcDir should have only the 90 // null provider version 2.0.0 in it, because we're faking that we're on 91 // Windows, and tmpDir should have no providers in it at all. 92 93 gotSrcInitial := srcDir.AllAvailablePackages() 94 wantSrcInitial := map[addrs.Provider][]CachedProvider{ 95 nullProvider: { 96 CachedProvider{ 97 Provider: nullProvider, 98 99 // We want 2.0.0 rather than 2.1.0 because the 2.1.0 package is 100 // still packed and thus not considered to be a cache member. 101 Version: versions.MustParseVersion("2.0.0"), 102 103 PackageDir: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/windows_amd64", 104 }, 105 }, 106 } 107 if diff := cmp.Diff(wantSrcInitial, gotSrcInitial); diff != "" { 108 t.Fatalf("incorrect initial source directory contents\n%s", diff) 109 } 110 111 gotTmpInitial := tmpDir.AllAvailablePackages() 112 wantTmpInitial := map[addrs.Provider][]CachedProvider{} 113 if diff := cmp.Diff(wantTmpInitial, gotTmpInitial); diff != "" { 114 t.Fatalf("incorrect initial temp directory contents\n%s", diff) 115 } 116 117 cacheEntry := srcDir.ProviderLatestVersion(nullProvider) 118 if cacheEntry == nil { 119 // This is weird because we just checked for the presence of this above 120 t.Fatalf("null provider has no latest version in source directory") 121 } 122 123 err = tmpDir.LinkFromOtherCache(cacheEntry, nil) 124 if err != nil { 125 t.Fatalf("LinkFromOtherCache failed: %s", err) 126 } 127 128 // Now we should see the same version reflected in the temporary directory. 129 got := tmpDir.AllAvailablePackages() 130 want := map[addrs.Provider][]CachedProvider{ 131 nullProvider: { 132 CachedProvider{ 133 Provider: nullProvider, 134 135 // We want 2.0.0 rather than 2.1.0 because the 2.1.0 package is 136 // still packed and thus not considered to be a cache member. 137 Version: versions.MustParseVersion("2.0.0"), 138 139 PackageDir: tmpDirPath + "/registry.terraform.io/hashicorp/null/2.0.0/windows_amd64", 140 }, 141 }, 142 } 143 if diff := cmp.Diff(want, got); diff != "" { 144 t.Errorf("wrong cache contents after link\n%s", diff) 145 } 146 }