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

feat: Add fuzz test for FirestoreDatabase #3340

Merged

Conversation

jasonvigil
Copy link
Collaborator

@jasonvigil jasonvigil commented Dec 9, 2024

Seems there were quite a few fields added to FirestoreDatabase since it was originally implemented.

Copy link
Collaborator

@yuwenma yuwenma left a comment

Choose a reason for hiding this comment

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

/approve

cc @jingyih for another pass

@@ -141,6 +141,9 @@ func (f *FuzzTest[ProtoT, KRMType]) Fuzz(t *testing.T, seed int64) {
t.Fatalf("error mapping from krm to proto: %v", ctx.Err())
}

// Remove any output only or known-unimplemented fields
fuzz.Visit("", p2.ProtoReflect(), nil, clearFields)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure why we need to clear fields again. cc @acpana

Copy link
Collaborator Author

@jasonvigil jasonvigil Dec 11, 2024

Choose a reason for hiding this comment

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

This controller takes an opinion on the values that should be set for type, app_engine_integration_mode, and deletion_protection_state. Values for those fields are set in the ToProto method, regardless of what is passed in. Therefore, the test fails if the "unimplemented fields" are not erased in the result object.

Copy link
Collaborator

@jingyih jingyih Dec 12, 2024

Choose a reason for hiding this comment

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

This behavior seems resource-specific, but is currently applied library-wide, affecting all resources. Ideally we want to scope it down to individual resource fuzz tests. A potential solution is to add something like a defaultFields to the KRMTypedFuzzer struct. The defaultFields can be set at resource level, and be cleared in library after p2 is calculated. It also helps distinguish between unimplemented fields vs. fields that will be defaulted by the controller.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just to clarify, those fields are not settable in the KRM resource. They are given hardcoded, default values in order to make the resource declarative. Not sure if this is a common scenario. Though, is there any reason we would not want this behavior (of clearing unimplemented fields) for all resources?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Though, is there any reason we would not want this behavior (of clearing unimplemented fields) for all resources?

My understanding is that it defeats the purpose of roundtrip. For example,

  • The KRM -> proto conversion is incorrectly adding data to ignored / unimplemented field.
  • or, the proto -> KRM conversion is converting data to the wrong field that then gets mapped back to ignored field (I am thinking about in a spec fuzz test where the mapper functions were incorrectly converting proto field to a status field).

Although this is likely a rare edge case, we should ideally catch it when it happens.

If a proper fix is not straightforward, we can merge this PR as is and add a TODO for later, to unblock other PRs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

My understanding is that it defeats the purpose of roundtrip.

Got it, makes sense. Thanks for the examples. I will try to figure out a better solution for this.

Copy link
Collaborator Author

@jasonvigil jasonvigil Dec 26, 2024

Choose a reason for hiding this comment

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

Ok, I have fixed this @jingyih. I moved the defaulting behavior out of the mapper and into a separate function, and marked all the "default value" fields as unimplemented (in the fuzz test). This organization is probably better for maintainability / readability anyway, and makes it so the defaulting behavior does not affect the fuzz test.

@jasonvigil jasonvigil force-pushed the firestoredatabase-fuzz branch from deef648 to fcd31c5 Compare December 26, 2024 23:53
@jasonvigil jasonvigil requested a review from jingyih December 26, 2024 23:57
@jasonvigil jasonvigil marked this pull request as ready for review December 26, 2024 23:57
@jasonvigil jasonvigil requested a review from yuwenma December 27, 2024 22:26
Copy link
Collaborator

@yuwenma yuwenma left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

@google-oss-prow google-oss-prow bot added the lgtm label Dec 30, 2024
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: yuwenma

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@google-oss-prow google-oss-prow bot merged commit 763fe18 into GoogleCloudPlatform:master Dec 30, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants