2023-05-02 11:33:06 -04:00
|
|
|
// Copyright IBM Corp. 2014, 2026
|
2023-08-10 18:43:27 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-05-02 11:33:06 -04:00
|
|
|
|
2015-11-05 09:47:08 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
|
|
import (
|
2020-12-11 13:09:25 -05:00
|
|
|
"encoding/json"
|
2026-02-26 11:33:01 -05:00
|
|
|
"io"
|
2017-08-28 15:01:11 -04:00
|
|
|
"os"
|
2020-12-11 13:09:25 -05:00
|
|
|
"path"
|
2015-11-05 09:47:08 -05:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
2016-02-08 17:04:24 -05:00
|
|
|
|
2020-12-11 13:09:25 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2023-12-20 06:04:10 -05:00
|
|
|
"github.com/hashicorp/cli"
|
2018-10-13 12:33:18 -04:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
2023-07-26 04:56:44 -04:00
|
|
|
testing_command "github.com/hashicorp/terraform/internal/command/testing"
|
|
|
|
|
"github.com/hashicorp/terraform/internal/command/views"
|
2021-05-17 15:17:09 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
2021-05-17 13:40:40 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/providers"
|
2021-03-18 11:14:58 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/terminal"
|
2026-02-16 05:09:26 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
2015-11-05 09:47:08 -05:00
|
|
|
)
|
|
|
|
|
|
2021-03-18 11:14:58 -04:00
|
|
|
func setupTest(t *testing.T, fixturepath string, args ...string) (*terminal.TestOutput, int) {
|
|
|
|
|
view, done := testView(t)
|
2018-10-13 12:33:18 -04:00
|
|
|
p := testProvider()
|
2021-02-18 10:13:43 -05:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 16:13:10 -05:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-10-13 12:33:18 -04:00
|
|
|
"test_instance": {
|
2025-03-04 10:33:43 -05:00
|
|
|
Body: &configschema.Block{
|
2021-01-12 16:13:10 -05:00
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
|
},
|
|
|
|
|
BlockTypes: map[string]*configschema.NestedBlock{
|
|
|
|
|
"network_interface": {
|
|
|
|
|
Nesting: configschema.NestingList,
|
|
|
|
|
Block: configschema.Block{
|
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
|
"device_index": {Type: cty.String, Optional: true},
|
|
|
|
|
"description": {Type: cty.String, Optional: true},
|
|
|
|
|
"name": {Type: cty.String, Optional: true},
|
|
|
|
|
},
|
2018-10-13 12:33:18 -04:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
2015-11-05 09:47:08 -05:00
|
|
|
c := &ValidateCommand{
|
|
|
|
|
Meta: Meta{
|
2018-10-13 12:33:18 -04:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2021-03-18 11:14:58 -04:00
|
|
|
View: view,
|
2015-11-05 09:47:08 -05:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 11:14:58 -04:00
|
|
|
args = append(args, "-no-color")
|
2017-07-05 12:32:29 -04:00
|
|
|
args = append(args, testFixturePath(fixturepath))
|
2015-11-05 09:47:08 -05:00
|
|
|
|
|
|
|
|
code := c.Run(args)
|
2021-03-18 11:14:58 -04:00
|
|
|
return done(t), code
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
2017-07-05 12:32:29 -04:00
|
|
|
|
2015-11-05 09:47:08 -05:00
|
|
|
func TestValidateCommand(t *testing.T) {
|
2021-03-18 11:14:58 -04:00
|
|
|
if output, code := setupTest(t, "validate-valid"); code != 0 {
|
|
|
|
|
t.Fatalf("unexpected non-successful exit code %d\n\n%s", code, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 15:01:11 -04:00
|
|
|
func TestValidateCommandWithTfvarsFile(t *testing.T) {
|
|
|
|
|
// Create a temporary working directory that is empty because this test
|
|
|
|
|
// requires scanning the current working directory by validate command.
|
2022-04-08 12:34:16 -04:00
|
|
|
td := t.TempDir()
|
2020-10-07 12:48:25 -04:00
|
|
|
testCopyDir(t, testFixturePath("validate-valid/with-tfvars-file"), td)
|
2025-07-16 11:04:10 -04:00
|
|
|
t.Chdir(td)
|
2017-08-28 15:01:11 -04:00
|
|
|
|
2021-03-18 11:14:58 -04:00
|
|
|
view, done := testView(t)
|
2017-08-28 15:01:11 -04:00
|
|
|
c := &ValidateCommand{
|
|
|
|
|
Meta: Meta{
|
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
2021-03-18 11:14:58 -04:00
|
|
|
View: view,
|
2017-08-28 15:01:11 -04:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args := []string{}
|
2021-03-18 11:14:58 -04:00
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("bad %d\n\n%s", code, output.Stderr())
|
2017-08-28 15:01:11 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-05 09:47:08 -05:00
|
|
|
func TestValidateFailingCommand(t *testing.T) {
|
2021-03-18 11:14:58 -04:00
|
|
|
if output, code := setupTest(t, "validate-invalid"); code != 1 {
|
|
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestValidateFailingCommandMissingQuote(t *testing.T) {
|
2021-03-18 11:14:58 -04:00
|
|
|
output, code := setupTest(t, "validate-invalid/missing_quote")
|
2015-11-05 09:47:08 -05:00
|
|
|
|
|
|
|
|
if code != 1 {
|
2021-03-18 11:14:58 -04:00
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
2020-12-11 13:09:25 -05:00
|
|
|
wantError := "Error: Invalid reference"
|
2021-03-18 11:14:58 -04:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestValidateFailingCommandMissingVariable(t *testing.T) {
|
2021-03-18 11:14:58 -04:00
|
|
|
output, code := setupTest(t, "validate-invalid/missing_var")
|
2015-11-05 09:47:08 -05:00
|
|
|
if code != 1 {
|
2021-03-18 11:14:58 -04:00
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
2020-12-11 13:09:25 -05:00
|
|
|
wantError := "Error: Reference to undeclared input variable"
|
2021-03-18 11:14:58 -04:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-05 13:13:58 -05:00
|
|
|
func TestSameProviderMultipleTimesShouldFail(t *testing.T) {
|
2021-03-18 11:14:58 -04:00
|
|
|
output, code := setupTest(t, "validate-invalid/multiple_providers")
|
2015-11-05 09:47:08 -05:00
|
|
|
if code != 1 {
|
2021-03-18 11:14:58 -04:00
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
command: beginnings of new config loader in "terraform validate"
As part of some light reorganization of our commands, this new
implementation no longer does validation of variables and will thus avoid
the need to spin up a fully-valid context. Instead, its focus is on
validating the configuration itself, regardless of any variables, state,
etc.
This change anticipates us later adding a -validate-only flag to
"terraform plan" which will then take over the related use-case of
checking if a particular execution of Terraform is valid, _including_ the
state, variables, etc.
Although leaving variables out of validate feels pretty arbitrary today
while all of the variable sources are local anyway, we have plans to
allow per-workspace variables to be stored in the backend in future and
at that point it will no longer be possible to fully validate variables
without accessing the backend. The "terraform plan" command explicitly
requires access to the backend, while "terraform validate" is now
explicitly for local-only validation of a single module.
In a future commit this will be extended to do basic type checking of
the configuration based on provider schemas, etc.
2018-02-28 20:14:05 -05:00
|
|
|
wantError := "Error: Duplicate provider configuration"
|
2021-03-18 11:14:58 -04:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSameModuleMultipleTimesShouldFail(t *testing.T) {
|
2021-03-18 11:14:58 -04:00
|
|
|
output, code := setupTest(t, "validate-invalid/multiple_modules")
|
2015-11-05 09:47:08 -05:00
|
|
|
if code != 1 {
|
2021-03-18 11:14:58 -04:00
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
command: beginnings of new config loader in "terraform validate"
As part of some light reorganization of our commands, this new
implementation no longer does validation of variables and will thus avoid
the need to spin up a fully-valid context. Instead, its focus is on
validating the configuration itself, regardless of any variables, state,
etc.
This change anticipates us later adding a -validate-only flag to
"terraform plan" which will then take over the related use-case of
checking if a particular execution of Terraform is valid, _including_ the
state, variables, etc.
Although leaving variables out of validate feels pretty arbitrary today
while all of the variable sources are local anyway, we have plans to
allow per-workspace variables to be stored in the backend in future and
at that point it will no longer be possible to fully validate variables
without accessing the backend. The "terraform plan" command explicitly
requires access to the backend, while "terraform validate" is now
explicitly for local-only validation of a single module.
In a future commit this will be extended to do basic type checking of
the configuration based on provider schemas, etc.
2018-02-28 20:14:05 -05:00
|
|
|
wantError := "Error: Duplicate module call"
|
2021-03-18 11:14:58 -04:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSameResourceMultipleTimesShouldFail(t *testing.T) {
|
2021-03-18 11:14:58 -04:00
|
|
|
output, code := setupTest(t, "validate-invalid/multiple_resources")
|
2015-11-05 09:47:08 -05:00
|
|
|
if code != 1 {
|
2021-03-18 11:14:58 -04:00
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
command: beginnings of new config loader in "terraform validate"
As part of some light reorganization of our commands, this new
implementation no longer does validation of variables and will thus avoid
the need to spin up a fully-valid context. Instead, its focus is on
validating the configuration itself, regardless of any variables, state,
etc.
This change anticipates us later adding a -validate-only flag to
"terraform plan" which will then take over the related use-case of
checking if a particular execution of Terraform is valid, _including_ the
state, variables, etc.
Although leaving variables out of validate feels pretty arbitrary today
while all of the variable sources are local anyway, we have plans to
allow per-workspace variables to be stored in the backend in future and
at that point it will no longer be possible to fully validate variables
without accessing the backend. The "terraform plan" command explicitly
requires access to the backend, while "terraform validate" is now
explicitly for local-only validation of a single module.
In a future commit this will be extended to do basic type checking of
the configuration based on provider schemas, etc.
2018-02-28 20:14:05 -05:00
|
|
|
wantError := `Error: Duplicate resource "aws_instance" configuration`
|
2021-03-18 11:14:58 -04:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-12 18:14:44 -04:00
|
|
|
func TestSameImportTargetMultipleTimesShouldFail(t *testing.T) {
|
|
|
|
|
output, code := setupTest(t, "validate-invalid/duplicate_import_targets")
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
|
|
|
|
}
|
|
|
|
|
wantError := `Error: Duplicate import configuration for "aws_instance.web"`
|
|
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-05 09:47:08 -05:00
|
|
|
func TestOutputWithoutValueShouldFail(t *testing.T) {
|
2021-03-18 11:14:58 -04:00
|
|
|
output, code := setupTest(t, "validate-invalid/outputs")
|
2015-11-05 09:47:08 -05:00
|
|
|
if code != 1 {
|
2021-03-18 11:14:58 -04:00
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
2018-11-24 15:46:49 -05:00
|
|
|
wantError := `The argument "value" is required, but no definition was found.`
|
2021-03-18 11:14:58 -04:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
command: beginnings of new config loader in "terraform validate"
As part of some light reorganization of our commands, this new
implementation no longer does validation of variables and will thus avoid
the need to spin up a fully-valid context. Instead, its focus is on
validating the configuration itself, regardless of any variables, state,
etc.
This change anticipates us later adding a -validate-only flag to
"terraform plan" which will then take over the related use-case of
checking if a particular execution of Terraform is valid, _including_ the
state, variables, etc.
Although leaving variables out of validate feels pretty arbitrary today
while all of the variable sources are local anyway, we have plans to
allow per-workspace variables to be stored in the backend in future and
at that point it will no longer be possible to fully validate variables
without accessing the backend. The "terraform plan" command explicitly
requires access to the backend, while "terraform validate" is now
explicitly for local-only validation of a single module.
In a future commit this will be extended to do basic type checking of
the configuration based on provider schemas, etc.
2018-02-28 20:14:05 -05:00
|
|
|
}
|
2018-11-24 15:46:49 -05:00
|
|
|
wantError = `An argument named "values" is not expected here. Did you mean "value"?`
|
2021-03-18 11:14:58 -04:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestModuleWithIncorrectNameShouldFail(t *testing.T) {
|
2021-03-18 11:14:58 -04:00
|
|
|
output, code := setupTest(t, "validate-invalid/incorrectmodulename")
|
2015-11-05 09:47:08 -05:00
|
|
|
if code != 1 {
|
2021-03-18 11:14:58 -04:00
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
|
|
|
|
|
command: beginnings of new config loader in "terraform validate"
As part of some light reorganization of our commands, this new
implementation no longer does validation of variables and will thus avoid
the need to spin up a fully-valid context. Instead, its focus is on
validating the configuration itself, regardless of any variables, state,
etc.
This change anticipates us later adding a -validate-only flag to
"terraform plan" which will then take over the related use-case of
checking if a particular execution of Terraform is valid, _including_ the
state, variables, etc.
Although leaving variables out of validate feels pretty arbitrary today
while all of the variable sources are local anyway, we have plans to
allow per-workspace variables to be stored in the backend in future and
at that point it will no longer be possible to fully validate variables
without accessing the backend. The "terraform plan" command explicitly
requires access to the backend, while "terraform validate" is now
explicitly for local-only validation of a single module.
In a future commit this will be extended to do basic type checking of
the configuration based on provider schemas, etc.
2018-02-28 20:14:05 -05:00
|
|
|
wantError := `Error: Invalid module instance name`
|
2021-03-18 11:14:58 -04:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestWronglyUsedInterpolationShouldFail(t *testing.T) {
|
2021-03-18 11:14:58 -04:00
|
|
|
output, code := setupTest(t, "validate-invalid/interpolation")
|
2015-11-05 09:47:08 -05:00
|
|
|
if code != 1 {
|
2021-03-18 11:14:58 -04:00
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
|
|
|
|
|
command: beginnings of new config loader in "terraform validate"
As part of some light reorganization of our commands, this new
implementation no longer does validation of variables and will thus avoid
the need to spin up a fully-valid context. Instead, its focus is on
validating the configuration itself, regardless of any variables, state,
etc.
This change anticipates us later adding a -validate-only flag to
"terraform plan" which will then take over the related use-case of
checking if a particular execution of Terraform is valid, _including_ the
state, variables, etc.
Although leaving variables out of validate feels pretty arbitrary today
while all of the variable sources are local anyway, we have plans to
allow per-workspace variables to be stored in the backend in future and
at that point it will no longer be possible to fully validate variables
without accessing the backend. The "terraform plan" command explicitly
requires access to the backend, while "terraform validate" is now
explicitly for local-only validation of a single module.
In a future commit this will be extended to do basic type checking of
the configuration based on provider schemas, etc.
2018-02-28 20:14:05 -05:00
|
|
|
wantError := `Error: Variables not allowed`
|
2021-03-18 11:14:58 -04:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
2019-06-18 17:58:48 -04:00
|
|
|
wantError = `A single static variable reference is required`
|
2021-03-18 11:14:58 -04:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 09:47:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
2017-07-05 12:32:29 -04:00
|
|
|
|
|
|
|
|
func TestMissingDefinedVar(t *testing.T) {
|
2021-03-18 11:14:58 -04:00
|
|
|
output, code := setupTest(t, "validate-invalid/missing_defined_var")
|
command: beginnings of new config loader in "terraform validate"
As part of some light reorganization of our commands, this new
implementation no longer does validation of variables and will thus avoid
the need to spin up a fully-valid context. Instead, its focus is on
validating the configuration itself, regardless of any variables, state,
etc.
This change anticipates us later adding a -validate-only flag to
"terraform plan" which will then take over the related use-case of
checking if a particular execution of Terraform is valid, _including_ the
state, variables, etc.
Although leaving variables out of validate feels pretty arbitrary today
while all of the variable sources are local anyway, we have plans to
allow per-workspace variables to be stored in the backend in future and
at that point it will no longer be possible to fully validate variables
without accessing the backend. The "terraform plan" command explicitly
requires access to the backend, while "terraform validate" is now
explicitly for local-only validation of a single module.
In a future commit this will be extended to do basic type checking of
the configuration based on provider schemas, etc.
2018-02-28 20:14:05 -05:00
|
|
|
// This is allowed because validate tests only that variables are referenced
|
|
|
|
|
// correctly, not that they all have defined values.
|
2017-07-05 12:32:29 -04:00
|
|
|
if code != 0 {
|
2021-03-18 11:14:58 -04:00
|
|
|
t.Fatalf("Should have passed: %d\n\n%s", code, output.Stderr())
|
2017-07-05 12:32:29 -04:00
|
|
|
}
|
|
|
|
|
}
|
2020-12-11 13:09:25 -05:00
|
|
|
|
2023-07-26 04:56:44 -04:00
|
|
|
func TestValidateWithInvalidTestFile(t *testing.T) {
|
|
|
|
|
// We're reusing some testing configs that were written for testing the
|
|
|
|
|
// test command here, so we have to initalise things slightly differently
|
|
|
|
|
// to the other tests.
|
|
|
|
|
|
|
|
|
|
view, done := testView(t)
|
|
|
|
|
provider := testing_command.NewProvider(nil)
|
|
|
|
|
c := &ValidateCommand{
|
|
|
|
|
Meta: Meta{
|
|
|
|
|
testingOverrides: metaOverridesForProvider(provider.Provider),
|
|
|
|
|
View: view,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var args []string
|
|
|
|
|
args = append(args, "-no-color")
|
|
|
|
|
args = append(args, testFixturePath("test/invalid"))
|
|
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wantError := "Error: Invalid `expect_failures` reference"
|
|
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestValidateWithInvalidTestModule(t *testing.T) {
|
|
|
|
|
// We're reusing some testing configs that were written for testing the
|
|
|
|
|
// test command here, so we have to initalise things slightly differently
|
|
|
|
|
// to the other tests.
|
|
|
|
|
|
|
|
|
|
td := t.TempDir()
|
|
|
|
|
testCopyDir(t, testFixturePath(path.Join("test", "invalid-module")), td)
|
2025-07-16 11:04:10 -04:00
|
|
|
t.Chdir(td)
|
2023-07-26 04:56:44 -04:00
|
|
|
|
|
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
view := views.NewView(streams)
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
|
|
|
|
|
|
provider := testing_command.NewProvider(nil)
|
|
|
|
|
|
|
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
|
|
|
|
"test": {"1.0.0"},
|
|
|
|
|
})
|
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
|
|
meta := Meta{
|
|
|
|
|
testingOverrides: metaOverridesForProvider(provider.Provider),
|
|
|
|
|
Ui: ui,
|
|
|
|
|
View: view,
|
|
|
|
|
Streams: streams,
|
|
|
|
|
ProviderSource: providerSource,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init := &InitCommand{
|
|
|
|
|
Meta: meta,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if code := init.Run(nil); code != 0 {
|
|
|
|
|
t.Fatalf("expected status code 0 but got %d: %s", code, ui.ErrorWriter)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c := &ValidateCommand{
|
|
|
|
|
Meta: meta,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var args []string
|
|
|
|
|
args = append(args, "-no-color")
|
|
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wantError := "Error: Reference to undeclared input variable"
|
|
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 04:36:06 -05:00
|
|
|
func TestValidateWithInvalidOverrides(t *testing.T) {
|
|
|
|
|
// We're reusing some testing configs that were written for testing the
|
|
|
|
|
// test command here, so we have to initalise things slightly differently
|
|
|
|
|
// to the other tests.
|
|
|
|
|
|
|
|
|
|
td := t.TempDir()
|
|
|
|
|
testCopyDir(t, testFixturePath(path.Join("test", "invalid-overrides")), td)
|
2025-07-16 11:04:10 -04:00
|
|
|
t.Chdir(td)
|
2023-11-13 04:36:06 -05:00
|
|
|
|
|
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
view := views.NewView(streams)
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
|
|
|
|
|
|
provider := testing_command.NewProvider(nil)
|
|
|
|
|
|
|
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
|
|
|
|
"test": {"1.0.0"},
|
|
|
|
|
})
|
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
|
|
meta := Meta{
|
|
|
|
|
testingOverrides: metaOverridesForProvider(provider.Provider),
|
|
|
|
|
Ui: ui,
|
|
|
|
|
View: view,
|
|
|
|
|
Streams: streams,
|
|
|
|
|
ProviderSource: providerSource,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init := &InitCommand{
|
|
|
|
|
Meta: meta,
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-24 01:57:29 -04:00
|
|
|
output := done(t)
|
2023-11-13 04:36:06 -05:00
|
|
|
if code := init.Run(nil); code != 0 {
|
2024-04-24 01:57:29 -04:00
|
|
|
t.Fatalf("expected status code 0 but got %d: %s", code, output.All())
|
2023-11-13 04:36:06 -05:00
|
|
|
}
|
|
|
|
|
|
2024-04-24 01:57:29 -04:00
|
|
|
// reset streams for next command
|
|
|
|
|
streams, done = terminal.StreamsForTesting(t)
|
|
|
|
|
meta.View = views.NewView(streams)
|
|
|
|
|
meta.Streams = streams
|
|
|
|
|
|
2023-11-13 04:36:06 -05:00
|
|
|
c := &ValidateCommand{
|
|
|
|
|
Meta: meta,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var args []string
|
|
|
|
|
args = append(args, "-no-color")
|
|
|
|
|
|
|
|
|
|
code := c.Run(args)
|
2024-04-24 01:57:29 -04:00
|
|
|
output = done(t)
|
2023-11-13 04:36:06 -05:00
|
|
|
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Errorf("Should have passed: %d\n\n%s", code, output.Stderr())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
actual := output.All()
|
2024-04-24 01:57:29 -04:00
|
|
|
expected := `
|
2023-11-13 04:36:06 -05:00
|
|
|
Warning: Invalid override target
|
|
|
|
|
|
|
|
|
|
on main.tftest.hcl line 4, in mock_provider "test":
|
|
|
|
|
4: target = test_resource.absent_one
|
|
|
|
|
|
|
|
|
|
The override target test_resource.absent_one does not exist within the
|
|
|
|
|
configuration under test. This could indicate a typo in the target address or
|
|
|
|
|
an unnecessary override.
|
|
|
|
|
|
|
|
|
|
(and 5 more similar warnings elsewhere)
|
|
|
|
|
Success! The configuration is valid, but there were some validation warnings
|
|
|
|
|
as shown above.
|
|
|
|
|
|
|
|
|
|
`
|
|
|
|
|
if diff := cmp.Diff(expected, actual); len(diff) > 0 {
|
|
|
|
|
t.Errorf("expected:\n%s\nactual:\n%s\ndiff:\n%s", expected, actual, diff)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-11 13:09:25 -05:00
|
|
|
func TestValidate_json(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
path string
|
|
|
|
|
valid bool
|
|
|
|
|
}{
|
|
|
|
|
{"validate-valid", true},
|
|
|
|
|
{"validate-invalid", false},
|
|
|
|
|
{"validate-invalid/missing_quote", false},
|
|
|
|
|
{"validate-invalid/missing_var", false},
|
|
|
|
|
{"validate-invalid/multiple_providers", false},
|
|
|
|
|
{"validate-invalid/multiple_modules", false},
|
|
|
|
|
{"validate-invalid/multiple_resources", false},
|
2023-05-12 18:14:44 -04:00
|
|
|
{"validate-invalid/duplicate_import_targets", false},
|
2020-12-11 13:09:25 -05:00
|
|
|
{"validate-invalid/outputs", false},
|
|
|
|
|
{"validate-invalid/incorrectmodulename", false},
|
|
|
|
|
{"validate-invalid/interpolation", false},
|
|
|
|
|
{"validate-invalid/missing_defined_var", true},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
|
t.Run(tc.path, func(t *testing.T) {
|
2026-02-26 11:33:01 -05:00
|
|
|
var want, got map[string]any
|
2020-12-11 13:09:25 -05:00
|
|
|
|
|
|
|
|
wantFile, err := os.Open(path.Join(testFixturePath(tc.path), "output.json"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to open output file: %s", err)
|
|
|
|
|
}
|
|
|
|
|
defer wantFile.Close()
|
2026-02-26 11:33:01 -05:00
|
|
|
wantBytes, err := io.ReadAll(wantFile)
|
2020-12-11 13:09:25 -05:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to read output file: %s", err)
|
|
|
|
|
}
|
|
|
|
|
err = json.Unmarshal([]byte(wantBytes), &want)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to unmarshal expected JSON: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 11:14:58 -04:00
|
|
|
output, code := setupTest(t, tc.path, "-json")
|
2020-12-11 13:09:25 -05:00
|
|
|
|
2021-03-18 11:14:58 -04:00
|
|
|
gotString := output.Stdout()
|
2020-12-11 13:09:25 -05:00
|
|
|
err = json.Unmarshal([]byte(gotString), &got)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to unmarshal actual JSON: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !cmp.Equal(got, want) {
|
|
|
|
|
t.Errorf("wrong output:\n %v\n", cmp.Diff(got, want))
|
|
|
|
|
t.Errorf("raw output:\n%s\n", gotString)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if tc.valid && code != 0 {
|
|
|
|
|
t.Errorf("wrong exit code: want 0, got %d", code)
|
|
|
|
|
} else if !tc.valid && code != 1 {
|
|
|
|
|
t.Errorf("wrong exit code: want 1, got %d", code)
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 11:14:58 -04:00
|
|
|
if errorOutput := output.Stderr(); errorOutput != "" {
|
2020-12-11 13:09:25 -05:00
|
|
|
t.Errorf("unexpected error output:\n%s", errorOutput)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-01 05:33:52 -04:00
|
|
|
|
|
|
|
|
func TestValidateWithInvalidListResource(t *testing.T) {
|
|
|
|
|
td := t.TempDir()
|
|
|
|
|
cases := []struct {
|
|
|
|
|
name string
|
|
|
|
|
path string
|
|
|
|
|
wantError string
|
|
|
|
|
args []string
|
|
|
|
|
code int
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "invalid-traversal with validate -query command",
|
|
|
|
|
path: "query/invalid-traversal",
|
|
|
|
|
wantError: `
|
|
|
|
|
Error: Invalid list resource traversal
|
|
|
|
|
|
|
|
|
|
on main.tfquery.hcl line 19, in list "test_instance" "test2":
|
|
|
|
|
19: ami = list.test_instance.test.state.instance_type
|
|
|
|
|
|
|
|
|
|
The first step in the traversal for a list resource must be an attribute
|
|
|
|
|
"data".
|
|
|
|
|
`,
|
|
|
|
|
args: []string{"-query"},
|
|
|
|
|
code: 1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "invalid-traversal with no -query",
|
|
|
|
|
path: "query/invalid-traversal",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
testCopyDir(t, testFixturePath(tc.path), td)
|
|
|
|
|
t.Chdir(td)
|
|
|
|
|
|
|
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
view := views.NewView(streams)
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
|
|
|
|
|
|
provider := queryFixtureProvider()
|
|
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
|
|
|
|
"test": {"1.0.0"},
|
|
|
|
|
})
|
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
|
|
meta := Meta{
|
|
|
|
|
testingOverrides: metaOverridesForProvider(provider),
|
|
|
|
|
Ui: ui,
|
|
|
|
|
View: view,
|
|
|
|
|
Streams: streams,
|
|
|
|
|
ProviderSource: providerSource,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init := &InitCommand{
|
|
|
|
|
Meta: meta,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if code := init.Run(nil); code != 0 {
|
|
|
|
|
t.Fatalf("expected status code 0 but got %d: %s", code, ui.ErrorWriter)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c := &ValidateCommand{
|
|
|
|
|
Meta: meta,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var args []string
|
|
|
|
|
args = append(args, "-no-color")
|
|
|
|
|
args = append(args, tc.args...)
|
|
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
|
|
|
|
|
if code != tc.code {
|
|
|
|
|
t.Fatalf("Expected status code %d but got %d: %s", tc.code, code, output.Stderr())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if diff := cmp.Diff(tc.wantError, output.Stderr()); diff != "" {
|
|
|
|
|
t.Fatalf("Expected error string %q but got %q\n\ndiff: \n%s", tc.wantError, output.Stderr(), diff)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-05 13:13:58 -05:00
|
|
|
|
|
|
|
|
func TestValidate_backendBlocks(t *testing.T) {
|
|
|
|
|
t.Run("invalid when block contains a repeated attribute", func(t *testing.T) {
|
|
|
|
|
output, code := setupTest(t, "invalid-backend-configuration/repeated-attr")
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("unexpected successful exit code %d\n\n%s", code, output.Stdout())
|
|
|
|
|
}
|
2025-12-17 13:09:54 -05:00
|
|
|
expectedErr := "Error: Attribute redefined"
|
|
|
|
|
if !strings.Contains(output.Stderr(), expectedErr) {
|
2025-12-05 13:13:58 -05:00
|
|
|
t.Fatalf("unexpected error content: wanted %q, got: %s",
|
2025-12-17 13:09:54 -05:00
|
|
|
expectedErr,
|
2025-12-05 13:13:58 -05:00
|
|
|
output.Stderr(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-13 10:31:57 -05:00
|
|
|
t.Run("invalid when the backend type is unknown", func(t *testing.T) {
|
2025-12-17 13:09:54 -05:00
|
|
|
output, code := setupTest(t, "invalid-backend-configuration/unknown-backend-type")
|
2026-02-13 10:31:57 -05:00
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("expected an unsuccessful exit code %d\n\n%s", code, output.Stderr())
|
2025-12-17 13:09:54 -05:00
|
|
|
}
|
2026-02-13 10:31:57 -05:00
|
|
|
expectedErr := "Error: Unsupported backend type"
|
|
|
|
|
if !strings.Contains(output.Stderr(), expectedErr) {
|
|
|
|
|
t.Fatalf("unexpected error content: wanted %q, got: %s",
|
|
|
|
|
expectedErr,
|
|
|
|
|
output.Stderr(),
|
2025-12-17 13:09:54 -05:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-13 10:31:57 -05:00
|
|
|
t.Run("invalid when there's an unknown attribute present", func(t *testing.T) {
|
2025-12-17 13:09:54 -05:00
|
|
|
output, code := setupTest(t, "invalid-backend-configuration/unknown-attr")
|
2026-02-13 10:31:57 -05:00
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("expected an unsuccessful exit code %d\n\n%s", code, output.Stdout())
|
2025-12-17 13:09:54 -05:00
|
|
|
}
|
2026-02-13 10:31:57 -05:00
|
|
|
expectedErr := "Error: Unsupported argument"
|
|
|
|
|
if !strings.Contains(output.Stderr(), expectedErr) {
|
|
|
|
|
t.Fatalf("unexpected error content: wanted %q, got: %s",
|
|
|
|
|
expectedErr,
|
|
|
|
|
output.Stderr(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("invalid when a required attribute is unset", func(t *testing.T) {
|
|
|
|
|
output, code := setupTest(t, "invalid-backend-configuration/missing-required-attr")
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("expected an unsuccessful exit code %d\n\n%s", code, output.Stdout())
|
|
|
|
|
}
|
|
|
|
|
expectedErr := "Error: Missing required argument"
|
|
|
|
|
if !strings.Contains(output.Stderr(), expectedErr) {
|
|
|
|
|
t.Fatalf("unexpected error content: wanted %q, got: %s",
|
|
|
|
|
expectedErr,
|
|
|
|
|
output.Stderr(),
|
2025-12-17 13:09:54 -05:00
|
|
|
)
|
2025-12-05 13:13:58 -05:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-16 05:09:26 -05:00
|
|
|
func TestValidate_stateStoreBlocks(t *testing.T) {
|
|
|
|
|
t.Run("invalid when state_store block contains a repeated attribute", func(t *testing.T) {
|
|
|
|
|
fixturePath := "invalid-state-store-configuration/repeated-attr-in-store"
|
|
|
|
|
view, done := testView(t)
|
|
|
|
|
mock := mockPluggableStateStorageProvider()
|
|
|
|
|
c := &ValidateCommand{
|
|
|
|
|
Meta: Meta{
|
|
|
|
|
testingOverrides: metaOverridesForProvider(mock),
|
|
|
|
|
View: view,
|
|
|
|
|
AllowExperimentalFeatures: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
args := []string{"-no-color", testFixturePath(fixturePath)}
|
|
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("unexpected successful exit code %d\n\n%s", code, output.Stdout())
|
|
|
|
|
}
|
|
|
|
|
expectedErr := "Error: Attribute redefined"
|
|
|
|
|
if !strings.Contains(output.Stderr(), expectedErr) {
|
|
|
|
|
t.Fatalf("unexpected error content: wanted %q, got: %s",
|
|
|
|
|
expectedErr,
|
|
|
|
|
output.Stderr(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("invalid when state_store's provider block contains a repeated attribute", func(t *testing.T) {
|
|
|
|
|
fixturePath := "invalid-state-store-configuration/repeated-attr-in-provider"
|
|
|
|
|
view, done := testView(t)
|
|
|
|
|
mock := mockPluggableStateStorageProvider()
|
|
|
|
|
c := &ValidateCommand{
|
|
|
|
|
Meta: Meta{
|
|
|
|
|
testingOverrides: metaOverridesForProvider(mock),
|
|
|
|
|
View: view,
|
|
|
|
|
AllowExperimentalFeatures: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
args := []string{"-no-color", testFixturePath(fixturePath)}
|
|
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("unexpected successful exit code %d\n\n%s", code, output.Stdout())
|
|
|
|
|
}
|
|
|
|
|
expectedErr := "Error: Attribute redefined"
|
|
|
|
|
if !strings.Contains(output.Stderr(), expectedErr) {
|
|
|
|
|
t.Fatalf("unexpected error content: wanted %q, got: %s",
|
|
|
|
|
expectedErr,
|
|
|
|
|
output.Stderr(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("invalid when the state store type is unknown in that provider", func(t *testing.T) {
|
|
|
|
|
fixturePath := "invalid-state-store-configuration/unknown-store-type"
|
|
|
|
|
view, done := testView(t)
|
|
|
|
|
mock := mockPluggableStateStorageProvider()
|
|
|
|
|
c := &ValidateCommand{
|
|
|
|
|
Meta: Meta{
|
|
|
|
|
testingOverrides: metaOverridesForProvider(mock),
|
|
|
|
|
View: view,
|
|
|
|
|
AllowExperimentalFeatures: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
args := []string{"-no-color", testFixturePath(fixturePath)}
|
|
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("unexpected successful exit code %d\n\n%s", code, output.Stdout())
|
|
|
|
|
}
|
|
|
|
|
expectedErr := "Error: State store not implemented by the provider"
|
|
|
|
|
if !strings.Contains(output.Stderr(), expectedErr) {
|
|
|
|
|
t.Fatalf("unexpected error content: wanted %q, got: %s",
|
|
|
|
|
expectedErr,
|
|
|
|
|
output.Stderr(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("invalid when the state store provider doesn't implement any stores", func(t *testing.T) {
|
|
|
|
|
fixturePath := "invalid-state-store-configuration/unknown-store-type" // ok to reuse; see mock provider for test setup
|
|
|
|
|
view, done := testView(t)
|
|
|
|
|
mock := mockPluggableStateStorageProvider()
|
|
|
|
|
mock.GetProviderSchemaResponse.StateStores = map[string]providers.Schema{} // override to have no state stores
|
|
|
|
|
c := &ValidateCommand{
|
|
|
|
|
Meta: Meta{
|
|
|
|
|
testingOverrides: metaOverridesForProvider(mock),
|
|
|
|
|
View: view,
|
|
|
|
|
AllowExperimentalFeatures: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
args := []string{"-no-color", testFixturePath(fixturePath)}
|
|
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("unexpected successful exit code %d\n\n%s", code, output.Stdout())
|
|
|
|
|
}
|
|
|
|
|
expectedErr := "Error: Provider does not support pluggable state storage"
|
|
|
|
|
if !strings.Contains(output.Stderr(), expectedErr) {
|
|
|
|
|
t.Fatalf("unexpected error content: wanted %q, got: %s",
|
|
|
|
|
expectedErr,
|
|
|
|
|
output.Stderr(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("invalid when there's an unknown attribute present in the states_store block", func(t *testing.T) {
|
|
|
|
|
fixturePath := "invalid-state-store-configuration/unknown-attr-in-store"
|
|
|
|
|
view, done := testView(t)
|
|
|
|
|
mock := mockPluggableStateStorageProvider()
|
|
|
|
|
c := &ValidateCommand{
|
|
|
|
|
Meta: Meta{
|
|
|
|
|
testingOverrides: metaOverridesForProvider(mock),
|
|
|
|
|
View: view,
|
|
|
|
|
AllowExperimentalFeatures: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
args := []string{"-no-color", testFixturePath(fixturePath)}
|
|
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("expected an unsuccessful exit code %d\n\n%s", code, output.Stdout())
|
|
|
|
|
}
|
|
|
|
|
expectedErr := "Error: Unsupported argument"
|
|
|
|
|
if !strings.Contains(output.Stderr(), expectedErr) {
|
|
|
|
|
t.Fatalf("unexpected error content: wanted %q, got: %s",
|
|
|
|
|
expectedErr,
|
|
|
|
|
output.Stderr(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("invalid when there's an unknown attribute present in the states_store's provider block", func(t *testing.T) {
|
|
|
|
|
fixturePath := "invalid-state-store-configuration/unknown-attr-in-provider"
|
|
|
|
|
view, done := testView(t)
|
|
|
|
|
mock := mockPluggableStateStorageProvider()
|
|
|
|
|
c := &ValidateCommand{
|
|
|
|
|
Meta: Meta{
|
|
|
|
|
testingOverrides: metaOverridesForProvider(mock),
|
|
|
|
|
View: view,
|
|
|
|
|
AllowExperimentalFeatures: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
args := []string{"-no-color", testFixturePath(fixturePath)}
|
|
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("expected an unsuccessful exit code %d\n\n%s", code, output.Stdout())
|
|
|
|
|
}
|
|
|
|
|
expectedErr := "Error: Unsupported argument"
|
|
|
|
|
if !strings.Contains(output.Stderr(), expectedErr) {
|
|
|
|
|
t.Fatalf("unexpected error content: wanted %q, got: %s",
|
|
|
|
|
expectedErr,
|
|
|
|
|
output.Stderr(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("invalid when a required attribute in state_store block is unset", func(t *testing.T) {
|
|
|
|
|
fixturePath := "invalid-state-store-configuration/missing-required-attr-in-store"
|
|
|
|
|
view, done := testView(t)
|
|
|
|
|
mock := mockPluggableStateStorageProvider()
|
|
|
|
|
c := &ValidateCommand{
|
|
|
|
|
Meta: Meta{
|
|
|
|
|
testingOverrides: metaOverridesForProvider(mock),
|
|
|
|
|
View: view,
|
|
|
|
|
AllowExperimentalFeatures: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
args := []string{"-no-color", testFixturePath(fixturePath)}
|
|
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("expected an unsuccessful exit code %d\n\n%s", code, output.Stdout())
|
|
|
|
|
}
|
|
|
|
|
expectedErr := "Error: Missing required argument"
|
|
|
|
|
if !strings.Contains(output.Stderr(), expectedErr) {
|
|
|
|
|
t.Fatalf("unexpected error content: wanted %q, got: %s",
|
|
|
|
|
expectedErr,
|
|
|
|
|
output.Stderr(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("invalid when a required attribute in state_store's provider block is unset", func(t *testing.T) {
|
|
|
|
|
fixturePath := "invalid-state-store-configuration/missing-required-attr-in-provider"
|
|
|
|
|
view, done := testView(t)
|
|
|
|
|
mock := mockPluggableStateStorageProvider()
|
|
|
|
|
// Make the provider's schema require an attribute that isn't set in the test fixture
|
|
|
|
|
mock.GetProviderSchemaResponse.Provider.Body = &configschema.Block{
|
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
|
"required_attr": {Type: cty.String, Required: true},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c := &ValidateCommand{
|
|
|
|
|
Meta: Meta{
|
|
|
|
|
testingOverrides: metaOverridesForProvider(mock),
|
|
|
|
|
View: view,
|
|
|
|
|
AllowExperimentalFeatures: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
args := []string{"-no-color", testFixturePath(fixturePath)}
|
|
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("expected an unsuccessful exit code %d\n\n%s", code, output.Stdout())
|
|
|
|
|
}
|
|
|
|
|
expectedErr := "Error: Missing required argument"
|
|
|
|
|
if !strings.Contains(output.Stderr(), expectedErr) {
|
|
|
|
|
t.Fatalf("unexpected error content: wanted %q, got: %s",
|
|
|
|
|
expectedErr,
|
|
|
|
|
output.Stderr(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("invalid when the state_store's provider's ValidateProviderConfig method returns an error", func(t *testing.T) {
|
|
|
|
|
fixturePath := "invalid-state-store-configuration/valid-config" // mock provider creates the errors
|
|
|
|
|
view, done := testView(t)
|
|
|
|
|
mock := mockPluggableStateStorageProvider()
|
|
|
|
|
// Make the provider respond as if there's a problem with the provider config.
|
|
|
|
|
mock.ValidateProviderConfigResponse = &providers.ValidateProviderConfigResponse{
|
|
|
|
|
Diagnostics: tfdiags.Diagnostics{}.Append(tfdiags.Sourceless(
|
|
|
|
|
tfdiags.Error,
|
|
|
|
|
"Error from test",
|
|
|
|
|
"This test is forcing an error to be returned from the provider's ValidateProviderConfig method.",
|
|
|
|
|
)),
|
|
|
|
|
}
|
|
|
|
|
c := &ValidateCommand{
|
|
|
|
|
Meta: Meta{
|
|
|
|
|
testingOverrides: metaOverridesForProvider(mock),
|
|
|
|
|
View: view,
|
|
|
|
|
AllowExperimentalFeatures: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
args := []string{"-no-color", testFixturePath(fixturePath)}
|
|
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("expected an unsuccessful exit code %d\n\n%s", code, output.Stdout())
|
|
|
|
|
}
|
|
|
|
|
expectedErr := "Error from test"
|
|
|
|
|
if !strings.Contains(output.Stderr(), expectedErr) {
|
|
|
|
|
t.Fatalf("unexpected error content: wanted %q, got: %s",
|
|
|
|
|
expectedErr,
|
|
|
|
|
output.Stderr(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("invalid when the state_store's provider's ValidateStateStoreConfig method returns an error", func(t *testing.T) {
|
|
|
|
|
fixturePath := "invalid-state-store-configuration/valid-config" // mock provider creates the errors
|
|
|
|
|
view, done := testView(t)
|
|
|
|
|
mock := mockPluggableStateStorageProvider()
|
|
|
|
|
// Make the provider respond as if there's a problem with the state store config.
|
|
|
|
|
// The provider's ValidateStateStoreConfig method is called inside the PrepareConfig method of a Pluggable.
|
|
|
|
|
mock.ValidateStateStoreConfigResponse = &providers.ValidateStateStoreConfigResponse{
|
|
|
|
|
Diagnostics: tfdiags.Diagnostics{}.Append(tfdiags.Sourceless(
|
|
|
|
|
tfdiags.Error,
|
|
|
|
|
"Error from test",
|
|
|
|
|
"This test is forcing an error to be returned from the provider's ValidateStateStoreConfig method.",
|
|
|
|
|
)),
|
|
|
|
|
}
|
|
|
|
|
// Make mock happy.
|
|
|
|
|
// This check -that a provider is configured before state store config is validated- is specific to the mock
|
|
|
|
|
// and isn't performed by user-facing code in Terraform. The mock's behaviour is to highlight abnormal situations/
|
|
|
|
|
// breaking of assumptions.
|
|
|
|
|
// Here we know validate doesn't configure the provider before using validation methods, so we can set this flag
|
|
|
|
|
// to bypass.
|
|
|
|
|
mock.ConfigureProviderCalled = true
|
|
|
|
|
c := &ValidateCommand{
|
|
|
|
|
Meta: Meta{
|
|
|
|
|
testingOverrides: metaOverridesForProvider(mock),
|
|
|
|
|
View: view,
|
|
|
|
|
AllowExperimentalFeatures: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
args := []string{"-no-color", testFixturePath(fixturePath)}
|
|
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("expected an unsuccessful exit code %d\n\n%s", code, output.Stdout())
|
|
|
|
|
}
|
|
|
|
|
expectedErr := "Error from test"
|
|
|
|
|
if !strings.Contains(output.Stderr(), expectedErr) {
|
|
|
|
|
t.Fatalf("unexpected error content: wanted %q, got: %s",
|
|
|
|
|
expectedErr,
|
|
|
|
|
output.Stderr(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-05 13:13:58 -05:00
|
|
|
// Resources are validated using their schemas, so unknown or missing required attributes are identified.
|
|
|
|
|
func TestValidate_resourceBlock(t *testing.T) {
|
|
|
|
|
t.Run("invalid when block contains a repeated attribute", func(t *testing.T) {
|
|
|
|
|
output, code := setupTest(t, "invalid-resource-configuration/repeated-attr")
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("unexpected successful exit code %d\n\n%s", code, output.Stdout())
|
|
|
|
|
}
|
|
|
|
|
expectedErr := "Error: Attribute redefined"
|
|
|
|
|
if !strings.Contains(output.Stderr(), expectedErr) {
|
|
|
|
|
t.Fatalf("unexpected error content: wanted %q, got: %s",
|
|
|
|
|
expectedErr,
|
|
|
|
|
output.Stderr(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("invalid when there's an unknown attribute present", func(t *testing.T) {
|
|
|
|
|
output, code := setupTest(t, "invalid-resource-configuration/unknown-attr")
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("unexpected successful exit code %d\n\n%s", code, output.Stdout())
|
|
|
|
|
}
|
|
|
|
|
expectedErr := "Error: Unsupported argument"
|
|
|
|
|
if !strings.Contains(output.Stderr(), expectedErr) {
|
|
|
|
|
t.Fatalf("unexpected error content: wanted %q, got: %s",
|
|
|
|
|
expectedErr,
|
|
|
|
|
output.Stderr(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("invalid when a required attribute is unset", func(t *testing.T) {
|
|
|
|
|
view, done := testView(t)
|
|
|
|
|
p := testProvider()
|
|
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
|
|
|
|
ResourceTypes: map[string]providers.Schema{
|
|
|
|
|
"test_instance": {
|
|
|
|
|
Body: &configschema.Block{
|
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
|
"ami": {Type: cty.String, Required: true},
|
|
|
|
|
},
|
|
|
|
|
BlockTypes: map[string]*configschema.NestedBlock{
|
|
|
|
|
"network_interface": {
|
|
|
|
|
Nesting: configschema.NestingList,
|
|
|
|
|
Block: configschema.Block{
|
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
|
"device_index": {Type: cty.String, Optional: true},
|
|
|
|
|
"description": {Type: cty.String, Optional: true},
|
|
|
|
|
"name": {Type: cty.String, Optional: true},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c := &ValidateCommand{
|
|
|
|
|
Meta: Meta{
|
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
|
View: view,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args := []string{"-no-color", testFixturePath("invalid-resource-configuration/missing-required-attr")}
|
|
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("expected non-successful exit code %d\n\n%s", code, output.Stdout())
|
|
|
|
|
}
|
|
|
|
|
expectedErr := "Error: Missing required argument"
|
|
|
|
|
if !strings.Contains(output.Stderr(), expectedErr) {
|
|
|
|
|
t.Fatalf("unexpected error content: wanted %q, got: %s",
|
|
|
|
|
expectedErr,
|
|
|
|
|
output.Stderr(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|