Skip to content

refactor: replace magic numbers with named constants in tests#987

Merged
emmaaroche merged 2 commits into
Kuadrant:mainfrom
Priyanshu-u07:test-magic-number-constants
Jul 7, 2026
Merged

refactor: replace magic numbers with named constants in tests#987
emmaaroche merged 2 commits into
Kuadrant:mainfrom
Priyanshu-u07:test-magic-number-constants

Conversation

@Priyanshu-u07

@Priyanshu-u07 Priyanshu-u07 commented May 28, 2026

Copy link
Copy Markdown
Contributor

Description

Replaces all hardcoded magic numbers in test files with named constants from testsuite/utils/constants.py.

Closes #930

Changes

Refactored Files (28)

Files
  • testsuite/tests/singlecluster/gateway/scaling/test_manual_scale_gateway.py
  • testsuite/tests/singlecluster/gateway/scaling/test_auto_scale_gateway.py
  • testsuite/tests/singlecluster/grpc/test_grpcroute.py
  • testsuite/tests/singlecluster/gateway/reconciliation/change_targetref/test_update_tlspolicy_target_ref.py
  • testsuite/tests/singlecluster/gateway/reconciliation/change_targetref/conftest.py
  • testsuite/tests/singlecluster/authorino/identity/x509/gateway_validation/test_multi_ca_trust.py
  • testsuite/tests/singlecluster/identical_hostnames/rlp/test_rlp_on_routes.py
  • testsuite/tests/singlecluster/identical_hostnames/rlp/test_rlp_on_gw_and_route.py
  • testsuite/tests/singlecluster/limitador/test_multiple_iterations.py
  • testsuite/tests/singlecluster/limitador/tokens_rate_limit/basic/test_trlp.py
  • testsuite/tests/singlecluster/limitador/tokens_rate_limit/basic/test_trlp_streaming.py
  • testsuite/tests/singlecluster/limitador/tokens_rate_limit/multiple_iterations/test_multiple_trlp_iterations.py
  • testsuite/tests/singlecluster/limitador/tokens_rate_limit/multiple_iterations/test_multiple_trlp_stream_iterations.py
  • testsuite/tests/singlecluster/egress/test_egress.py
  • testsuite/tests/singlecluster/authorino/authorization/opa/external_registry/test_cache.py
  • testsuite/tests/multicluster/load_balanced/test_change_default_geo.py
  • testsuite/tests/multicluster/load_balanced/test_change_strategy.py
  • testsuite/tests/singlecluster/gateway/dnspolicy/health_check/test_remove_endpoint.py
  • testsuite/tests/singlecluster/gateway/reconciliation/test_gw_doesnt_exist.py
  • testsuite/tests/singlecluster/gateway/reconciliation/test_invalid_issuer_reference.py
  • testsuite/tests/singlecluster/gateway/reconciliation/test_same_target.py
  • testsuite/tests/singlecluster/tracing/control_plane/test_control_plane_errors.py
  • testsuite/tests/singlecluster/tracing/control_plane/test_control_plane_lifecycle.py
  • testsuite/tests/singlecluster/observability/conftest.py
  • testsuite/tests/singlecluster/egress/credentials_injection/conftest.py
  • testsuite/tests/singlecluster/authorino/wristband/conftest.py
  • testsuite/tests/singlecluster/gateway/dnspolicy/health_check/test_additional_headers.py
  • testsuite/tests/singlecluster/authorino/identity/plain/test_jwt_user_story.py

Fix

  • test_rlp_on_routes.py: Fixed typos 1rp10m/2rp10m to 1rp10s/2rp10s (limit window is 10 seconds not minutes).

  • observability/conftest.py: Fixed backoff.on_predicate predicate inversion (== to !=), ensuring retry continues until
    expected monitor count is reached.

Verification

  • make reformat: Passed
  • make commit-acceptance: Passed
  • All constant values match original hardcoded values exactly. No behavioral change.

Note

Intentionally left as hardcoded

  • sleep(1) in test_jwt_user_story.py: Fixed 1 second wait used between retries. It is only a small delay while the main configurable value is JWT_STARTUP_MAX_RETRIES.
  • max_tries=7 in mtls/conftest.py: Kept hardcoded because it is tied to the backoff.fibo. Making it a constant would make the retry behavior harder to understand.

Summary by CodeRabbit

Summary by CodeRabbit

  • Chores
    • Centralised timing/delay values into shared constants to reduce duplication and make updates safer.
    • Replaced multiple hardcoded sleeps and wait timeouts with shared constants for DNS propagation, TLS/secret propagation, rate-limit and token-rate-limit resets, observability polling, and JWT startup retries.
    • Standardised common service port values (HTTP/HTTPS) and other test-suite timing parameters to improve consistency across scenarios.

@coderabbitai

coderabbitai Bot commented May 28, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 69c6676c-92b1-4cd3-9ea7-aedef831ce2b

📥 Commits

Reviewing files that changed from the base of the PR and between 56b4930 and fc69c55.

📒 Files selected for processing (29)
  • testsuite/tests/multicluster/load_balanced/test_change_default_geo.py
  • testsuite/tests/multicluster/load_balanced/test_change_strategy.py
  • testsuite/tests/singlecluster/authorino/authorization/opa/external_registry/test_cache.py
  • testsuite/tests/singlecluster/authorino/identity/plain/test_jwt_user_story.py
  • testsuite/tests/singlecluster/authorino/identity/x509/gateway_validation/test_multi_ca_trust.py
  • testsuite/tests/singlecluster/authorino/wristband/conftest.py
  • testsuite/tests/singlecluster/egress/credentials_injection/conftest.py
  • testsuite/tests/singlecluster/egress/test_egress.py
  • testsuite/tests/singlecluster/gateway/dnspolicy/health_check/test_additional_headers.py
  • testsuite/tests/singlecluster/gateway/dnspolicy/health_check/test_remove_endpoint.py
  • testsuite/tests/singlecluster/gateway/reconciliation/change_targetref/conftest.py
  • testsuite/tests/singlecluster/gateway/reconciliation/change_targetref/test_update_tlspolicy_target_ref.py
  • testsuite/tests/singlecluster/gateway/reconciliation/test_gw_doesnt_exist.py
  • testsuite/tests/singlecluster/gateway/reconciliation/test_invalid_issuer_reference.py
  • testsuite/tests/singlecluster/gateway/reconciliation/test_same_target.py
  • testsuite/tests/singlecluster/gateway/scaling/test_auto_scale_gateway.py
  • testsuite/tests/singlecluster/gateway/scaling/test_manual_scale_gateway.py
  • testsuite/tests/singlecluster/grpc/test_grpcroute.py
  • testsuite/tests/singlecluster/identical_hostnames/rlp/test_rlp_on_gw_and_route.py
  • testsuite/tests/singlecluster/identical_hostnames/rlp/test_rlp_on_routes.py
  • testsuite/tests/singlecluster/limitador/test_multiple_iterations.py
  • testsuite/tests/singlecluster/limitador/tokens_rate_limit/basic/test_trlp.py
  • testsuite/tests/singlecluster/limitador/tokens_rate_limit/basic/test_trlp_streaming.py
  • testsuite/tests/singlecluster/limitador/tokens_rate_limit/multiple_iterations/test_multiple_trlp_iterations.py
  • testsuite/tests/singlecluster/limitador/tokens_rate_limit/multiple_iterations/test_multiple_trlp_stream_iterations.py
  • testsuite/tests/singlecluster/observability/conftest.py
  • testsuite/tests/singlecluster/tracing/control_plane/test_control_plane_errors.py
  • testsuite/tests/singlecluster/tracing/control_plane/test_control_plane_lifecycle.py
  • testsuite/utils/constants.py
✅ Files skipped from review due to trivial changes (11)
  • testsuite/tests/singlecluster/gateway/reconciliation/test_same_target.py
  • testsuite/tests/multicluster/load_balanced/test_change_strategy.py
  • testsuite/tests/singlecluster/gateway/scaling/test_manual_scale_gateway.py
  • testsuite/tests/singlecluster/gateway/reconciliation/test_invalid_issuer_reference.py
  • testsuite/tests/singlecluster/limitador/tokens_rate_limit/multiple_iterations/test_multiple_trlp_iterations.py
  • testsuite/tests/singlecluster/authorino/wristband/conftest.py
  • testsuite/tests/singlecluster/gateway/reconciliation/change_targetref/test_update_tlspolicy_target_ref.py
  • testsuite/tests/singlecluster/grpc/test_grpcroute.py
  • testsuite/tests/singlecluster/egress/credentials_injection/conftest.py
  • testsuite/tests/singlecluster/authorino/authorization/opa/external_registry/test_cache.py
  • testsuite/tests/singlecluster/gateway/reconciliation/test_gw_doesnt_exist.py
🚧 Files skipped from review as they are similar to previous changes (16)
  • testsuite/tests/singlecluster/egress/test_egress.py
  • testsuite/tests/singlecluster/gateway/dnspolicy/health_check/test_additional_headers.py
  • testsuite/tests/singlecluster/limitador/tokens_rate_limit/multiple_iterations/test_multiple_trlp_stream_iterations.py
  • testsuite/tests/multicluster/load_balanced/test_change_default_geo.py
  • testsuite/tests/singlecluster/gateway/reconciliation/change_targetref/conftest.py
  • testsuite/tests/singlecluster/identical_hostnames/rlp/test_rlp_on_gw_and_route.py
  • testsuite/tests/singlecluster/authorino/identity/x509/gateway_validation/test_multi_ca_trust.py
  • testsuite/tests/singlecluster/gateway/dnspolicy/health_check/test_remove_endpoint.py
  • testsuite/tests/singlecluster/limitador/tokens_rate_limit/basic/test_trlp_streaming.py
  • testsuite/tests/singlecluster/tracing/control_plane/test_control_plane_errors.py
  • testsuite/tests/singlecluster/gateway/scaling/test_auto_scale_gateway.py
  • testsuite/tests/singlecluster/limitador/tokens_rate_limit/basic/test_trlp.py
  • testsuite/tests/singlecluster/tracing/control_plane/test_control_plane_lifecycle.py
  • testsuite/tests/singlecluster/limitador/test_multiple_iterations.py
  • testsuite/tests/singlecluster/identical_hostnames/rlp/test_rlp_on_routes.py
  • testsuite/utils/constants.py

📝 Walkthrough

Walkthrough

This PR centralises shared timing, polling, and port values in testsuite/utils/constants.py and updates tests and fixtures to use those constants instead of hardcoded waits, retries, and ports.

Changes

Centralise timing and port constants

Layer / File(s) Summary
Constants module expansion
testsuite/utils/constants.py
Adds shared constants for policy, DNS, rate-limit, token-rate-limit, observability, port, and workaround timing values.
DNS waits and health checks
testsuite/tests/multicluster/load_balanced/test_change_default_geo.py, test_change_strategy.py, testsuite/tests/singlecluster/gateway/dnspolicy/health_check/test_additional_headers.py, test_remove_endpoint.py, testsuite/tests/singlecluster/gateway/reconciliation/change_targetref/conftest.py
Replaces hardcoded DNS propagation waits, health-check timeouts, and related port values with shared constants.
Policy and deletion timeout updates
testsuite/tests/singlecluster/gateway/reconciliation/test_gw_doesnt_exist.py, test_invalid_issuer_reference.py, test_same_target.py, testsuite/tests/singlecluster/tracing/control_plane/test_control_plane_errors.py, test_control_plane_lifecycle.py
Uses shared policy-condition and object-deletion timeouts in reconciliation and tracing waits.
Rate-limit reset timing
testsuite/tests/singlecluster/egress/test_egress.py, testsuite/tests/singlecluster/gateway/scaling/test_auto_scale_gateway.py, test_manual_scale_gateway.py, testsuite/tests/singlecluster/grpc/test_grpcroute.py, testsuite/tests/singlecluster/identical_hostnames/rlp/test_rlp_on_gw_and_route.py, test_rlp_on_routes.py, testsuite/tests/singlecluster/limitador/test_multiple_iterations.py, testsuite/tests/singlecluster/limitador/tokens_rate_limit/basic/test_trlp.py, test_trlp_streaming.py, testsuite/tests/singlecluster/limitador/tokens_rate_limit/multiple_iterations/test_multiple_trlp_iterations.py, test_multiple_trlp_stream_iterations.py
Replaces fixed sleep durations with shared reset and iteration wait constants for RLP and TRLP tests.
Ports, observability, and workaround waits
testsuite/tests/singlecluster/egress/credentials_injection/conftest.py, testsuite/tests/singlecluster/authorino/wristband/conftest.py, testsuite/tests/singlecluster/observability/conftest.py, testsuite/tests/singlecluster/authorino/authorization/opa/external_registry/test_cache.py, testsuite/tests/singlecluster/authorino/identity/plain/test_jwt_user_story.py, testsuite/tests/singlecluster/authorino/identity/x509/gateway_validation/test_multi_ca_trust.py
Uses shared HTTP/OIDC ports, observability backoff constants, and cache/secret/plugin/JWT timing constants in fixtures and tests.

Estimated code review effort: 2 (Simple) | ~15 minutes

Possibly related PRs

Suggested labels: refactor

Suggested reviewers: emmaaroche, zkraus

Poem

🐰 I hopped through waits both short and long,
And tucked each number where it belongs.
Now ports and timeouts share one nest,
The tests can wait without a guess.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: replacing magic numbers with named constants in tests.
Linked Issues check ✅ Passed The PR meets the issue goals by centralising timing and port values in testsuite/utils/constants.py and updating the affected tests.
Out of Scope Changes check ✅ Passed No clear out-of-scope changes are evident; the edits stay within the tests and shared constants refactor.
Docstring Coverage ✅ Passed Docstring coverage is 92.50% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

Tools execution failed with the following error:

Failed to run tools: 13 INTERNAL: Received RST_STREAM with code 2 (Internal server error)


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@testsuite/tests/singlecluster/observability/conftest.py`:
- Around line 44-46: The polling predicates passed to backoff.on_predicate are
inverted: backoff retries when the predicate returns True, so change the lambdas
in the observability monitor decorators to return True on failure (e.g., use
len(x) != 5 instead of len(x) == 5, and len(x) != 1 instead of len(x) == 1) so
retries occur while the expected condition is not yet met; keep the existing
interval and max_tries (OBSERVABILITY_MONITOR_POLL_INTERVAL /
OBSERVABILITY_MONITOR_MAX_RETRIES) unchanged and only flip the predicate logic
in the two decorated monitor functions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 967620a5-c5af-4432-982b-3143ad23aa64

📥 Commits

Reviewing files that changed from the base of the PR and between 24325bf and 56b4930.

📒 Files selected for processing (29)
  • testsuite/tests/multicluster/load_balanced/test_change_default_geo.py
  • testsuite/tests/multicluster/load_balanced/test_change_strategy.py
  • testsuite/tests/singlecluster/authorino/authorization/opa/external_registry/test_cache.py
  • testsuite/tests/singlecluster/authorino/identity/plain/test_jwt_user_story.py
  • testsuite/tests/singlecluster/authorino/identity/x509/gateway_validation/test_multi_ca_trust.py
  • testsuite/tests/singlecluster/authorino/wristband/conftest.py
  • testsuite/tests/singlecluster/egress/credentials_injection/conftest.py
  • testsuite/tests/singlecluster/egress/test_egress.py
  • testsuite/tests/singlecluster/gateway/dnspolicy/health_check/test_additional_headers.py
  • testsuite/tests/singlecluster/gateway/dnspolicy/health_check/test_remove_endpoint.py
  • testsuite/tests/singlecluster/gateway/reconciliation/change_targetref/conftest.py
  • testsuite/tests/singlecluster/gateway/reconciliation/change_targetref/test_update_tlspolicy_target_ref.py
  • testsuite/tests/singlecluster/gateway/reconciliation/test_gw_doesnt_exist.py
  • testsuite/tests/singlecluster/gateway/reconciliation/test_invalid_issuer_reference.py
  • testsuite/tests/singlecluster/gateway/reconciliation/test_same_target.py
  • testsuite/tests/singlecluster/gateway/scaling/test_auto_scale_gateway.py
  • testsuite/tests/singlecluster/gateway/scaling/test_manual_scale_gateway.py
  • testsuite/tests/singlecluster/grpc/test_grpcroute.py
  • testsuite/tests/singlecluster/identical_hostnames/rlp/test_rlp_on_gw_and_route.py
  • testsuite/tests/singlecluster/identical_hostnames/rlp/test_rlp_on_routes.py
  • testsuite/tests/singlecluster/limitador/test_multiple_iterations.py
  • testsuite/tests/singlecluster/limitador/tokens_rate_limit/basic/test_trlp.py
  • testsuite/tests/singlecluster/limitador/tokens_rate_limit/basic/test_trlp_streaming.py
  • testsuite/tests/singlecluster/limitador/tokens_rate_limit/multiple_iterations/test_multiple_trlp_iterations.py
  • testsuite/tests/singlecluster/limitador/tokens_rate_limit/multiple_iterations/test_multiple_trlp_stream_iterations.py
  • testsuite/tests/singlecluster/observability/conftest.py
  • testsuite/tests/singlecluster/tracing/control_plane/test_control_plane_errors.py
  • testsuite/tests/singlecluster/tracing/control_plane/test_control_plane_lifecycle.py
  • testsuite/utils/constants.py

Comment thread testsuite/tests/singlecluster/observability/conftest.py Outdated
Signed-off-by: Priyanshu-u07 <connect.priyanshu8271@gmail.com>
@Priyanshu-u07 Priyanshu-u07 force-pushed the test-magic-number-constants branch from 56b4930 to 29a744e Compare May 28, 2026 06:40
@emmaaroche

Copy link
Copy Markdown
Member

Hi @Priyanshu-u07, your PR is almost ready for approval, but you need to resolve the conflicts first 👍

@Priyanshu-u07

Copy link
Copy Markdown
Contributor Author

Thank you for the review @emmaaroche! I've resolved the merge conflicts. PTAL.

@emmaaroche emmaaroche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thank you for working on this!

@emmaaroche emmaaroche merged commit b8019e4 into Kuadrant:main Jul 7, 2026
6 checks passed
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.

Refactor Magic numbers to constants, and collect all in the centralized shared file

2 participants