NO-ISSUE: Move common test database setup to the suite level - #635
Conversation
|
@jhernand: This pull request explicitly references no jira issue. DetailsIn response to this:
Instructions 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 openshift-eng/jira-lifecycle-plugin repository. |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: jhernand The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository: osac-project/coderabbit/.coderabbit.yaml Review profile: ASSERTIVE Plan: Enterprise Run ID: 📒 Files selected for processing (36)
💤 Files with no reviewable changes (14)
WalkthroughCentralizes per-test DB/transaction wiring into ChangesServer test harness centralization
Sequence DiagramsequenceDiagram
participant ServerSuite
participant Database
participant TxManager
participant TestCase
ServerSuite->>Database: build instance & pool
ServerSuite->>TxManager: create tx manager
TxManager->>Database: begin transaction
ServerSuite->>TestCase: inject tx into ctx (database.TxIntoContext)
TestCase->>Database: reuse ctx-backed tx or call database.TxFromContext(ctx)
🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 10 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (10 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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 `@internal/servers/host_types_server_test.go`:
- Around line 229-230: The test calls database.TxFromContext(ctx) and
immediately uses tx.Exec without checking err; update the test to assert or fail
if err != nil before dereferencing tx (i.e., after calling TxFromContext and
before calling tx.Exec) so the test fails with a clear message instead of
panicking; reference the call to database.TxFromContext(ctx) and the tx variable
in the host_types_server_test test to locate and fix the assertion.
In `@internal/servers/servers_suite_test.go`:
- Around line 97-98: BeforeEach currently does ctx = context.Background() which
lacks a deadline; change it to create a cancellable context with a timeout (use
context.WithTimeout(context.Background(), <reasonable duration>)) and assign
returned ctx and cancel to the test-scoped variables (ctx and cancel). Ensure
the corresponding AfterEach calls cancel() to release resources and prevent test
hangs; update imports to include time if needed. Locate the setup in BeforeEach
and the cleanup in AfterEach in servers_suite_test.go and modify those functions
(ctx variable, BeforeEach, AfterEach) accordingly.
🪄 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: Repository: osac-project/coderabbit/.coderabbit.yaml
Review profile: ASSERTIVE
Plan: Enterprise
Run ID: b6942d95-f659-4bd5-a5e6-474b59e2e280
📒 Files selected for processing (36)
internal/controllers/computeinstance/computeinstance_reconciler_function_test.gointernal/servers/cluster_catalog_items_server_test.gointernal/servers/compute_instances_server_test.gointernal/servers/generic_mapper_test.gointernal/servers/host_types_server_test.gointernal/servers/network_classes_server_test.gointernal/servers/organizations_server_test.gointernal/servers/private_cluster_catalog_items_server_test.gointernal/servers/private_cluster_templates_server_test.gointernal/servers/private_clusters_server_test.gointernal/servers/private_compute_instance_catalog_items_server_test.gointernal/servers/private_compute_instance_templates_server_test.gointernal/servers/private_compute_instances_server_test.gointernal/servers/private_host_types_server_test.gointernal/servers/private_hubs_server_test.gointernal/servers/private_organizations_server_test.gointernal/servers/private_projects_server_test.gointernal/servers/private_public_ip_attachments_server_test.gointernal/servers/private_public_ip_pools_server_test.gointernal/servers/private_public_ips_server_test.gointernal/servers/private_role_bindings_server_test.gointernal/servers/private_roles_server_test.gointernal/servers/private_subnets_server_test.gointernal/servers/private_users_server_test.gointernal/servers/private_virtual_networks_server_test.gointernal/servers/projects_server_test.gointernal/servers/public_ip_attachments_server_test.gointernal/servers/public_ips_server_test.gointernal/servers/role_bindings_server_test.gointernal/servers/roles_server_test.gointernal/servers/security_groups_server_test.gointernal/servers/servers_suite_test.gointernal/servers/servers_tenancy_test.gointernal/servers/subnets_server_test.gointernal/servers/users_server_test.gointernal/servers/virtual_networks_server_test.go
💤 Files with no reviewable changes (14)
- internal/controllers/computeinstance/computeinstance_reconciler_function_test.go
- internal/servers/private_clusters_server_test.go
- internal/servers/private_public_ips_server_test.go
- internal/servers/generic_mapper_test.go
- internal/servers/private_compute_instance_catalog_items_server_test.go
- internal/servers/organizations_server_test.go
- internal/servers/servers_tenancy_test.go
- internal/servers/private_cluster_templates_server_test.go
- internal/servers/private_compute_instance_templates_server_test.go
- internal/servers/private_public_ip_attachments_server_test.go
- internal/servers/private_host_types_server_test.go
- internal/servers/private_hubs_server_test.go
- internal/servers/private_public_ip_pools_server_test.go
- internal/servers/private_cluster_catalog_items_server_test.go
Every server test file in `internal/servers` repeated the same `BeforeEach` block that created a background context, started a database instance and pool, built a transaction manager, opened a transaction, and injected it into the context. This duplicated roughly 20 lines per file across more than 30 test files. This change extracts that boilerplate into a single global `BeforeEach` in `servers_suite_test.go` and promotes `ctx` to a package-level variable so all tests share the setup automatically. Tests that still need direct access to the underlying transaction (for raw SQL such as adding finalizers) now obtain it with `database.TxFromContext(ctx)` instead of relying on a local `tx` variable. Assisted-by: Cursor Signed-off-by: Juan Hernandez <juan.hernandez@redhat.com>
701183b to
5be0077
Compare
Summary
pool, transaction manager, and transaction) from every server test file into a single global
BeforeEachinservers_suite_test.go, promotingctxto a package-level variable.database.TxFromContext(ctx)instead of relying on a localtxvariable.Test plan
ginkgo run -r internalpasses with no regressions.Summary by CodeRabbit
Tests
Chores