#17636·kyverno

[Test]: Add unit tests for pkg/controllers/exceptions package

Author: JituRewarCreated Sep 18, 2026Updated Sep 18, 2026
Labelsbugtriage

Kyverno Version

1.18.0

Description

Summary

The pkg/controllers/exceptions package responsible for managing and indexing PolicyException objects for quick rule-based lookups currently has zero unit tests.

Proposed Changes / Scope

Add comprehensive unit tests in pkg/controllers/exceptions/controller_test.go to ensure correctness and prevent future regressions:

  1. Lookup Logic (Find):

    • Querying an empty index returns nil without errors.
    • Exact hits for matching policy and rule names.
    • Misses when querying non-existent policies or rules.
  2. Event Handlers (addPolex, updatePolex, deletePolex):

    • Enqueueing referenced policy names into the workqueue.
    • Deduplication of policy names within the same exception.
    • Enqueueing the union of policy names during exception updates.
  3. Reconciliation (reconcile):

    • Building and updating the policy-rule index for both ClusterPolicy and namespaced Policy resources.
    • Proper deletion of cache entries when a policy is deleted (NotFound).
    • Namespace scoping: ensuring a controller configured for a specific namespace only indexes exceptions from that namespace.
    • Deterministic ordering of exceptions by (namespace, name).
  4. Concurrency & Thread Safety:

    • Concurrent Find operations across multiple goroutines.
    • Concurrent reconcile (writes) and Find (reads) verified under Go race detector (-race).

Benefit

Improves test coverage for exception controller caching and guarantees thread-safe index mutation and lookup.

Slack discussion

No response

Troubleshooting

  • I have read and followed the documentation AND the troubleshooting guide.
  • I have searched other issues in this repository and mine is not recorded.