Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tfsdk: TestServerPlanResourceChange/attr_plan_modifiers_diags Race Condition #121

Closed
bflad opened this issue Sep 1, 2021 · 1 comment · Fixed by #122
Closed

tfsdk: TestServerPlanResourceChange/attr_plan_modifiers_diags Race Condition #121

bflad opened this issue Sep 1, 2021 · 1 comment · Fixed by #122
Assignees
Labels
bug Something isn't working
Milestone

Comments

@bflad
Copy link
Contributor

bflad commented Sep 1, 2021

Module version

Post #102 merge

Relevant framework source code

func (rt testServeResourceTypeAttributePlanModifiers) GetSchema(_ context.Context) (Schema, diag.Diagnostics) {
	return Schema{
		Version: 1,
		Attributes: map[string]Attribute{
			"name": {
				Required: true,
				Type:     types.StringType,
				// For the purposes of testing, these plan modifiers behave
				// differently for certain values of the attribute.
				// By default, they do nothing.
				PlanModifiers: []AttributePlanModifier{
					testWarningDiagModifier{},
					testErrorDiagModifier{},
					testAttrPlanValueModifierOne{},
					testAttrPlanValueModifierTwo{},
				},
			},
			// ... other Attribute ...
		},
	}, nil
}
func (t testWarningDiagModifier) Modify(ctx context.Context, req ModifyAttributePlanRequest, resp *ModifyAttributePlanResponse) {
	attrVal, ok := req.AttributePlan.(types.String)
	if !ok {
		return
	}

	if attrVal.Value == "TESTDIAG" {
		resp.Diagnostics.AddWarning(
			"Warning diag",
			"This is a warning",
		)
	}
}

func (t testErrorDiagModifier) Modify(ctx context.Context, req ModifyAttributePlanRequest, resp *ModifyAttributePlanResponse) {
	attrVal, ok := req.AttributePlan.(types.String)
	if !ok {
		return
	}

	if attrVal.Value == "TESTDIAG" {
		resp.Diagnostics.AddError(
			"Error diag",
			"This is an error",
		)
	}
}
"attr_plan_modifiers_diags": {
			priorState: tftypes.NewValue(testServeResourceTypeAttributePlanModifiersType, map[string]tftypes.Value{
				"name": tftypes.NewValue(tftypes.String, "TESTDIAG"),
				"size": tftypes.NewValue(tftypes.Number, 3),
				"scratch_disk": tftypes.NewValue(tftypes.Object{AttributeTypes: map[string]tftypes.Type{
					"id":        tftypes.String,
					"interface": tftypes.String,
					"filesystem": tftypes.Object{AttributeTypes: map[string]tftypes.Type{
						"size":   tftypes.Number,
						"format": tftypes.String,
					}},
				}}, map[string]tftypes.Value{
					"id":        tftypes.NewValue(tftypes.String, "my-scr-disk"),
					"interface": tftypes.NewValue(tftypes.String, "scsi"),
					"filesystem": tftypes.NewValue(tftypes.Object{AttributeTypes: map[string]tftypes.Type{
						"size":   tftypes.Number,
						"format": tftypes.String,
					}}, nil),
				}),
				"region": tftypes.NewValue(tftypes.String, "region1"),
			}),
			proposedNewState: tftypes.NewValue(testServeResourceTypeAttributePlanModifiersType, map[string]tftypes.Value{
				"name": tftypes.NewValue(tftypes.String, "TESTDIAG"),
				"size": tftypes.NewValue(tftypes.Number, 3),
				"scratch_disk": tftypes.NewValue(tftypes.Object{AttributeTypes: map[string]tftypes.Type{
					"id":        tftypes.String,
					"interface": tftypes.String,
					"filesystem": tftypes.Object{AttributeTypes: map[string]tftypes.Type{
						"size":   tftypes.Number,
						"format": tftypes.String,
					}},
				}}, map[string]tftypes.Value{
					"id":        tftypes.NewValue(tftypes.String, "my-scr-disk"),
					"interface": tftypes.NewValue(tftypes.String, "scsi"),
					"filesystem": tftypes.NewValue(tftypes.Object{AttributeTypes: map[string]tftypes.Type{
						"size":   tftypes.Number,
						"format": tftypes.String,
					}}, nil),
				}),
				"region": tftypes.NewValue(tftypes.String, "region1"),
			}),
			config: tftypes.NewValue(testServeResourceTypeAttributePlanModifiersType, map[string]tftypes.Value{
				"name": tftypes.NewValue(tftypes.String, "TESTDIAG"),
				"size": tftypes.NewValue(tftypes.Number, 3),
				"scratch_disk": tftypes.NewValue(tftypes.Object{AttributeTypes: map[string]tftypes.Type{
					"id":        tftypes.String,
					"interface": tftypes.String,
					"filesystem": tftypes.Object{AttributeTypes: map[string]tftypes.Type{
						"size":   tftypes.Number,
						"format": tftypes.String,
					}},
				}}, map[string]tftypes.Value{
					"id":        tftypes.NewValue(tftypes.String, "my-scr-disk"),
					"interface": tftypes.NewValue(tftypes.String, "scsi"),
					"filesystem": tftypes.NewValue(tftypes.Object{AttributeTypes: map[string]tftypes.Type{
						"size":   tftypes.Number,
						"format": tftypes.String,
					}}, nil),
				}),
				"region": tftypes.NewValue(tftypes.String, "region1"),
			}),
			expectedPlannedState: tftypes.NewValue(testServeResourceTypeAttributePlanModifiersType, map[string]tftypes.Value{
				"name": tftypes.NewValue(tftypes.String, "TESTDIAG"),
				"size": tftypes.NewValue(tftypes.Number, 3),
				"scratch_disk": tftypes.NewValue(tftypes.Object{AttributeTypes: map[string]tftypes.Type{
					"id":        tftypes.String,
					"interface": tftypes.String,
					"filesystem": tftypes.Object{AttributeTypes: map[string]tftypes.Type{
						"size":   tftypes.Number,
						"format": tftypes.String,
					}},
				}}, map[string]tftypes.Value{
					"id":        tftypes.NewValue(tftypes.String, "my-scr-disk"),
					"interface": tftypes.NewValue(tftypes.String, "scsi"),
					"filesystem": tftypes.NewValue(tftypes.Object{AttributeTypes: map[string]tftypes.Type{
						"size":   tftypes.Number,
						"format": tftypes.String,
					}}, nil),
				}),
				"region": tftypes.NewValue(tftypes.String, "region1"),
			}),
			resource:     "test_attribute_plan_modifiers",
			resourceType: testServeResourceTypeAttributePlanModifiersType,
			expectedDiags: []*tfprotov6.Diagnostic{
				{
					Severity: tfprotov6.DiagnosticSeverityWarning,
					Summary:  "Warning diag",
					Detail:   "This is a warning",
				},
				{
					Severity: tfprotov6.DiagnosticSeverityError,
					Summary:  "Error diag",
					Detail:   "This is an error",
				},
			},
			expectedRequiresReplace: []*tftypes.AttributePath{tftypes.NewAttributePath().WithAttributeName("scratch_disk").WithAttributeName("interface")},
		},

Expected Behavior

Consistent unit test results.

Actual Behavior

Race condition based on non-deterministic map access. If name is accessed early (and therefore returns its error diagnostic):

    --- FAIL: TestServerPlanResourceChange/attr_plan_modifiers_diags (0.00s)
        serve_test.go:2664: Unexpected diff in requires replace (+wanted, -got):   []*tftypes.AttributePath(
            -   nil,
            +   {s`AttributeName("scratch_disk").AttributeName("interface")`},
              )

Steps to Reproduce

  1. go test -count=10 -race -run='TestServerPlanResourceChange/attr_plan_modifiers_diags' ./tfsdk

References

@bflad bflad added the bug Something isn't working label Sep 1, 2021
@bflad bflad self-assigned this Sep 1, 2021
@bflad bflad closed this as completed in #122 Sep 1, 2021
@bflad bflad added this to the v0.3.0 milestone Sep 1, 2021
bflad added a commit that referenced this issue Sep 1, 2021
Reference: #121

This is to prevent situations where warning diagnostics in other attributes are skipped based on when an error diagnostic might be non-deterministically found.
bflad added a commit that referenced this issue Sep 7, 2021
…123)

Reference: #121

This is to prevent situations where warning diagnostics in other attributes are skipped based on when an error diagnostic might be non-deterministically found.
@github-actions
Copy link

github-actions bot commented Oct 2, 2021

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
1 participant