Skip to content

test(scheduler): add unit tests for metrics helper functions - #2447

Closed
adity1raut wants to merge 1 commit into
Project-HAMi:masterfrom
adity1raut:test/scheduler-metrics-helper-coverage
Closed

test(scheduler): add unit tests for metrics helper functions#2447
adity1raut wants to merge 1 commit into
Project-HAMi:masterfrom
adity1raut:test/scheduler-metrics-helper-coverage

Conversation

@adity1raut

@adity1raut adity1raut commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind feature

What this PR does / why we need it:

Adds direct unit tests for normalizeAMDCoreMetrics, sendMetric, and sendLegacyMetric in cmd/scheduler/metrics.go. These three helpers had no direct test coverage — they were only exercised indirectly through the collector-level Collect/Gather tests, so their branches (AMD vs. non-AMD normalization, non-positive total, the sendMetric error path on mismatched labels, and the nil-descriptor no-op in sendLegacyMetric) weren't independently verified.

This is a small, standalone piece of the metrics gap analysis discussed in #2126 (test coverage for the existing scheduler metrics surface before further changes are layered on top).

Which issue(s) this PR fixes:
Relates to #2126

Special notes for your reviewer:

Test-only change, no production code touched. Ran locally:

go test ./cmd/scheduler/... -short --race -count=1
golangci-lint run ./cmd/scheduler/...

Does this PR introduce a user-facing change?:

None

AI assistance disclosure: This PR was written primarily by Claude Code (test cases and implementation), based on my own review of the existing metrics.go coverage gaps.

Summary by CodeRabbit

  • Tests
    • Expanded coverage for AMD core metric normalization, including case handling, invalid totals, non-AMD metrics, and percentage rounding.
    • Added validation for metric submission behavior, ensuring valid metrics are sent while invalid data is rejected safely.
    • Added coverage for legacy metric handling, including nil descriptors, error suppression, and successful submissions.

normalizeAMDCoreMetrics, sendMetric, and sendLegacyMetric in
cmd/scheduler/metrics.go had no direct unit test coverage; they were
only exercised indirectly through the collector-level tests. Add
focused tests for the AMD core normalization math and for the
success/error paths of both metric-sending helpers.

Signed-off-by: Aditya Raut <araut7798@gmail.com>
@hami-robot
hami-robot Bot requested review from FouoF and wawa0210 August 7, 2026 09:28
@hami-robot

hami-robot Bot commented Aug 7, 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 fouof 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

@github-actions github-actions Bot removed the kind/feature new function label Aug 7, 2026
@hami-robot hami-robot Bot added the size/L label Aug 7, 2026
@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Added scheduler metric tests for AMD core normalization, standard metric sending, and legacy metric sending. The tests cover valid, invalid, nil, case-insensitive, rounding, and error-handling cases.

Changes

Scheduler metrics test coverage

Layer / File(s) Summary
Metric normalization and emission tests
cmd/scheduler/metrics_test.go
Tests cover AMD normalization, non-AMD passthrough, percentage rounding, valid metric emission, label-count errors, nil legacy descriptors, swallowed errors, and valid legacy metric emission.

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

Suggested labels: enhancement

Suggested reviewers: peachest

Poem

Hop, hop, the metrics bloom,
AMD numbers find their room.
Valid sparks are sent with cheer,
Bad ones stop and disappear.
A rabbit tests each path just right.

🚥 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 added unit tests for scheduler metric helper functions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 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 `@cmd/scheduler/metrics_test.go`:
- Around line 236-243: Add a separate negative-total AMD test case in the table
covering normalizeAMDCoreMetrics, using a negative total and expected total and
allocated values unchanged. Keep the existing zero-total case, and ensure the
new case verifies the total <= 0 pass-through behavior.
🪄 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: 03aff813-b048-4f35-a28b-2e63457538f3

📥 Commits

Reviewing files that changed from the base of the PR and between 3616313 and a66fc83.

📒 Files selected for processing (1)
  • cmd/scheduler/metrics_test.go

Comment on lines +236 to +243
{
name: "AMD device with non-positive total is passed through unchanged",
deviceType: "AMD",
total: 0,
allocated: 0,
wantTotal: 0,
wantAllocated: 0,
},

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Cover the negative-total branch.

This case tests total == 0 only. normalizeAMDCoreMetrics passes through every total <= 0 value. Add an AMD case with a negative total to detect a regression that changes the condition to total == 0.

Proposed test case
+		{
+			name:          "AMD device with negative total is passed through unchanged",
+			deviceType:    "AMD",
+			total:         -1,
+			allocated:     1,
+			wantTotal:     -1,
+			wantAllocated: 1,
+		},
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{
name: "AMD device with non-positive total is passed through unchanged",
deviceType: "AMD",
total: 0,
allocated: 0,
wantTotal: 0,
wantAllocated: 0,
},
{
name: "AMD device with non-positive total is passed through unchanged",
deviceType: "AMD",
total: 0,
allocated: 0,
wantTotal: 0,
wantAllocated: 0,
},
{
name: "AMD device with negative total is passed through unchanged",
deviceType: "AMD",
total: -1,
allocated: 1,
wantTotal: -1,
wantAllocated: 1,
},
🤖 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 `@cmd/scheduler/metrics_test.go` around lines 236 - 243, Add a separate
negative-total AMD test case in the table covering normalizeAMDCoreMetrics,
using a negative total and expected total and allocated values unchanged. Keep
the existing zero-total case, and ensure the new case verifies the total <= 0
pass-through behavior.

@codecov

codecov Bot commented Aug 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unittests 64.30% <ø> (+0.21%) ⬆️

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

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

@FouoF

FouoF commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Thanks for the contribution. Most of this PR adds direct coverage for thin metrics helpers that are already exercised through collector-level tests, without addressing a concrete failure. The AMD normalization edge cases may be worth testing if they are tied to a confirmed behavior contract, but they do not justify a separate coverage-only PR in the current form. We are closing this PR. A future focused change should first confirm the expected behavior with a maintainer and keep only the tests that protect that contract.

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.

2 participants