opentofu/internal/terraform/schemas_test.go
Martin Atkins 38ec730b0e core: Opportunistic schema loading during graph construction
Previously the graph builders all expected to be given a full manifest
of all of the plugin component schemas that they could need during their
analysis work. That made sense when terraform.NewContext would always
proactively load all of the schemas before doing any other work, but we
now have a load-as-needed strategy for schemas.

We'll now have the graph builders use the contextPlugins object they each
already hold to retrieve individual schemas when needed. This avoids the
need to prepare a redundant data structure to pass alongside the
contextPlugins object, and leans on the memoization behavior inside
contextPlugins to preserve the old behavior of loading each provider's
schema only once.
2021-09-10 14:56:49 -07:00

66 lines
1.9 KiB
Go

package terraform
import (
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/configs/configschema"
"github.com/hashicorp/terraform/internal/providers"
)
func simpleTestSchemas() *Schemas {
provider := simpleMockProvider()
provisioner := simpleMockProvisioner()
return &Schemas{
Providers: map[addrs.Provider]*ProviderSchema{
addrs.NewDefaultProvider("test"): provider.ProviderSchema(),
},
Provisioners: map[string]*configschema.Block{
"test": provisioner.GetSchemaResponse.Provisioner,
},
}
}
// schemaOnlyProvidersForTesting is a testing helper that constructs a
// plugin library that contains a set of providers that only know how to
// return schema, and will exhibit undefined behavior if used for any other
// purpose.
//
// The intended use for this is in testing components that use schemas to
// drive other behavior, such as reference analysis during graph construction,
// but that don't actually need to interact with providers otherwise.
func schemaOnlyProvidersForTesting(schemas map[addrs.Provider]*ProviderSchema) *contextPlugins {
factories := make(map[addrs.Provider]providers.Factory, len(schemas))
for providerAddr, schema := range schemas {
resp := &providers.GetProviderSchemaResponse{
Provider: providers.Schema{
Block: schema.Provider,
},
ResourceTypes: make(map[string]providers.Schema),
DataSources: make(map[string]providers.Schema),
}
for t, tSchema := range schema.ResourceTypes {
resp.ResourceTypes[t] = providers.Schema{
Block: tSchema,
Version: int64(schema.ResourceTypeSchemaVersions[t]),
}
}
for t, tSchema := range schema.DataSources {
resp.DataSources[t] = providers.Schema{
Block: tSchema,
}
}
provider := &MockProvider{
GetProviderSchemaResponse: resp,
}
factories[providerAddr] = func() (providers.Interface, error) {
return provider, nil
}
}
return newContextPlugins(factories, nil)
}