Skip to content

test(nvidia): raise calculate_score.go coverage from 18% to 94% - #2174

Closed
adity1raut wants to merge 3 commits into
Project-HAMi:masterfrom
adity1raut:test/nvidia-calculate-score-coverage
Closed

test(nvidia): raise calculate_score.go coverage from 18% to 94%#2174
adity1raut wants to merge 3 commits into
Project-HAMi:masterfrom
adity1raut:test/nvidia-calculate-score-coverage

Conversation

@adity1raut

@adity1raut adity1raut commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?
/kind feature

What this PR does / why we need it:
Adds unit tests for pkg/device/nvidia/calculate_score.go, raising its
statement coverage from ~18% to ~94%.

  • Filter, calculateGPUScore, and every branch of
    calculateGPUPairScore (all P2PLink/NVLink types, nil handling, and
    the asymmetric-link panic) are now directly tested.
  • newDevice and deviceListBuilder.build() (including its NVML
    Init/GetDevices/GetP2PLink/GetNVLink error paths) are exercised using
    fake nvml.Interface and nvlib/device.Interface implementations
    built on the moq-generated mocks already vendored under
    go-nvml/pkg/nvml/mock, so no real GPU/driver is required.
  • CalculateGPUScore's success path still isn't covered: NewDevices()
    calls nvml.New() directly, so exercising it would need either a real
    NVML driver or a source change to inject the interface — out of scope
    for this test-only PR.

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

Summary by CodeRabbit

  • Tests
    • Added comprehensive coverage for NVIDIA device discovery, construction, enumeration, and PCI identifier handling.
    • Added validation for device filtering, GPU pair scoring, topology links, P2P connections, and NVLink levels.
    • Added checks for expected error handling across initialization, linking, and unknown-device scenarios.

@hami-robot

hami-robot Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: adity1raut
Once this PR has been reviewed and has the lgtm label, please assign archlitchi for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@hami-robot hami-robot Bot added the size/XL label Jul 28, 2026
@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The PR adds mocked tests for NVIDIA device construction and device-list building. It also adds table-driven coverage for device filtering, topology and NVLink pair scoring, error cases, symmetry, and all NVLink levels.

Changes

NVIDIA device and scoring tests

Layer / File(s) Summary
NVIDIA device construction and list building
pkg/device/nvidia/calculate_score_build_test.go
Adds NVML mocks and tests device creation, PCI bus ID handling, initialization and enumeration failures, topology linking, and NVLink error propagation.
NVIDIA filtering and pair-score validation
pkg/device/nvidia/calculate_score_test.go
Tests ordered device filtering, topology and NVLink scores, unknown links, symmetric results, and NVLink levels 1–18.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related issues

Possibly related PRs

Suggested labels: enhancement

Suggested reviewers: archlitchi

Poem

A rabbit tests each GPU link,
With mock-made paths in careful ink.
NVLinks climb from one to eighteen,
While paired scores stay neat and clean.
Devices build, errors show—
Hop by hop, the checks now grow.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the primary change: adding NVIDIA calculate_score.go tests to increase coverage.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 requested a review from archlitchi July 28, 2026 12:12
@adity1raut
adity1raut force-pushed the test/nvidia-calculate-score-coverage branch from 6f67ac3 to 4a139e8 Compare July 28, 2026 12:14
@hami-robot hami-robot Bot added size/L and removed size/XL labels Jul 28, 2026

@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: 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 `@pkg/device/kunlun/topo_test.go`:
- Around line 274-298: Require TestInterconnect_TwoDevices and
TestInterconnect_FourDevices to assert exactly 2 and 4 selected devices
respectively when all devices are available. Remove the len(got) == 0 acceptance
while preserving the existing duplicate-index checks.

In `@pkg/device/nvidia/calculate_score_build_test.go`:
- Around line 124-129: Extend the success-case assertions around newDevice to
verify that the constructed device’s PCI BusID matches the expected value
derived from the fake device’s PCI address. Keep the existing index and UUID
assertions unchanged, and use the populated PCI-info field returned by
newDevice.
🪄 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 Plus

Run ID: 47484058-7f09-4635-9388-3b3ddfac922d

📥 Commits

Reviewing files that changed from the base of the PR and between 37730dd and 6f67ac3.

📒 Files selected for processing (3)
  • pkg/device/kunlun/topo_test.go
  • pkg/device/nvidia/calculate_score_build_test.go
  • pkg/device/nvidia/calculate_score_test.go

Comment thread pkg/device/kunlun/topo_test.go
Comment thread pkg/device/nvidia/calculate_score_build_test.go Outdated
adity1raut added a commit to adity1raut/HAMi that referenced this pull request Jul 28, 2026
Addresses a CodeRabbit review comment on PR Project-HAMi#2174: the newDevice
success case didn't verify the PciInfo -> BusID conversion, so a
regression there would have passed silently.

Signed-off-by: Aditya Raut <araut7798@gmail.com>
@github-actions github-actions Bot removed the kind/feature new function label Jul 28, 2026

@mesutoezdil mesutoezdil left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

2 notes inline.

Comment thread pkg/device/nvidia/calculate_score_test.go Outdated
Comment thread pkg/device/nvidia/calculate_score_test.go Outdated
@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unittests 64.36% <ø> (+0.98%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.
see 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@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.

🧹 Nitpick comments (1)
pkg/device/nvidia/calculate_score_test.go (1)

114-117: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Add a regression test for aggregate error propagation.

The asymmetric-link test validates calculateGPUPairScore, but no visible case drives that error through calculateGPUScore and its new return nil, err branch. Add an asymmetric fixture to Test_calculateGPUScore and assert the propagated error.

🤖 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 `@pkg/device/nvidia/calculate_score_test.go` around lines 114 - 117, Add an
asymmetric-link fixture to Test_calculateGPUScore that causes
calculateGPUPairScore to return an error, then assert calculateGPUScore returns
that same error with a nil score list. Keep the existing successful cases
unchanged and cover the new return nil, err branch directly.
🤖 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 `@pkg/device/nvidia/calculate_score_test.go`:
- Around line 114-117: Add an asymmetric-link fixture to Test_calculateGPUScore
that causes calculateGPUPairScore to return an error, then assert
calculateGPUScore returns that same error with a nil score list. Keep the
existing successful cases unchanged and cover the new return nil, err branch
directly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3a3b2f15-5ec8-4dc4-b359-b98f4e072622

📥 Commits

Reviewing files that changed from the base of the PR and between bf74296 and 26d8791.

📒 Files selected for processing (2)
  • pkg/device/nvidia/calculate_score.go
  • pkg/device/nvidia/calculate_score_test.go

@mesutoezdil

Copy link
Copy Markdown
Contributor

Production behavior also changed (panic to error), worth reflecting in the title/description.

@adity1raut

Copy link
Copy Markdown
Contributor Author

Production behavior also changed (panic to error), worth reflecting in the title/description.

You're right, I missed that. I'll update the title and description to reflect the behavior change as well. Thanks!

@mesutoezdil

Copy link
Copy Markdown
Contributor

asymmetric pair errors out here, so RegisterInAnnotation never patches the node, blocking all device registration on it. #2306 scores it 0 instead and keeps registering.

/hold

@archlitchi

Copy link
Copy Markdown
Member

please resolve these conflicts

…o bool-based asymmetric API

Master merged Project-HAMi#2306 while this PR was open, which fixed the same
calculateGPUPairScore panic-on-asymmetric-links bug this PR targeted,
but with a different, already-accepted approach: score the pair as 0
and set an asymmetric bool flag instead of returning an error. That
change is not reapplied here — calculate_score.go is left as-is.

This carries forward the still-valid parts of this PR: the new
calculate_score_build_test.go (NVML mock helpers covering device
construction, list building, UUID filtering, and topology/NVLink
derivation) and the calculateGPUPairScore / DeviceList.Filter test
coverage from calculate_score_test.go, updated to call the current
(int, bool) signature instead of the (int, error) signature this PR
originally introduced. The old panic/error-specific asymmetric-link
test is dropped since master's Test_calculateGPUPairScore_asymmetric
already covers that scenario against the merged behavior.

Signed-off-by: Aditya Raut <araut7798@gmail.com>
@adity1raut
adity1raut force-pushed the test/nvidia-calculate-score-coverage branch from 26d8791 to b8b413a Compare August 7, 2026 02:08
@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@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: 3

🤖 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 `@pkg/device/nvidia/calculate_score_build_test.go`:
- Around line 124-130: Update the newDevice test fixture to use an
eight-character zero-padded PCI domain in the address passed to newFakeDevice,
then adjust the PCI BusID assertion to expect the valid four-character domain
remaining after BusID conversion. Keep the existing device and topology
assertions unchanged.

In `@pkg/device/nvidia/calculate_score_test.go`:
- Around line 236-238: Add an asymmetric-link test case in the table-driven
tests around the linkTypes cases, using different forward and backward link
counts such as one link versus zero. Assert that calculateGPUPairScore reports
asymmetric as true and verify the expected score for that condition, while
preserving the existing symmetric cases.
- Around line 219-221: Update the wantErr branch of the DeviceList.Filter test
to assert that the returned got value is nil, in addition to checking the
existing error text. Preserve the current early return after both assertions.
🪄 Autofix

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 Plus

Run ID: 6f8f0d1b-0c8f-48f5-8930-9c76005d3a64

📥 Commits

Reviewing files that changed from the base of the PR and between 87d9795 and b8b413a.

📒 Files selected for processing (2)
  • pkg/device/nvidia/calculate_score_build_test.go
  • pkg/device/nvidia/calculate_score_test.go

Comment thread pkg/device/nvidia/calculate_score_build_test.go Outdated
Comment thread pkg/device/nvidia/calculate_score_test.go
Comment thread pkg/device/nvidia/calculate_score_test.go Outdated
- Use a realistic 8-digit PCI domain fixture (00000000:00:00.0) so the
  asserted BusID() output (0000:00:00.0) looks like a real bus ID,
  instead of asserting the domain-stripped :00:00.0.
- Assert Filter's wantErr branch also returns a nil DeviceList, not
  just the expected error text.
- Add an asymmetric-link-count case to the calculateGPUPairScore table
  test so the wantAsymmetric=true branch is actually exercised.

Signed-off-by: Aditya Raut <araut7798@gmail.com>
Project-HAMi#2313 added a LimitSet flag that FitQuota now gates on instead of
Limit != 0, and updated TestFitResourceQuota's fixture accordingly,
but missed three sibling tests in the same file that also build
device.Quota directly: TestFitResourceQuotaNonNvidia,
TestFitResourceQuotaCountsEveryDevice, and
TestFitResourceQuotaAscendMemoryFactor. Their fixtures defaulted to
LimitSet: false, so FitQuota treated the configured limits as unset
and admitted every pod, failing the denial assertions in all three
tests on current master.

Set LimitSet: true on the five affected fixture entries to match what
AddQuota produces, mirroring the fix already applied to
TestFitResourceQuota.

Signed-off-by: Aditya Raut <araut7798@gmail.com>
@FouoF

FouoF commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Thanks for the NVIDIA scoring tests. This test-only branch has remained on hold, and the production issue it was intended to support has since been addressed by the merged #2306. Keeping the old branch would preserve tests against a superseded implementation and require another large review without a current gap. We are closing this PR. If #2306 still lacks an important regression case, please propose only that case from the latest master and explain the failure it protects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants