Skip to content

test(api-core): extract reusable API test support builder#2149

Open
poroh wants to merge 1 commit into
NVIDIA:mainfrom
poroh:tests-refactoring-p2
Open

test(api-core): extract reusable API test support builder#2149
poroh wants to merge 1 commit into
NVIDIA:mainfrom
poroh:tests-refactoring-p2

Conversation

@poroh
Copy link
Copy Markdown
Contributor

@poroh poroh commented Jun 3, 2026

Description

Move shared API test fixture helpers into test_support, including network fixtures, IB fabric setup, and a TestApiBuilder for constructing test Api instances with configurable dependencies. Refactor existing API fixtures to use the new builder while preserving current test environment behavior.

Type of Change

  • Add - New feature or capability
  • Change - Changes in existing functionality
  • Fix - Bug fixes
  • Remove - Removed features or deprecated functionality
  • Internal - Internal changes (refactoring, tests, docs, etc.)

Related Issues (Optional)

#2001

Breaking Changes

  • This PR contains breaking changes

Testing

  • Unit tests added/updated
  • Integration tests added/updated
  • Manual testing performed
  • No testing required (docs, internal refactor, etc.)

Additional Notes

Move shared API test fixture helpers into `test_support`, including
network fixtures, IB fabric setup, and a `TestApiBuilder` for
constructing test `Api` instances with configurable
dependencies. Refactor existing API fixtures to use the new builder
while preserving current test environment behavior.

Signed-off-by: Dmitry Porokh <dporokh@nvidia.com>
@poroh poroh requested a review from a team as a code owner June 3, 2026 00:51
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 3, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 3bbd577d-451a-4bff-a0ca-75a0bb107402

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • ✅ Review completed - (🔄 Check again to review again)
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (3)
crates/api-core/src/test_support/builder.rs (1)

151-243: ⚡ Quick win

Add module-level documentation for TestApiBuilder.

The builder is a critical test infrastructure component but lacks documentation explaining its purpose, usage, and relationship to test fixtures.

📝 Proposed documentation

Add before the struct definition:

/// Builder for constructing `Api` instances in tests.
///
/// This builder provides a fluent interface for creating test `Api` instances
/// with configurable dependencies. Required fields (database pool, common pools,
/// and work lock manager) must be provided to `new()`, while optional components
/// can be overridden via `with_*` methods.
///
/// # Examples
///
/// ```no_run
/// let api = TestApiBuilder::new(db_pool, common_pools, work_lock_manager)
///     .with_runtime_config(config)
///     .with_credential_manager(creds)
///     .build();
/// ```
pub struct TestApiBuilder {
🤖 Prompt for 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.

In `@crates/api-core/src/test_support/builder.rs` around lines 151 - 243, Add
module-level documentation for the TestApiBuilder struct: create a doc comment
block immediately before the TestApiBuilder struct definition describing its
purpose as a builder for constructing Api instances in tests, noting required
inputs (database pool, common pools, work lock manager), optional with_*
override methods, and include a brief usage example showing
new(...).with_runtime_config(...).with_credential_manager(...).build();
reference the TestApiBuilder and Api types so readers know what it constructs.
crates/api-core/src/test_support/ib_fabric.rs (1)

26-60: ⚡ Quick win

Replace unwrap() with expect() to provide panic context.

Line 58 uses unwrap() when creating the IB fabric manager. In test support code, panics should provide clear diagnostic context to aid debugging.

♻️ Proposed fix with diagnostic context
     )
-    .unwrap()
+    .expect("Failed to create test IB fabric manager")
     .into()
 }
🤖 Prompt for 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.

In `@crates/api-core/src/test_support/ib_fabric.rs` around lines 26 - 60, In
ib_fabric_test_manager, replace the bare unwrap() on the result of
ib::create_ib_fabric_manager(...) with expect(...) to provide a clear panic
message; update the call site (the unwrap() after ib::create_ib_fabric_manager
in ib_fabric_test_manager) to use expect("failed to create IB Fabric manager for
test: <brief context>") so test failures include diagnostic context when
create_ib_fabric_manager returns an Err.
crates/api-core/src/test_support/network.rs (1)

36-104: ⚡ Quick win

Refactor repetitive IpNetwork::new().unwrap() calls into a helper or iterator pattern.

The lazy_static block contains 11 nearly identical repetitive blocks, each calling IpNetwork::new(...).unwrap(). This pattern violates DRY principles and increases maintenance burden.

♻️ Proposed refactor using iterator pattern
 lazy_static! {
     pub static ref TEST_SITE_PREFIXES: Vec<IpNetwork> = vec![
-        IpNetwork::new(
-            FIXTURE_ADMIN_NETWORK_SEGMENT_GATEWAY.network(),
-            FIXTURE_ADMIN_NETWORK_SEGMENT_GATEWAY.prefix(),
-        )
-        .unwrap(),
-        IpNetwork::new(
-            FIXTURE_UNDERLAY_NETWORK_SEGMENT_GATEWAY.network(),
-            FIXTURE_UNDERLAY_NETWORK_SEGMENT_GATEWAY.prefix(),
-        )
-        .unwrap(),
-        IpNetwork::new(
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[0].network(),
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[0].prefix(),
-        )
-        .unwrap(),
-        IpNetwork::new(
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[1].network(),
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[1].prefix(),
-        )
-        .unwrap(),
-        IpNetwork::new(
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[2].network(),
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[2].prefix(),
-        )
-        .unwrap(),
-        IpNetwork::new(
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[3].network(),
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[3].prefix(),
-        )
-        .unwrap(),
-        IpNetwork::new(
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[4].network(),
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[4].prefix(),
-        )
-        .unwrap(),
-        IpNetwork::new(
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[5].network(),
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[5].prefix(),
-        )
-        .unwrap(),
-        IpNetwork::new(
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[6].network(),
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[6].prefix(),
-        )
-        .unwrap(),
-        IpNetwork::new(
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[7].network(),
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[7].prefix(),
-        )
-        .unwrap(),
-        IpNetwork::new(
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[8].network(),
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[8].prefix(),
-        )
-        .unwrap(),
-        IpNetwork::new(
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[9].network(),
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[9].prefix(),
-        )
-        .unwrap(),
-        IpNetwork::new(
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[10].network(),
-            FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS[10].prefix(),
-        )
-        .unwrap(),
+        FIXTURE_ADMIN_NETWORK_SEGMENT_GATEWAY,
+        FIXTURE_UNDERLAY_NETWORK_SEGMENT_GATEWAY,
+    ]
+    .into_iter()
+    .chain(FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS.iter().copied())
+    .map(|gw| IpNetwork::new(gw.network(), gw.prefix()).unwrap())
+    .collect();
-    ];
 }
🤖 Prompt for 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.

In `@crates/api-core/src/test_support/network.rs` around lines 36 - 104,
TEST_SITE_PREFIXES currently repeats IpNetwork::new(...).unwrap() for each
fixture; refactor by building the Vec via an iterator or small helper to avoid
duplication: create either a helper function (e.g., fn make_ipnetwork(gw:
&GatewayType) -> IpNetwork { IpNetwork::new(gw.network(), gw.prefix()).unwrap()
}) and call it for each fixture, or collect from an iterator over a slice of
fixtures (e.g., [FIXTURE_ADMIN_NETWORK_SEGMENT_GATEWAY,
FIXTURE_UNDERLAY_NETWORK_SEGMENT_GATEWAY].iter().chain(FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS.iter()).map(|g|
IpNetwork::new(g.network(), g.prefix()).unwrap()).collect()) inside the
lazy_static block to produce TEST_SITE_PREFIXES; update any symbol names used
(TEST_SITE_PREFIXES, FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS, IpNetwork::new)
accordingly.
🤖 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.

Nitpick comments:
In `@crates/api-core/src/test_support/builder.rs`:
- Around line 151-243: Add module-level documentation for the TestApiBuilder
struct: create a doc comment block immediately before the TestApiBuilder struct
definition describing its purpose as a builder for constructing Api instances in
tests, noting required inputs (database pool, common pools, work lock manager),
optional with_* override methods, and include a brief usage example showing
new(...).with_runtime_config(...).with_credential_manager(...).build();
reference the TestApiBuilder and Api types so readers know what it constructs.

In `@crates/api-core/src/test_support/ib_fabric.rs`:
- Around line 26-60: In ib_fabric_test_manager, replace the bare unwrap() on the
result of ib::create_ib_fabric_manager(...) with expect(...) to provide a clear
panic message; update the call site (the unwrap() after
ib::create_ib_fabric_manager in ib_fabric_test_manager) to use expect("failed to
create IB Fabric manager for test: <brief context>") so test failures include
diagnostic context when create_ib_fabric_manager returns an Err.

In `@crates/api-core/src/test_support/network.rs`:
- Around line 36-104: TEST_SITE_PREFIXES currently repeats
IpNetwork::new(...).unwrap() for each fixture; refactor by building the Vec via
an iterator or small helper to avoid duplication: create either a helper
function (e.g., fn make_ipnetwork(gw: &GatewayType) -> IpNetwork {
IpNetwork::new(gw.network(), gw.prefix()).unwrap() }) and call it for each
fixture, or collect from an iterator over a slice of fixtures (e.g.,
[FIXTURE_ADMIN_NETWORK_SEGMENT_GATEWAY,
FIXTURE_UNDERLAY_NETWORK_SEGMENT_GATEWAY].iter().chain(FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS.iter()).map(|g|
IpNetwork::new(g.network(), g.prefix()).unwrap()).collect()) inside the
lazy_static block to produce TEST_SITE_PREFIXES; update any symbol names used
(TEST_SITE_PREFIXES, FIXTURE_TENANT_NETWORK_SEGMENT_GATEWAYS, IpNetwork::new)
accordingly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 5be81b61-52e4-4c5f-851c-a3e36c9a0519

📥 Commits

Reviewing files that changed from the base of the PR and between b91cb44 and 489f76c.

📒 Files selected for processing (6)
  • crates/api-core/src/test_support/builder.rs
  • crates/api-core/src/test_support/ib_fabric.rs
  • crates/api-core/src/test_support/mod.rs
  • crates/api-core/src/test_support/network.rs
  • crates/api-core/src/test_support/network_segment.rs
  • crates/api-core/src/tests/common/api_fixtures/mod.rs

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.

1 participant