github.com/opentofu/opentofu@v1.7.1/internal/command/cliconfig/provider_installation_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 cliconfig 7 8 import ( 9 "path/filepath" 10 "testing" 11 12 "github.com/google/go-cmp/cmp" 13 "github.com/opentofu/opentofu/internal/addrs" 14 "github.com/opentofu/opentofu/internal/getproviders" 15 ) 16 17 func TestLoadConfig_providerInstallation(t *testing.T) { 18 for _, configFile := range []string{"provider-installation", "provider-installation.json"} { 19 t.Run(configFile, func(t *testing.T) { 20 got, diags := loadConfigFile(filepath.Join(fixtureDir, configFile)) 21 if diags.HasErrors() { 22 t.Errorf("unexpected diagnostics: %s", diags.Err().Error()) 23 } 24 25 want := &Config{ 26 ProviderInstallation: []*ProviderInstallation{ 27 { 28 Methods: []*ProviderInstallationMethod{ 29 { 30 Location: ProviderInstallationFilesystemMirror("/tmp/example1"), 31 Include: []string{"example.com/*/*"}, 32 }, 33 { 34 Location: ProviderInstallationNetworkMirror("https://tf-Mirror.example.com/"), 35 Include: []string{"registry.opentofu.org/*/*"}, 36 Exclude: []string{"registry.OpenTofu.org/foobar/*"}, 37 }, 38 { 39 Location: ProviderInstallationFilesystemMirror("/tmp/example2"), 40 }, 41 { 42 Location: ProviderInstallationDirect, 43 Exclude: []string{"example.com/*/*"}, 44 }, 45 }, 46 47 DevOverrides: map[addrs.Provider]getproviders.PackageLocalDir{ 48 addrs.MustParseProviderSourceString("hashicorp/boop"): getproviders.PackageLocalDir(filepath.FromSlash("/tmp/boop")), 49 addrs.MustParseProviderSourceString("hashicorp/blorp"): getproviders.PackageLocalDir(filepath.FromSlash("/tmp/blorp")), 50 }, 51 }, 52 }, 53 } 54 55 if diff := cmp.Diff(want, got); diff != "" { 56 t.Errorf("wrong result\n%s", diff) 57 } 58 }) 59 } 60 } 61 62 func TestLoadConfig_providerInstallationErrors(t *testing.T) { 63 _, diags := loadConfigFile(filepath.Join(fixtureDir, "provider-installation-errors")) 64 want := `7 problems: 65 66 - Invalid provider_installation method block: Unknown provider installation method "not_a_thing" at 2:3. 67 - Invalid provider_installation method block: Invalid filesystem_mirror block at 1:1: "path" argument is required. 68 - Invalid provider_installation method block: Invalid network_mirror block at 1:1: "url" argument is required. 69 - Invalid provider_installation method block: The items inside the provider_installation block at 1:1 must all be blocks. 70 - Invalid provider_installation method block: The blocks inside the provider_installation block at 1:1 may not have any labels. 71 - Invalid provider_installation block: The provider_installation block at 9:1 must not have any labels. 72 - Invalid provider_installation block: The provider_installation block at 11:1 must not be introduced with an equals sign.` 73 74 // The above error messages include only line/column location information 75 // and not file location information because HCL 1 does not store 76 // information about the filename a location belongs to. (There is a field 77 // for it in token.Pos but it's always an empty string in practice.) 78 79 if got := diags.Err().Error(); got != want { 80 t.Errorf("wrong diagnostics\ngot:\n%s\nwant:\n%s", got, want) 81 } 82 }