github.com/opentofu/opentofu@v1.7.1/internal/tofu/schemas_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 tofu 7 8 import ( 9 "testing" 10 11 "github.com/opentofu/opentofu/internal/addrs" 12 "github.com/opentofu/opentofu/internal/configs/configschema" 13 "github.com/opentofu/opentofu/internal/providers" 14 ) 15 16 func simpleTestSchemas() *Schemas { 17 provider := simpleMockProvider() 18 provisioner := simpleMockProvisioner() 19 20 return &Schemas{ 21 Providers: map[addrs.Provider]providers.ProviderSchema{ 22 addrs.NewDefaultProvider("test"): provider.GetProviderSchema(), 23 }, 24 Provisioners: map[string]*configschema.Block{ 25 "test": provisioner.GetSchemaResponse.Provisioner, 26 }, 27 } 28 } 29 30 // schemaOnlyProvidersForTesting is a testing helper that constructs a 31 // plugin library that contains a set of providers that only know how to 32 // return schema, and will exhibit undefined behavior if used for any other 33 // purpose. 34 // 35 // The intended use for this is in testing components that use schemas to 36 // drive other behavior, such as reference analysis during graph construction, 37 // but that don't actually need to interact with providers otherwise. 38 func schemaOnlyProvidersForTesting(schemas map[addrs.Provider]providers.ProviderSchema, t *testing.T) *contextPlugins { 39 factories := make(map[addrs.Provider]providers.Factory, len(schemas)) 40 41 for providerAddr, schema := range schemas { 42 schema := schema 43 44 provider := &MockProvider{ 45 GetProviderSchemaResponse: &schema, 46 } 47 48 factories[providerAddr] = func() (providers.Interface, error) { 49 return provider, nil 50 } 51 } 52 53 return newContextPluginsForTest(factories, t) 54 }