terraform: return initialization required error when provider schemas not found (#24715)

A side effect of the various changes to the provider installer included losing the initialization required error message which would occur if a user removed or modified the .terraform directory.

Previously, plugin factories were created after the configuration was loaded, in terraform.NewContext. Terraform would compare the required providers (from config and state) to the available providers and return the aforementioned error if a provider was missing.

Provider factories are now loaded at the beginning of any terraform command, before terraform even loads the configuration, and therefore before terraform has a list of required providers.

This commit replaces the current error when a providers' schema cannot be found in the provider factories with the init error, and adds a command test (to plan tests, for no real reason other than that's what I thought of first).
This commit is contained in:
Kristin Laemmert 2020-04-21 16:29:27 -04:00 committed by GitHub
parent eb76f41031
commit 8108face36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 2 deletions

View File

@ -854,6 +854,34 @@ func TestPlan_shutdown(t *testing.T) {
}
}
func TestPlan_init_required(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(testFixturePath("plan")); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
// Running plan without setting testingOverrides is similar to plan without init
Ui: ui,
},
}
args := []string{}
if code := c.Run(args); code != 1 {
t.Fatalf("expected error, got success")
}
output := ui.ErrorWriter.String()
if !strings.Contains(output, `Plugin reinitialization required. Please run "terraform init".`) {
t.Fatal("wrong error message in output:", output)
}
}
// planFixtureSchema returns a schema suitable for processing the
// configuration in testdata/plan . This schema should be
// assigned to a mock provider named "test".

View File

@ -177,7 +177,11 @@ func NewContext(opts *ContextOpts) (*Context, tfdiags.Diagnostics) {
log.Printf("[TRACE] terraform.NewContext: loading provider schemas")
schemas, err := LoadSchemas(opts.Config, opts.State, components)
if err != nil {
diags = diags.Append(err)
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Could not load plugin",
fmt.Sprintf(errPluginInit, err),
))
return nil, diags
}

View File

@ -230,5 +230,7 @@ don't satisfy the version constraints, or are otherwise incompatible.
Terraform automatically discovers provider requirements from your
configuration, including providers used in child modules. To see the
requirements and constraints from each module, run "terraform providers".
requirements and constraints, run "terraform providers".
%s
`