helper/resource: Get schemas from Terraform context
Previously the test harness was preloading schemas from the providers before running any test steps. Since terraform.NewContext already deals with loading provider schemas, we can instead just use the schemas it loaded for our shimming needs, avoiding the need to reimplement the schema lookup behavior and thus the need to create a throwaway provider instance with which to do it.
This commit is contained in:
parent
a4991c5780
commit
55469cd416
|
@ -22,7 +22,6 @@ import (
|
|||
"github.com/hashicorp/terraform/addrs"
|
||||
"github.com/hashicorp/terraform/configs"
|
||||
"github.com/hashicorp/terraform/configs/configload"
|
||||
"github.com/hashicorp/terraform/configs/configschema"
|
||||
"github.com/hashicorp/terraform/helper/logging"
|
||||
"github.com/hashicorp/terraform/providers"
|
||||
"github.com/hashicorp/terraform/states"
|
||||
|
@ -478,42 +477,6 @@ func Test(t TestT, c TestCase) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// collect the provider schemas
|
||||
schemas := &terraform.Schemas{
|
||||
Providers: make(map[string]*terraform.ProviderSchema),
|
||||
}
|
||||
factories, err := testProviderFactories(c)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for providerName, f := range factories {
|
||||
p, err := f()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
resp := p.GetSchema()
|
||||
if resp.Diagnostics.HasErrors() {
|
||||
t.Fatal(fmt.Sprintf("error fetching schema for %q: %v", providerName, resp.Diagnostics.Err()))
|
||||
}
|
||||
|
||||
providerSchema := &terraform.ProviderSchema{
|
||||
Provider: resp.Provider.Block,
|
||||
ResourceTypes: make(map[string]*configschema.Block),
|
||||
DataSources: make(map[string]*configschema.Block),
|
||||
}
|
||||
|
||||
for r, s := range resp.ResourceTypes {
|
||||
providerSchema.ResourceTypes[r] = s.Block
|
||||
}
|
||||
|
||||
for d, s := range resp.DataSources {
|
||||
providerSchema.DataSources[d] = s.Block
|
||||
}
|
||||
|
||||
schemas.Providers[providerName] = providerSchema
|
||||
}
|
||||
|
||||
opts := terraform.ContextOpts{ProviderResolver: providerResolver}
|
||||
|
||||
// A single state variable to track the lifecycle, starting with no state
|
||||
|
@ -550,9 +513,9 @@ func Test(t TestT, c TestCase) {
|
|||
|
||||
// Can optionally set step.Config in addition to
|
||||
// step.ImportState, to provide config for the import.
|
||||
state, err = testStepImportState(opts, state, step, schemas)
|
||||
state, err = testStepImportState(opts, state, step)
|
||||
} else {
|
||||
state, err = testStepConfig(opts, state, step, schemas)
|
||||
state, err = testStepConfig(opts, state, step)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -637,7 +600,7 @@ func Test(t TestT, c TestCase) {
|
|||
}
|
||||
|
||||
log.Printf("[WARN] Test: Executing destroy step")
|
||||
state, err := testStep(opts, state, destroyStep, schemas)
|
||||
state, err := testStep(opts, state, destroyStep)
|
||||
if err != nil {
|
||||
t.Error(fmt.Sprintf(
|
||||
"Error destroying resource! WARNING: Dangling resources\n"+
|
||||
|
|
|
@ -24,12 +24,11 @@ import (
|
|||
func testStepConfig(
|
||||
opts terraform.ContextOpts,
|
||||
state *terraform.State,
|
||||
step TestStep,
|
||||
schemas *terraform.Schemas) (*terraform.State, error) {
|
||||
return testStep(opts, state, step, schemas)
|
||||
step TestStep) (*terraform.State, error) {
|
||||
return testStep(opts, state, step)
|
||||
}
|
||||
|
||||
func testStep(opts terraform.ContextOpts, state *terraform.State, step TestStep, schemas *terraform.Schemas) (*terraform.State, error) {
|
||||
func testStep(opts terraform.ContextOpts, state *terraform.State, step TestStep) (*terraform.State, error) {
|
||||
if !step.Destroy {
|
||||
if err := testStepTaint(state, step); err != nil {
|
||||
return state, err
|
||||
|
@ -63,6 +62,10 @@ func testStep(opts terraform.ContextOpts, state *terraform.State, step TestStep,
|
|||
log.Printf("[WARN] Config warnings:\n%s", stepDiags)
|
||||
}
|
||||
|
||||
// We will need access to the schemas in order to shim to the old-style
|
||||
// testing API.
|
||||
schemas := ctx.Schemas()
|
||||
|
||||
// Refresh!
|
||||
newState, stepDiags := ctx.Refresh()
|
||||
// shim the state first so the test can check the state on errors
|
||||
|
|
|
@ -19,8 +19,8 @@ import (
|
|||
func testStepImportState(
|
||||
opts terraform.ContextOpts,
|
||||
state *terraform.State,
|
||||
step TestStep,
|
||||
schemas *terraform.Schemas) (*terraform.State, error) {
|
||||
step TestStep) (*terraform.State, error) {
|
||||
|
||||
// Determine the ID to import
|
||||
var importId string
|
||||
switch {
|
||||
|
@ -62,6 +62,10 @@ func testStepImportState(
|
|||
return state, stepDiags.Err()
|
||||
}
|
||||
|
||||
// We will need access to the schemas in order to shim to the old-style
|
||||
// testing API.
|
||||
schemas := ctx.Schemas()
|
||||
|
||||
// The test step provides the resource address as a string, so we need
|
||||
// to parse it to get an addrs.AbsResourceAddress to pass in to the
|
||||
// import method.
|
||||
|
|
Loading…
Reference in New Issue