Skip to content

test(memoryrequirements): add regression test for init+regular containers with limits set - #1232

Open
pujitha24 wants to merge 2 commits into
stackrox:mainfrom
pujitha24:auto/issue-428
Open

test(memoryrequirements): add regression test for init+regular containers with limits set#1232
pujitha24 wants to merge 2 commits into
stackrox:mainfrom
pujitha24:auto/issue-428

Conversation

@pujitha24

Copy link
Copy Markdown

Motivation:
Issue #428 reported that unset-memory-requirements produced a false
positive on a Deployment with both initContainers and containers when
resources were set on both. Reproducing this against current main with
a hand-built kube-linter binary and a Deployment YAML containing an
initContainer and a regular container, each with memory/cpu requests
and limits set, shows the check now correctly reports no violations.
The fix landed via earlier work (PodSpec.AllContainers(), used by
util.PerContainerCheck) that iterates init, regular, and ephemeral
containers while checking each container's own Resources field
independently. There was, however, no test locking in this behavior,
so the scenario could regress silently.

Approach:

  • Add memoryrequirements/template_test.go using the repo's
    templates.TemplateTestSuite, covering:
    • an init container and a regular container both with memory
      requests/limits set -> no diagnostics (the exact [BUG] unset-memory-requirements validation broken #428 scenario).
    • an init container missing resources alongside a regular container
      that has them set -> the init container is still correctly
      flagged by name, proving the check isn't vacuously passing.
  • Add AddInitContainerToDeployment to pkg/lintcontext/mocks/container.go,
    mirroring the existing AddContainerToDeployment helper, since no mock
    helper previously existed for populating a Deployment's init
    containers in tests.
  • Extract the "memory-requirements" string literal into a templateKey
    const in template.go (matching the convention already used by
    sibling templates such as latesttag) so the new test can reference it
    via s.Init(templateKey).

Validation:

  • go build ./...
  • go test ./pkg/templates/memoryrequirements/... ./pkg/lintcontext/mocks/... (pass)
  • go test ./... (all packages pass)
  • golangci-lint run ./pkg/templates/memoryrequirements/... ./pkg/lintcontext/mocks/... (clean)

Impact: this is test-only coverage, not a functional fix. The
unset-memory-requirements/unset-cpu-requirements checks already behave
correctly on current main for the reported scenario; user-visible
behavior is unchanged. The benefit is a regression guard preventing
this specific false positive from silently reappearing.

Fixes #428

Signed-off-by: Pujitha Paladugu 10557236+pujitha24@users.noreply.github.com

…ners with limits set

Motivation:
Issue stackrox#428 reported that unset-memory-requirements produced a false
positive on a Deployment with both initContainers and containers when
resources were set on both. Reproducing this against current main with
a hand-built kube-linter binary and a Deployment YAML containing an
initContainer and a regular container, each with memory/cpu requests
and limits set, shows the check now correctly reports no violations.
The fix landed via earlier work (PodSpec.AllContainers(), used by
util.PerContainerCheck) that iterates init, regular, and ephemeral
containers while checking each container's own Resources field
independently. There was, however, no test locking in this behavior,
so the scenario could regress silently.

Approach:
- Add memoryrequirements/template_test.go using the repo's
  templates.TemplateTestSuite, covering:
  - an init container and a regular container both with memory
    requests/limits set -> no diagnostics (the exact stackrox#428 scenario).
  - an init container missing resources alongside a regular container
    that has them set -> the init container is still correctly
    flagged by name, proving the check isn't vacuously passing.
- Add AddInitContainerToDeployment to pkg/lintcontext/mocks/container.go,
  mirroring the existing AddContainerToDeployment helper, since no mock
  helper previously existed for populating a Deployment's init
  containers in tests.
- Extract the "memory-requirements" string literal into a templateKey
  const in template.go (matching the convention already used by
  sibling templates such as latesttag) so the new test can reference it
  via s.Init(templateKey).

Validation:
- go build ./...
- go test ./pkg/templates/memoryrequirements/... ./pkg/lintcontext/mocks/... (pass)
- go test ./... (all packages pass)
- golangci-lint run ./pkg/templates/memoryrequirements/... ./pkg/lintcontext/mocks/... (clean)

Impact: this is test-only coverage, not a functional fix. The
unset-memory-requirements/unset-cpu-requirements checks already behave
correctly on current main for the reported scenario; user-visible
behavior is unchanged. The benefit is a regression guard preventing
this specific false positive from silently reappearing.

Fixes stackrox#428

Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 29, 2026 22:09
@pujitha24
pujitha24 requested a review from rhybrillou as a code owner July 29, 2026 22:09

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Central YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 9919085d-169b-4236-82c2-b3e50faede33

📥 Commits

Reviewing files that changed from the base of the PR and between b834bf8 and 5cde5df.

📒 Files selected for processing (1)
  • pkg/templates/memoryrequirements/template_test.go

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Improved memory requirements validation for Kubernetes init containers.
    • Deployments are no longer incorrectly reported when memory requirements are correctly set across init and regular containers.
    • Init containers missing required memory limits are now correctly flagged, even when regular containers are configured properly.
  • Tests

    • Added regression coverage for init and regular container memory requirements behavior, including the “missing init limit” case.
    • Extended test helpers to support easier deployment init-container setup.

Walkthrough

The memory requirements template now uses a package-level key constant. Mock deployment helpers support adding init containers, and regression tests cover memory limits on both init and regular containers.

Changes

Memory requirements validation

Layer / File(s) Summary
Template key and test harness
pkg/templates/memoryrequirements/template.go, pkg/lintcontext/mocks/container.go, pkg/templates/memoryrequirements/template_test.go
Centralizes the template key and adds test setup, resource helpers, and mock support for init containers.
Init-container validation tests
pkg/templates/memoryrequirements/template_test.go
Tests deployments with configured init and regular container limits and flags init containers missing memory limits.

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

Suggested reviewers: rhybrillou

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: adding regression tests for memory requirements with init and regular containers.
Description check ✅ Passed The description matches the changeset and explains the regression test, mock helper, and template key refactor.
Linked Issues check ✅ Passed The PR adds the requested regression coverage for issue #428 and validates initContainers plus regular containers as expected.
Out of Scope Changes check ✅ Passed The mock helper and template key extraction support the tests and stay within the issue's regression-coverage scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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 `@pkg/templates/memoryrequirements/template_test.go`:
- Around line 54-58: Update the test cases passed to Validate in the memory
requirements template test so request handling is covered alongside limit
handling: either change the existing RequirementsType to "any" or add a separate
"request" case while retaining limit coverage.
🪄 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: Central YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 0d29b887-d360-4cc9-9f98-0aad80592919

📥 Commits

Reviewing files that changed from the base of the PR and between 75b6ee5 and b834bf8.

📒 Files selected for processing (3)
  • pkg/lintcontext/mocks/container.go
  • pkg/templates/memoryrequirements/template.go
  • pkg/templates/memoryrequirements/template_test.go

Comment thread pkg/templates/memoryrequirements/template_test.go
@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 20.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 31.72%. Comparing base (dbd7529) to head (5cde5df).
⚠️ Report is 338 commits behind head on main.

Files with missing lines Patch % Lines
pkg/lintcontext/mocks/container.go 0.00% 4 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #1232       +/-   ##
===========================================
- Coverage   62.36%   31.72%   -30.64%     
===========================================
  Files         197      239       +42     
  Lines        4854     6565     +1711     
===========================================
- Hits         3027     2083      -944     
- Misses       1439     4305     +2866     
+ Partials      388      177      -211     
Flag Coverage Δ
unit 31.72% <20.00%> (-30.64%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

…krox#428 regression case

CodeRabbit noted that TestInitAndRegularContainersWithLimitsSet builds
containers with both memory Requests and Limits set, but only validated
with RequirementsType "limit", leaving request handling untested. Switch
to "any" so the test exercises both code paths.

Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
@pujitha24

Copy link
Copy Markdown
Author

Good catch — the fixture set both Requests and Limits, but the test only validated with RequirementsType: "limit", so request handling wasn't actually exercised. Updated TestInitAndRegularContainersWithLimitsSet to use RequirementsType: "any", which checks both request and limit for the container per template.go. Confirmed it still passes with no diagnostics for the #428 regression scenario, and the full test suite (go test ./...) passes.

Re the failing codecov/patch and codecov/project checks: codecov/project appears to be comparing against a stale base commit (338 commits behind head) — the same large, bogus coverage drop shows up on unrelated open PRs (e.g. #1216) too, so that one looks like a pre-existing infra issue, not something from this diff. codecov/patch is flagging the new AddInitContainerToDeployment mock helper as uncovered, but that mirrors every other file in pkg/lintcontext/mocks (none have dedicated tests — they're exercised indirectly via other packages' tests, which the repo's go test ./... without -coverpkg doesn't attribute). I didn't want to unilaterally add a coverage-only test that breaks that convention, so leaving that as-is for a maintainer call.

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.

[BUG] unset-memory-requirements validation broken

2 participants