Verify WantDeleteCollections in TableTest - #3380
Conversation
Fixes knative#2455 Motivation: `TableRow.WantDeleteCollections` has existed since it was added to the struct, but `(r *TableRow) Test()` never compared it against the actual recorded DeleteCollection actions. A test author setting `WantDeleteCollections` gets no verification at all: whether the reconciler under test issues the expected DeleteCollection call, issues the wrong one, or issues none, the test silently passes either way. A maintainer comment on the issue speculated this couldn't be fixed because of an upstream fake-client limitation (kubernetes/kubernetes#105357), where DeleteCollection calls with a label selector don't actually remove matching objects from the fake ObjectTracker. That bug is real, but it's orthogonal to this issue: `reconciler/testing/actions.go`'s `ActionsByVerb()` already captures `delete-collection` verb actions into `Actions.DeleteCollections` from the fake clientset's recorded action list, independent of whether the tracker actually performed the deletion. So the missing verification in `Test()` is a separate, fixable gap in the test framework itself, not blocked by the upstream issue. Approach: Add a verification block in `Test()`, alongside the existing `WantDeletes` check, that compares `r.WantDeleteCollections` against `actions.DeleteCollections`. `DeleteCollectionActionImpl` has no `GetName()` (it targets a namespace + label/field selector, not a single named object), so it can't reuse the `WantDeletes` comparison verbatim. Instead, a new `deleteCollectionKey` helper builds a comparison key from resource + label selector string + field selector string (+ namespace, unless `SkipNamespaceValidation` is set), and the two sets of keys are compared the same way `WantDeletes`/`gotDeletes` are. Nil label/field selectors (as would result from a hand-written `DeleteCollectionActionImpl{}` literal in a test's `WantDeleteCollections`) are treated as `labels.Everything()` / `fields.Everything()`, matching what client-go's own `NewDeleteCollectionActionWithOptions` defaults them to internally, so constructing a "delete everything" expectation doesn't panic on a nil selector's `.String()`. This does not change behavior for any existing test: a repo-wide search found no existing use of `WantDeleteCollections` anywhere in knative/pkg, since it was never actually verified before. It only turns a previously-silent no-op field into a working one, so tests written against it going forward actually enforce their expectations. Validation: go build ./... go test ./reconciler/... -count=1 go test ./webhook/psbinding/... -count=1 both pass. Added `reconciler/testing/table_test.go` with `TestDeleteCollectionKey`, unit-testing the new helper directly: constructor-built vs. hand-written-literal actions key identically, nil-selector defaults match an explicit "everything" selector, and namespace affects the key unless `SkipNamespaceValidation` is set. /kind bug Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
|
Welcome @pujitha24! It looks like this is your first PR to knative/pkg 🎉 |
|
Hi @pujitha24. Thanks for your PR. I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with Regular contributors should join the org to skip this step. Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: pujitha24 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Fixes #2455
Motivation:
TableRow.WantDeleteCollectionshas existed since it was added to thestruct, but
(r *TableRow) Test()never compared it against the actualrecorded DeleteCollection actions. A test author setting
WantDeleteCollectionsgets no verification at all: whether thereconciler under test issues the expected DeleteCollection call, issues
the wrong one, or issues none, the test silently passes either way.
A maintainer comment on the issue speculated this couldn't be fixed
because of an upstream fake-client limitation
(kubernetes/kubernetes#105357), where DeleteCollection calls with a
label selector don't actually remove matching objects from the fake
ObjectTracker. That bug is real, but it's orthogonal to this issue:
reconciler/testing/actions.go'sActionsByVerb()already capturesdelete-collectionverb actions intoActions.DeleteCollectionsfromthe fake clientset's recorded action list, independent of whether the
tracker actually performed the deletion. So the missing verification in
Test()is a separate, fixable gap in the test framework itself, notblocked by the upstream issue.
Approach:
Add a verification block in
Test(), alongside the existingWantDeletescheck, that comparesr.WantDeleteCollectionsagainstactions.DeleteCollections.DeleteCollectionActionImplhas noGetName()(it targets a namespace + label/field selector, not a singlenamed object), so it can't reuse the
WantDeletescomparison verbatim.Instead, a new
deleteCollectionKeyhelper builds a comparison key fromresource + label selector string + field selector string (+ namespace,
unless
SkipNamespaceValidationis set), and the two sets of keys arecompared the same way
WantDeletes/gotDeletesare. Nil label/fieldselectors (as would result from a hand-written
DeleteCollectionActionImpl{}literal in a test'sWantDeleteCollections) are treated aslabels.Everything()/fields.Everything(), matching what client-go's ownNewDeleteCollectionActionWithOptionsdefaults them to internally, soconstructing a "delete everything" expectation doesn't panic on a nil
selector's
.String().This does not change behavior for any existing test: a repo-wide search
found no existing use of
WantDeleteCollectionsanywhere inknative/pkg, since it was never actually verified before. It only turns
a previously-silent no-op field into a working one, so tests written
against it going forward actually enforce their expectations.
Validation:
go build ./...
go test ./reconciler/... -count=1
go test ./webhook/psbinding/... -count=1
both pass. Added
reconciler/testing/table_test.gowithTestDeleteCollectionKey, unit-testing the new helper directly:constructor-built vs. hand-written-literal actions key identically,
nil-selector defaults match an explicit "everything" selector, and
namespace affects the key unless
SkipNamespaceValidationis set./kind bug
Signed-off-by: Pujitha Paladugu 10557236+pujitha24@users.noreply.github.com