Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/backend/local/backend_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ func (b *Local) localRun(op *backendrun.Operation) (*backendrun.LocalRun, *confi
// If validation is enabled, validate
if b.OpValidation {
log.Printf("[TRACE] backend/local: running validation operation")
validateDiags := ret.Core.Validate(ret.Config, nil)
// TODO: Implement query validate command. op.Query is false when running the command "terraform validate"
opts := &terraform.ValidateOpts{Query: op.Query}
validateDiags := ret.Core.Validate(ret.Config, opts)
diags = diags.Append(validateDiags)
}
}
Expand Down
75 changes: 75 additions & 0 deletions internal/terraform/context_apply2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4271,3 +4271,78 @@ resource "test_resource" "foo" {
t.Fatalf("missing identity in state, got %q", fooState.Current.IdentityJSON)
}
}

func TestContext2Apply_noListValidated(t *testing.T) {
tests := map[string]struct {
name string
mainConfig string
queryConfig string
query bool
}{
"query files not validated in default validate mode": {
mainConfig: `
terraform {
required_providers {
test = {
source = "hashicorp/test"
version = "1.0.0"
}
}
}
`,
queryConfig: `
// This config is invalid, but should not be validated in default validate mode
list "test_resource" "test" {
provider = test

config {
filter = {
attr = list.non_existent.attr
}
}
}

locals {
test = list.non_existent.attr
}
`,
query: false,
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
configFiles := map[string]string{"main.tf": tc.mainConfig}
if tc.queryConfig != "" {
configFiles["main.tfquery.hcl"] = tc.queryConfig
}

opts := []configs.Option{}
if tc.query {
opts = append(opts, configs.MatchQueryFiles())
}

m := testModuleInline(t, configFiles, opts...)

p := testProvider("test")
p.GetProviderSchemaResponse = getListProviderSchemaResp()

ctx := testContext2(t, &ContextOpts{
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
})

diags := ctx.Validate(m, &ValidateOpts{
Query: tc.query,
})
tfdiags.AssertNoErrors(t, diags)

plan, diags := ctx.Plan(m, states.NewState(), SimplePlanOpts(plans.NormalMode, testInputValuesUnset(m.Module.Variables)))
tfdiags.AssertNoErrors(t, diags)

_, diags = ctx.Apply(plan, m, nil)
tfdiags.AssertNoErrors(t, diags)
})
}
}
24 changes: 15 additions & 9 deletions internal/terraform/context_plan_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,11 @@ list "test_resource" "test1" {
},
},
"query run, action references resource": {
toBeImplemented: true, // TODO: Fix the graph built by query operations.
module: map[string]string{
"main.tf": `
action "test_action" "hello" {
config {
attr = resource.test_object.a
attr = resource.test_object.a.name
}
}
resource "test_object" "a" {
Expand Down Expand Up @@ -3313,7 +3312,17 @@ resource "test_object" "a" {
t.Skip("Test not implemented yet")
}

m := testModuleInline(t, tc.module)
opts := SimplePlanOpts(plans.NormalMode, InputValues{})
if tc.planOpts != nil {
opts = tc.planOpts
}

configOpts := []configs.Option{}
if opts.Query {
configOpts = append(configOpts, configs.MatchQueryFiles())
}

m := testModuleInline(t, tc.module, configOpts...)

p := &testing_provider.MockProvider{
GetProviderSchemaResponse: &providers.GetProviderSchemaResponse{
Expand Down Expand Up @@ -3444,7 +3453,9 @@ resource "test_object" "a" {
},
})

diags := ctx.Validate(m, &ValidateOpts{})
diags := ctx.Validate(m, &ValidateOpts{
Query: opts.Query,
})
if tc.expectValidateDiagnostics != nil {
tfdiags.AssertDiagnosticsMatch(t, diags, tc.expectValidateDiagnostics(m))
} else if tc.assertValidateDiagnostics != nil {
Expand All @@ -3462,11 +3473,6 @@ resource "test_object" "a" {
prevRunState = states.BuildState(tc.buildState)
}

opts := SimplePlanOpts(plans.NormalMode, InputValues{})
if tc.planOpts != nil {
opts = tc.planOpts
}

plan, diags := ctx.Plan(m, prevRunState, opts)

if tc.expectPlanDiagnostics != nil {
Expand Down
Loading