core: even more nil checks to catch missing objects

These are all things that ought to be present in normal use but can end up
being nil in incorrect tests. Test debugging is simpler if these things
return errors gracefully, rather than crashing.
This commit is contained in:
Martin Atkins 2018-05-04 19:56:40 -07:00
parent 26c1584317
commit c82e3ec92f
7 changed files with 25 additions and 5 deletions

View File

@ -97,6 +97,10 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
provider := *n.Provider
providerSchema := *n.ProviderSchema
if providerSchema == nil {
return nil, fmt.Errorf("provider schema is unavailable for %s", n.Addr)
}
var diags tfdiags.Diagnostics
// The provider and hook APIs still expect our legacy InstanceInfo type.

View File

@ -59,6 +59,9 @@ func (n *EvalConfigProvider) Eval(ctx EvalContext) (interface{}, error) {
diags = diags.Append(err)
return nil, diags.NonFatalErr()
}
if schema == nil {
return nil, fmt.Errorf("schema not available for %s", n.Addr)
}
configSchema := schema.Provider
configBody := buildProviderConfig(ctx, n.Addr, config.Config)

View File

@ -30,6 +30,10 @@ type EvalReadDataDiff struct {
func (n *EvalReadDataDiff) Eval(ctx EvalContext) (interface{}, error) {
// TODO: test
if n.ProviderSchema == nil || *n.ProviderSchema == nil {
return nil, fmt.Errorf("provider schema not available for %s", n.Addr)
}
var diags tfdiags.Diagnostics
// The provider and hook APIs still expect our legacy InstanceInfo type.

View File

@ -83,6 +83,9 @@ func (n *EvalValidateProvider) Eval(ctx EvalContext) (interface{}, error) {
diags = diags.Append(err)
return nil, diags.NonFatalErr()
}
if schema == nil {
return nil, fmt.Errorf("no schema is available for %s", n.Addr)
}
configSchema := schema.Provider
configBody := buildProviderConfig(ctx, n.Addr, config.Config)
@ -319,7 +322,7 @@ type EvalValidateResource struct {
}
func (n *EvalValidateResource) Eval(ctx EvalContext) (interface{}, error) {
if n.ProviderSchema == nil {
if n.ProviderSchema == nil || *n.ProviderSchema == nil {
return nil, fmt.Errorf("EvalValidateResource has nil schema for %s", n.Addr)
}

View File

@ -193,8 +193,10 @@ func (n *NodeAbstractResource) References() []*addrs.Reference {
if p.When != configs.ProvisionerWhenCreate {
continue
}
refs, _ = lang.ReferencesInBlock(p.Connection.Config, connectionBlockSupersetSchema)
result = append(result, refs...)
if p.Connection != nil {
refs, _ = lang.ReferencesInBlock(p.Connection.Config, connectionBlockSupersetSchema)
result = append(result, refs...)
}
schema := n.ProvisionerSchemas[p.Type]
refs, _ = lang.ReferencesInBlock(p.Config, schema)

View File

@ -83,7 +83,9 @@ func (n *NodeDestroyResourceInstance) References() []*addrs.Reference {
schema := n.ProvisionerSchemas[p.Type]
if p.When == configs.ProvisionerWhenDestroy {
result = append(result, ReferencesFromConfig(p.Connection.Config, connectionBlockSupersetSchema)...)
if p.Connection != nil {
result = append(result, ReferencesFromConfig(p.Connection.Config, connectionBlockSupersetSchema)...)
}
result = append(result, ReferencesFromConfig(p.Config, schema)...)
}
}

View File

@ -93,7 +93,9 @@ func testModule(t *testing.T, name string) *configs.Config {
// sources only this ultimately just records all of the module paths
// in a JSON file so that we can load them below.
diags := loader.InstallModules(dir, true, configload.InstallHooksImpl{})
t.Fatal(diags.Error())
if diags.HasErrors() {
t.Fatal(diags.Error())
}
config, diags := loader.LoadConfig(dir)
if diags.HasErrors() {