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

Ensure all sdkv2 test providers have an id and an update method #2723

Merged
merged 1 commit into from
Dec 13, 2024

Conversation

VenelinMartinov
Copy link
Contributor

@VenelinMartinov VenelinMartinov commented Dec 11, 2024

This change tweaks the EnsureProviderIsValid method in pulcheck to always give resources an id property and ensure they pass the TF validation with respect to Update methods.

Previously we'd run into trouble with TF validation when a resource needs an Update and doesn't have one or it doesn't need an Update and has one. The rules are non-trivial so I've opted to just make all resources need an Update and ensure they have one.

Copy link

codecov bot commented Dec 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 69.64%. Comparing base (5b45441) to head (0f1206a).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2723      +/-   ##
==========================================
- Coverage   69.64%   69.64%   -0.01%     
==========================================
  Files         301      301              
  Lines       38726    38726              
==========================================
- Hits        26971    26969       -2     
- Misses      10237    10240       +3     
+ Partials     1518     1517       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@iwahbe iwahbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand this change. We believe that resourceNeedsUpdate is wrong, but are still using it?

Where does this cause us problems?

Comment on lines 78 to 102
if resourceNeedsUpdate(r) && r.UpdateContext == nil {
if r.UpdateContext == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to add the if !resourceNeedsUpdate(r) { ... } block if we remove the conditional.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like EnsureProviderValid is actually doing something like populating defaults into the resource schemas. Adding update_prop is really non-intuitive to callers, why would it do that?

What if we just inlined and got rid of EnsureProviderValid and made the resource schemas in every test realistic, with comments e.g. if update_prop is added then why it's added?

@VenelinMartinov
Copy link
Contributor Author

VenelinMartinov commented Dec 13, 2024

Yeah, that's a bit of a weird one. Turns out Terraform is not consistent with its own rules and some valid schemas can not pass validation.

We call InternalValidate on all schemas in pulcheck.EnsureProviderValid. That function also fills in some reasonable defaults. Unfortunately, the TFInternalValidate rejects some valid schemas or if the rules from InternalValidate are enforced the schemas fail at runtime. Consider this:

	"test": {
		Type:     schema.TypeSet,
		Optional: true,
		ForceNew: true,
		Computed: true,
		Elem: &schema.Resource{
			Schema: map[string]*schema.Schema{
				"nested": {
					Type:     schema.TypeString,
					Optional: true,
				},
				"computed": {
					Type:     schema.TypeString,
					Optional: true,
					Computed: true,
				},
			},
		},
	},

This schema has all its top-level properties as ForceNew, so according to InternalValidate its Update is redundant. This is not correct, as a user might only update the nested property which is not ForceNew, and doing that fails during runtime.

This behaviour is very non-intuitive and took me a while to figure out that TF Validation has a bug here. We could say that each person who writes a test is responsible for dealing with this and working around this but that seems harder.

I've instead opted to work around the whole mess by adding an Optional update_prop to all schemas, which makes sure that Update is necessary. Having extra properties in the schema seems like the lesser evil here. I'll leave a note with the explanation.

Base automatically changed from vvm/refactor_sdkv2_set_detailed_diff_tests to master December 13, 2024 14:02
@t0yv0
Copy link
Member

t0yv0 commented Dec 13, 2024

Thanks for the explanation, this makes sense! At the very least this needs to be a comment I think on the "update_prop" - that is it's a no-op prop added to make InternalValidate pass.

@VenelinMartinov VenelinMartinov force-pushed the vvm/sdkv2_pulcheck_id_and_update branch from 2e69e46 to 0f1206a Compare December 13, 2024 16:33
@VenelinMartinov VenelinMartinov enabled auto-merge (squash) December 13, 2024 16:36
@VenelinMartinov VenelinMartinov merged commit 4842bf4 into master Dec 13, 2024
31 checks passed
@VenelinMartinov VenelinMartinov deleted the vvm/sdkv2_pulcheck_id_and_update branch December 13, 2024 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants