github.com/opentofu/opentofu@v1.7.1/internal/providercache/dir_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 providercache 7 8 import ( 9 "testing" 10 11 "github.com/apparentlymart/go-versions/versions" 12 "github.com/google/go-cmp/cmp" 13 14 "github.com/opentofu/opentofu/internal/addrs" 15 "github.com/opentofu/opentofu/internal/getproviders" 16 ) 17 18 func TestDirReading(t *testing.T) { 19 testDir := "testdata/cachedir" 20 21 // We'll force using particular platforms for unit testing purposes, 22 // so that we'll get consistent results on all platforms. 23 windowsPlatform := getproviders.Platform{ // only null 2.0.0 is cached 24 OS: "windows", 25 Arch: "amd64", 26 } 27 linuxPlatform := getproviders.Platform{ // various provider versions are cached 28 OS: "linux", 29 Arch: "amd64", 30 } 31 32 nullProvider := addrs.NewProvider( 33 addrs.DefaultProviderRegistryHost, "hashicorp", "null", 34 ) 35 randomProvider := addrs.NewProvider( 36 addrs.DefaultProviderRegistryHost, "hashicorp", "random", 37 ) 38 randomBetaProvider := addrs.NewProvider( 39 addrs.DefaultProviderRegistryHost, "hashicorp", "random-beta", 40 ) 41 nonExistProvider := addrs.NewProvider( 42 addrs.DefaultProviderRegistryHost, "bloop", "nonexist", 43 ) 44 legacyProvider := addrs.NewLegacyProvider("legacy") 45 missingExecutableProvider := addrs.NewProvider( 46 addrs.DefaultProviderRegistryHost, "missing", "executable", 47 ) 48 49 t.Run("ProviderLatestVersion", func(t *testing.T) { 50 t.Run("exists", func(t *testing.T) { 51 dir := NewDirWithPlatform(testDir, windowsPlatform) 52 53 got := dir.ProviderLatestVersion(nullProvider) 54 want := &CachedProvider{ 55 Provider: nullProvider, 56 57 // We want 2.0.0 rather than 2.1.0 because the 2.1.0 package is 58 // still packed and thus not considered to be a cache member. 59 Version: versions.MustParseVersion("2.0.0"), 60 61 PackageDir: "testdata/cachedir/registry.opentofu.org/hashicorp/null/2.0.0/windows_amd64", 62 } 63 64 if diff := cmp.Diff(want, got); diff != "" { 65 t.Errorf("wrong result\n%s", diff) 66 } 67 }) 68 t.Run("no package for current platform", func(t *testing.T) { 69 dir := NewDirWithPlatform(testDir, windowsPlatform) 70 71 // random provider is only cached for linux_amd64 in our fixtures dir 72 got := dir.ProviderLatestVersion(randomProvider) 73 var want *CachedProvider 74 75 if diff := cmp.Diff(want, got); diff != "" { 76 t.Errorf("wrong result\n%s", diff) 77 } 78 }) 79 t.Run("no versions available at all", func(t *testing.T) { 80 dir := NewDirWithPlatform(testDir, windowsPlatform) 81 82 // nonexist provider is not present in our fixtures dir at all 83 got := dir.ProviderLatestVersion(nonExistProvider) 84 var want *CachedProvider 85 86 if diff := cmp.Diff(want, got); diff != "" { 87 t.Errorf("wrong result\n%s", diff) 88 } 89 }) 90 }) 91 92 t.Run("ProviderVersion", func(t *testing.T) { 93 t.Run("exists", func(t *testing.T) { 94 dir := NewDirWithPlatform(testDir, windowsPlatform) 95 96 got := dir.ProviderVersion(nullProvider, versions.MustParseVersion("2.0.0")) 97 want := &CachedProvider{ 98 Provider: nullProvider, 99 Version: versions.MustParseVersion("2.0.0"), 100 101 PackageDir: "testdata/cachedir/registry.opentofu.org/hashicorp/null/2.0.0/windows_amd64", 102 } 103 104 if diff := cmp.Diff(want, got); diff != "" { 105 t.Errorf("wrong result\n%s", diff) 106 } 107 }) 108 t.Run("specified version is not cached", func(t *testing.T) { 109 dir := NewDirWithPlatform(testDir, windowsPlatform) 110 111 // there is no v5.0.0 package in our fixtures dir 112 got := dir.ProviderVersion(nullProvider, versions.MustParseVersion("5.0.0")) 113 var want *CachedProvider 114 115 if diff := cmp.Diff(want, got); diff != "" { 116 t.Errorf("wrong result\n%s", diff) 117 } 118 }) 119 t.Run("no package for current platform", func(t *testing.T) { 120 dir := NewDirWithPlatform(testDir, windowsPlatform) 121 122 // random provider 1.2.0 is only cached for linux_amd64 in our fixtures dir 123 got := dir.ProviderVersion(randomProvider, versions.MustParseVersion("1.2.0")) 124 var want *CachedProvider 125 126 if diff := cmp.Diff(want, got); diff != "" { 127 t.Errorf("wrong result\n%s", diff) 128 } 129 }) 130 t.Run("no versions available at all", func(t *testing.T) { 131 dir := NewDirWithPlatform(testDir, windowsPlatform) 132 133 // nonexist provider is not present in our fixtures dir at all 134 got := dir.ProviderVersion(nonExistProvider, versions.MustParseVersion("1.0.0")) 135 var want *CachedProvider 136 137 if diff := cmp.Diff(want, got); diff != "" { 138 t.Errorf("wrong result\n%s", diff) 139 } 140 }) 141 }) 142 143 t.Run("AllAvailablePackages", func(t *testing.T) { 144 dir := NewDirWithPlatform(testDir, linuxPlatform) 145 146 got := dir.AllAvailablePackages() 147 want := map[addrs.Provider][]CachedProvider{ 148 legacyProvider: { 149 { 150 Provider: legacyProvider, 151 Version: versions.MustParseVersion("1.0.0"), 152 PackageDir: "testdata/cachedir/registry.opentofu.org/-/legacy/1.0.0/linux_amd64", 153 }, 154 }, 155 nullProvider: { 156 { 157 Provider: nullProvider, 158 Version: versions.MustParseVersion("2.0.0"), 159 PackageDir: "testdata/cachedir/registry.opentofu.org/hashicorp/null/2.0.0/linux_amd64", 160 }, 161 }, 162 randomProvider: { 163 { 164 Provider: randomProvider, 165 Version: versions.MustParseVersion("1.2.0"), 166 PackageDir: "testdata/cachedir/registry.opentofu.org/hashicorp/random/1.2.0/linux_amd64", 167 }, 168 }, 169 randomBetaProvider: { 170 { 171 Provider: randomBetaProvider, 172 Version: versions.MustParseVersion("1.2.0"), 173 PackageDir: "testdata/cachedir/registry.opentofu.org/hashicorp/random-beta/1.2.0/linux_amd64", 174 }, 175 }, 176 missingExecutableProvider: { 177 { 178 Provider: missingExecutableProvider, 179 Version: versions.MustParseVersion("2.0.0"), 180 PackageDir: "testdata/cachedir/registry.opentofu.org/missing/executable/2.0.0/linux_amd64", 181 }, 182 }, 183 } 184 185 if diff := cmp.Diff(want, got); diff != "" { 186 t.Errorf("wrong result\n%s", diff) 187 } 188 }) 189 }