github.com/pulumi/terraform@v1.4.0/pkg/earlyconfig/config_test.go (about) 1 package earlyconfig 2 3 import ( 4 "log" 5 "path/filepath" 6 "testing" 7 8 "github.com/google/go-cmp/cmp" 9 version "github.com/hashicorp/go-version" 10 "github.com/hashicorp/terraform-config-inspect/tfconfig" 11 svchost "github.com/hashicorp/terraform-svchost" 12 "github.com/pulumi/terraform/pkg/addrs" 13 "github.com/pulumi/terraform/pkg/getproviders" 14 "github.com/pulumi/terraform/pkg/tfdiags" 15 ) 16 17 func TestConfigProviderRequirements(t *testing.T) { 18 cfg := testConfig(t, "testdata/provider-reqs") 19 20 impliedProvider := addrs.NewProvider( 21 addrs.DefaultProviderRegistryHost, 22 "hashicorp", "implied", 23 ) 24 nullProvider := addrs.NewProvider( 25 addrs.DefaultProviderRegistryHost, 26 "hashicorp", "null", 27 ) 28 randomProvider := addrs.NewProvider( 29 addrs.DefaultProviderRegistryHost, 30 "hashicorp", "random", 31 ) 32 tlsProvider := addrs.NewProvider( 33 addrs.DefaultProviderRegistryHost, 34 "hashicorp", "tls", 35 ) 36 happycloudProvider := addrs.NewProvider( 37 svchost.Hostname("tf.example.com"), 38 "awesomecorp", "happycloud", 39 ) 40 41 got, diags := cfg.ProviderRequirements() 42 if diags.HasErrors() { 43 t.Fatalf("unexpected diagnostics: %s", diags.Err().Error()) 44 } 45 want := getproviders.Requirements{ 46 // the nullProvider constraints from the two modules are merged 47 nullProvider: getproviders.MustParseVersionConstraints("~> 2.0.0, 2.0.1"), 48 randomProvider: getproviders.MustParseVersionConstraints("~> 1.2.0"), 49 tlsProvider: getproviders.MustParseVersionConstraints("~> 3.0"), 50 impliedProvider: nil, 51 happycloudProvider: nil, 52 } 53 54 if diff := cmp.Diff(want, got); diff != "" { 55 t.Errorf("wrong result\n%s", diff) 56 } 57 } 58 59 func testConfig(t *testing.T, baseDir string) *Config { 60 rootMod, diags := LoadModule(baseDir) 61 if diags.HasErrors() { 62 t.Fatalf("unexpected diagnostics: %s", diags.Err().Error()) 63 } 64 65 cfg, diags := BuildConfig(rootMod, ModuleWalkerFunc(testModuleWalkerFunc)) 66 if diags.HasErrors() { 67 t.Fatalf("unexpected diagnostics: %s", diags.Err().Error()) 68 } 69 70 return cfg 71 } 72 73 // testModuleWalkerFunc is a simple implementation of ModuleWalkerFunc that 74 // only understands how to resolve relative filesystem paths, using source 75 // location information from the call. 76 func testModuleWalkerFunc(req *ModuleRequest) (*tfconfig.Module, *version.Version, tfdiags.Diagnostics) { 77 callFilename := req.CallPos.Filename 78 sourcePath := req.SourceAddr.String() 79 finalPath := filepath.Join(filepath.Dir(callFilename), sourcePath) 80 log.Printf("[TRACE] %s in %s -> %s", sourcePath, callFilename, finalPath) 81 82 newMod, diags := LoadModule(finalPath) 83 return newMod, version.Must(version.NewVersion("0.0.0")), diags 84 }