Skip to content

Commit 4334eda

Browse files
query: propagate graph node removal to descendants (#37664) (#37673)
Co-authored-by: Samsondeen <40821565+dsa0x@users.noreply.github.com>
1 parent 1f56b48 commit 4334eda

11 files changed

+432
-259
lines changed

internal/backend/local/backend_local.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ func (b *Local) localRun(op *backendrun.Operation) (*backendrun.LocalRun, *confi
132132
// If validation is enabled, validate
133133
if b.OpValidation {
134134
log.Printf("[TRACE] backend/local: running validation operation")
135-
validateDiags := ret.Core.Validate(ret.Config, nil)
135+
// TODO: Implement query validate command. op.Query is false when running the command "terraform validate"
136+
opts := &terraform.ValidateOpts{Query: op.Query}
137+
validateDiags := ret.Core.Validate(ret.Config, opts)
136138
diags = diags.Append(validateDiags)
137139
}
138140
}

internal/terraform/context_apply2_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4271,3 +4271,78 @@ resource "test_resource" "foo" {
42714271
t.Fatalf("missing identity in state, got %q", fooState.Current.IdentityJSON)
42724272
}
42734273
}
4274+
4275+
func TestContext2Apply_noListValidated(t *testing.T) {
4276+
tests := map[string]struct {
4277+
name string
4278+
mainConfig string
4279+
queryConfig string
4280+
query bool
4281+
}{
4282+
"query files not validated in default validate mode": {
4283+
mainConfig: `
4284+
terraform {
4285+
required_providers {
4286+
test = {
4287+
source = "hashicorp/test"
4288+
version = "1.0.0"
4289+
}
4290+
}
4291+
}
4292+
`,
4293+
queryConfig: `
4294+
// This config is invalid, but should not be validated in default validate mode
4295+
list "test_resource" "test" {
4296+
provider = test
4297+
4298+
config {
4299+
filter = {
4300+
attr = list.non_existent.attr
4301+
}
4302+
}
4303+
}
4304+
4305+
locals {
4306+
test = list.non_existent.attr
4307+
}
4308+
`,
4309+
query: false,
4310+
},
4311+
}
4312+
4313+
for name, tc := range tests {
4314+
t.Run(name, func(t *testing.T) {
4315+
configFiles := map[string]string{"main.tf": tc.mainConfig}
4316+
if tc.queryConfig != "" {
4317+
configFiles["main.tfquery.hcl"] = tc.queryConfig
4318+
}
4319+
4320+
opts := []configs.Option{}
4321+
if tc.query {
4322+
opts = append(opts, configs.MatchQueryFiles())
4323+
}
4324+
4325+
m := testModuleInline(t, configFiles, opts...)
4326+
4327+
p := testProvider("test")
4328+
p.GetProviderSchemaResponse = getListProviderSchemaResp()
4329+
4330+
ctx := testContext2(t, &ContextOpts{
4331+
Providers: map[addrs.Provider]providers.Factory{
4332+
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
4333+
},
4334+
})
4335+
4336+
diags := ctx.Validate(m, &ValidateOpts{
4337+
Query: tc.query,
4338+
})
4339+
tfdiags.AssertNoErrors(t, diags)
4340+
4341+
plan, diags := ctx.Plan(m, states.NewState(), SimplePlanOpts(plans.NormalMode, testInputValuesUnset(m.Module.Variables)))
4342+
tfdiags.AssertNoErrors(t, diags)
4343+
4344+
_, diags = ctx.Apply(plan, m, nil)
4345+
tfdiags.AssertNoErrors(t, diags)
4346+
})
4347+
}
4348+
}

internal/terraform/context_plan_actions_test.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,11 @@ list "test_resource" "test1" {
176176
},
177177
},
178178
"query run, action references resource": {
179-
toBeImplemented: true, // TODO: Fix the graph built by query operations.
180179
module: map[string]string{
181180
"main.tf": `
182181
action "test_action" "hello" {
183182
config {
184-
attr = resource.test_object.a
183+
attr = resource.test_object.a.name
185184
}
186185
}
187186
resource "test_object" "a" {
@@ -3313,7 +3312,17 @@ resource "test_object" "a" {
33133312
t.Skip("Test not implemented yet")
33143313
}
33153314

3316-
m := testModuleInline(t, tc.module)
3315+
opts := SimplePlanOpts(plans.NormalMode, InputValues{})
3316+
if tc.planOpts != nil {
3317+
opts = tc.planOpts
3318+
}
3319+
3320+
configOpts := []configs.Option{}
3321+
if opts.Query {
3322+
configOpts = append(configOpts, configs.MatchQueryFiles())
3323+
}
3324+
3325+
m := testModuleInline(t, tc.module, configOpts...)
33173326

33183327
p := &testing_provider.MockProvider{
33193328
GetProviderSchemaResponse: &providers.GetProviderSchemaResponse{
@@ -3444,7 +3453,9 @@ resource "test_object" "a" {
34443453
},
34453454
})
34463455

3447-
diags := ctx.Validate(m, &ValidateOpts{})
3456+
diags := ctx.Validate(m, &ValidateOpts{
3457+
Query: opts.Query,
3458+
})
34483459
if tc.expectValidateDiagnostics != nil {
34493460
tfdiags.AssertDiagnosticsMatch(t, diags, tc.expectValidateDiagnostics(m))
34503461
} else if tc.assertValidateDiagnostics != nil {
@@ -3462,11 +3473,6 @@ resource "test_object" "a" {
34623473
prevRunState = states.BuildState(tc.buildState)
34633474
}
34643475

3465-
opts := SimplePlanOpts(plans.NormalMode, InputValues{})
3466-
if tc.planOpts != nil {
3467-
opts = tc.planOpts
3468-
}
3469-
34703476
plan, diags := ctx.Plan(m, prevRunState, opts)
34713477

34723478
if tc.expectPlanDiagnostics != nil {

0 commit comments

Comments
 (0)