test framework: fix bug preventing remaining assertions being checked after one has failed (#33601)

This commit is contained in:
Liam Cervante 2023-08-01 09:56:51 +02:00 committed by GitHub
parent 4560a83721
commit bf6f32c19a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 6 deletions

View File

@ -100,22 +100,25 @@ func (ctx *TestContext) evaluate(state *states.SyncState, changes *plans.Changes
// Now validate all the assertions within this run block.
for _, rule := range run.Config.CheckRules {
var diags tfdiags.Diagnostics
refs, moreDiags := lang.ReferencesInExpr(addrs.ParseRefFromTestingScope, rule.Condition)
run.Diagnostics = run.Diagnostics.Append(moreDiags)
diags = diags.Append(moreDiags)
moreRefs, moreDiags := lang.ReferencesInExpr(addrs.ParseRefFromTestingScope, rule.ErrorMessage)
run.Diagnostics = run.Diagnostics.Append(moreDiags)
diags = diags.Append(moreDiags)
refs = append(refs, moreRefs...)
hclCtx, moreDiags := scope.EvalContext(refs)
run.Diagnostics = run.Diagnostics.Append(moreDiags)
diags = diags.Append(moreDiags)
errorMessage, moreDiags := evalCheckErrorMessage(rule.ErrorMessage, hclCtx)
run.Diagnostics = run.Diagnostics.Append(moreDiags)
diags = diags.Append(moreDiags)
runVal, hclDiags := rule.Condition.Value(hclCtx)
run.Diagnostics = run.Diagnostics.Append(hclDiags)
diags = diags.Append(hclDiags)
if run.Diagnostics.HasErrors() {
run.Diagnostics = run.Diagnostics.Append(diags)
if diags.HasErrors() {
run.Status = run.Status.Merge(moduletest.Error)
continue
}

View File

@ -202,6 +202,73 @@ run "test_case" {
},
},
},
"two_failing_assertions": {
configs: map[string]string{
"main.tf": `
resource "test_resource" "a" {
value = "Hello, world!"
}
`,
"main.tftest.hcl": `
run "test_case" {
assert {
condition = test_resource.a.value == "incorrect!"
error_message = "invalid value"
}
assert {
condition = test_resource.a.value == "also incorrect!"
error_message = "still invalid"
}
}
`,
},
state: states.BuildState(func(state *states.SyncState) {
state.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_resource",
Name: "a",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: encodeCtyValue(t, cty.ObjectVal(map[string]cty.Value{
"value": cty.StringVal("Hello, world!"),
})),
},
addrs.AbsProviderConfig{
Module: addrs.RootModule,
Provider: addrs.NewDefaultProvider("test"),
})
}),
provider: &MockProvider{
GetProviderSchemaResponse: &providers.GetProviderSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_resource": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"value": {
Type: cty.String,
Required: true,
},
},
},
},
},
},
},
expectedStatus: moduletest.Fail,
expectedDiags: []tfdiags.Description{
{
Summary: "Test assertion failed",
Detail: "invalid value",
},
{
Summary: "Test assertion failed",
Detail: "still invalid",
},
},
},
}
for name, tc := range tcs {
t.Run(name, func(t *testing.T) {